maspack.matrix
Class VectorNd

java.lang.Object
  extended bymaspack.matrix.VectorObject
      extended bymaspack.matrix.VectorNd
All Implemented Interfaces:
java.io.Serializable

public class VectorNd
extends VectorObject
implements java.io.Serializable

Implements general vectors, along with their most commonly used operations.

These vectors can be resized, either explicitly through a call to setSize, or implicitly through operations that require the vector size to be modified.

See Also:
Serialized Form

Constructor Summary
VectorNd(double[] values)
          Creates a vector from an array of doubles.
VectorNd(int size)
          Creates a vector of a specific size, and initializes its elements to 0.
VectorNd(int size, double[] values)
          Creates a vector of a specific size, and initializes its elements from an array of values.
VectorNd(VectorObject v)
          Creates a vector whose size and elements are the same as an existing VectorObject.
 
Method Summary
 void absolute()
          Sets the elements of this vector to their absolute values.
 void absolute(VectorNd v1)
          Sets the elements of this vector to the absolute value of v1.
 void add(VectorNd v1)
          Adds this vector to v1 and places the result in this vector.
 void add(VectorNd v1, VectorNd v2)
          Adds vector v1 to v2 and places the result in this vector.
 double angle(VectorNd v1)
          Returns the angle between this vector and v1.
 void combine(double s1, VectorNd v1, double s2, VectorNd v2)
          Computes s1 v1 + s2 v2 and places the result in this vector.
 double dot(VectorNd v1)
          Returns the dot product of this vector and v1.
 boolean epsilonEquals(VectorNd v1, double eps)
          Returns true if the elements of this vector equal those of vector v1within a prescribed tolerance epsilon.
 boolean equals(VectorNd 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.
 double get(int i)
          Gets a single element of this vector.
 double[] getBuffer()
          Returns the internal buffer used to store the elements in this vector.
 double infinityNorm()
          Returns the infinity norm of this vector.
 void interpolate(double s, VectorNd v1)
          Computes the interpolation (1-s) this + s v1 and places the result in this vector.
 void interpolate(VectorNd v1, double s, VectorNd v2)
          Computes the interpolation (1-s) v1 + s v2 and places the result in this vector.
 boolean isFixedSize()
          Returns true if this vector is of fixed size.
 double maxElement()
          Returns the maximum element value of this vector.
 double minElement()
          Returns the minimum element value of this vector.
 void mul(MatrixNd M, VectorNd b)
          Multiplies matrix M by the vector b and places the result in this vector.
 void mulTranspose(MatrixNd M, VectorNd b)
          Multiplies the transpose of matrix M by the vector b and places the result in this vector.
 void negate()
          Negates this vector in place.
 void negate(VectorNd v1)
          Sets this vector to the negative of v1.
 double norm()
          Returns the 2 norm of this vector.
 void normalize()
          Normalizes this vector in place.
 void normalize(VectorNd v1)
          Computes a unit vector in the direction of v1 and places the result in this vector.
 double normSquared()
          Returns the square of the 2 norm of this vector.
 double oneNorm()
          Returns the 1 norm of this vector.
 void scale(double s)
          Scales the elements of this vector by s.
 void scale(double s, VectorNd v1)
          Scales the elements of vector v1 by s and places the results in this vector.
 void scaleAdd(double s, VectorNd v1)
          Computes s this + v1 and places the result in this vector.
 void scaleAdd(double s, VectorNd v1, VectorNd v2)
          Computes s v1 + v2 and places the result in this vector.
 void set(double[] values)
          Sets the elements of this vector from an array of doubles.
 void set(int i, double value)
          Sets a single element of this vector.
 void set(VectorNd v1)
          Sets the size and values of this vector to those of v1.
 void setBuffer(int size, double[] buffer)
          Explicitly sets the size and internal buffer associated with this vector.
 void setRandom()
          Sets the elements of this vector to uniformly distributed random values in the range -0.5 (inclusive) to 0.5 (exclusive).
 void setRandom(double lower, double upper)
          Sets the elements of this vector to uniformly distributed random values in a specified range.
 void setRandom(double lower, double upper, java.util.Random generator)
          Sets the elements of this vector to uniformly distributed random values in a specified range, using a supplied random number generator.
 void setSize(int newSize)
          Sets the size of this vector.
 void setZero()
          Sets the elements of this vector to zero.
 int size()
          Returns the size of this vector.
 void sort()
          Sorts the contents of this vector by element value, from largest to smallest value.
 void sort(VectorNd v1)
          Sorts the contents of vector v1 by element value, from largest to smallest value, and places the result into this vector.
 void sub(VectorNd v1)
          Subtracts v1 from this vector and places the result in this vector.
 void sub(VectorNd v1, VectorNd v2)
          Subtracts vector v1 from v2 and places the result in this vector.
 void unsetBuffer()
          Removes an explicit buffer provided for this vector and replaces it with a default implicit buffer.
 
Methods inherited from class maspack.matrix.VectorObject
dot, epsilonEquals, equals, equals, get, scan, set, toString, toString, toString
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

VectorNd

public VectorNd(int size)
         throws ImproperSizeException
Creates a vector of a specific size, and initializes its elements to 0. It is legal to create a vector with a size of zero.

Parameters:
size - size of the vector
Throws:
ImproperSizeException - if size is negative

VectorNd

public VectorNd(int size,
                double[] values)
         throws ImproperSizeException
Creates a vector of a specific size, and initializes its elements from an array of values.

Parameters:
size - size of the vector
values - element values for the new vector
Throws:
ImproperSizeException - if size is negative

VectorNd

public VectorNd(double[] values)
Creates a vector from an array of doubles. The vector size is determined by the size of this array.

Parameters:
values - element values for the new vector

VectorNd

public VectorNd(VectorObject v)
Creates a vector whose size and elements are the same as an existing VectorObject.

Parameters:
v - vector object to be copied.
Method Detail

getBuffer

public double[] getBuffer()
Returns the internal buffer used to store the elements in this vector. When possible, applications should access the vector elements using the various set and get methods. However, if efficiency requires it, this buffer can be used directly.

Note that the buffer may be larger than the vector. The i-th element in the vector corresponds to the i-th entry in the buffer.

If this vector is resized, then the internal buffer may change and the buffer previously returned by this routine may no longer be valid.

Returns:
internal buffer for this vector
See Also:
setBuffer(int, double[])

size

public int size()
Returns the size of this vector.

Specified by:
size in class VectorObject
Returns:
size of the vector

isFixedSize

public boolean isFixedSize()
Returns true if this vector is of fixed size. In the present implementation, VectorNd objects always have variable size, and so this routine always returns false.

Overrides:
isFixedSize in class VectorObject
Returns:
true if this vector is of fixed size
See Also:
setSize(int)

setSize

public void setSize(int newSize)
             throws ImproperSizeException
Sets the size of this vector. This operation may enlarge the internal buffer associated with this vector, invalidating buffers previously returned by getBuffer.

If a vector is resized, then previous element values which are still within the new vector dimension are preserved. Other (new) element values are undefined.

Overrides:
setSize in class VectorObject
Parameters:
newSize - new vector size
Throws:
ImproperSizeException - if this vector has an explicit internal buffer and that buffer is too small for the requested size
See Also:
VectorObject.isFixedSize()

setBuffer

public void setBuffer(int size,
                      double[] buffer)
Explicitly sets the size and internal buffer associated with this vector. Any previous values will be discarded, and the vector will assume the new values presently contained within the buffer. The length of buffer must equal or exceed the specified size. The vector can continue to be resized as long as the requested sizes do not exceed the buffer size.

Parameters:
size - new vector size
buffer - explicit buffer for this vector
Throws:
ImproperSizeException - if the specified buffer is too small for the requested size
See Also:
unsetBuffer()

unsetBuffer

public void unsetBuffer()
Removes an explicit buffer provided for this vector and replaces it with a default implicit buffer. The vector retains its present size but all values are replaced with zero.

Throws:
java.lang.IllegalStateException - if this vector does not have an explicit buffer given by setBuffer
See Also:
setBuffer(int, double[])

get

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

Specified by:
get in class VectorObject
Parameters:
i - element index
Returns:
element value

get

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

Overrides:
get in class VectorObject
Parameters:
values - array into which values are copied

set

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

Specified by:
set in class VectorObject
Parameters:
i - element index
value - element value

set

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

Overrides:
set in class VectorObject
Parameters:
values - array from which values are copied

set

public void set(VectorNd v1)
Sets the size and values of this vector to those of v1.

Parameters:
v1 - vector whose size and values are copied
Throws:
ImproperSizeException - if this vector needs resizing but is of fixed size

add

public void add(VectorNd v1,
                VectorNd v2)
         throws ImproperSizeException
Adds vector v1 to v2 and places the result in this vector. This vector is resized if necessary.

Parameters:
v1 - left-hand vector
v2 - right-hand vector
Throws:
ImproperSizeException - if v1 and v2 have different sizes, or if this vector needs resizing but is of fixed size

add

public void add(VectorNd v1)
         throws ImproperSizeException
Adds this vector to v1 and places the result in this vector.

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

sub

public void sub(VectorNd v1,
                VectorNd v2)
         throws ImproperSizeException
Subtracts vector v1 from v2 and places the result in this vector. This vector is resized if necessary.

Parameters:
v1 - left-hand vector
v2 - right-hand vector
Throws:
ImproperSizeException - if v1 and v2 have different sizes, or if this vector needs resizing but is of fixed size

sub

public void sub(VectorNd v1)
         throws ImproperSizeException
Subtracts v1 from this vector and places the result in this vector.

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

negate

public void negate(VectorNd v1)
Sets this vector to the negative of v1. This vector is resized if necessary.

Parameters:
v1 - vector to negate
Throws:
ImproperSizeException - if this vector needs resizing but is of fixed size

negate

public void negate()
Negates this vector in place.


scale

public void scale(double s)
Scales the elements of this vector by s.

Parameters:
s - scaling factor

scale

public void scale(double s,
                  VectorNd v1)
Scales the elements of vector v1 by s and places the results in this vector. This vector is resized if necessary.

Parameters:
s - scaling factor
v1 - vector to be scaled
Throws:
ImproperSizeException - if this vector needs resizing but is of fixed size

interpolate

public void interpolate(VectorNd v1,
                        double s,
                        VectorNd v2)
                 throws ImproperSizeException
Computes the interpolation (1-s) v1 + s v2 and places the result in this vector. This vector is resized if necessary.

Parameters:
v1 - left-hand vector
s - interpolation factor
v2 - right-hand vector
Throws:
ImproperSizeException - if v1 and v2 have different sizes, or if this vector needs resizing but is of fixed size

interpolate

public void interpolate(double s,
                        VectorNd v1)
                 throws ImproperSizeException
Computes the interpolation (1-s) this + s v1 and places the result in this vector.

Parameters:
s - interpolation factor
v1 - right-hand vector
Throws:
ImproperSizeException - if this vector and v1 have different sizes

scaleAdd

public void scaleAdd(double s,
                     VectorNd v1,
                     VectorNd v2)
              throws ImproperSizeException
Computes s v1 + v2 and places the result in this vector. This vector is resized if necessary.

Parameters:
s - scaling factor
v1 - vector to be scaled
v2 - vector to be added
Throws:
ImproperSizeException - if v1 and v2 have different sizes, or if this vector needs resizing but is of fixed size

scaleAdd

public void scaleAdd(double s,
                     VectorNd v1)
              throws ImproperSizeException
Computes s this + v1 and places the result in this vector.

Parameters:
s - scaling factor
v1 - vector to be added
Throws:
ImproperSizeException - if this vector and v1 have different sizes

combine

public void combine(double s1,
                    VectorNd v1,
                    double s2,
                    VectorNd v2)
             throws ImproperSizeException
Computes s1 v1 + s2 v2 and places the result in this vector. This vector is resized if necessary.

Parameters:
s1 - left-hand scaling factor
v1 - left-hand vector
s2 - right-hand scaling factor
v2 - right-hand vector
Throws:
ImproperSizeException - if v1 and v2 have different sizes, or if this vector needs resizing but is of fixed size

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.

Overrides:
norm in class VectorObject
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.

Overrides:
normSquared in class VectorObject
Returns:
square of the 2 norm

maxElement

public double maxElement()
Returns the maximum element value of this vector.

Overrides:
maxElement in class VectorObject
Returns:
maximal element

minElement

public double minElement()
Returns the minimum element value of this vector.

Overrides:
minElement in class VectorObject
Returns:
minimal element

infinityNorm

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

Overrides:
infinityNorm in class VectorObject
Returns:
vector infinity norm

oneNorm

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

Overrides:
oneNorm in class VectorObject
Returns:
vector 1 norm

dot

public double dot(VectorNd 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

angle

public double angle(VectorNd v1)
             throws ImproperSizeException
Returns the angle between this vector and v1. The angle is defined as acos(c), where c is the dot product of unit vectors parallel to this vector and v1.

Parameters:
v1 - right-hand vector
Returns:
angle between vectors, in radians
Throws:
ImproperSizeException - if this vector and v1 have different sizes

normalize

public void normalize()
Normalizes this vector in place.


normalize

public void normalize(VectorNd v1)
Computes a unit vector in the direction of v1 and places the result in this vector. This vector is resized if necessary.

Parameters:
v1 - vector to normalize
Throws:
ImproperSizeException - if this vector needs resizing but is of fixed size

epsilonEquals

public boolean epsilonEquals(VectorNd 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(VectorNd 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

setZero

public void setZero()
Sets the elements of this vector to zero.


absolute

public void absolute()
Sets the elements of this vector to their absolute values.


absolute

public void absolute(VectorNd v1)
Sets the elements of this vector to the absolute value of v1. This vector is resized if necessary.

Parameters:
v1 - vector to take the absolute value of
Throws:
ImproperSizeException - if this vector needs resizing but is of fixed size

sort

public void sort()
Sorts the contents of this vector by element value, from largest to smallest value.


sort

public void sort(VectorNd v1)
Sorts the contents of vector v1 by element value, from largest to smallest value, and places the result into this vector.

Parameters:
v1 - vector to sort

mul

public void mul(MatrixNd M,
                VectorNd b)
Multiplies matrix M by the vector b and places the result in this vector. This vector is resized if necessary.

Parameters:
M - left-hand matrix
b - right-hand vector
Throws:
ImproperSizeException - if the size of b does not equal the number of columns of M, or if this vector needs resizing but is of fixed size

mulTranspose

public void mulTranspose(MatrixNd M,
                         VectorNd b)
Multiplies the transpose of matrix M by the vector b and places the result in this vector. Note that this is equivalent to pre-multiplying M by b. This vector is resized if necessary.

Parameters:
M - left-hand matrix
b - right-hand vector
Throws:
ImproperSizeException - if the size of b does not equal the number of rows of M, or if this vector needs resizing but is of fixed size

setRandom

public void setRandom()
Sets the elements of this vector to uniformly distributed random values in the range -0.5 (inclusive) to 0.5 (exclusive).


setRandom

public void setRandom(double lower,
                      double upper)
Sets the elements of this vector to uniformly distributed random values in a specified range.

Parameters:
lower - lower random value (inclusive)
upper - upper random value (exclusive)

setRandom

public void setRandom(double lower,
                      double upper,
                      java.util.Random generator)
Sets the elements of this vector to uniformly distributed random values in a specified range, using a supplied random number generator.

Parameters:
lower - lower random value (inclusive)
upper - upper random value (exclusive)
generator - random number generator