|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectmaspack.matrix.VectorBase
maspack.matrix.VectorNd
public class VectorNd
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.
Constructor Summary | |
---|---|
VectorNd()
Creates a vector with an initial size of zero. |
|
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(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. |
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(int i,
double value)
Adds a value to the i-th element of this vector. |
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. |
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 |
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. |
VectorNd |
clone()
|
void |
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 |
get(double[] values)
Copies the elements of this vector into an array of doubles. |
void |
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,
VectorNd 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,
VectorNd 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()
|
void |
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. |
void |
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. |
void |
mul(Matrix M,
VectorNd b)
Multiplies matrix M by the vector b and places the result in this vector. |
void |
mulTranspose(Matrix 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 |
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 |
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 |
scaledAdd(double s,
VectorNd v1)
Computes s v1 and adds the result to this vector. |
void |
scaledAdd(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(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(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 |
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,
VectorNd 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,
VectorNd 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. |
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. |
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. |
Methods inherited from class maspack.matrix.VectorBase |
---|
containsNaN, copy, copyAndAdd, copyAndNegate, copyAndScale, copyAndSub, copyAndSubLeft, dot, epsilonEquals, equals, get, getDefaultFormat, hasNaN, isColumnVectorStringsVertical, scan, set, set, setColumnVectorStringsVertical, setDefaultFormat, toString, toString, toString, write, write |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public VectorNd()
public VectorNd(int size) throws ImproperSizeException
size
- size of the vector
ImproperSizeException
- if size is negativepublic VectorNd(int size, double[] values) throws ImproperSizeException
size
- size of the vectorvalues
- element values for the new vector
ImproperSizeException
- if size is negativepublic VectorNd(double[] 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.Method Detail |
---|
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 size
ImproperSizeException
- 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 vector
ImproperSizeException
- 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 index
public void get(double[] values)
get
in interface Vector
get
in class VectorBase
values
- array into which values are copiedpublic void 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 vector
ImproperSizeException
- if the capacity needs to be increased but
the internal buffer is explicit and so cannot be increased.public void set(double[] values)
set
in interface Vector
set
in class VectorBase
values
- array from which values are copiedpublic void set(double[] values, int idx)
values
- array into which values are copiedidx
- starting point within values from which copying should beginpublic void set(VectorNd v1)
v1
- vector whose size and values are copied
ImproperSizeException
- if this vector needs resizing but is of fixed sizepublic void getSubVector(int off, VectorNd v1)
v1
.
off
- offset where copying should begin in this vectorv1
- vector returning the sub-vector values
ImproperSizeException
- if this vector is not large enough to accomodate the specified subvectorpublic void setSubVector(int off, VectorNd v1)
off
- offset where copying should begin in this vectorv1
- vector whose values are copied
ImproperSizeException
- if this vector is not large enough to accomodate the specified subvectorpublic void getSubVector(int[] idxs, VectorNd 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 values
ImproperSizeException
- 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, VectorNd 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 values
ImproperSizeException
- if the size of idxs
is less than v1
java.lang.ArrayIndexOutOfBoundsException
- if any of the indices in
idxs
are out of boundspublic void add(VectorNd v1, VectorNd v2) throws ImproperSizeException
v1
- left-hand vectorv2
- right-hand vector
ImproperSizeException
- if v1 and v2 have different sizes, or if this vector needs resizing but is
of fixed sizepublic void add(VectorNd v1) throws ImproperSizeException
v1
- right-hand vector
ImproperSizeException
- if v1 has a size less than this vectorpublic void add(int i, double value)
i
- index of the elementvalue
- value to be addedpublic void sub(VectorNd v1, VectorNd v2) throws ImproperSizeException
v1
- left-hand vectorv2
- right-hand vector
ImproperSizeException
- if v1 and v2 have different sizes, or if this vector needs resizing but is
of fixed sizepublic void sub(VectorNd v1) throws ImproperSizeException
v1
- right-hand vector
ImproperSizeException
- if v1 has a size less than this vectorpublic void negate(VectorNd v1)
v1
- vector to negate
ImproperSizeException
- 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, VectorNd v1)
s
and places the
results in this vector. This vector is resized if necessary.
s
- scaling factorv1
- vector to be scaled
ImproperSizeException
- if this vector needs resizing but is of fixed sizepublic 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 vector
ImproperSizeException
- 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 vector
ImproperSizeException
- if v1 has a size less than this vectorpublic void 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 added
ImproperSizeException
- if v1 and v2 have different sizes, or if this vector needs resizing but is
of fixed sizepublic void scaledAdd(double s, VectorNd v1) throws ImproperSizeException
s v1
and adds the result to this vector.
s
- scaling factorv1
- vector to be scaled and added
ImproperSizeException
- if v1 has a size less than this vectorpublic void 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 vector
ImproperSizeException
- 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 vector
ImproperSizeException
- 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 vector
ImproperSizeException
- if this vector and v1 have different sizespublic void normalize()
public void normalize(VectorNd v1)
v1
- vector to normalize
ImproperSizeException
- if this vector needs resizing but is of fixed sizepublic boolean epsilonEquals(VectorNd v1, double eps) throws ImproperSizeException
v1
within a prescribed tolerance epsilon
.
v1
- vector to compare witheps
- comparison tolerance
ImproperSizeException
public boolean equals(VectorNd v1) throws ImproperSizeException
v1
.
v1
- vector to compare with
ImproperSizeException
public double sum()
public void setZero()
public void absolute()
public void absolute(VectorNd v1)
v1
- vector to take the absolute value of
ImproperSizeException
- if this vector needs resizing but is of fixed sizepublic void sort()
public void sort(VectorNd v1)
v1
- vector to sortpublic void mul(Matrix M, VectorNd b)
M
- left-hand matrixb
- right-hand vector
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 sizepublic void mulTranspose(Matrix M, VectorNd b)
M
- left-hand matrixb
- right-hand vector
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 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 void max(VectorNd v) throws ImproperSizeException
v
- vector to compare with
ImproperSizeException
- if this vector and v have different sizespublic void min(VectorNd v) throws ImproperSizeException
v
- vector to compare with
ImproperSizeException
- 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 exchanges
ImproperSizeException
- if the length of permutation
is less than the size of this
vector.public double distance(VectorNd v)
v
- vector to find distance with respect to
public 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 duration
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |