public interface Vector
Vector
represents an n-dimensional value. A vector
provides a fixed number of dimensions, with a value for each
dimension.
The optional operation setValue(int,double)
allows values
to be set.
Equality for vectors is defined so that to be equal, two vectors
must have the same dimensionality and all values must be equal.
The required hash code for this definition of equality is defined
in the documentation for hashCode()
.
Modifier and Type | Method and Description |
---|---|
Vector |
add(Vector v)
Returns a new vector that is the reuslt of adding this vector
to the specified vector.
|
double |
cosine(Vector v)
Returns the cosine product of this vector with the specified
vector.
|
double |
dotProduct(Vector v)
Returns the dot product of this vector with the specified
vector.
|
boolean |
equals(Object that)
Returns
true if the specified object is a vector
that has the same dimensionality and values as this vector. |
int |
hashCode()
Return the hash code for this vector.
|
void |
increment(double scale,
Vector v)
Adds the specified vector multiplied by the specified scalar to
this vector.
|
double |
length()
Returns the length of this vector.
|
int[] |
nonZeroDimensions()
Returns the array of dimensions that have non-zero
values.
|
int |
numDimensions()
Returns the number of dimensions of this vector.
|
void |
setValue(int dimension,
double value)
Sets the value of the specified dimension to the specified
value.
|
double |
value(int dimension)
Returns the value of this vector for the specified dimension.
|
int numDimensions()
int[] nonZeroDimensions()
This method is only required to return all the non-zero dimensions. It may also return some dimensions that have zero values.
void increment(double scale, Vector v)
scale
- The scalar multiplier for the added vector.v
- Vector to scale and add to this vector.IllegalArgumentException
- If the specified vector is not
of the same dimensionality as this vector.double value(int dimension)
dimension
- The specified dimension.void setValue(int dimension, double value)
This operation is optional. Implementations may throw unsupported operation exceptions.
dimension
- The specified dimension.value
- The new value for the specified dimension.UnsupportedOperationException
- If this operation is not supported.double dotProduct(Vector v)
The dot product is defined as follows:
v1 . v2
= Σ0 <= n < v1.numDimensions() v1.value(n) * v2.value(n)
v
- The specified vector.IllegalArgumentException
- If the specified vector
is not of the same dimensionality as this vector.double cosine(Vector v)
The cosine of two vectors is defined as their dot product divided by their lengths:
cos(v1,v2) = v1.dotProduct(v2) / (v1.length() * v2.length())
Applying Math.acos(double)
to the result returns the
angle in radians, ranging from 0.0 through Math.PI
. This
value can be converted to degrees with Math.toDegrees(double)
.
Thus Math.acos(cosine(v))
is the angle in radians
between this vector and the vector v, and
Math.toDegrees(Math.acos(cosine(v)))
is the same
angle in degrees.
v
- The specified vector.IllegalArgumentException
- If the specified vector
is not of the same dimensionality as this vector.double length()
The length of a vector is defined as the square root of its dot product with itself:
| v | = (v.dotProduct(v))1/2
Vector add(Vector v)
v
- The vector to add to this vector.IllegalArgumentException
- If the specified vector is not
of the same dimensionality as this vector.int hashCode()
List
of
Double
objects. Hash codes are computed as
follows:
int hashCode = 1; for (int i = 0; i < numRows(); ++i) { int v = Double.doubleToLongBits(value(i)); int valHash = (int) (v^(v>>>32)); hashCode = 31*hashCode + valHash; }Note that this definition is consistent with
equals(Object)
. Subclasses that implement this method should
return a result that would be the same as if it were computed
by the above procedure.boolean equals(Object that)
true
if the specified object is a vector
that has the same dimensionality and values as this vector.
Note that this definition is consistent with the definition of
hashCode()
.