vec_math
Class VectorG

java.lang.Object
  extended by vec_math.VectorG
All Implemented Interfaces:
Serializable, Cloneable
Direct Known Subclasses:
LineVector, Vector1D, vector2D, Vector2D, vector3D, Vector3D

public class VectorG
extends Object
implements Cloneable, Serializable

The definition of a N-dimensional vector. Normally, one uses the daugther classes, if appropriate. The base class is only a container for an N-dimensinal double array.

See Also:
Serialized Form

Nested Class Summary
static class VectorG.CloneTest
          Testing cloning.
static class VectorG.EuclidianMetric
           
private static class VectorG.IComp
          Special index comparator.
static class VectorG.LengthComparator
          Compares the length of two vectors.
static interface VectorG.Metric
          Interface for distance matching.
 
Field Summary
protected  double[] a
           
 
Constructor Summary
protected VectorG()
          Empty constructor.
  VectorG(double[] a)
          Constructs a new, n-dimensional vector.
  VectorG(Double[] d)
          Constructs a new, n-dimensional vector.
  VectorG(int n)
          Constructs a new nvector with the specified dimension.
 
Method Summary
static VectorG add(VectorG s1, VectorG s2, VectorG dest)
          Returns the addition of two vectors with equal dimension.
 Object clone()
          Returns a clone of this VectorG.
 int dimension()
          Returns the dimension of the vector.
static Matrix direct(VectorG a, VectorG b)
          Returns the direct product of this vector with the argumental vector.
static VectorG directDivide(VectorG a, VectorG b)
          Direct multiplication of the components of two vectors.
static VectorG directMultiply(VectorG a, VectorG b)
          Direct multiplication of the components of two vectors.
static double dot(VectorG a, VectorG b)
          Returns the scalar product of this vector with the target vector.
static VectorG[] doubleParse(String doubletokenize)
          Double-tokenizes a string such that the input is split on the semicolon (actually the OUTERLIST separator of StringTool) and each of these splits is used to parse a single vector out of it.
static boolean equalData(VectorG[] a1, VectorG[] a2)
          Compares two arrays of vectors.
 boolean equals(Object that)
          Checks for equality.
static VectorG fillFrom(VectorG src, VectorG dest)
          Fills the destination vector into the source vector, starting from index 0.
static VectorG fillFrom(VectorG src, VectorG dest, int off)
          Fills the destination vector into the source vector, starting from the specified index.
static VectorG fromDoubles(List<Double> l)
           
static VectorG fromString(String tostr)
          Inversion of the toString method.
 double get(int index)
          Returns coordinate with specified index.
 double[] getAsArray()
          Returns the coordinates as an array of doubles.
 double getLength()
          Returns the length of the vector.
 int hashCode()
          A hash code for this vector.
static Map<Integer,Integer> match(List<VectorG> l1, List<VectorG> l2, double diff)
          Match two list of vectors in the sense that a vector of the first list is termed equal to a vector of the second list, if its VectorG.Metric.arclength(vec_math.VectorG, vec_math.VectorG) distance lies below the specified threshold.
static Map<Integer,Integer> match(List<VectorG> l1, List<VectorG> l2, VectorG.Metric gij, double diff)
          Match two list of vectors in the sense that a vector of the first list is termed equal to a vector of the second list, if its VectorG.Metric.arclength(vec_math.VectorG, vec_math.VectorG) distance lies below the specified threshold.
static VectorG multiply(double lambda, VectorG s1, VectorG dest)
          Multiplies this vector with a scalar.
static VectorG parse(String comma)
          Parses an VectorG from a comma-separated list of doubles.
static int search(VectorG[] sorted, int index, double max)
          Returns the integer such that
static int[] searchMinMax(VectorG[] data, int coor)
          Returns the index of the minimum and maximum vector component in the specified array.
 void set(int index, double value)
          Sets the coordinate with the specified index.
static
<T extends VectorG>
T[]
sort(T[] unsorted, int index)
          Data must be sorted according to given indices values.
static
<T extends VectorG>
T[]
sortToLength(T[] unsorted)
          An array of vectors is sorted according to their length.
static VectorG subtract(VectorG s1, VectorG s2, VectorG dest)
          Returns the subtraction of two vectors with equal dimension.
 String toString()
          Converts this vector into a String.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

a

protected double[] a
Constructor Detail

VectorG

protected VectorG()
Empty constructor. Do not use!


VectorG

public VectorG(int n)
Constructs a new nvector with the specified dimension.


VectorG

public VectorG(double[] a)
Constructs a new, n-dimensional vector. The argument array is not cloned, so be careful with re-using it.


VectorG

public VectorG(Double[] d)
Constructs a new, n-dimensional vector. The argument array is cloned, so it can be re-used.

Method Detail

parse

public static VectorG parse(String comma)
Parses an VectorG from a comma-separated list of doubles. For one, two, or three dimensional vectors, the appropriate subclass is returned.


fromDoubles

public static VectorG fromDoubles(List<Double> l)
                           throws ParseException
Throws:
ParseException

fromString

public static VectorG fromString(String tostr)
                          throws ParseException
Inversion of the toString method.

Throws:
ParseException

doubleParse

public static VectorG[] doubleParse(String doubletokenize)
Double-tokenizes a string such that the input is split on the semicolon (actually the OUTERLIST separator of StringTool) and each of these splits is used to parse a single vector out of it. Note that it is not necessary that all vectors have the same dimension.

See Also:
StringTool.OUTERLIST

get

public double get(int index)
Returns coordinate with specified index. Note the C-style counting.

Parameters:
index - Index in coordinate array

getAsArray

public double[] getAsArray()
Returns the coordinates as an array of doubles.


set

public void set(int index,
                double value)
Sets the coordinate with the specified index. Note the C-style counting.

Parameters:
index - Index in coordinate array.
value - New value

dimension

public int dimension()
Returns the dimension of the vector. This is simple the length of the double array or zero if the vector is undefined.


fillFrom

public static VectorG fillFrom(VectorG src,
                               VectorG dest)
Fills the destination vector into the source vector, starting from index 0. Throws a IllegalArguementException if the dimension is too small.


fillFrom

public static VectorG fillFrom(VectorG src,
                               VectorG dest,
                               int off)
Fills the destination vector into the source vector, starting from the specified index. Throws a IllegalArguementException if the dimension is too small.


multiply

public static VectorG multiply(double lambda,
                               VectorG s1,
                               VectorG dest)
Multiplies this vector with a scalar. After the operation, this contains the stretched vector if the destination is this. If the destination is null, a new vector is allocated.

Returns:
The resulting vector.

dot

public static double dot(VectorG a,
                         VectorG b)
Returns the scalar product of this vector with the target vector. The scalar product is only defined if the dimension of the two vectors are equal. In index notion, the scalar product is defined as:
 a⋅b = δijaibi
 

Returns:
The scalar product of the two argument vectors.
Throws:
IllegalArgumentException - If the dimension of the two vectors are not identical.

getLength

public double getLength()
Returns the length of the vector. This is the dot product of this vector with itself and square-rooted.


direct

public static Matrix direct(VectorG a,
                            VectorG b)
Returns the direct product of this vector with the argumental vector. The result is a (this.dimension() x argument.dimension()) matrix. In index notation the direct product reads as:
 (a⊗b)ij=aibj
 
Note that the direct product is not commutative.


directMultiply

public static VectorG directMultiply(VectorG a,
                                     VectorG b)
Direct multiplication of the components of two vectors.
 v_i=v1_i*v2_i,
 


directDivide

public static VectorG directDivide(VectorG a,
                                   VectorG b)
Direct multiplication of the components of two vectors.
 v_i=v1_i/v2_i,
 


add

public static VectorG add(VectorG s1,
                          VectorG s2,
                          VectorG dest)
Returns the addition of two vectors with equal dimension. If the destination argument is null, a new vector is allocated. It is legal to specify equal source and destination, where in this case the source is overwirttn.


subtract

public static VectorG subtract(VectorG s1,
                               VectorG s2,
                               VectorG dest)
Returns the subtraction of two vectors with equal dimension. If the destination argument is null, a new vector is allocated. It is legal to specify equal source and destination, where in this case the source is overwritten.

Returns:
s1-s2

equals

public boolean equals(Object that)
Checks for equality. Two VectorGs are considered equal, if

Overrides:
equals in class Object
Parameters:
that - The VectorG to check.

toString

public String toString()
Converts this vector into a String.

Overrides:
toString in class Object

hashCode

public int hashCode()
A hash code for this vector. See "Effective Java".

Overrides:
hashCode in class Object

clone

public Object clone()
Returns a clone of this VectorG. In particular, a new, N-dimensional array a is created in the cloned VectorG and its values are copied.

Overrides:
clone in class Object

sort

public static <T extends VectorG> T[] sort(T[] unsorted,
                                           int index)
Data must be sorted according to given indices values. This can be done here. Affects the argument as well. Returns the sorted VectorG array, which is the input argument.


sortToLength

public static <T extends VectorG> T[] sortToLength(T[] unsorted)
An array of vectors is sorted according to their length. Also sorts vectors of non-equal dimension. This comparison is consistent with equals, as for equal length it checks first the dimension, sorting for lower dimension, if not equal, then diving further into the indices until a difference is found. Zero is returned only if the two vectors are equal.


equalData

public static boolean equalData(VectorG[] a1,
                                VectorG[] a2)
Compares two arrays of vectors. Returns true if the arrays are of same length and have the same data, ignoring indices. Copies the original data into new arrays, if needed, sorts them and returns true, if in the length-sorted array, all vectors are equal.


search

public static int search(VectorG[] sorted,
                         int index,
                         double max)
Returns the integer such that
 VectorG[index]>=f&&VectorG[index-1]<f
 
or -1.


searchMinMax

public static int[] searchMinMax(VectorG[] data,
                                 int coor)
Returns the index of the minimum and maximum vector component in the specified array. The index given is the coordinate on which to search.

Parameters:
coor - Search is performed on this vector index, 0 to dimension-1.

match

public static Map<Integer,Integer> match(List<VectorG> l1,
                                         List<VectorG> l2,
                                         double diff)
Match two list of vectors in the sense that a vector of the first list is termed equal to a vector of the second list, if its VectorG.Metric.arclength(vec_math.VectorG, vec_math.VectorG) distance lies below the specified threshold. If more than a single vector matches, the one with the smalles arclength is considered the match. This version uses the default euclidian metric g=1 ds&pow2;=dxμdxμ

Returns:
a mapping of indices of the vector, only matched.

match

public static Map<Integer,Integer> match(List<VectorG> l1,
                                         List<VectorG> l2,
                                         VectorG.Metric gij,
                                         double diff)
Match two list of vectors in the sense that a vector of the first list is termed equal to a vector of the second list, if its VectorG.Metric.arclength(vec_math.VectorG, vec_math.VectorG) distance lies below the specified threshold. If more than a single vector matches, the one with the smalles arclength is considered the match. This version uses a general metric gij ds&pow2;= gijdxidxj

Returns:
a mapping of indices of the vector, only matched.