Interface RealVector
- All Known Implementing Classes:
AbstractRealVector,DoubleRealVector,EncodedRealVector,FloatRealVector,HalfRealVector,MutableDoubleRealVector
DoubleRealVector stores 64-bit doubles, FloatRealVector stores 32-bit floats
truncated to doubles, HalfRealVector stores 16-bit halves likewise. All numerical
methods on this interface return values in double regardless of the underlying
precision; the precision difference shows up as round-trip loss in
toHalfRealVector() / toFloatRealVector() conversions, not in arithmetic.
The interface exposes three families of operations:
- Shape and access —
getNumDimensions(),getComponent(int),getData(). - Arithmetic —
add,subtract,multiply,dot,normalize, plus the squared-/raw-norm pairl2SquaredNorm()andl2Norm()and the cheaperl2SquaredDistance(RealVector)pairwise distance. Default implementations allocate a fresh result vector and never mutate the receiver; the mutating variants live onMutableDoubleRealVector. - Conversion and serialization — precision conversions
(
toHalfRealVector(),toFloatRealVector(),toDoubleRealVector()), mutability swaps (toMutable(),toImmutable()), and the byte-array round-trip (getRawData()/fromBytes(byte[])).
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final doubleThreshold (in L2-norm units) below which a vector is treated as "effectively zero" — i.e.static final com.google.common.collect.ImmutableList<VectorType> Cached snapshot ofVectorType.values()as an immutable list. -
Method Summary
Modifier and TypeMethodDescriptiondefault RealVectoradd(double scalar) Returns a new vector withscalaradded to every component of this vector.default RealVectoradd(RealVector other) Returns a new vector whose components are the element-wise sum of this vector andother.default doubledot(RealVector other) Returns the dot productΣ_i this[i] * other[i].static RealVectorfromBytes(byte[] vectorBytes) Creates aRealVectorfrom a byte array.static RealVectorfromBytes(VectorType vectorType, byte[] vectorBytes) Creates aRealVectorfrom a byte array.static VectorTypefromVectorTypeOrdinal(int ordinal) Returns theVectorTypewith the given ordinal inVectorType.values(), looked up from the cachedVECTOR_TYPESlist.doublegetComponent(int dimension) Gets the component of this object at the specified dimension.double[]getData()Returns the underlying data array.intReturns the number of elements in the vector, i.e.byte[]Gets the raw byte data representation of this object.default booleanReturnstruewhen this vector's L2 norm is at or below theEPSthreshold, i.e.default doublel2Norm()Returns the L2 (Euclidean) normsqrt(Σ_i this[i]^2).default doublel2SquaredDistance(RealVector other) Returns the squared Euclidean distance toother, i.e.doubleReturns the squared L2 normΣ_i this[i]^2.default RealVectormultiply(double scalar) Returns a new vector with every component of this vector scaled byscalar.default RealVectorReturns a new vector pointing in the same direction as this vector but scaled to unit L2 norm.default RealVectorsubtract(double scalar) Returns a new vector withscalarsubtracted from every component of this vector.default RealVectorsubtract(RealVector other) Returns a new vector whose components are the element-wise difference of this vector andother.Converts this vector into aDoubleRealVector.Converts this object into aRealVectorof single precision floating-point numbers.Converts this object into aRealVectorofHalfprecision floating-point numbers.Returns an immutable view of this vector — i.e.default MutableDoubleRealVectorReturns aMutableDoubleRealVectorcarrying the same components as this vector.withData(double[] data) Returns a new vector of the same precision and length as the receiver but with the given component data.
-
Field Details
-
EPS
static final double EPSThreshold (in L2-norm units) below which a vector is treated as "effectively zero" — i.e. its direction is considered undefined for metrics that depend on it, principally cosine similarity. Used byisNearlyZeroNorm(), which compares the squared L2 norm againstEPS * EPSto avoid asqrt.The value is sized to sit well above the floating-point noise of typical double-precision accumulations and well below any norm a meaningful vector would have in practice. Callers generally shouldn't need to consult this constant directly; prefer
isNearlyZeroNorm().- See Also:
-
VECTOR_TYPES
Cached snapshot ofVectorType.values()as an immutable list. Used byfromVectorTypeOrdinal(int)so the type lookup avoids re-cloning the enum's value array on every call.
-
-
Method Details
-
getNumDimensions
int getNumDimensions()Returns the number of elements in the vector, i.e. the number of dimensions.- Returns:
- the number of dimensions
-
getComponent
double getComponent(int dimension) Gets the component of this object at the specified dimension.The dimension is a zero-based index. For a 3D vector, for example, dimension 0 might correspond to the x-component, 1 to the y-component, and 2 to the z-component. This method provides direct access to the underlying data element.
- Parameters:
dimension- the zero-based index of the component to retrieve.- Returns:
- the component at the specified dimension, which is guaranteed to be non-null.
- Throws:
IndexOutOfBoundsException- if thedimensionis negative or greater than or equal to the number of dimensions of this object.
-
getData
@Nonnull double[] getData()Returns the underlying data array.The returned array is guaranteed to be non-null. Note that this method returns a direct reference to the internal array, not a copy.
- Returns:
- the data array of type
R[], nevernull.
-
withData
Returns a new vector of the same precision and length as the receiver but with the given component data. Implementations decide whether the returned vector aliasesdata(immutable subtypes typically do; mutable subtypes copy through their existing storage).- Parameters:
data- the components for the new vector; length must match this vector's dimensionality- Returns:
- a non-null vector with the given data
-
getRawData
@Nonnull byte[] getRawData()Gets the raw byte data representation of this object.This method provides a direct, unprocessed view of the object's underlying data. The format of the byte array is implementation-specific and should be documented by the concrete class that implements this method.
- Returns:
- a non-null byte array containing the raw data.
-
toHalfRealVector
Converts this object into aRealVectorofHalfprecision floating-point numbers.As this is an abstract method, implementing classes are responsible for defining the specific conversion logic from their internal representation to a
RealVectorusingHalfobjects to serialize and deserialize the vector. If this object already is aHalfRealVectorthis method should returnthis.- Returns:
- a non-null
HalfRealVectorcontaining theHalfprecision floating-point representation of this object.
-
toFloatRealVector
Converts this object into aRealVectorof single precision floating-point numbers.As this is an abstract method, implementing classes are responsible for defining the specific conversion logic from their internal representation to a
RealVectorusing floating point numbers to serialize and deserialize the vector. If this object already is aFloatRealVectorthis method should returnthis.- Returns:
- a non-null
FloatRealVectorcontaining the single precision floating-point representation of this object.
-
toDoubleRealVector
Converts this vector into aDoubleRealVector.This method provides a way to obtain a double-precision floating-point representation of the vector. If the vector is already an instance of
DoubleRealVector, this method may return the instance itself. Otherwise, it will create a newDoubleRealVectorcontaining the same elements, which may involve a conversion of the underlying data type.- Returns:
- a non-null
DoubleRealVectorrepresentation of this vector.
-
toMutable
Returns aMutableDoubleRealVectorcarrying the same components as this vector. The default implementation clones the underlying data so the returned mutable instance is independent of the receiver;MutableDoubleRealVector.toMutable()overrides this to returnthissince it's already mutable.- Returns:
- a fresh (or in the case of
MutableDoubleRealVector, the same) mutable double-precision vector
-
toImmutable
Returns an immutable view of this vector — i.e. one whose components cannot be subsequently mutated through any reference. Immutable subtypes returnthis;MutableDoubleRealVector.toImmutable()returns a freshDoubleRealVectorwith cloned data.- Returns:
- a non-null immutable vector with the same components as this vector
-
dot
Returns the dot productΣ_i this[i] * other[i]. The receiver is not mutated.- Parameters:
other- the right operand; must have the same dimensionality as this vector- Returns:
- the dot product
- Throws:
IllegalArgumentException- ifotherhas a different dimensionality
-
l2SquaredDistance
Returns the squared Euclidean distance toother, i.e.Σ_i (this[i] - other[i])^2. Equivalent to but cheaper thansubtract(other).l2SquaredNorm()(no temporary allocation), and cheaper thanMath.pow(estimator.distance(this, other), 2)for the Euclidean metric (skips asqrtthat would just be squared again). -
isNearlyZeroNorm
default boolean isNearlyZeroNorm()Returnstruewhen this vector's L2 norm is at or below theEPSthreshold, i.e. when the vector is "effectively zero" and its direction is undefined for cosine-style metrics. Implemented as a squared-norm comparison so nosqrtis needed.- Returns:
trueif this vector is effectively zero
-
l2Norm
default double l2Norm()Returns the L2 (Euclidean) normsqrt(Σ_i this[i]^2). Preferl2SquaredNorm()when you only need to compare or threshold magnitudes — it skips thesqrt.- Returns:
- the L2 norm of this vector
-
l2SquaredNorm
double l2SquaredNorm()Returns the squared L2 normΣ_i this[i]^2. Implementations typically memoize this since the value is reused byl2Norm()and several distance helpers.- Returns:
- the squared L2 norm of this vector
-
normalize
Returns a new vector pointing in the same direction as this vector but scaled to unit L2 norm. The receiver is not mutated; the result is a fresh allocation of the same precision type.- Returns:
- a non-null unit-norm vector
- Throws:
IllegalArgumentException- if this vector's L2 norm is zero, infinite, or NaN — direction is undefined in those cases
-
add
Returns a new vector whose components are the element-wise sum of this vector andother. The receiver is not mutated.- Parameters:
other- the right operand; must have the same dimensionality as this vector- Returns:
- a non-null vector with
result[i] = this[i] + other[i] - Throws:
IllegalArgumentException- ifotherhas a different dimensionality
-
add
Returns a new vector withscalaradded to every component of this vector. The receiver is not mutated.- Parameters:
scalar- the value to add to each component- Returns:
- a non-null vector with
result[i] = this[i] + scalar
-
subtract
Returns a new vector whose components are the element-wise difference of this vector andother. The receiver is not mutated.- Parameters:
other- the right operand; must have the same dimensionality as this vector- Returns:
- a non-null vector with
result[i] = this[i] - other[i] - Throws:
IllegalArgumentException- ifotherhas a different dimensionality
-
subtract
Returns a new vector withscalarsubtracted from every component of this vector. The receiver is not mutated.- Parameters:
scalar- the value to subtract from each component- Returns:
- a non-null vector with
result[i] = this[i] - scalar
-
multiply
Returns a new vector with every component of this vector scaled byscalar. The receiver is not mutated.- Parameters:
scalar- the factor to scale each component by- Returns:
- a non-null vector with
result[i] = this[i] * scalar
-
fromVectorTypeOrdinal
Returns theVectorTypewith the given ordinal inVectorType.values(), looked up from the cachedVECTOR_TYPESlist. Used while deserializing a vector to dispatch to the right subtype'sfromBytes.- Parameters:
ordinal- the type's enum ordinal- Returns:
- the matching
VectorType; nevernull - Throws:
IndexOutOfBoundsException- ifordinalis not a valid enum ordinal
-
fromBytes
Creates aRealVectorfrom a byte array.This method interprets the input byte array by interpreting the first byte of the array as the type of vector. It then delegates to
fromBytes(VectorType, byte[])to do the actual deserialization.- Parameters:
vectorBytes- the non-null byte array to convert.- Returns:
- a new
RealVectorinstance created from the byte array.
-
fromBytes
Creates aRealVectorfrom a byte array.This implementation dispatches to the actual logic that deserialize a byte array to a vector which is located in the respective implementations of
RealVector.- Parameters:
vectorType- the vector type of the serialized vectorvectorBytes- the non-null byte array to convert.- Returns:
- a new
RealVectorinstance created from the byte array.
-