vec_math
Class NVector

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

public class NVector
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 NVector.CloneTest
          Testing cloning.
private static class NVector.IComp
          Special index comparator.
 
Field Summary
protected  double[] a
           
 
Constructor Summary
protected NVector()
          Empty constructor.
  NVector(double[] a)
          Constructs a new, n-dimensional vector.
  NVector(Double[] d)
          Constructs a new, n-dimensional vector.
  NVector(int n)
          Constructs a new nvector with the specified dimension.
 
Method Summary
static NVector add(NVector s1, NVector s2, NVector dest)
          Returns the addition of two vectors with equal dimension.
 Object clone()
          Returns a clone of this NVector.
 int dimension()
          Returns the dimension of the vector.
static Matrix direct(NVector a, NVector b)
          Returns the direct product of this vector with the argumental vector.
static double dot(NVector a, NVector b)
          Returns the scalar product of this vector with the target vector.
static NVector[] 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.
 boolean equals(Object that)
          Checks for equality.
 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 NVector multiply(double lambda, NVector s1, NVector dest)
          Multiplies this vector with a scalar.
static NVector parse(String comma)
          Parses an NVector from a comma-sparated list of doubles.
static int search(NVector[] sorted, int index, double max)
          Returns the integer such that
static int[] searchMinMax(NVector[] 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 NVector>
T[]
sort(T[] unsorted, int index)
          Data must be sorted according to given indices values.
static NVector subtract(NVector s1, NVector s2, NVector 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

NVector

protected NVector()
Empty constructor. Do not use!


NVector

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


NVector

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


NVector

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

Method Detail

parse

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


doubleParse

public static NVector[] 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.


multiply

public static NVector multiply(double lambda,
                               NVector s1,
                               NVector 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(NVector a,
                         NVector 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(NVector a,
                            NVector 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.


add

public static NVector add(NVector s1,
                          NVector s2,
                          NVector 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 NVector subtract(NVector s1,
                               NVector s2,
                               NVector 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 overwirttn.


equals

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

Overrides:
equals in class Object
Parameters:
that - The NVector 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 NVector. In particular, a new, N-dimensional array a is created in the cloned NVector and its values are copied.

Overrides:
clone in class Object

sort

public static <T extends NVector> 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 NVector array, which is the input argument.


search

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


searchMinMax

public static int[] searchMinMax(NVector[] 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.