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
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedDenseVector(double[] values) Does not defensively copy the input, used internally.DenseVector(int size) DenseVector(int size, double value) protectedDenseVector(DenseVector other) -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(int index, double value) Addsvalueto the element atindex.Addsotherto this vector, producing a newDenseVector.copy()Returns a deep copy of this vector.static DenseVectorcreateDenseVector(double[] values) Defensively copies the values before construction.doubleCalculates the dot product between this vector andother.booleanEquals is defined mathematically, that is two SGDVectors are equal iff they have the same indices and the same values at those indices.doubleeuclideanDistance(SGDVector other) The l2 or euclidean distance between this vector and the other vector.voidexpNormalize(double total) An optimisation for the exponential normalizer when you already know the normalization constant.voidfill(double value) Fills thisDenseVectorwithvalue.voidApplies aDoubleUnaryOperatorelementwise to thisTensor.doubleget(int index) Gets an element from this vector.int[]getShape()Returns an int array specifying the shape of thisTensor.voidhadamardProductInPlace(Tensor other, DoubleUnaryOperator f) Updates thisTensorwith the Hadamard product (i.e., a term by term multiply) of this andother.inthashCode()intReturns the index of the maximum value.voidintersectAndAddInPlace(Tensor other, DoubleUnaryOperator f) Updates thisTensorby adding all the values from the intersection withother.iterator()doublel1Distance(SGDVector other) The l1 or Manhattan distance between this vector and the other vector.doublemaxValue()Returns the maximum value.doubleminValue()Returns the minimum value.voidnormalize(VectorNormalizer normalizer) Normalizes the vector using the supplied vector normalizer.intReturns the number of non-zero elements (on construction, an element could be set to zero and it would still remain active).doubleoneNorm()Calculates the Manhattan norm for this vector.Generates the matrix representing the outer product between the two vectors.doublereduce(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.voidset(int index, double value) Sets theindexto thevalue.voidsetElements(DenseVector other) Sets all the elements of this vector to be the same asother.intsize()Returns the dimensionality of this vector.sparsify()Generates aSparseVectorrepresentation from this dense vector, removing all values with absolute value belowVectorTuple.DELTA.sparsify(double tolerance) Generates aSparseVectorrepresentation from this dense vector, removing all values with absolute value below the supplied tolerance.Subtractsotherfrom this vector, producing a newDenseVector.doublesum()Calculates the sum of this vector.doubledouble[]toArray()Generates a copy of the values in this DenseVector.toString()doubletwoNorm()Calculates the euclidean norm for this vector.doublevariance(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, waitMethods inherited from interface java.lang.Iterable
forEach, spliteratorMethods inherited from interface org.tribuo.math.la.SGDVector
cosineDistance, cosineSimilarity, l2Distance, varianceMethods inherited from interface org.tribuo.math.la.Tensor
hadamardProductInPlace, intersectAndAddInPlace, scalarAddInPlace, scaleInPlace
-
Field Details
-
elements
-
-
Constructor Details
-
DenseVector
-
DenseVector
-
DenseVector
Does not defensively copy the input, used internally.- Parameters:
values- The values of this dense vector.
-
DenseVector
-
-
Method Details
-
createDenseVector
Defensively copies the values before construction.- Parameters:
values- The values of this dense vector.- Returns:
- A new dense vector.
-
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
-
reshape
Description copied from interface:TensorReshapes the Tensor to the supplied shape. ThrowsIllegalArgumentExceptionif the shape isn't compatible. -
copy
Description copied from interface:SGDVectorReturns a deep copy of this vector. -
size
-
numActiveElements
Description copied from interface:SGDVectorReturns the number of non-zero elements (on construction, an element could be set to zero and it would still remain active).- Specified by:
numActiveElementsin interfaceSGDVector- Returns:
- The number of non-zero elements.
-
reduce
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
Equals is defined mathematically, that is two SGDVectors are equal iff they have the same indices and the same values at those indices. -
hashCode
-
add
Addsotherto this vector, producing a newDenseVector.- Specified by:
addin interfaceSGDVector- Parameters:
other- The vector to add.- Returns:
- A new
DenseVectorwhere each element value = this.get(i) + other.get(i).
-
subtract
Subtractsotherfrom this vector, producing a newDenseVector.- Specified by:
subtractin interfaceSGDVector- Parameters:
other- The vector to subtract.- Returns:
- A new
DenseVectorwhere each element value = this.get(i) - other.get(i).
-
intersectAndAddInPlace
Description copied from interface:TensorUpdates thisTensorby adding all the values from the intersection withother.The function
fis applied to all values fromotherbefore the addition.Each value is updated as value += f(otherValue).
- Specified by:
intersectAndAddInPlacein interfaceTensor- Parameters:
other- The otherTensor.f- A function to apply.
-
hadamardProductInPlace
Description copied from interface:TensorUpdates thisTensorwith the Hadamard product (i.e., a term by term multiply) of this andother.The function
fis applied to all values fromotherbefore the addition.Each value is updated as value *= f(otherValue).
- Specified by:
hadamardProductInPlacein interfaceTensor- Parameters:
other- The otherTensor.f- A function to apply.
-
foreachInPlace
Description copied from interface:TensorApplies aDoubleUnaryOperatorelementwise to thisTensor.- Specified by:
foreachInPlacein interfaceTensor- Parameters:
f- The function to apply.
-
scale
-
add
-
dot
-
outer
-
sum
-
sum
-
twoNorm
-
oneNorm
-
get
-
set
-
setElements
Sets all the elements of this vector to be the same asother.- Parameters:
other- TheDenseVectorto copy.
-
fill
Fills thisDenseVectorwithvalue.- Parameters:
value- The value to store in this vector.
-
indexOfMax
Description copied from interface:SGDVectorReturns the index of the maximum value. Requires probing the array.- Specified by:
indexOfMaxin interfaceSGDVector- Returns:
- The index of the maximum value.
-
maxValue
-
minValue
-
normalize
Description copied from interface:SGDVectorNormalizes the vector using the supplied vector normalizer. -
expNormalize
An optimisation for the exponential normalizer when you already know the normalization constant. Used in the CRF.- Parameters:
total- The normalization constant.
-
toString
-
variance
-
iterator
- Specified by:
iteratorin interfaceIterable<VectorTuple>
-
sparsify
Generates aSparseVectorrepresentation from this dense vector, removing all values with absolute value belowVectorTuple.DELTA.- Returns:
- A
SparseVector.
-
sparsify
Generates aSparseVectorrepresentation 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:
euclideanDistancein 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:
l1Distancein interfaceSGDVector- Parameters:
other- The other vector.- Returns:
- The l1 distance.
-