package math.matrix; import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; import java.util.List; import math.Maths; import math.geometry.Point; import thisandthat.WObject; import exception.IllegalDimensionException; /** * Vector of real numbers, v∈ℝ. * @author Daniel Weschke */ public class Vector extends WObject implements Cloneable, Serializable{ /** * UID */ private static final long serialVersionUID = 7909642082289701909L; protected List vector; // protected double[] vector; public Vector(){ } /** * Create zero vector * @param n */ public Vector(int n){ int i; // vector = new double[n]; vector = new ArrayList(); for(i=0; i data){ this(data.size()); int i; int n = data.size(); for(i=0; i get(){ return vector; } /** * @param i index * @return corresponding coordinate */ public double get(int i){ return vector.get(i); } /** * @return vector array (integers) */ public int[] getInts(){ int i; int[] result = new int[n()]; for(i=0; i0?get(0):Double.NaN; } /** * get y (second element) * @return y value */ public double y(){ return n()>1?get(1):Double.NaN; } /** * get z (third element) * @return z value */ public double z(){ return n()>2?get(2):Double.NaN; } /** * length of vector * @return dimension */ public int n(){ return length(); } /** * length of vector * @return dimension */ public int length(){ // return vector.length; return vector.size(); } /** * Create vector with zeros * @param n size * @return zero vector */ public static Vector zeros(int n){ return new Vector(n); } /** * Create vector with ones * @param n size * @return one vector */ public static Vector ones(int n){ return fill(n, 1); } /** * Create vector with given scalar * @param n size * @param s scalar * @return filled vector */ public static Vector fill(int n, double s){ Vector a = new Vector(n); int i; for(i=0; i0 ? 1 : -1; return fill(start, increment, end); } /** * Create vector and fill it from a start value to an end value with n equidistant steps * @param start * @param end * @param n steps * @return filled vector [start,(end-start)*1/n,(end-start)*2/n,...,end] */ public static Vector fillN(double start, double end, int n){ double increment = (end-start)/n; return fill(start, increment, end); } /** * Create vector and fill it from a start value and increments it to an end value * @param start * @param increment * @param end * @return filled vector [start,start+increment,start+2*increment,...,end] */ public static Vector fill(double start, double increment, double end){ double delta = end-start; int n = (int)(delta/increment)+1; if((delta>=0&&increment<0)||(delta<0&&increment>=0)||increment==0) return new Vector(); Vector a = new Vector(n); int i; for(i=0; iC = AB = ATB * @throws IllegalDimensionException Inner matrix dimensions must agree. */ public Vector times(Matrix B) throws IllegalDimensionException{ int i,j; if(n() != B.getM()) throw new IllegalDimensionException("Inner matrix dimensions must agree."); double[] c = new double[n()]; for(i=0; iTx. * A • B = a1b1 + a2b2 + * … + anbn; * a scalar quantity * @return scalar * @throws IllegalDimensionException */ public double dot() throws IllegalDimensionException{ return dot(this); } /** * Dot / inner / scalar product xTy. * A • B = a1b1 + a2b2 + * … + anbn; * a scalar quantity * @param a vector * @return scalar * @throws IllegalDimensionException */ public double dot(Vector a) throws IllegalDimensionException{ if(n() != a.n()) throw new IllegalDimensionException("Illegal vector dimension."); int i; double c = 0; for(i=0; ii = aibi * @param b vector * @return vector */ public Vector timesE(Vector b){ int i; Vector c = create(n()); for(i=0; ii = ai/bi * @param b vector * @return vector */ public Vector overE(Vector b){ int i; Vector c = create(n()); for(i=0; ia/||a|| * @return unit vector */ public Vector normalize(){ Vector c = times(1.0/norm()); this.vector = c.vector; return this; } /** * @return the corresponding unit vector */ public Vector direction() { if(norm() == 0.0) throw new RuntimeException("Zero-vector has no direction"); return times(1.0 / norm()); } /** * Element-wise cosine of argument in radians. * @return the cosine for each element of the matrix */ public Vector cos() { int i; int n = n(); Vector c = new Vector(n); for(i=0; i1|, |v2|, …, |vn| } * @return vector with absolute values */ public Vector abs(){ int i; Vector c = create(n()); for(i=0; i=0 ? get(i) : -get(i)); return c; } /** * Reflection against a plane due to a vector * @return reflected vector * @throws IllegalDimensionException */ public Vector flip(Vector v) throws IllegalDimensionException{ // P = I - alpha v v'; alpha = 2 / (v'v) return Matrix.mirror(v).times(this); } /** * Maximum value of the vector * @return maximum value in vector, -∞ if no such value. */ public double max(){ return Maths.max(getArray()); } /** * Minimum value of the vector * @return minimum value in vector, +∞ if no such value. */ public double min(){ return Maths.min(getArray()); } /** * Find value in vector. * @param a value * @return index */ public int indexOf(double a){ int i; for(i=0; i= 0; i--, j++) tmp.set(j, get(i)); vector = tmp.vector; return this; } /** * Zero vector? * @return boolean */ public boolean isZero(){ int i; boolean result = true; for(i=0; i 0){ result = false; break; } } return result; } public boolean isNormalized(){ return Math.abs(norm()-1)2. * n=2 * @return true or false */ public boolean isR2(){ return (n()==2); } /** * Vector in euclidean plane ℝ3. * n=3 * @return true or false */ public boolean isR3(){ return (n()==3); } /** * * @param a int array * @param b int array * @return true if two integer arrays have same length and * all corresponding pairs of integers are equal */ public static boolean equals(int[] a, int[] b){ if(a.length != b.length) return false; // same length? int i; for(i=0; i