public class VectorNi extends VectoriBase 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 |
---|
VectorNi()
Creates a vector with an initial size of zero.
|
VectorNi(int size)
Creates a vector of a specific size, and initializes its elements to 0.
|
VectorNi(int[] values)
Creates a vector from an array of ints.
|
VectorNi(int size,
int[] values)
Creates a vector of a specific size, and initializes its elements from an
array of values.
|
VectorNi(Vectori vec)
Creates a new VectorNi from an existing Vectori.
|
VectorNi(VectorNi vec)
Creates a new VectorNi from an existing one.
|
Modifier and Type | Method and Description |
---|---|
VectorNi |
absolute()
Sets the elements of this vector to their absolute values.
|
VectorNi |
absolute(VectorNi v1)
Sets the elements of this vector to the absolute value of v1.
|
void |
add(int i,
int value)
Adds a value to the i-th element of this vector.
|
VectorNi |
add(VectorNi v1)
Adds this vector to v1 and places the result in this vector.
|
VectorNi |
add(VectorNi v1,
VectorNi v2)
Adds vector v1 to v2 and places the result in this vector.
|
int |
adjustSize(int inc) |
void |
append(int value)
Appends a value to the end of this vector, increasing its size by one.
|
void |
append(VectorNi vec)
Appends the values of
vec to the end of this vector, increasing
its size by the size of vec . |
VectorNi |
clone() |
boolean |
equals(Vectori v1)
Returns true if the elements of this vector exactly equal those of vector
v1 . |
boolean |
equals(VectorNi v1)
Returns true if the elements of this vector exactly equal those of vector
v1 . |
int |
get(int i)
Gets a single element of this vector.
|
void |
get(int[] values)
Copies the elements of this vector into an array of doubles.
|
int |
get(int[] values,
int idx)
Copies the elements of this vector into an array of int,
starting at a particular location.
|
int[] |
getBuffer()
Returns the internal buffer used to store the elements in this vector.
|
int |
getCapacity() |
void |
getSubVector(int off,
VectorNi v1)
Gets a subset of the values of this vector, beginning at a specified
offset, and places them in
v1 . |
int |
infinityNorm()
Returns the infinity norm of this vector.
|
boolean |
isFixedSize()
Returns true if this vector is of fixed size.
|
VectorNi |
max(VectorNi v)
Computes the element-wise maximum of this vector and vector v and places
the result in this vector.
|
int |
maxElement()
Returns the maximum element value.
|
VectorNi |
min(VectorNi v)
Computes the element-wise minimum of this vector and vector v and places
the result in this vector.
|
int |
minElement()
Returns the minimum element value.
|
VectorNi |
negate()
Negates this vector in place.
|
VectorNi |
negate(VectorNi v1)
Sets this vector to the negative of v1.
|
double |
norm()
Returns the 2 norm of this vector.
|
double |
normSquared()
Returns the square of the 2 norm of this vector.
|
int |
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. |
VectorNi |
scale(double s)
Scales the elements of this vector by
s . |
VectorNi |
scale(double s,
VectorNi v1)
Scales the elements of vector v1 by
s and places the
results in this vector. |
VectorNi |
scaledAdd(double s,
VectorNi v1)
Computes
s v1 and adds the result to this vector. |
VectorNi |
scaledAdd(double s,
VectorNi v1,
VectorNi v2)
Computes
s v1 + v2 and places the result in this vector. |
void |
scan(ReaderTokenizer rtok)
Sets the contents of this vector to values read from a ReaderTokenizer.
|
void |
set(int[] values)
Sets the elements of this vector from an array of doubles.
|
int |
set(int[] values,
int idx)
Sets the elements of this vector from an array of ints,
starting from a particular location.
|
void |
set(int i,
int value)
Sets a single element of this vector.
|
void |
set(VectorNi v1)
Sets the size and values of this vector to those of v1.
|
void |
setAll(int value)
Sets the elements of this vector to a specific value.
|
void |
setBuffer(int size,
int[] 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 -1000 (inclusive) to 1000 (exclusive).
|
void |
setRandom(int lower,
int upper)
Sets the elements of this vector to uniformly distributed random values in
a specified range.
|
void |
setRandom(int lower,
int 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 |
setSubVector(int off,
VectorNi 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 number of elements in this vector.
|
void |
sort()
Sorts the contents of this vector by element value, from largest to
smallest value.
|
void |
sort(VectorNi v1)
Sorts the contents of vector v1 by element value, from largest to smallest
value, and places the result into this vector.
|
VectorNi |
sub(VectorNi v1)
Subtracts v1 from this vector and places the result in this vector.
|
VectorNi |
sub(VectorNi v1,
VectorNi v2)
Subtracts vector v1 from v2 and places the result in this vector.
|
int |
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.
|
getDefaultFormat, set, setDefaultFormat, toString, toString, toString, write, write
public VectorNi()
public VectorNi(int size) throws ImproperSizeException
size
- size of the vectorImproperSizeException
- if size is negativepublic VectorNi(int size, int[] values) throws ImproperSizeException
size
- size of the vectorvalues
- element values for the new vectorImproperSizeException
- if size is negativepublic VectorNi(int[] values)
values
- element values for the new vectorpublic VectorNi(VectorNi vec)
vec
- VectorNi to be copiedpublic VectorNi(Vectori vec)
vec
- Vectori to be copiedpublic int[] 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, int[])
public int size()
size
in interface Vectori
size
in class VectoriBase
public int adjustSize(int inc) throws ImproperSizeException
ImproperSizeException
public int getCapacity()
public void setCapacity(int newcap)
public void setBuffer(int size, int[] 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, int[])
public int get(int i)
get
in interface Vectori
get
in class VectoriBase
i
- element indexpublic void get(int[] values)
>=
the size of the vector.get
in interface Vectori
get
in class VectoriBase
values
- array into which values are copiedpublic int get(int[] values, int idx)
values
- array into which values are copiedidx
- starting point within values where copying should beginpublic void set(int i, int value)
set
in interface Vectori
set
in class VectoriBase
i
- element indexvalue
- element valuepublic void append(int 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(VectorNi 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(int[] values)
>=
the current vector size. Otherwise, the vector is resized to the
array length.set
in interface Vectori
set
in class VectoriBase
values
- array from which values are copiedpublic int set(int[] values, int idx)
values
- array into which values are copiedidx
- starting point within values from which copying should beginpublic void set(VectorNi v1)
v1
- vector whose size and values are copiedpublic void getSubVector(int off, VectorNi 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, VectorNi 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 VectorNi add(VectorNi v1, VectorNi 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 VectorNi add(VectorNi v1) throws ImproperSizeException
v1
- right-hand vectorImproperSizeException
- if v1 has a size less than this vectorpublic void add(int i, int value)
i
- index of the elementvalue
- value to be addedpublic VectorNi sub(VectorNi v1, VectorNi 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 VectorNi sub(VectorNi v1) throws ImproperSizeException
v1
- right-hand vectorImproperSizeException
- if v1 has a size less than this vectorpublic VectorNi negate(VectorNi v1)
v1
- vector to negatepublic VectorNi negate()
public VectorNi scale(double s)
s
.s
- scaling factorpublic VectorNi scale(double s, VectorNi v1)
s
and places the
results in this vector. This vector is resized if necessary.s
- scaling factorv1
- vector to be scaledpublic VectorNi scaledAdd(double s, VectorNi v1, VectorNi 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 sizespublic VectorNi scaledAdd(double s, VectorNi 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 boolean isFixedSize()
isFixedSize
in interface Vectori
isFixedSize
in class VectoriBase
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 Vectori
setSize
in class VectoriBase
newSize
- new vector sizeImproperSizeException
- if this vector has an explicit internal buffer and that buffer is too
small for the requested sizeVectori.isFixedSize()
public double norm()
norm
in interface Vectori
norm
in class VectoriBase
public double normSquared()
normSquared
in interface Vectori
normSquared
in class VectoriBase
public int oneNorm()
oneNorm
in interface Vectori
oneNorm
in class VectoriBase
public int infinityNorm()
infinityNorm
in interface Vectori
infinityNorm
in class VectoriBase
public int maxElement()
maxElement
in interface Vectori
maxElement
in class VectoriBase
public int minElement()
minElement
in interface Vectori
minElement
in class VectoriBase
public boolean equals(Vectori v1)
v1
. If the vectors have different sizes, false is
returned.equals
in interface Vectori
equals
in class VectoriBase
v1
- vector to compare withpublic boolean equals(VectorNi v1)
v1
.v1
- vector to compare withpublic int sum()
public void setZero()
public void setAll(int value)
public VectorNi absolute()
public VectorNi absolute(VectorNi v1)
v1
- vector to take the absolute value ofImproperSizeException
- if this vector needs resizing but is of fixed sizepublic void sort()
public void sort(VectorNi v1)
v1
- vector to sortpublic VectorNi max(VectorNi v) throws ImproperSizeException
v
- vector to compare withImproperSizeException
- if this vector and v have different sizespublic VectorNi min(VectorNi v) throws ImproperSizeException
v
- vector to compare withImproperSizeException
- if this vector and v have different sizespublic VectorNi clone()
clone
in interface Clonable
clone
in class VectoriBase
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 setRandom()
public void setRandom(int lower, int upper)
lower
- lower random value (inclusive)upper
- upper random value (exclusive)public void setRandom(int lower, int upper, java.util.Random generator)
lower
- lower random value (inclusive)upper
- upper random value (exclusive)generator
- random number generatorpublic void scan(ReaderTokenizer rtok) throws java.io.IOException
[ ]
.
If the input is not surrounded by square brackets, then the number of
values should equal the current size
of this vector.
If the input is surrounded by square brackets, then all values up to the closing bracket are read, and this vector will be resized to resulting number of values. For example,
[ 1 4 5 3 ]defines a vector of size 4.
scan
in interface Vectori
scan
in class VectoriBase
rtok
- Tokenizer from which vector values are read. Number parsing should be
enabled.ImproperSizeException
- if this vector has a fixed size which is incompatible with the inputjava.io.IOException