public class VectorNd extends VectorBase implements java.io.Serializable, Clonable, VectorObject<VectorNd>
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 |
---|
VectorNd()
Creates a vector with an initial size of zero.
|
VectorNd(double... values)
Creates a vector from an array of doubles.
|
VectorNd(float... values)
Creates a vector from an array of floats.
|
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(Matrix M)
Creates a vector whose size and elements match a particular Matrix.
|
VectorNd(Vector v)
Creates a vector whose size and elements are the same as an existing
Vector.
|
Modifier and Type | Method and Description |
---|---|
VectorNd |
absolute()
Sets the elements of this vector to their absolute values.
|
VectorNd |
absolute(VectorNd v1)
Sets the elements of this vector to the absolute value of v1.
|
void |
add(int i,
double value)
Adds a value to the i-th element of this vector.
|
VectorNd |
add(VectorNd v1)
Adds this vector to v1 and places the result in this vector.
|
VectorNd |
add(VectorNd v1,
VectorNd v2)
Adds vector v1 to v2 and places the result in this vector.
|
void |
addNoise(double eps)
Adds a random perturbation to this vector, in the range
[-lim,lim] , where |
void |
addNoise(double eps,
java.util.Random generator)
Adds a random perturbation to this vector, in the range
[-lim,lim] , where |
void |
addObj(VectorNd v1)
Adds
v1 to this vector. |
void |
addScaledSubVector(int off,
double scale,
Vector v1)
Adds a scaled subset of the values of this vector, beginning at a
specified offset, to the values of v1.
|
int |
adjustSize(int inc) |
double |
angle(VectorNd v1)
Returns the angle between this vector and v1.
|
void |
append(double value)
Appends a value to the end of this vector, increasing its size by one.
|
void |
append(VectorNd vec)
Appends the values of
vec to the end of this vector, increasing
its size by the size of vec . |
VectorNd |
clone() |
VectorNd |
combine(double s1,
VectorNd v1,
double s2,
VectorNd v2)
Computes
s1 v1 + s2 v2 and places the result in this
vector. |
double |
distance(VectorNd v)
Returns the distance between this vector and another.
|
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
v1 within a prescribed tolerance epsilon . |
boolean |
equals(VectorNd v1)
Returns true if the elements of this vector exactly equal those of vector
v1 . |
void |
exchangeElements(int i,
int j)
Exchange elements i and j of this vector.
|
void |
get(double[] values)
Copies the elements of this vector into an array of doubles.
|
int |
get(double[] values,
int idx)
Copies the elements of this vector into an array of doubles,
starting at a particular location.
|
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.
|
int |
getCapacity() |
void |
getSubVector(int[] idxs,
Vector v1)
Gets a subset of the values of this vector, whose indices are
specified by
idxs , and places them in v1 . |
void |
getSubVector(int off,
Vector v1)
Gets a subset of the values of this vector, beginning at a specified
offset, and places them in
v1 . |
static void |
hermiteInterpolate(VectorNd pr,
VectorNd p0,
VectorNd v0,
VectorNd p1,
VectorNd v1,
double s,
double h)
Interpolates the value at a location along a cubic Hermite spline.
|
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.
|
boolean |
isRowVector() |
VectorNd |
max(VectorNd v)
Computes the element-wise maximum of this vector and vector v and places
the result in this vector.
|
double |
maxElement()
Returns the maximum element value of this vector.
|
int |
maxIndex()
Returns the index of the maximum element of this vector.
|
double |
mean()
Returns the mean value of the elements of this vector.
|
VectorNd |
min(VectorNd v)
Computes the element-wise minimum of this vector and vector v and places
the result in this vector.
|
double |
minElement()
Returns the minimum element value of this vector.
|
int |
minIndex()
Returns the index of the minimum element of this vector.
|
VectorNd |
mul(Matrix M,
VectorNd b)
Multiplies matrix M by the vector b and places the result in this vector.
|
VectorNd |
mulTranspose(Matrix M,
VectorNd b)
Multiplies the transpose of matrix M by the vector b and places the result
in this vector.
|
VectorNd |
negate()
Negates this vector in place.
|
VectorNd |
negate(VectorNd v1)
Sets this vector to the negative of v1.
|
double |
norm()
Returns the 2 norm of this vector.
|
VectorNd |
normalize()
Normalizes this vector in place.
|
VectorNd |
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 |
permute(int[] permutation)
Rearrange the elements of this vector according to the specified
permutation, such that each element i is replaced by element
permutation[i].
|
void |
remove(int i)
Removes the
i -th value from this vector, decreasing its size by
one. |
VectorNd |
scale(double s)
Scales the elements of this vector by
s . |
VectorNd |
scale(double s,
VectorNd v1)
Scales the elements of vector v1 by
s and places the
results in this vector. |
VectorNd |
scaledAdd(double s,
VectorNd v1)
Computes
s v1 and adds the result to this vector. |
VectorNd |
scaledAdd(double s,
VectorNd v1,
VectorNd v2)
Computes
s v1 + v2 and places the result in this vector. |
void |
scaledAddObj(double s,
VectorNd v1)
Scales
v1 by s and adds it to this vector. |
void |
scaleObj(double s)
Scales this vector by
s . |
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(float[] values)
Sets the elements of this vector from an array of floats.
|
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 |
setAll(double value)
Sets the elements of this vector to a specific value.
|
void |
setBuffer(int size,
double[] buffer)
Explicitly sets the size and internal buffer associated with this vector.
|
void |
setCapacity(int newcap) |
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.
|
boolean |
setRowVector(boolean isRow) |
void |
setSize(int newSize)
Sets the size of this vector.
|
void |
setSubVector(int[] idxs,
Vector v1)
Sets a subset of the values of this vector, whose indices are
specified by
idxs , to the values of v1 . |
void |
setSubVector(int off,
Vector v1)
Sets a subset of the values of this vector, beginning at a specified
offset, to the values of v1.
|
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.
|
VectorNd |
sub(VectorNd v1)
Subtracts v1 from this vector and places the result in this vector.
|
VectorNd |
sub(VectorNd v1,
VectorNd v2)
Subtracts vector v2 from v1 and places the result in this vector.
|
double |
sum()
Returns the sum of all the elements in this vector.
|
void |
unsetBuffer()
Removes an explicit buffer provided for this vector and replaces it with a
default implicit buffer.
|
containsNaN, copy, copyAndAdd, copyAndNegate, copyAndScale, copyAndSub, copyAndSubLeft, dot, epsilonEquals, equals, get, getDefaultFormat, hasInf, hasNaN, isColumnVectorStringsVertical, isWritable, scan, scan, set, set, setColumnVectorStringsVertical, setDefaultFormat, toString, toString, toString, write, write, write, writeToFile
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
getThreeVectorValue
isWritable, scan, write
public VectorNd()
public VectorNd(int size) throws ImproperSizeException
size
- size of the vectorImproperSizeException
- if size is negativepublic VectorNd(int size, double[] values) throws ImproperSizeException
size
- size of the vectorvalues
- element values for the new vectorImproperSizeException
- if size is negativepublic VectorNd(double... values)
values
- element values for the new vectorpublic VectorNd(float... values)
values
- element values for the new vectorpublic VectorNd(Vector v)
v
- vector object to be copied.public VectorNd(Matrix M)
M
- matrix object to be copied.ImproperSizeException
- if the matrix cannot be interpreted as a row or column vector.public double[] getBuffer()
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.
setBuffer(int, double[])
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
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.
setSize
in interface Vector
setSize
in class VectorBase
newSize
- new vector sizeImproperSizeException
- if this vector has an explicit internal buffer and that buffer is too
small for the requested sizeVector.isFixedSize()
public int adjustSize(int inc) throws ImproperSizeException
ImproperSizeException
public int getCapacity()
public void setCapacity(int newcap)
public void setBuffer(int size, double[] buffer)
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.size
- new vector sizebuffer
- explicit buffer for this vectorImproperSizeException
- if the specified buffer is too small for the requested sizeunsetBuffer()
public void unsetBuffer()
java.lang.IllegalStateException
- if this vector does not have an explicit buffer given by
setBuffer
setBuffer(int, double[])
public double get(int i)
get
in interface Vector
get
in class VectorBase
i
- element indexpublic void get(double[] values)
>=
the size of the vector.get
in interface Vector
get
in class VectorBase
values
- array into which values are copiedpublic int get(double[] values, int idx)
values
- array into which values are copiedidx
- starting point within values where copying should beginpublic void set(int i, double value)
set
in interface Vector
set
in class VectorBase
i
- element indexvalue
- element valuepublic void append(double value)
append
calls.value
- value to append to the end of this vectorImproperSizeException
- if the capacity needs to be increased but
the internal buffer is explicit and so cannot be increased.public void append(VectorNd vec)
vec
to the end of this vector, increasing
its size by the size of vec
. If the vector's capacity (i.e., the
length of the underlying array) needs to be increased, this is done by an
extended amount in order to reduce the overall number of capacity
increases that may be incurred by a sequence of append
calls.vec
- vector to append to the end of this vectorImproperSizeException
- if the capacity needs to be increased but
the internal buffer is explicit and so cannot be increased.public void remove(int i)
i
-th value from this vector, decreasing its size by
one. Indices for all values greater than i
are decremented by
one.i
- index of the value to be removedpublic void set(double[] values)
set
in interface Vector
set
in class VectorBase
values
- array from which values are copiedpublic void set(float[] values)
values
- array from which values are copiedpublic int set(double[] values, int idx)
public void set(VectorNd v1)
set
in interface VectorObject<VectorNd>
v1
- vector whose size and values are copiedpublic void getSubVector(int off, Vector v1)
v1
.off
- offset where copying should begin in this vectorv1
- vector returning the sub-vector valuesImproperSizeException
- if this vector is not large enough to accomodate the specified subvectorpublic void setSubVector(int off, Vector v1)
off
- offset where copying should begin in this vectorv1
- vector whose values are copiedImproperSizeException
- if this vector is not large enough to accomodate the specified subvectorpublic void addScaledSubVector(int off, double scale, Vector v1)
off
- offset where adding should begin in this vectorscale
- scale factor that v1
should be multiplied byv1
- vector whose values are to be addedImproperSizeException
- if this vector is not large enough to accommodate the specified subvectorpublic void getSubVector(int[] idxs, Vector v1)
idxs
, and places them in v1
.idxs
- indices of the values in this vector that are to be obtainedv1
- vector returning the sub-vector valuesImproperSizeException
- if the size of idxs
is less than v1
java.lang.ArrayIndexOutOfBoundsException
- if any of the indices in
idxs
are out of boundspublic void setSubVector(int[] idxs, Vector v1)
idxs
, to the values of v1
.idxs
- indices of the values in this vector that are to be setv1
- vector containing the sub-vector valuesImproperSizeException
- if the size of idxs
is less than v1
java.lang.ArrayIndexOutOfBoundsException
- if any of the indices in
idxs
are out of boundspublic VectorNd add(VectorNd v1, VectorNd 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 VectorNd add(VectorNd v1) throws ImproperSizeException
v1
- right-hand vectorImproperSizeException
- if v1 has a size less than this vectorpublic void add(int i, double value)
i
- index of the elementvalue
- value to be addedpublic VectorNd sub(VectorNd v1, VectorNd 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 VectorNd sub(VectorNd v1) throws ImproperSizeException
v1
- right-hand vectorImproperSizeException
- if v1 has a size less than this vectorpublic VectorNd negate(VectorNd v1)
v1
- vector to negatepublic VectorNd negate()
public VectorNd scale(double s)
s
.s
- scaling factorpublic VectorNd scale(double s, VectorNd v1)
s
and places the
results in this vector. This vector is resized if necessary.s
- scaling factorv1
- vector to be scaledpublic void interpolate(VectorNd v1, double s, VectorNd 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, VectorNd v1) throws ImproperSizeException
(1-s) this + s v1
and places the
result in this vector.s
- interpolation factorv1
- right-hand vectorImproperSizeException
- if v1 has a size less than this vectorpublic VectorNd scaledAdd(double s, VectorNd v1, VectorNd 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 VectorNd scaledAdd(double s, VectorNd v1) throws ImproperSizeException
s v1
and adds the result to this vector.s
- scaling factorv1
- vector to be scaled and addedImproperSizeException
- if v1 has a size less than this vectorpublic VectorNd combine(double s1, VectorNd v1, double s2, VectorNd 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 int maxIndex()
public double minElement()
minElement
in interface Vector
minElement
in class VectorBase
public int minIndex()
public double infinityNorm()
infinityNorm
in interface Vector
infinityNorm
in class VectorBase
public double oneNorm()
oneNorm
in interface Vector
oneNorm
in class VectorBase
public double mean()
public double dot(VectorNd v1) throws ImproperSizeException
v1
- right-hand vectorImproperSizeException
- if this vector and v1 have different sizespublic double angle(VectorNd 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 VectorNd normalize()
public VectorNd normalize(VectorNd v1)
v1
- vector to normalizeImproperSizeException
- if this vector needs resizing but is of fixed sizepublic boolean epsilonEquals(VectorNd v1, double eps)
v1
within a prescribed tolerance epsilon
.epsilonEquals
in interface VectorObject<VectorNd>
v1
- vector to compare witheps
- comparison tolerancepublic boolean equals(VectorNd v1)
v1
.v1
- vector to compare withpublic double sum()
public void setZero()
setZero
in interface VectorObject<VectorNd>
public void setAll(double value)
public VectorNd absolute()
public VectorNd absolute(VectorNd v1)
v1
- vector to take the absolute value ofImproperSizeException
- if this vector needs resizing but is of fixed sizepublic void sort()
public void sort(VectorNd v1)
v1
- vector to sortpublic VectorNd mul(Matrix M, VectorNd b)
M
- left-hand matrixb
- right-hand vectorImproperSizeException
- if the size of b does not equal the number of columns of M, or if this
vector needs resizing but is of fixed sizepublic VectorNd mulTranspose(Matrix M, VectorNd b)
M
- left-hand matrixb
- right-hand vectorImproperSizeException
- if the size of b does not equal the number of rows of M, or 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 void addNoise(double eps)
[-lim,lim]
, where
lim = eps * infNormwith
eps
supplied as an argument and infNorm
is the infinityNorm of this vector.eps
- generates the noise rangepublic void addNoise(double eps, java.util.Random generator)
[-lim,lim]
, where
lim = eps * infNormwith
eps
supplied as an argument and infNorm
is the infinityNorm of this vector.eps
- generates the noise rangegenerator
- random number generator to usepublic VectorNd max(VectorNd v) throws ImproperSizeException
v
- vector to compare withImproperSizeException
- if this vector and v have different sizespublic VectorNd min(VectorNd v) throws ImproperSizeException
v
- vector to compare withImproperSizeException
- if this vector and v have different sizespublic boolean isRowVector()
isRowVector
in class VectorBase
public boolean setRowVector(boolean isRow)
setRowVector
in class VectorBase
public VectorNd clone()
clone
in interface Clonable
clone
in class VectorBase
public void permute(int[] permutation)
permutation
- describes the element exchangesImproperSizeException
- if the length of permutation
is less than the size of this
vector.public void exchangeElements(int i, int j)
public double distance(VectorNd v)
v
- vector to find distance with respect topublic static void hermiteInterpolate(VectorNd pr, VectorNd p0, VectorNd v0, VectorNd p1, VectorNd v1, double s, double h)
s
in the range [0,1], and the actual time duration of the interval
is given by h
.pr
- returns interpolated positionp0
- position at the interval beginningv0
- velocity at the interval beginningp1
- position at the interval endv1
- velocity at the interval ends
- interpolation location on the interval (in the range [0,1])h
- interval time durationpublic void scaleObj(double s)
s
.scaleObj
in interface VectorObject<VectorNd>
public void addObj(VectorNd v1)
v1
to this vector.addObj
in interface VectorObject<VectorNd>
public void scaledAddObj(double s, VectorNd v1)
v1
by s
and adds it to this vector.scaledAddObj
in interface VectorObject<VectorNd>