maspack.matrix
Class VectorObject

java.lang.Object
  extended bymaspack.matrix.VectorObject
Direct Known Subclasses:
Quaternion, SpatialVector, Vector2d, Vector3d, Vector4d, VectorNd

public abstract class VectorObject
extends java.lang.Object

Base class for vectors. It provides methods which allow one to set and get elements of a vector, to do various non-modifying queries such as finding out its size, computing it's norms, or comparing it with other vectors. There are also methods fo producing a string representation of the vector and for reading its contents from a tokenizer. There is an abstract method setSize for resizing, which can be used unless the vector size is fixed (which can be determined using isFixedSize).

This base class does not publicly support vector operations such as addition or scaling. The reason for that is that subclasses may implement vectors with a specific structure, which could then be compromised by arbitrary operations. For instance, if a subclass of VectorObject implements unit vectors, it would not make sense for that class to allow an add operation.

Of course, it is possible to corrupt any special subclass structure using the set methods provided in this base class, but it was felt that not including such routines would be overly restrictive. It is therefore up to the user to safeguard subclass integrity when using them.

Note that indices for vector elements are zero-based, so that the range of valid indices for a vector of length n is [0, ... , n-1].


Constructor Summary
VectorObject()
           
 
Method Summary
 double dot(VectorObject v1)
          Returns the dot product of this vector and v1.
 boolean epsilonEquals(VectorObject v1, double eps)
          Returns true if the elements of this vector equal those of vector v1within a prescribed tolerance epsilon.
 boolean equals(java.lang.Object obj)
          Returns true if this vector and a specified object have the same class type and if all the elements are exactly equal.
 boolean equals(VectorObject v1)
          Returns true if the elements of this vector exactly equal those of vector v1.
 void get(double[] values)
          Copies the elements of this vector into an array of doubles.
abstract  double get(int i)
          Gets a single element of this vector.
 void get(VectorObject v1)
          Copies the elements of this vector into another vector object.
 double infinityNorm()
          Returns the infinity norm of this vector.
 boolean isFixedSize()
          Returns true if this vector is of fixed size.
 double maxElement()
          Returns the maximum element value.
 double minElement()
          Returns the minimum element value.
 double norm()
          Returns the 2 norm of this vector.
 double normSquared()
          Returns the square of the 2 norm of this vector.
 double oneNorm()
          Returns the 1 norm of this vector.
 void scan(java.io.StreamTokenizer stok)
          Sets the contents of this vector to values read from a StreamTokenizer.
 void set(double[] values)
          Sets the elements of this vector from an array of doubles.
abstract  void set(int i, double value)
          Sets a single element of this vector.
 void set(VectorObject v)
          Sets the values of this vector to those of another vector.
 void setSize(int n)
          Sets the size of this vector.
abstract  int size()
          Returns the number of elements in this vector.
 java.lang.String toString()
          Returns a String representation of this vector, using the default format "%g" (see toString(String).
 java.lang.String toString(NumberFormat fmt)
          Returns a String representation of this vector, in which each element is formated using a C printf style as decribed by the parameter NumberFormat.
 java.lang.String toString(java.lang.String fmtStr)
          Returns a String representation of this vector, in which each element is formated using a C printf style format string.
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

VectorObject

public VectorObject()
Method Detail

size

public abstract int size()
Returns the number of elements in this vector.

Returns:
number of elements

get

public abstract double get(int i)
Gets a single element of this vector.

Parameters:
i - element index
Returns:
element value

get

public void get(double[] values)
Copies the elements of this vector into an array of doubles.

Parameters:
values - array into which values are copied

get

public void get(VectorObject v1)
         throws ImproperSizeException
Copies the elements of this vector into another vector object.

Parameters:
v1 - vector object into which values are copied
Throws:
ImproperSizeExcept - if the vector objects have different sizes
ImproperSizeException

set

public abstract void set(int i,
                         double value)
Sets a single element of this vector.

Parameters:
i - element index
value - element value

set

public void set(double[] values)
Sets the elements of this vector from an array of doubles.

Parameters:
values - array from which values are copied

set

public void set(VectorObject v)
Sets the values of this vector to those of another vector.

Parameters:
v - vector from which values are copied
Throws:
ImproperSizeException - vectors have different sizes and this vector cannot be resized accordingly

isFixedSize

public boolean isFixedSize()
Returns true if this vector is of fixed size. If this vector is not of fixed size, then it can be resized dynamically, either explicitly using setSize, or implicitly when used as a result for various vector operations.

Returns:
true if this vector is of fixed size
See Also:
setSize(int)

setSize

public void setSize(int n)
Sets the size of this vector. This operation is only supported if isFixedSize returns false.

Parameters:
n - new size
Throws:
java.lang.UnsupportedOperationException - if this operation is not supported
See Also:
isFixedSize()

norm

public double norm()
Returns the 2 norm of this vector. This is the square root of the sum of the squares of the elements.

Returns:
vector 2 norm

normSquared

public double normSquared()
Returns the square of the 2 norm of this vector. This is the sum of the squares of the elements.

Returns:
square of the 2 norm

oneNorm

public double oneNorm()
Returns the 1 norm of this vector. This is the sum of the absolute values of the elements.

Returns:
vector 1 norm

infinityNorm

public double infinityNorm()
Returns the infinity norm of this vector. This is the maximum absolute value over all elements.

Returns:
vector infinity norm

maxElement

public double maxElement()
Returns the maximum element value.

Returns:
maximal element

minElement

public double minElement()
Returns the minimum element value.

Returns:
minimal element

dot

public double dot(VectorObject v1)
           throws ImproperSizeException
Returns the dot product of this vector and v1.

Parameters:
v1 - right-hand vector
Returns:
dot product
Throws:
ImproperSizeException - if this vector and v1 have different sizes

epsilonEquals

public boolean epsilonEquals(VectorObject v1,
                             double eps)
                      throws ImproperSizeException
Returns true if the elements of this vector equal those of vector v1within a prescribed tolerance epsilon.

Parameters:
v1 - vector to compare with
eps - comparison tolerance
Returns:
false if the vectors are not equal within the specified tolerance, or have different sizes
Throws:
ImproperSizeException

equals

public boolean equals(VectorObject v1)
               throws ImproperSizeException
Returns true if the elements of this vector exactly equal those of vector v1.

Parameters:
v1 - vector to compare with
Returns:
false if the vectors are not equal or have different sizes
Throws:
ImproperSizeException

equals

public boolean equals(java.lang.Object obj)
Returns true if this vector and a specified object have the same class type and if all the elements are exactly equal.

Parameters:
obj - object to compare with
Returns:
false if the objects are not equal

toString

public java.lang.String toString()
Returns a String representation of this vector, using the default format "%g" (see toString(String).

Returns:
String representation of this vector

toString

public java.lang.String toString(java.lang.String fmtStr)
Returns a String representation of this vector, in which each element is formated using a C printf style format string. For a description of the format string syntax, see NumberFormat.

Parameters:
fmtStr - numeric format specification
Returns:
String representation of this vector

toString

public java.lang.String toString(NumberFormat fmt)
Returns a String representation of this vector, in which each element is formated using a C printf style as decribed by the parameter NumberFormat. When called numerous times, this routine can be more efficient than toString(String), because the NumberFormat does not need to be recreated each time from a specification string.

Parameters:
fmt - numeric format
Returns:
String representation of this vector

scan

public void scan(java.io.StreamTokenizer stok)
          throws java.io.IOException
Sets the contents of this vector to values read from a StreamTokenizer. The vector elements should be separated by white space, and the entire set of elements should be surrounded by square brackets. For example,
 [ 1.2 4 5 3.1 ]
 
defines a vector of size 4.

If the vector is not of fixed-size, it will be resized to fit the input. The StreamTokenizer will be set by this method to parse numbers, and it should already be configured to recognize the characters '+' and - as tokens.

Note:This method disables numeric parsing in the StreamTokenizer. This is because the method does its own numeric parsing, in order to handle exponents.

Parameters:
stok - Tokenizer from which vector values are read
Throws:
ImproperSizeException - if this vector has a fixed size which is incompatible with the input
java.io.IOException