Package org.tribuo.math.la
Class DenseVector
java.lang.Object
org.tribuo.math.la.DenseVector
- All Implemented Interfaces:
Serializable
,Iterable<VectorTuple>
,SGDVector
,Tensor
- Direct Known Subclasses:
ShrinkingVector
A dense vector, backed by a double array.
- See Also:
-
Field Summary
-
Constructor Summary
ModifierConstructorDescriptionprotected
DenseVector
(double[] values) Does not defensively copy the input, used internally.DenseVector
(int size) Creates an empty dense vector of the specified size.DenseVector
(int size, double value) Creates a dense vector of the specified size where each element is initialised to the specified value.protected
DenseVector
(DenseVector other) Copy constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(int index, double value) Addsvalue
to the element atindex
.Addsother
to this vector, producing a newDenseVector
.copy()
Returns a deep copy of this vector.static DenseVector
createDenseVector
(double[] values) Defensively copies the values before construction.static <T extends Output<T>>
DenseVectorcreateDenseVector
(Example<T> example, ImmutableFeatureMap featureInfo, boolean addBias) Builds aDenseVector
from anExample
.double
Calculates the dot product between this vector andother
.boolean
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 thisDenseVector
withvalue
.void
Applies aToDoubleBiFunction
elementwise to thisSGDVector
.void
Applies aDoubleUnaryOperator
elementwise to thisTensor
.double
get
(int index) Gets an element from this vector.int[]
getShape()
Returns an int array specifying the shape of thisTensor
.void
hadamardProductInPlace
(Tensor other, DoubleUnaryOperator f) Updates thisTensor
with the Hadamard product (i.e., a term by term multiply) of this andother
.int
hashCode()
int
Returns the index of the maximum value.void
intersectAndAddInPlace
(Tensor other, DoubleUnaryOperator f) Updates thisTensor
by adding all the values from the intersection withother
.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
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.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.reshape
(int[] newShape) Reshapes the Tensor to the supplied shape.scale
(double coefficient) Generates a new vector with each element scaled bycoefficient
.void
set
(int index, double value) Sets theindex
to thevalue
.void
setElements
(DenseVector other) Sets all the elements of this vector to be the same asother
.int
size()
Returns the dimensionality of this vector.sparsify()
Generates aSparseVector
representation from this dense vector, removing all values with absolute value belowVectorTuple.DELTA
.sparsify
(double tolerance) Generates aSparseVector
representation from this dense vector, removing all values with absolute value below the supplied tolerance.Subtractsother
from this vector, producing a newDenseVector
.double
sum()
Calculates the sum of this vector.double
Sums this vector, applying the supplied function to each element first.double[]
toArray()
Generates a copy of the values in this DenseVector.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 java.lang.Iterable
forEach, spliterator
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
-
Field Details
-
elements
protected final double[] elements
-
-
Constructor Details
-
DenseVector
public DenseVector(int size) Creates an empty dense vector of the specified size.- Parameters:
size
- The vector size.
-
DenseVector
public DenseVector(int size, double value) Creates a dense vector of the specified size where each element is initialised to the specified value.- Parameters:
size
- The vector size.value
- The initial value.
-
DenseVector
protected DenseVector(double[] values) Does not defensively copy the input, used internally.- Parameters:
values
- The values of this dense vector.
-
DenseVector
Copy constructor.- Parameters:
other
- The vector to copy.
-
-
Method Details
-
createDenseVector
Defensively copies the values before construction.- Parameters:
values
- The values of this dense vector.- Returns:
- A new dense vector.
-
createDenseVector
public static <T extends Output<T>> DenseVector createDenseVector(Example<T> example, ImmutableFeatureMap featureInfo, boolean addBias) Builds aDenseVector
from anExample
.Used in training and inference.
Throws
IllegalArgumentException
if the Example contains NaN-valued features or if no features in this Example are present in the feature map..Unspecified features are set to zero.
- Type Parameters:
T
- The type parameter of theexample
.- Parameters:
example
- The example to convert.featureInfo
- The feature information, used to calculate the dimension of this DenseVector.addBias
- Add a bias feature.- Returns:
- A DenseVector representing the example's features.
-
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.
-
getShape
public int[] getShape()Description copied from interface:Tensor
Returns an int array specifying the shape of thisTensor
. -
reshape
Description copied from interface:Tensor
Reshapes the Tensor to the supplied shape. ThrowsIllegalArgumentException
if the shape isn't compatible. -
copy
Description copied from interface:SGDVector
Returns a deep copy of this vector. -
size
public int size()Description copied from interface:SGDVector
Returns the dimensionality of this 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 interfaceSGDVector
- Returns:
- The number of non-zero elements.
-
reduce
Performs a reduction from left to right of this vector. -
equals
Equals is defined mathematically, that is two SGDVectors are equal iff they have the same indices and the same values at those indices. -
hashCode
public int hashCode() -
add
Addsother
to this vector, producing a newDenseVector
.- Specified by:
add
in interfaceSGDVector
- Parameters:
other
- The vector to add.- Returns:
- A new
DenseVector
where each element value = this.get(i) + other.get(i).
-
subtract
Subtractsother
from this vector, producing a newDenseVector
.- Specified by:
subtract
in interfaceSGDVector
- Parameters:
other
- The vector to subtract.- Returns:
- A new
DenseVector
where each element value = this.get(i) - other.get(i).
-
intersectAndAddInPlace
Description copied from interface:Tensor
Updates thisTensor
by adding all the values from the intersection withother
.The function
f
is applied to all values fromother
before the addition.Each value is updated as value += f(otherValue).
- Specified by:
intersectAndAddInPlace
in interfaceTensor
- Parameters:
other
- The otherTensor
.f
- A function to apply.
-
hadamardProductInPlace
Description copied from interface:Tensor
Updates thisTensor
with the Hadamard product (i.e., a term by term multiply) of this andother
.The function
f
is applied to all values fromother
before the addition.Each value is updated as value *= f(otherValue).
- Specified by:
hadamardProductInPlace
in interfaceTensor
- Parameters:
other
- The otherTensor
.f
- A function to apply.
-
foreachInPlace
Description copied from interface:Tensor
Applies aDoubleUnaryOperator
elementwise to thisTensor
.- Specified by:
foreachInPlace
in interfaceTensor
- Parameters:
f
- The function to apply.
-
foreachIndexedInPlace
Description copied from interface:SGDVector
Applies aToDoubleBiFunction
elementwise to thisSGDVector
.The first argument to the function is the index, the second argument is the current value.
- Specified by:
foreachIndexedInPlace
in interfaceSGDVector
- Parameters:
f
- The function to apply.
-
scale
Description copied from interface:SGDVector
Generates a new vector with each element scaled bycoefficient
. -
add
public void add(int index, double value) Description copied from interface:SGDVector
Addsvalue
to the element atindex
. -
dot
Description copied from interface:SGDVector
Calculates the dot product between this vector andother
. -
outer
Description copied from interface:SGDVector
Generates the matrix representing the outer product between the two vectors. -
sum
public double sum()Description copied from interface:SGDVector
Calculates the sum of this vector. -
sum
Sums this vector, applying the supplied function to each element first.- Parameters:
f
- The function to apply to the elements.- Returns:
- The sum of f(x).
-
twoNorm
public double twoNorm()Description copied from interface:SGDVector
Calculates the euclidean norm for this vector. -
oneNorm
public double oneNorm()Description copied from interface:SGDVector
Calculates the Manhattan norm for this vector. -
get
public double get(int index) Description copied from interface:SGDVector
Gets an element from this vector. -
set
public void set(int index, double value) Description copied from interface:SGDVector
Sets theindex
to thevalue
. -
setElements
Sets all the elements of this vector to be the same asother
.- Parameters:
other
- TheDenseVector
to copy.
-
fill
public void fill(double value) Fills thisDenseVector
withvalue
.- 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 interfaceSGDVector
- Returns:
- The index of the maximum value.
-
maxValue
public double maxValue()Description copied from interface:SGDVector
Returns the maximum value. Requires probing the array. -
minValue
public double minValue()Description copied from interface:SGDVector
Returns the minimum value. Requires probing the array. -
normalize
Description copied from interface:SGDVector
Normalizes the vector using the supplied vector normalizer. -
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
-
variance
public double variance(double mean) Description copied from interface:SGDVector
Calculates the variance of this vector based on the supplied mean. -
iterator
- Specified by:
iterator
in interfaceIterable<VectorTuple>
-
sparsify
Generates aSparseVector
representation from this dense vector, removing all values with absolute value belowVectorTuple.DELTA
.- Returns:
- A
SparseVector
.
-
sparsify
Generates aSparseVector
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
The l2 or euclidean distance between this vector and the other vector.- Specified by:
euclideanDistance
in interfaceSGDVector
- Parameters:
other
- The other vector.- Returns:
- The euclidean distance between them.
-
l1Distance
The l1 or Manhattan distance between this vector and the other vector.- Specified by:
l1Distance
in interfaceSGDVector
- Parameters:
other
- The other vector.- Returns:
- The l1 distance.
-