org.apache.mahout.math

## Class AbstractVector

• ### Nested Class Summary

Nested Classes
Modifier and Type Class and Description
`protected class ` `AbstractVector.LocalElement`
• ### Nested classes/interfaces inherited from interface org.apache.mahout.math.Vector

`Vector.Element`
• ### Field Summary

Fields
Modifier and Type Field and Description
`protected double` `lengthSquared`
• ### Constructor Summary

Constructors
Modifier Constructor and Description
`protected ` `AbstractVector(int size)`
• ### Method Summary

Methods
Modifier and Type Method and Description
`double` ```aggregate(DoubleDoubleFunction aggregator, DoubleFunction map)```
Aggregates a vector by applying a mapping function fm(x) to every component and aggregating the results with an aggregating function fa(x, y).
`double` ```aggregate(Vector other, DoubleDoubleFunction aggregator, DoubleDoubleFunction combiner)```
Generalized inner product - take two vectors, iterate over them both, using the combiner to combine together (and possibly map in some way) each pair of values, which are then aggregated with the previous accumulated value in the combiner.
`Iterable<Vector.Element>` `all()`
`String` `asFormatString()`
`Vector` `assign(double value)`
Assign the value to all elements of the receiver
`Vector` `assign(double[] values)`
Assign the values to the receiver
`Vector` ```assign(DoubleDoubleFunction f, double y)```
Apply the function to each element of the receiver, using the y value as the second argument of the DoubleDoubleFunction
`Vector` `assign(DoubleFunction f)`
Apply the function to each element of the receiver
`Vector` `assign(Vector other)`
Assign the other vector values to the receiver
`Vector` ```assign(Vector other, DoubleDoubleFunction function)```
Apply the function to each element of the receiver and the corresponding element of the other argument
`Vector` `clone()`
Return a copy of the recipient
`protected Vector` `createOptimizedCopy()`
Copy the current vector in the most optimum fashion.
`Matrix` `cross(Vector other)`
Return the cross product of the receiver and the other vector
`Vector` `divide(double x)`
Return a new vector containing the values of the recipient divided by the argument
`double` `dot(Vector x)`
Return the dot product of the recipient and the argument
`protected double` `dotSelf()`
`boolean` `equals(Object o)`
Determines whether this `Vector` represents the same logical vector as another object.
`double` `get(int index)`
Return the value at the given index
`double` `getDistanceSquared(Vector that)`
Get the square of the distance between this vector and the other vector.
`Vector.Element` `getElement(int index)`
Return an object of Vector.Element representing an element of this Vector.
`double` `getLengthSquared()`
Return the sum of squares of all elements in the vector.
`int` `getNumNonZeroElements()`
Return the number of non zero elements in the vector.
`int` `hashCode()`
`void` ```incrementQuick(int index, double increment)```
Increment the value at the given index by the given value.
`void` `invalidateCachedLength()`
Invalidates the length cache.
`protected abstract Iterator<Vector.Element>` `iterateNonZero()`
Iterates over all non-zero elements.
`protected abstract Iterator<Vector.Element>` `iterator()`
Iterates over all elements

* NOTE: Implementations may choose to reuse the Element returned for performance reasons, so if you need a copy of it, you should call `getElement(int)` for the given index

`Vector` `logNormalize()`
Return a new vector containing the log(1 + entry)/ L_2 norm values of the recipient
`Vector` `logNormalize(double power)`
Return a new Vector with a normalized value calculated as log_power(1 + entry)/ L_power norm.
`Vector` ```logNormalize(double power, double normLength)```
`protected abstract Matrix` ```matrixLike(int rows, int columns)```
Subclasses must override to return an appropriately sparse or dense result
`double` `maxValue()`
`int` `maxValueIndex()`
`Vector` `minus(Vector that)`
Return a new vector containing the element by element difference of the recipient and the argument
`double` `minValue()`
`int` `minValueIndex()`
`Iterable<Vector.Element>` `nonZeroes()`
`double` `norm(double power)`
Return the k-norm of the vector.
`Vector` `normalize()`
Return a new vector containing the normalized (L_2 norm) values of the recipient
`Vector` `normalize(double power)`
Return a new Vector containing the normalized (L_power norm) values of the recipient.
`Vector` `plus(double x)`
Return a new vector containing the sum of each value of the recipient and the argument
`Vector` `plus(Vector that)`
Return a new vector containing the element by element sum of the recipient and the argument
`void` ```set(int index, double value)```
Set the value at the given index
`int` `size()`
Return the cardinality of the recipient (the maximum number of values)
`String` `sparseVectorToString()`
toString() implementation for sparse vectors via `nonZeroes()` method
`Vector` `times(double x)`
Return a new vector containing the product of each value of the recipient and the argument
`Vector` `times(Vector that)`
Return a new vector containing the element-wise product of the recipient and the argument
`String` `toString()`
`String` `toString(String[] dictionary)`
`Vector` ```viewPart(int offset, int length)```
Return a new vector containing the subset of the recipient
`double` `zSum()`
Return the sum of all the elements of the receiver
• ### Methods inherited from class java.lang.Object

`finalize, getClass, notify, notifyAll, wait, wait, wait`
• ### Methods inherited from interface org.apache.mahout.math.Vector

`getIteratorAdvanceCost, getLookupCost, getNumNondefaultElements, getQuick, isAddConstantTime, isDense, isSequentialAccess, like, mergeUpdates, setQuick`
• ### Field Detail

• #### lengthSquared

`protected double lengthSquared`
• ### Constructor Detail

• #### AbstractVector

`protected AbstractVector(int size)`
• ### Method Detail

• #### all

`public Iterable<Vector.Element> all()`
Specified by:
`all` in interface `Vector`
• #### nonZeroes

`public Iterable<Vector.Element> nonZeroes()`
Specified by:
`nonZeroes` in interface `Vector`
• #### iterator

`protected abstract Iterator<Vector.Element> iterator()`
Iterates over all elements

* NOTE: Implementations may choose to reuse the Element returned for performance reasons, so if you need a copy of it, you should call `getElement(int)` for the given index

Returns:
An `Iterator` over all elements
• #### iterateNonZero

`protected abstract Iterator<Vector.Element> iterateNonZero()`
Iterates over all non-zero elements.

NOTE: Implementations may choose to reuse the Element returned for performance reasons, so if you need a copy of it, you should call `getElement(int)` for the given index

Returns:
An `Iterator` over all non-zero elements
• #### aggregate

```public double aggregate(DoubleDoubleFunction aggregator,
DoubleFunction map)```
Aggregates a vector by applying a mapping function fm(x) to every component and aggregating the results with an aggregating function fa(x, y).
Specified by:
`aggregate` in interface `Vector`
Parameters:
`aggregator` - used to combine the current value of the aggregation with the result of map.apply(nextValue)
`map` - a function to apply to each element of the vector in turn before passing to the aggregator
Returns:
the result of the aggregation
• #### aggregate

```public double aggregate(Vector other,
DoubleDoubleFunction aggregator,
DoubleDoubleFunction combiner)```
Description copied from interface: `Vector`

Generalized inner product - take two vectors, iterate over them both, using the combiner to combine together (and possibly map in some way) each pair of values, which are then aggregated with the previous accumulated value in the combiner.

Example: dot(other) could be expressed as aggregate(other, Plus, Times), and kernelized inner products (which are symmetric on the indices) work similarly.

Specified by:
`aggregate` in interface `Vector`
Parameters:
`other` - a vector to aggregate in combination with
`aggregator` - function we're aggregating with; fa
`combiner` - function we're combining with; fc
Returns:
the final aggregation; if r0 = fc(this, other), ri = fa(r_{i-1}, fc(this[i], other[i])) for all i > 0
• #### matrixLike

```protected abstract Matrix matrixLike(int rows,
int columns)```
Subclasses must override to return an appropriately sparse or dense result
Parameters:
`rows` - the row cardinality
`columns` - the column cardinality
Returns:
a Matrix
• #### viewPart

```public Vector viewPart(int offset,
int length)```
Description copied from interface: `Vector`
Return a new vector containing the subset of the recipient
Specified by:
`viewPart` in interface `Vector`
Parameters:
`offset` - an int offset into the receiver
`length` - the cardinality of the desired result
Returns:
a new Vector
• #### clone

`public Vector clone()`
Description copied from interface: `Vector`
Return a copy of the recipient
Specified by:
`clone` in interface `Vector`
Overrides:
`clone` in class `Object`
Returns:
a new Vector
• #### divide

`public Vector divide(double x)`
Description copied from interface: `Vector`
Return a new vector containing the values of the recipient divided by the argument
Specified by:
`divide` in interface `Vector`
Parameters:
`x` - a double value
Returns:
a new Vector
• #### dot

`public double dot(Vector x)`
Description copied from interface: `Vector`
Return the dot product of the recipient and the argument
Specified by:
`dot` in interface `Vector`
Parameters:
`x` - a Vector
Returns:
a new Vector
• #### dotSelf

`protected double dotSelf()`
• #### get

`public double get(int index)`
Description copied from interface: `Vector`
Return the value at the given index
Specified by:
`get` in interface `Vector`
Parameters:
`index` - an int index
Returns:
the double at the index
• #### getElement

`public Vector.Element getElement(int index)`
Description copied from interface: `Vector`
Return an object of Vector.Element representing an element of this Vector. Useful when designing new iterator types.
Specified by:
`getElement` in interface `Vector`
Parameters:
`index` - Index of the Vector.Element required
Returns:
The Vector.Element Object
• #### normalize

`public Vector normalize()`
Description copied from interface: `Vector`
Return a new vector containing the normalized (L_2 norm) values of the recipient
Specified by:
`normalize` in interface `Vector`
Returns:
a new Vector
• #### normalize

`public Vector normalize(double power)`
Description copied from interface: `Vector`
Return a new Vector containing the normalized (L_power norm) values of the recipient.

See http://en.wikipedia.org/wiki/Lp_space

Technically, when 0 < power < 1, we don't have a norm, just a metric, but we'll overload this here.

Also supports power == 0 (number of non-zero elements) and power = `Double.POSITIVE_INFINITY` (max element). Again, see the Wikipedia page for more info

Specified by:
`normalize` in interface `Vector`
Parameters:
`power` - The power to use. Must be >= 0. May also be `Double.POSITIVE_INFINITY`. See the Wikipedia link for more on this.
Returns:
a new Vector x such that norm(x, power) == 1
• #### logNormalize

`public Vector logNormalize()`
Description copied from interface: `Vector`
Return a new vector containing the log(1 + entry)/ L_2 norm values of the recipient
Specified by:
`logNormalize` in interface `Vector`
Returns:
a new Vector
• #### logNormalize

`public Vector logNormalize(double power)`
Description copied from interface: `Vector`
Return a new Vector with a normalized value calculated as log_power(1 + entry)/ L_power norm.

Specified by:
`logNormalize` in interface `Vector`
Parameters:
`power` - The power to use. Must be > 1. Cannot be `Double.POSITIVE_INFINITY`.
Returns:
a new Vector
• #### logNormalize

```public Vector logNormalize(double power,
double normLength)```
• #### norm

`public double norm(double power)`
Description copied from interface: `Vector`
Return the k-norm of the vector.

See http://en.wikipedia.org/wiki/Lp_space

Technically, when 0 > power < 1, we don't have a norm, just a metric, but we'll overload this here. Also supports power == 0 (number of non-zero elements) and power = `Double.POSITIVE_INFINITY` (max element). Again, see the Wikipedia page for more info.

Specified by:
`norm` in interface `Vector`
Parameters:
`power` - The power to use.
`Vector.normalize(double)`
• #### getLengthSquared

`public double getLengthSquared()`
Description copied from interface: `Vector`
Return the sum of squares of all elements in the vector. Square root of this value is the length of the vector.
Specified by:
`getLengthSquared` in interface `Vector`
Returns:
The sum of the squares of all elements in the vector.
• #### invalidateCachedLength

`public void invalidateCachedLength()`
Invalidates the length cache. This should be called by all mutators of the vector.
• #### getDistanceSquared

`public double getDistanceSquared(Vector that)`
Description copied from interface: `Vector`
Get the square of the distance between this vector and the other vector.
Specified by:
`getDistanceSquared` in interface `Vector`
• #### maxValue

`public double maxValue()`
Specified by:
`maxValue` in interface `Vector`
Returns:
The maximum value in the Vector
• #### maxValueIndex

`public int maxValueIndex()`
Specified by:
`maxValueIndex` in interface `Vector`
Returns:
The index of the maximum value
• #### minValue

`public double minValue()`
Specified by:
`minValue` in interface `Vector`
Returns:
The minimum value in the Vector
• #### minValueIndex

`public int minValueIndex()`
Specified by:
`minValueIndex` in interface `Vector`
Returns:
The index of the minimum value
• #### plus

`public Vector plus(double x)`
Description copied from interface: `Vector`
Return a new vector containing the sum of each value of the recipient and the argument
Specified by:
`plus` in interface `Vector`
Parameters:
`x` - a double
Returns:
a new Vector
• #### plus

`public Vector plus(Vector that)`
Description copied from interface: `Vector`
Return a new vector containing the element by element sum of the recipient and the argument
Specified by:
`plus` in interface `Vector`
Parameters:
`that` - a Vector
Returns:
a new Vector
• #### minus

`public Vector minus(Vector that)`
Description copied from interface: `Vector`
Return a new vector containing the element by element difference of the recipient and the argument
Specified by:
`minus` in interface `Vector`
Parameters:
`that` - a Vector
Returns:
a new Vector
• #### set

```public void set(int index,
double value)```
Description copied from interface: `Vector`
Set the value at the given index
Specified by:
`set` in interface `Vector`
Parameters:
`index` - an int index into the receiver
`value` - a double value to set
• #### incrementQuick

```public void incrementQuick(int index,
double increment)```
Description copied from interface: `Vector`
Increment the value at the given index by the given value.
Specified by:
`incrementQuick` in interface `Vector`
Parameters:
`index` - an int index into the receiver
`increment` - sets the value at the given index to value + increment;
• #### times

`public Vector times(double x)`
Description copied from interface: `Vector`
Return a new vector containing the product of each value of the recipient and the argument
Specified by:
`times` in interface `Vector`
Parameters:
`x` - a double argument
Returns:
a new Vector
• #### createOptimizedCopy

`protected Vector createOptimizedCopy()`
Copy the current vector in the most optimum fashion. Used by immutable methods like plus(), minus(). Use this instead of vector.like().assign(vector). Sub-class can choose to override this method.
Returns:
a copy of the current vector.
• #### times

`public Vector times(Vector that)`
Description copied from interface: `Vector`
Return a new vector containing the element-wise product of the recipient and the argument
Specified by:
`times` in interface `Vector`
Parameters:
`that` - a Vector argument
Returns:
a new Vector
• #### zSum

`public double zSum()`
Description copied from interface: `Vector`
Return the sum of all the elements of the receiver
Specified by:
`zSum` in interface `Vector`
Returns:
a double
• #### getNumNonZeroElements

`public int getNumNonZeroElements()`
Description copied from interface: `Vector`
Return the number of non zero elements in the vector.
Specified by:
`getNumNonZeroElements` in interface `Vector`
Returns:
an int
• #### assign

`public Vector assign(double value)`
Description copied from interface: `Vector`
Assign the value to all elements of the receiver
Specified by:
`assign` in interface `Vector`
Parameters:
`value` - a double value
Returns:
• #### assign

`public Vector assign(double[] values)`
Description copied from interface: `Vector`
Assign the values to the receiver
Specified by:
`assign` in interface `Vector`
Parameters:
`values` - a double[] of values
Returns:
• #### assign

`public Vector assign(Vector other)`
Description copied from interface: `Vector`
Assign the other vector values to the receiver
Specified by:
`assign` in interface `Vector`
Parameters:
`other` - a Vector
Returns:
• #### assign

```public Vector assign(DoubleDoubleFunction f,
double y)```
Description copied from interface: `Vector`
Apply the function to each element of the receiver, using the y value as the second argument of the DoubleDoubleFunction
Specified by:
`assign` in interface `Vector`
Parameters:
`f` - a DoubleDoubleFunction to be applied
`y` - a double value to be argument to the function
Returns:
• #### assign

`public Vector assign(DoubleFunction f)`
Description copied from interface: `Vector`
Apply the function to each element of the receiver
Specified by:
`assign` in interface `Vector`
Parameters:
`f` - a DoubleFunction to apply
Returns:
• #### assign

```public Vector assign(Vector other,
DoubleDoubleFunction function)```
Description copied from interface: `Vector`
Apply the function to each element of the receiver and the corresponding element of the other argument
Specified by:
`assign` in interface `Vector`
Parameters:
`other` - a Vector containing the second arguments to the function
`function` - a DoubleDoubleFunction to apply
Returns:
• #### cross

`public Matrix cross(Vector other)`
Description copied from interface: `Vector`
Return the cross product of the receiver and the other vector
Specified by:
`cross` in interface `Vector`
Parameters:
`other` - another Vector
Returns:
a Matrix
• #### size

`public final int size()`
Description copied from interface: `Vector`
Return the cardinality of the recipient (the maximum number of values)
Specified by:
`size` in interface `Vector`
Returns:
an int
• #### asFormatString

`public String asFormatString()`
Specified by:
`asFormatString` in interface `Vector`
Returns:
a formatted String suitable for output
• #### hashCode

`public int hashCode()`
Overrides:
`hashCode` in class `Object`
• #### equals

`public boolean equals(Object o)`
Determines whether this `Vector` represents the same logical vector as another object. Two `Vector`s are equal (regardless of implementation) if the value at each index is the same, and the cardinalities are the same.
Overrides:
`equals` in class `Object`
• #### toString

`public String toString()`
Overrides:
`toString` in class `Object`
• #### toString

`public String toString(String[] dictionary)`
• #### sparseVectorToString

`public String sparseVectorToString()`
toString() implementation for sparse vectors via `nonZeroes()` method
Returns:
String representation of the vector