public class SparseVectorNd extends VectorBase implements java.io.Serializable
These vectors can be resized, either explicitly through a call to setSize
, or implicitly through operations that require the vector
size to be modified.
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
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
v1 within 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.
|
int |
set(double[] values,
int idx)
Sets the elements of this vector from an array of doubles,
starting from a particular location.
|
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.
|
containsNaN, copy, copyAndAdd, copyAndNegate, copyAndScale, copyAndSub, copyAndSubLeft, dot, epsilonEquals, equals, get, getDefaultFormat, hasInf, hasNaN, isColumnVectorStringsVertical, isRowVector, isWritable, scan, scan, set, set, setColumnVectorStringsVertical, setDefaultFormat, setRowVector, toString, toString, toString, write, write, write, writeToFile
public SparseVectorNd(int size) throws ImproperSizeException
size
- size of the vectorImproperSizeException
- if size is negativepublic SparseVectorNd(int size, double[] values) throws ImproperSizeException
size
- size of the vectorvalues
- element values for the new vectorImproperSizeException
- if size is negativepublic SparseVectorNd(double[] values)
values
- element values for the new vectorpublic SparseVectorNd(Vector v)
v
- vector object to be copied.public int size()
size
in interface Vector
size
in class VectorBase
public boolean isFixedSize()
isFixedSize
in interface Vector
isFixedSize
in class VectorBase
setSize(int)
public void setSize(int newSize) throws ImproperSizeException
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.
setSize
in interface Vector
setSize
in class VectorBase
newSize
- new vector sizeImproperSizeException
Vector.isFixedSize()
public void addEntry(SparseVectorCell cell, SparseVectorCell prev)
public void removeEntry(SparseVectorCell cell, SparseVectorCell prev)
public double get(int i)
get
in interface Vector
get
in class VectorBase
i
- element indexpublic SparseVectorCell getCells()
public void get(double[] values)
>=
the size of the vector.get
in interface Vector
get
in class VectorBase
values
- array into which values are copiedpublic void set(int i, double value)
set
in interface Vector
set
in class VectorBase
i
- element indexvalue
- element valuepublic void set(double[] values)
>=
the
current vector size. Otherwise, the vector is resized to the array
length.set
in interface Vector
set
in class VectorBase
values
- array from which values are copiedpublic int set(double[] values, int idx)
public void set(SparseVectorNd v1)
v1
- vector whose size and values are copiedImproperSizeException
- if this vector needs resizing but is of fixed sizepublic void add(SparseVectorNd v1, SparseVectorNd v2) throws ImproperSizeException
v1
- left-hand vectorv2
- right-hand vectorImproperSizeException
- if v1 and v2 have different sizes, or if this vector needs resizing but is
of fixed sizepublic void add(SparseVectorNd v1) throws ImproperSizeException
v1
- right-hand vectorImproperSizeException
- if this vector and v1 have different sizespublic void sub(SparseVectorNd v1, SparseVectorNd v2) throws ImproperSizeException
v1
- left-hand vectorv2
- right-hand vectorImproperSizeException
- if v1 and v2 have different sizes, or if this vector needs resizing but is
of fixed sizepublic void sub(SparseVectorNd v1) throws ImproperSizeException
v1
- right-hand vectorImproperSizeException
- if this vector and v1 have different sizespublic void negate(SparseVectorNd v1)
v1
- vector to negateImproperSizeException
- if this vector needs resizing but is of fixed sizepublic void negate()
public void scale(double s)
s
.s
- scaling factorpublic void scale(double s, SparseVectorNd v1)
s
and places the
results in this vector. This vector is resized if necessary.s
- scaling factorv1
- vector to be scaledImproperSizeException
- if this vector needs resizing but is of fixed sizepublic void interpolate(SparseVectorNd v1, double s, SparseVectorNd v2) throws ImproperSizeException
(1-s) v1 + s v2
and places the
result in this vector. This vector is resized if necessary.v1
- left-hand vectors
- interpolation factorv2
- right-hand vectorImproperSizeException
- if v1 and v2 have different sizes, or if this vector needs resizing but is
of fixed sizepublic void interpolate(double s, SparseVectorNd v1) throws ImproperSizeException
(1-s) this + s v1
and places the
result in this vector.s
- interpolation factorv1
- right-hand vectorImproperSizeException
- if this vector and v1 have different sizespublic void scaledAdd(double s, SparseVectorNd v1, SparseVectorNd v2) throws ImproperSizeException
s v1 + v2
and places the result in this vector.
This vector is resized if necessary.s
- scaling factorv1
- vector to be scaledv2
- vector to be addedImproperSizeException
- if v1 and v2 have different sizes, or if this vector needs resizing but is
of fixed sizepublic void scaledAdd(double s, SparseVectorNd v1) throws ImproperSizeException
s v1
and adds the result to this vector.s
- scaling factorv1
- vector to be scaled and addedImproperSizeException
- if this vector and v1 have different sizespublic void combine(double s1, SparseVectorNd v1, double s2, SparseVectorNd v2) throws ImproperSizeException
s1 v1 + s2 v2
and places the result in this
vector. This vector is resized if necessary.s1
- left-hand scaling factorv1
- left-hand vectors2
- right-hand scaling factorv2
- right-hand vectorImproperSizeException
- if v1 and v2 have different sizes, or if this vector needs resizing but is
of fixed sizepublic double norm()
norm
in interface Vector
norm
in class VectorBase
public double normSquared()
normSquared
in interface Vector
normSquared
in class VectorBase
public double maxElement()
maxElement
in interface Vector
maxElement
in class VectorBase
public double minElement()
minElement
in interface Vector
minElement
in class VectorBase
public double infinityNorm()
infinityNorm
in interface Vector
infinityNorm
in class VectorBase
public double oneNorm()
oneNorm
in interface Vector
oneNorm
in class VectorBase
public double dot(SparseVectorNd v1) throws ImproperSizeException
v1
- right-hand vectorImproperSizeException
- if this vector and v1 have different sizespublic double angle(SparseVectorNd v1) throws ImproperSizeException
acos(c)
, where c
is the dot product of unit
vectors parallel to this vector and v1.v1
- right-hand vectorImproperSizeException
- if this vector and v1 have different sizespublic void normalize()
public void normalize(SparseVectorNd v1)
v1
- vector to normalizeImproperSizeException
- if this vector needs resizing but is of fixed sizepublic boolean epsilonEquals(SparseVectorNd v1, double eps) throws ImproperSizeException
v1
within a prescribed tolerance epsilon
.v1
- vector to compare witheps
- comparison toleranceImproperSizeException
public boolean equals(SparseVectorNd v1) throws ImproperSizeException
v1
.v1
- vector to compare withImproperSizeException
public void setZero()
public void absolute()
public void absolute(SparseVectorNd v1)
v1
- vector to take the absolute value ofImproperSizeException
- if this vector needs resizing but is of fixed sizepublic void setRandom()
public void setRandom(double lower, double upper)
lower
- lower random value (inclusive)upper
- upper random value (exclusive)public void setRandom(double lower, double upper, java.util.Random generator)
lower
- lower random value (inclusive)upper
- upper random value (exclusive)generator
- random number generatorpublic SparseVectorNd clone()
clone
in interface Clonable
clone
in class VectorBase