org.tribuo.math.la

## Class DenseVector

• ### Field Summary

Fields
Modifier and Type Field and Description
`protected double[]` `elements`
• ### Constructor Summary

Constructors
Modifier Constructor and Description
`protected ` `DenseVector(DenseVector other)`
`protected ` `DenseVector(double[] values)`
Does not defensively copy the input, used internally.
` ` `DenseVector(int size)`
` ` ```DenseVector(int size, double value)```
• ### Method Summary

All Methods
Modifier and Type Method and Description
`void` ```add(int index, double value)```
Adds `value` to the element at `index`.
`DenseVector` `add(SGDVector other)`
Adds `other` to this vector, producing a new `DenseVector`.
`DenseVector` `copy()`
Returns a deep copy of this vector.
`static DenseVector` `createDenseVector(double[] values)`
Defensively copies the values before construction.
`double` `dot(SGDVector other)`
Calculates the dot product between this vector and `other`.
`boolean` `equals(Object other)`
Equals is defined mathematically, that is two SGDVectors are equal iff they have the same indices and the same values at those indices.
`double` `euclideanDistance(SGDVector other)`
The l2 or euclidean distance between this vector and the other vector.
`void` `expNormalize(double total)`
An optimisation for the exponential normalizer when you already know the normalization constant.
`void` `fill(double value)`
Fills this `DenseVector` with `value`.
`void` `foreachInPlace(DoubleUnaryOperator f)`
`double` `get(int index)`
Gets an element from this vector.
`int[]` `getShape()`
Returns an int array specifying the shape of this `Tensor`.
`void` ```hadamardProductInPlace(Tensor other, DoubleUnaryOperator f)```
Updates this `Tensor` with the Hadamard product (i.e., a term by term multiply) of this and `other`.
`int` `hashCode()`
`int` `indexOfMax()`
Returns the index of the maximum value.
`void` ```intersectAndAddInPlace(Tensor other, DoubleUnaryOperator f)```
Updates this `Tensor` by adding all the values from the intersection with `other`.
`VectorIterator` `iterator()`
`double` `l1Distance(SGDVector other)`
The l1 or Manhattan distance between this vector and the other vector.
`double` `maxValue()`
Returns the maximum value.
`double` `minValue()`
Returns the minimum value.
`void` `normalize(VectorNormalizer normalizer)`
Normalizes the vector using the supplied vector normalizer.
`int` `numActiveElements()`
Returns the number of non-zero elements (on construction, an element could be set to zero and it would still remain active).
`double` `oneNorm()`
Calculates the Manhattan norm for this vector.
`Matrix` `outer(SGDVector other)`
Generates the matrix representing the outer product between the two vectors.
`double` ```reduce(double initialValue, DoubleUnaryOperator op, DoubleBinaryOperator reduction)```
Performs a reduction from left to right of this vector.
`Tensor` `reshape(int[] newShape)`
Reshapes the Tensor to the supplied shape.
`DenseVector` `scale(double coefficient)`
Generates a new vector with each element scaled by `coefficient`.
`void` ```set(int index, double value)```
Sets the `index` to the `value`.
`void` `setElements(DenseVector other)`
Sets all the elements of this vector to be the same as `other`.
`int` `size()`
Returns the dimensionality of this vector.
`SparseVector` `sparsify()`
Generates a `SparseVector` representation from this dense vector, removing all values with absolute value below `VectorTuple.DELTA`.
`SparseVector` `sparsify(double tolerance)`
Generates a `SparseVector` representation from this dense vector, removing all values with absolute value below the supplied tolerance.
`DenseVector` `subtract(SGDVector other)`
Subtracts `other` from this vector, producing a new `DenseVector`.
`double` `sum()`
Calculates the sum of this vector.
`double` `sum(DoubleUnaryOperator f)`
`double[]` `toArray()`
Generates a copy of the values in this DenseVector.
`String` `toString()`
`double` `twoNorm()`
Calculates the euclidean norm for this vector.
`double` `variance(double mean)`
Calculates the variance of this vector based on the supplied mean.
• ### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`
• ### Methods inherited from interface org.tribuo.math.la.SGDVector

`cosineDistance, cosineSimilarity, l2Distance, variance`
• ### Methods inherited from interface org.tribuo.math.la.Tensor

`hadamardProductInPlace, intersectAndAddInPlace, scalarAddInPlace, scaleInPlace, shapeCheck, shapeSum`
• ### Methods inherited from interface java.lang.Iterable

`forEach, spliterator`
• ### Field Detail

• #### elements

`protected final double[] elements`
• ### Constructor Detail

• #### DenseVector

`public DenseVector(int size)`
• #### DenseVector

```public DenseVector(int size,
double value)```
• #### DenseVector

`protected DenseVector(double[] values)`
Does not defensively copy the input, used internally.
Parameters:
`values` - The values of this dense vector.
• #### DenseVector

`protected DenseVector(DenseVector other)`
• ### Method Detail

• #### createDenseVector

`public static DenseVector createDenseVector(double[] values)`
Defensively copies the values before construction.
Parameters:
`values` - The values of this dense vector.
Returns:
A new dense vector.
• #### toArray

`public double[] toArray()`
Generates a copy of the values in this DenseVector.

This implementation uses Arrays.copyOf, and should be overridden if the get function has been modified.

Returns:
A copy of the values in this DenseVector.
• #### getShape

`public int[] getShape()`
Description copied from interface: `Tensor`
Returns an int array specifying the shape of this `Tensor`.
Specified by:
`getShape` in interface `Tensor`
Returns:
An int array.
• #### reshape

`public Tensor reshape(int[] newShape)`
Description copied from interface: `Tensor`
Reshapes the Tensor to the supplied shape. Throws `IllegalArgumentException` if the shape isn't compatible.
Specified by:
`reshape` in interface `Tensor`
Parameters:
`newShape` - The desired shape.
Returns:
A Tensor of the desired shape.
• #### copy

`public DenseVector copy()`
Description copied from interface: `SGDVector`
Returns a deep copy of this vector.
Specified by:
`copy` in interface `SGDVector`
Returns:
A copy of this vector.
• #### size

`public int size()`
Description copied from interface: `SGDVector`
Returns the dimensionality of this vector.
Specified by:
`size` in interface `SGDVector`
Returns:
The dimensionality of the vector.
• #### numActiveElements

`public int numActiveElements()`
Description copied from interface: `SGDVector`
Returns the number of non-zero elements (on construction, an element could be set to zero and it would still remain active).
Specified by:
`numActiveElements` in interface `SGDVector`
Returns:
The number of non-zero elements.
• #### reduce

```public double reduce(double initialValue,
DoubleUnaryOperator op,
DoubleBinaryOperator reduction)```
Performs a reduction from left to right of this vector.
Parameters:
`initialValue` - The initial value.
`op` - The element wise operation to apply before reducing.
`reduction` - The reduction operation (should be commutative).
Returns:
The reduced value.
• #### equals

`public boolean equals(Object other)`
Equals is defined mathematically, that is two SGDVectors are equal iff they have the same indices and the same values at those indices.
Overrides:
`equals` in class `Object`
Parameters:
`other` - Object to compare against.
Returns:
True if this vector and the other vector contain the same values in the same order.
• #### hashCode

`public int hashCode()`
Overrides:
`hashCode` in class `Object`

`public DenseVector add(SGDVector other)`
Adds `other` to this vector, producing a new `DenseVector`.
Specified by:
`add` in interface `SGDVector`
Parameters:
`other` - The vector to add.
Returns:
A new `DenseVector` where each element value = this.get(i) + other.get(i).
• #### subtract

`public DenseVector subtract(SGDVector other)`
Subtracts `other` from this vector, producing a new `DenseVector`.
Specified by:
`subtract` in interface `SGDVector`
Parameters:
`other` - The vector to subtract.
Returns:
A new `DenseVector` where each element value = this.get(i) - other.get(i).

```public void intersectAndAddInPlace(Tensor other,
DoubleUnaryOperator f)```
Description copied from interface: `Tensor`
Updates this `Tensor` by adding all the values from the intersection with `other`.

The function `f` is applied to all values from `other` before the addition.

Each value is updated as value += f(otherValue).

Specified by:
`intersectAndAddInPlace` in interface `Tensor`
Parameters:
`other` - The other `Tensor`.
`f` - A function to apply.

```public void hadamardProductInPlace(Tensor other,
DoubleUnaryOperator f)```
Description copied from interface: `Tensor`
Updates this `Tensor` with the Hadamard product (i.e., a term by term multiply) of this and `other`.

The function `f` is applied to all values from `other` before the addition.

Each value is updated as value *= f(otherValue).

Specified by:
`hadamardProductInPlace` in interface `Tensor`
Parameters:
`other` - The other `Tensor`.
`f` - A function to apply.
• #### foreachInPlace

`public void foreachInPlace(DoubleUnaryOperator f)`
Description copied from interface: `Tensor`
Specified by:
`foreachInPlace` in interface `Tensor`
Parameters:
`f` - The function to apply.
• #### scale

`public DenseVector scale(double coefficient)`
Description copied from interface: `SGDVector`
Generates a new vector with each element scaled by `coefficient`.
Specified by:
`scale` in interface `SGDVector`
Parameters:
`coefficient` - The coefficient to scale the elements by.
Returns:
A new `SGDVector`.

```public void add(int index,
double value)```
Description copied from interface: `SGDVector`
Adds `value` to the element at `index`.
Specified by:
`add` in interface `SGDVector`
Parameters:
`index` - The index to update.
`value` - The value to add.
• #### dot

`public double dot(SGDVector other)`
Description copied from interface: `SGDVector`
Calculates the dot product between this vector and `other`.
Specified by:
`dot` in interface `SGDVector`
Parameters:
`other` - The other vector.
Returns:
The dot product.
• #### outer

`public Matrix outer(SGDVector other)`
Description copied from interface: `SGDVector`
Generates the matrix representing the outer product between the two vectors.
Specified by:
`outer` in interface `SGDVector`
Parameters:
`other` - Another `SGDVector`
Returns:
The outer product `Matrix`.
• #### sum

`public double sum()`
Description copied from interface: `SGDVector`
Calculates the sum of this vector.
Specified by:
`sum` in interface `SGDVector`
Returns:
The sum.
• #### sum

`public double sum(DoubleUnaryOperator f)`
• #### twoNorm

`public double twoNorm()`
Description copied from interface: `SGDVector`
Calculates the euclidean norm for this vector.
Specified by:
`twoNorm` in interface `SGDVector`
Specified by:
`twoNorm` in interface `Tensor`
Returns:
The euclidean norm.
• #### oneNorm

`public double oneNorm()`
Description copied from interface: `SGDVector`
Calculates the Manhattan norm for this vector.
Specified by:
`oneNorm` in interface `SGDVector`
Returns:
The Manhattan norm.
• #### get

`public double get(int index)`
Description copied from interface: `SGDVector`
Gets an element from this vector.
Specified by:
`get` in interface `SGDVector`
Parameters:
`index` - The index of the element.
Returns:
The value at that index.
• #### set

```public void set(int index,
double value)```
Description copied from interface: `SGDVector`
Sets the `index` to the `value`.
Specified by:
`set` in interface `SGDVector`
Parameters:
`index` - The index to set.
`value` - The value to set it to.
• #### setElements

`public void setElements(DenseVector other)`
Sets all the elements of this vector to be the same as `other`.
Parameters:
`other` - The `DenseVector` to copy.
• #### fill

`public void fill(double value)`
Fills this `DenseVector` with `value`.
Parameters:
`value` - The value to store in this vector.
• #### indexOfMax

`public int indexOfMax()`
Description copied from interface: `SGDVector`
Returns the index of the maximum value. Requires probing the array.
Specified by:
`indexOfMax` in interface `SGDVector`
Returns:
The index of the maximum value.
• #### maxValue

`public double maxValue()`
Description copied from interface: `SGDVector`
Returns the maximum value. Requires probing the array.
Specified by:
`maxValue` in interface `SGDVector`
Returns:
The maximum value.
• #### minValue

`public double minValue()`
Description copied from interface: `SGDVector`
Returns the minimum value. Requires probing the array.
Specified by:
`minValue` in interface `SGDVector`
Returns:
The minimum value.
• #### normalize

`public void normalize(VectorNormalizer normalizer)`
Description copied from interface: `SGDVector`
Normalizes the vector using the supplied vector normalizer.
Specified by:
`normalize` in interface `SGDVector`
Parameters:
`normalizer` - The kind of normalization to apply.
• #### expNormalize

`public void expNormalize(double total)`
An optimisation for the exponential normalizer when you already know the normalization constant. Used in the CRF.
Parameters:
`total` - The normalization constant.
• #### toString

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

`public double variance(double mean)`
Description copied from interface: `SGDVector`
Calculates the variance of this vector based on the supplied mean.
Specified by:
`variance` in interface `SGDVector`
Parameters:
`mean` - The mean of the vector.
Returns:
The variance of the vector.
• #### iterator

`public VectorIterator iterator()`
Specified by:
`iterator` in interface `Iterable<VectorTuple>`
• #### sparsify

`public SparseVector sparsify(double tolerance)`
Generates a `SparseVector` representation from this dense vector, removing all values with absolute value below the supplied tolerance.
Parameters:
`tolerance` - The threshold below which to set a value to zero.
Returns:
A `SparseVector`.
• #### euclideanDistance

`public double euclideanDistance(SGDVector other)`
The l2 or euclidean distance between this vector and the other vector.
Specified by:
`euclideanDistance` in interface `SGDVector`
Parameters:
`other` - The other vector.
Returns:
The euclidean distance between them.
• #### l1Distance

`public double l1Distance(SGDVector other)`
The l1 or Manhattan distance between this vector and the other vector.
Specified by:
`l1Distance` in interface `SGDVector`
Parameters:
`other` - The other vector.
Returns:
The l1 distance.