com.aliasi.matrix

## Interface Vector

• All Known Implementing Classes:
AbstractVector, DenseVector, SparseFloatVector

`public interface Vector`
A `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()`.

Since:
LingPipe2.0
Version:
3.5
Author:
Bob Carpenter
• ### Method Summary

All Methods
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.
• ### Method Detail

• #### numDimensions

`int numDimensions()`
Returns the number of dimensions of this vector.
Returns:
The number of dimensions of this vector.
• #### nonZeroDimensions

`int[] nonZeroDimensions()`
Returns the array of dimensions that have non-zero values.

This method is only required to return all the non-zero dimensions. It may also return some dimensions that have zero values.

Returns:
The dimensions with non-zero values.
• #### increment

```void increment(double scale,
Vector v)```
Adds the specified vector multiplied by the specified scalar to this vector.
Parameters:
`scale` - The scalar multiplier for the added vector.
`v` - Vector to scale and add to this vector.
Throws:
`IllegalArgumentException` - If the specified vector is not of the same dimensionality as this vector.
• #### value

`double value(int dimension)`
Returns the value of this vector for the specified dimension.
Parameters:
`dimension` - The specified dimension.
Returns:
The value of this vector for the specified dimension.
• #### setValue

```void setValue(int dimension,
double value)```
Sets the value of the specified dimension to the specified value.

This operation is optional. Implementations may throw unsupported operation exceptions.

Parameters:
`dimension` - The specified dimension.
`value` - The new value for the specified dimension.
Throws:
`UnsupportedOperationException` - If this operation is not supported.
• #### dotProduct

`double dotProduct(Vector v)`
Returns the dot product of this vector with the specified vector. The dot product is also known as the inner product. The specified vector must have the same length as this vector. Dot products may be either positive or negative and will be zero only if the vectors are orthogonal.

The dot product is defined as follows:

``` v1 . v2 = Σ0 <= n < v1.numDimensions() v1.value(n) * v2.value(n) ```
Parameters:
`v` - The specified vector.
Returns:
The dot product of this vector with the specified vector.
Throws:
`IllegalArgumentException` - If the specified vector is not of the same dimensionality as this vector.
• #### cosine

`double cosine(Vector v)`
Returns the cosine product of this vector with the specified vector. The specified vector must have the same length as this vector. If all entries in the vector are defined and finite, values fall in the range from -1.0 to 1.0 inclusive, with a value of 1.0 or -1.0 if they are identical or opposite, and 0.0 if they are orthogonal.

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.

Parameters:
`v` - The specified vector.
Returns:
The cosine of this vector with the specified vector.
Throws:
`IllegalArgumentException` - If the specified vector is not of the same dimensionality as this vector.
• #### length

`double length()`
Returns the length of this vector.

The length of a vector is defined as the square root of its dot product with itself:

``` | v | = (v.dotProduct(v))1/2 ```
Returns:
The length of this vector.

`Vector add(Vector v)`
Returns a new vector that is the reuslt of adding this vector to the specified vector.
Parameters:
`v` - The vector to add to this vector.
Returns:
The result of adding the specified vector to this vector.
Throws:
`IllegalArgumentException` - If the specified vector is not of the same dimensionality as this vector.
• #### hashCode

`int hashCode()`
Return the hash code for this vector. The hash code for vectors is as if they were a one-by-n matrix, which in turn is the same as if they were a `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.
Overrides:
`hashCode` in class `Object`
Returns:
The hash code for this vector.
• #### equals

`boolean equals(Object that)`
Returns `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()`.
Overrides:
`equals` in class `Object`
Parameters:
`that` - Object to test for equality with this vector.
Returns:
`true` if the specified object is a vector that has the same dimensionality and values as this vector.