import java.io.*;
import java.util.*;

//This class contains everything needed to calculate the norm
/*In particular, this class contains a method:  public map getNorm(Map vMap). 
vMap must be a mapping of nodes (Integers) to associated attribute vectors (Vector of Doubles).
This method returns a mapping of nodes (Integers) to their norms (Doubles).
I plan to use this only for membership vectors, but any vector (as long as it is a Vector of Doubles) can be used.*/


class NormCalculator
{
 public static void main(String args[])
  {
 
  }

//MUST GET METHODS TO GET CLUSTER LIST
//MUST GET METHODS TO CALCULATE MEMBERSHIP VECTOR
//MUST GET METHODS TO CALCULATE NORM
//MUST GET METHODS TO CHANGE FILE TO CONTAIN NORM

public Map getNorm(Map vMap)
{
Map normMap = new HashMap();
try
	{
		//Get map which maps node to associated vector
		Map vectorMap = vMap;
		
		
		//CALCULATE NORM
		
		//FIRST, GET NODE LIST
		Set nodeSet = vectorMap.keySet();//gives a set of all nodeIDs
		int numberOfNodes = nodeSet.size();//gives number of nodes
		Iterator nodeIterator = nodeSet.iterator();
	
	while (nodeIterator.hasNext()) //loop over all nodes
	{
	//FIND NORM FOR ALL NODES
		Integer node = (Integer)nodeIterator.next();//got node
//System.out.println("Current node: " + node);
		Map ratioMap = (Map)vectorMap.get(node);//get associated vector
		Collection nonzeroRatios = ratioMap.values();//get all nonzero ratios
		Iterator iterator = nonzeroRatios.iterator();
		double sumOfSquares = 0;
		
		//loop over vector to find norm		
		while (iterator.hasNext())
		{
//System.out.println("Membership for cluster " + j + " is " + vector[j]);
			Double next = (Double)iterator.next();
			double element = next.doubleValue(); //get next term
			double nextSquare = element * element; //square term
			sumOfSquares = sumOfSquares + nextSquare;//add square term
		}
		double norm = Math.sqrt(sumOfSquares);
//System.out.println("Norm: " + norm);

		Double normObject = new Double(norm);
		normMap.put(node, normObject);//place node and associated norm in map
		
	}


    	}

	catch (Exception e)
	{//Catch exception if any
  	System.err.println("Error: " + e.getMessage());
  	}

return normMap;
}


}