maspack.matrix
Class VectorBase

java.lang.Object
  extended by maspack.matrix.VectorBase
All Implemented Interfaces:
java.lang.Cloneable, Vector, Clonable
Direct Known Subclasses:
Quaternion, SparseVectorNd, SpatialVector, Vector2d, Vector3d, Vector4d, VectorNd, VoidVector

public abstract class VectorBase
extends java.lang.Object
implements Vector

Base implementation of Vector.


Constructor Summary
VectorBase()
           
 
Method Summary
 VectorBase clone()
           
 boolean containsNaN()
          Returns true if any element of this vector is not a number.
 VectorNd copy()
           
 VectorNd copyAndAdd(Vector v2)
           
 VectorNd copyAndNegate()
           
 VectorNd copyAndScale(double s)
           
 VectorNd copyAndSub(Vector v2)
           
 VectorNd copyAndSubLeft(Vector v2)
           
 double dot(Vector v1)
          Returns the dot product of this vector and v1.
 boolean epsilonEquals(Vector v1, double eps)
          Returns true if the elements of this vector equal those of vector v1within a prescribed tolerance epsilon.
 boolean equals(Vector 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(Vector v1)
          Copies the elements of this vector into another vector object.
static java.lang.String getDefaultFormat()
          Returns the default format string used in toString.
 boolean hasNaN()
          Returns true if one or more elements of this vector is NaN.
 double infinityNorm()
          Returns the infinity norm of this vector.
static boolean isColumnVectorStringsVertical()
          Returns true if the string representation of column vectors is vertical.
 boolean isFixedSize()
          Returns true if this vector is of fixed size.
 boolean isRowVector()
           
 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(ReaderTokenizer rtok)
          Sets the contents of this vector to values read from a ReaderTokenizer.
 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(Matrix M)
          Sets the values of this vector to those of a matrix object.
 void set(Vector v)
          Sets the values of this vector to those of another vector.
static void setColumnVectorStringsVertical(boolean enable)
          Sets the string representation of column vectors to be vertical.
static void setDefaultFormat(java.lang.String fmtStr)
          Sets the default format string used in toString.
 boolean setRowVector(boolean isRow)
           
 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 returned by getDefaultFormat.
 java.lang.String toString(NumberFormat fmt)
          Returns a String representation of this vector, in which each element is formatted using a C printf style format 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 formatted using a C printf style format string.
 void write(java.io.PrintWriter pw, NumberFormat fmt)
          Writes the contents of this vector to a PrintWriter.
 void write(java.io.PrintWriter pw, NumberFormat fmt, boolean withBrackets)
          Writes the contents of this vector to a PrintWriter.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

VectorBase

public VectorBase()
Method Detail

setDefaultFormat

public static void setDefaultFormat(java.lang.String fmtStr)
Sets the default format string used in toString. For a description of the format string syntax, see NumberFormat.

Parameters:
fmtStr - new format string
Throws:
java.lang.IllegalArgumentException - if the format string is invalid
See Also:
getDefaultFormat()

getDefaultFormat

public static java.lang.String getDefaultFormat()
Returns the default format string used in toString. If unset, this string is "%g". For a description of the format string syntax, see NumberFormat.

Returns:
Default format string

setColumnVectorStringsVertical

public static void setColumnVectorStringsVertical(boolean enable)
Sets the string representation of column vectors to be vertical. By default, this is false.

Parameters:
enable - if true, column vector strings are arranged vertically.
See Also:
isColumnVectorStringsVertical()

isColumnVectorStringsVertical

public static boolean isColumnVectorStringsVertical()
Returns true if the string representation of column vectors is vertical.

Returns:
true if column vector strings are arranged vertically.
See Also:
setColumnVectorStringsVertical(boolean)

size

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

Specified by:
size in interface Vector
Returns:
number of elements

get

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

Specified by:
get in interface 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.

Specified by:
get in interface Vector
Parameters:
values - array into which values are copied

get

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

Specified by:
get in interface Vector
Parameters:
v1 - vector object into which values are copied
Throws:
ImproperSizeException

set

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

Specified by:
set in interface 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.

Specified by:
set in interface Vector
Parameters:
values - array from which values are copied

set

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

Specified by:
set in interface Vector
Parameters:
v - vector from which values are copied

set

public void set(Matrix M)
Sets the values of this vector to those of a matrix object. The matrix must have either one column or one row; in the latter case, an attempt will be made to declare this vector a column vector, and if this fails, then an ImproperSizeException will be raised.

Specified by:
set in interface Vector
Parameters:
M - matrix from which values are copied

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.

Specified by:
isFixedSize in interface Vector
Returns:
true if this vector is of fixed size
See Also:
Vector.setSize(int)

setSize

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

Specified by:
setSize in interface Vector
Parameters:
n - new size
See Also:
Vector.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.

Specified by:
norm in interface Vector
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.

Specified by:
normSquared in interface Vector
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.

Specified by:
oneNorm in interface Vector
Returns:
vector 1 norm

infinityNorm

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

Specified by:
infinityNorm in interface Vector
Returns:
vector infinity norm

maxElement

public double maxElement()
Returns the maximum element value.

Specified by:
maxElement in interface Vector
Returns:
maximal element

minElement

public double minElement()
Returns the minimum element value.

Specified by:
minElement in interface Vector
Returns:
minimal element

dot

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

Specified by:
dot in interface Vector
Parameters:
v1 - right-hand vector
Returns:
dot product
Throws:
ImproperSizeException - if this vector and v1 have different sizes

epsilonEquals

public boolean epsilonEquals(Vector v1,
                             double eps)
Returns true if the elements of this vector equal those of vector v1within a prescribed tolerance epsilon. If the vectors have different sizes, false is returned.

Specified by:
epsilonEquals in interface Vector
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

equals

public boolean equals(Vector v1)
Returns true if the elements of this vector exactly equal those of vector v1. If the vectors have different sizes, false is returned.

Specified by:
equals in interface Vector
Parameters:
v1 - vector to compare with
Returns:
false if the vectors are not equal or have different sizes

toString

public java.lang.String toString()
Returns a String representation of this vector, using the default format returned by getDefaultFormat.

Overrides:
toString in class java.lang.Object
Returns:
String representation of this vector
See Also:
toString(String)

toString

public java.lang.String toString(java.lang.String fmtStr)
Returns a String representation of this vector, in which each element is formatted using a C printf style format string. For a description of the format string syntax, see NumberFormat. Note that when called numerous times, toString(NumberFormat) will be more efficient because the NumberFormat will not need to be recreated each time from a specification string.

Parameters:
fmtStr - numeric format specification
Returns:
String representation of this vector
See Also:
isColumnVectorStringsVertical()

toString

public java.lang.String toString(NumberFormat fmt)
Returns a String representation of this vector, in which each element is formatted using a C printf style format as decribed by the parameter NumberFormat.

Specified by:
toString in interface Vector
Parameters:
fmt - numeric format
Returns:
String representation of this vector
See Also:
isColumnVectorStringsVertical()

write

public void write(java.io.PrintWriter pw,
                  NumberFormat fmt)
           throws java.io.IOException
Writes the contents of this vector to a PrintWriter. Element values are separated by spaces, and each element is formatted using a C printf style as decribed by the parameter NumberFormat.

Specified by:
write in interface Vector
Parameters:
pw - PrintWriter to write this vector to
fmt - numeric format
Throws:
java.io.IOException

write

public void write(java.io.PrintWriter pw,
                  NumberFormat fmt,
                  boolean withBrackets)
           throws java.io.IOException
Writes the contents of this vector to a PrintWriter. Element values are separated by spaces, and optionally surrounded by square brackets [ ] if withBrackets is set true. Each element is formatted using a C printf style as decribed by the parameter NumberFormat.

Specified by:
write in interface Vector
Parameters:
pw - PrintWriter to write this vector to
fmt - numeric format
withBrackets - if true, causes the output to be surrounded by square brackets.
Throws:
java.io.IOException

scan

public void scan(ReaderTokenizer rtok)
          throws java.io.IOException
Sets the contents of this vector to values read from a ReaderTokenizer. The input should consist of a sequence of numbers, separated by white space and optionally surrounded by square brackets [ ].

If the input is not surrounded by square brackets, then the number of values should equal the current size of this vector.

If the input is surrounded by square brackets, then all values up to the closing bracket are read, and the resulting number of values should either equal the current size of this vector, or this vector should be resizeable to fit the input. For example,

 [ 1.2 4 5 3.1 ]
 
defines a vector of size 4.

Specified by:
scan in interface Vector
Parameters:
rtok - Tokenizer from which vector values are read. Number parsing should be enabled.
Throws:
java.io.IOException

containsNaN

public boolean containsNaN()
Returns true if any element of this vector is not a number.

Returns:
true if any element is NaN

copy

public VectorNd copy()

copyAndNegate

public VectorNd copyAndNegate()

copyAndAdd

public VectorNd copyAndAdd(Vector v2)

copyAndSub

public VectorNd copyAndSub(Vector v2)

copyAndSubLeft

public VectorNd copyAndSubLeft(Vector v2)

copyAndScale

public VectorNd copyAndScale(double s)

isRowVector

public boolean isRowVector()

setRowVector

public boolean setRowVector(boolean isRow)

hasNaN

public boolean hasNaN()
Returns true if one or more elements of this vector is NaN.

Returns:
true if one or more elements is NaN

clone

public VectorBase clone()
Specified by:
clone in interface Clonable
Overrides:
clone in class java.lang.Object