maspack.matrix
Class SparseVectorNd

java.lang.Object
  extended by maspack.matrix.VectorBase
      extended by maspack.matrix.SparseVectorNd
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, Vector, Clonable

public class SparseVectorNd
extends VectorBase
implements java.io.Serializable

Implements sparse 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
SparseVectorNd(double[] values)
          Creates a vector from an array of doubles.
SparseVectorNd(int size)
          Creates a vector of a specific size, and initializes its elements to 0.
SparseVectorNd(int size, double[] values)
          Creates a vector of a specific size, and initializes its elements from an array of values.
SparseVectorNd(Vector v)
          Creates a vector whose size and elements are the same as an existing Vector.
 
Method Summary
 void absolute()
          Sets the elements of this vector to their absolute values.
 void absolute(SparseVectorNd v1)
          Sets the elements of this vector to the absolute value of v1.
 void add(SparseVectorNd v1)
          Adds this vector to v1 and places the result in this vector.
 void add(SparseVectorNd v1, SparseVectorNd v2)
          Adds vector v1 to v2 and places the result in this vector.
 void addEntry(SparseVectorCell cell, SparseVectorCell prev)
           
 double angle(SparseVectorNd v1)
          Returns the angle between this vector and v1.
 SparseVectorNd clone()
           
 void combine(double s1, SparseVectorNd v1, double s2, SparseVectorNd v2)
          Computes s1 v1 + s2 v2 and places the result in this vector.
 double dot(SparseVectorNd v1)
          Returns the dot product of this vector and v1.
 boolean epsilonEquals(SparseVectorNd v1, double eps)
          Returns true if the elements of this vector equal those of vector v1within a prescribed tolerance epsilon.
 boolean equals(SparseVectorNd 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.
 SparseVectorCell getCells()
           
 double infinityNorm()
          Returns the infinity norm of this vector.
 void interpolate(double s, SparseVectorNd v1)
          Computes the interpolation (1-s) this + s v1 and places the result in this vector.
 void interpolate(SparseVectorNd v1, double s, SparseVectorNd 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 negate()
          Negates this vector in place.
 void negate(SparseVectorNd 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(SparseVectorNd 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 removeEntry(SparseVectorCell cell, SparseVectorCell prev)
           
 void scale(double s)
          Scales the elements of this vector by s.
 void scale(double s, SparseVectorNd v1)
          Scales the elements of vector v1 by s and places the results in this vector.
 void scaledAdd(double s, SparseVectorNd v1)
          Computes s v1 and adds the result to this vector.
 void scaledAdd(double s, SparseVectorNd v1, SparseVectorNd 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(SparseVectorNd v1)
          Sets the size and values of this vector to those of v1.
 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 sub(SparseVectorNd v1)
          Subtracts v1 from this vector and places the result in this vector.
 void sub(SparseVectorNd v1, SparseVectorNd v2)
          Subtracts vector v1 from v2 and places the result in this vector.
 
Methods inherited from class maspack.matrix.VectorBase
containsNaN, copy, copyAndAdd, copyAndNegate, copyAndScale, copyAndSub, copyAndSubLeft, dot, epsilonEquals, equals, get, getDefaultFormat, hasNaN, isColumnVectorStringsVertical, isRowVector, scan, set, set, setColumnVectorStringsVertical, setDefaultFormat, setRowVector, toString, toString, toString, write, write
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SparseVectorNd

public SparseVectorNd(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

SparseVectorNd

public SparseVectorNd(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

SparseVectorNd

public SparseVectorNd(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

SparseVectorNd

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

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

size

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

Specified by:
size in interface Vector
Specified by:
size in class VectorBase
Returns:
size of the vector

isFixedSize

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

Specified by:
isFixedSize in interface Vector
Overrides:
isFixedSize in class VectorBase
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.

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.

Specified by:
setSize in interface Vector
Overrides:
setSize in class VectorBase
Parameters:
newSize - new vector size
Throws:
ImproperSizeException
See Also:
Vector.isFixedSize()

addEntry

public void addEntry(SparseVectorCell cell,
                     SparseVectorCell prev)

removeEntry

public void removeEntry(SparseVectorCell cell,
                        SparseVectorCell prev)

get

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

Specified by:
get in interface Vector
Specified by:
get in class VectorBase
Parameters:
i - element index
Returns:
element value

getCells

public SparseVectorCell getCells()

get

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

Specified by:
get in interface Vector
Overrides:
get in class VectorBase
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 interface Vector
Specified by:
set in class VectorBase
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
Overrides:
set in class VectorBase
Parameters:
values - array from which values are copied

set

public void set(SparseVectorNd 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(SparseVectorNd v1,
                SparseVectorNd 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(SparseVectorNd 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(SparseVectorNd v1,
                SparseVectorNd 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(SparseVectorNd 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(SparseVectorNd 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,
                  SparseVectorNd 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(SparseVectorNd v1,
                        double s,
                        SparseVectorNd 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,
                        SparseVectorNd 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

scaledAdd

public void scaledAdd(double s,
                      SparseVectorNd v1,
                      SparseVectorNd 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

scaledAdd

public void scaledAdd(double s,
                      SparseVectorNd v1)
               throws ImproperSizeException
Computes s v1 and adds the result to this vector.

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

combine

public void combine(double s1,
                    SparseVectorNd v1,
                    double s2,
                    SparseVectorNd 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.

Specified by:
norm in interface Vector
Overrides:
norm in class VectorBase
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
Overrides:
normSquared in class VectorBase
Returns:
square of the 2 norm

maxElement

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

Specified by:
maxElement in interface Vector
Overrides:
maxElement in class VectorBase
Returns:
maximal element

minElement

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

Specified by:
minElement in interface Vector
Overrides:
minElement in class VectorBase
Returns:
minimal element

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
Overrides:
infinityNorm in class VectorBase
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.

Specified by:
oneNorm in interface Vector
Overrides:
oneNorm in class VectorBase
Returns:
vector 1 norm

dot

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

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

clone

public SparseVectorNd clone()
Specified by:
clone in interface Clonable
Overrides:
clone in class VectorBase