package clothSimulator;

import no.uib.cipr.matrix.DenseVector;

public class arrayAsVector {

	/** Here we are just creating a vector type class so that we can put all the stupid, simple
	 * vector methods ( like that used to add two vectors which are really just arrays) in one place.
	 */
	
	private int theLength;
	public double[] theVector;
	public arrayAsVector(int length){
		theVector=new double[length];
		this.theLength=length;
	}
	
	/** sets an arrayAsVector object to another arrayAsVector object
	 * 
	 */
	public void set(arrayAsVector aVector){
		this.theVector=new double[aVector.length()];
		for(int i=0; i<this.theVector.length;i++){
			this.theVector[i]=aVector.theVector[i];
		}
	}
	
	/** returns the length of the vector
	 * 
	 */
	public int length(){
		return this.theLength;
	}
	
	/** Since we are treating double arrays as vectors, we need some simple methods to do vector math
	 * with double arrays.  These methods include adding two vectors and scaling a vector
	 */
	public void scaleVector(double scalar){
		for(int i=0; i<this.theVector.length;i++){
			this.theVector[i]=this.theVector[i]*scalar;
		}	
	}	
	
	/**this method adds inVector2 to the object being passed the method
	 * 
	 * @param inVector2
	 */
	public void addVectors(arrayAsVector inVector2) {
		for(int i=0; i< this.theVector.length;i++){
			this.theVector[i]=this.theVector[i]+inVector2.theVector[i];
		}
	}
	
	private double theInner;
	/** here is a method to return the inner product of two vectors
	 * 
	 */
	public double inner(arrayAsVector vector2){
		//for now do nothing
		theInner=0;
		for(int i=0; i< this.theVector.length;i++){
			theInner+=this.theVector[i]*vector2.theVector[i];
		}
		return theInner;
	}
	
	
	
	private double epsilon;
	/** this just checks to see if an array is all zeros
	 * ...I don't know if we want to check if r is close to zero or if it is exactly zero.  For now, we will
	 * just leave epsilon really close to zero and we will just check that each entry is less than epsilon.
	 * @param anArray
	 * @return
	 */
	public Boolean isZero(){
		epsilon =1E-15;
		Boolean theBool=true;
		for(int i=0; i<this.theVector.length;i++){
			if(Math.abs(this.theVector[i])>epsilon){
				theBool=false;
			}
		}
		return theBool;
	}
	
	/** to test the system out we are going to do some conversion into theMTJ vector format.  Here we have a
	 method to convert an arrayAsVector into a DenseVector */	 
	public DenseVector convertToMTJVector(){
		DenseVector x=new DenseVector(theLength);
		for(int i=0; i< theLength; i++){
			x.set(i,this.theVector[i]);
		}
		return x;
	}

	private double max;
	public int maxLoc;
	//this returns the largest value in an arrayAsVector
	public double findMax(){
		max=0;
		for(int i=0; i<this.theLength;i++){
			if(max<Math.abs(this.theVector[i])){
				max=Math.abs(this.theVector[i]);
				maxLoc=i;
			}
		}
		return max;
	}	
	
	/**the absolute value norm is the sum of the absolute values of all the constraints */
	private double AbsValNorm;
	double tempAVN;
	public double getAbsValNorm(){
		tempAVN=0;
		for(int i=0; i<theVector.length;i++){
			tempAVN+=Math.abs(theVector[i]);
		}
		AbsValNorm=tempAVN;
		return AbsValNorm;
	}
	

	
}
