package ProGAL.geomNd; import ProGAL.math.Constants; import ProGAL.math.Matrix; import ProGAL.math.Randomization; /** * A vector in metric space represented with double precision. * @todo cache the length so several calls to getLengthSquared and getLength * takes less time. This can be relevant e.g. in Line3d for multiple * projections, but can also cause serious problems if anyone chooses to extend the * Vector object. */ public class Vector { protected double[] coords; protected final int dim; /** Construct a vector pointing from origo to p. */ public Vector(Point p) { dim = p.dim; coords = new double[dim]; for(int d=0;dConstants.EPSILON) return false; return true; } /** Get the dot-product of this vector and v. */ public double dot(Vector v){ double sum = 0; for(int d=0;ddec decimals precision. */ public String toString(int dec) { StringBuilder sb = new StringBuilder(); sb.append("Vector["); for(int d=0;dSystem.out. */ public void toConsole() { toConsole(2); } /** Writes this vector to System.out with dec decimals precision. */ public void toConsole(int dec) { System.out.println(toString(dec)); } /** Return true if this vector equals v. */ public boolean equals(Vector v){ for(int d=0;dConstants.EPSILON) return false; return true; } public boolean equals(Object v){ if(v instanceof Vector) return equals((Vector)v); return false; } /** Create a clone of this vector. */ public Vector clone(){ double[] newCoords = new double[dim]; for(int d=0;dlen pointing a random direction */ public static Vector randomVector(int dim, double len) { Vector ret = new Vector(dim); do{ for(int d=0;d1.0); ret.multiplyThis(len/ret.length()); return ret; } }