Class AbstractRealVector
- All Implemented Interfaces:
RealVector
- Direct Known Subclasses:
DoubleRealVector,FloatRealVector,HalfRealVector
RealVector subtype in this package. Holds
the canonical double[] storage, memoizes the three derived values that are pure
functions of the data (hashCode(), getRawData(), l2SquaredNorm()),
and delegates the precision-specific bits (raw-byte format, conversions to other vector
representations) to concrete subclasses.
The class is intentionally precision-agnostic at this level — every accessor returns
double, and subclasses only differ in storage resolution (which they truncate
to in their constructors) and in the wire format produced by computeRawData().
Mutable storage is the special case modeled by MutableDoubleRealVector; everything
else inheriting from this class is treated as immutable for the lifetime of the instance
(per the constructor contract).
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final double[]Fields inherited from interface com.apple.foundationdb.linear.RealVector
EPS, VECTOR_TYPES -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractRealVector(double[] data) Constructs a new RealVector with the given data. -
Method Summary
Modifier and TypeMethodDescriptionprotected intComputes a hash code based on the internaldataarray.protected doubleComputes the squared L2 norm from scratch asdot(this).protected abstract byte[]Computes the raw byte data representation of this object.booleanCompares this vector to the specified object for equality.protected static double[]fromInts(int[] ints) Widens anint[]to a freshly allocateddouble[]suitable for handing tothe array-by-reference constructor.protected static double[]fromLongs(long[] longs) Widens along[]to a freshly allocateddouble[]suitable for handing tothe array-by-reference constructor.doublegetComponent(int dimension) Gets the component of this object at the specified dimension.double[]getData()Returns the underlyingdouble[]data array.intReturns the number of elements in the vector.byte[]Gets the raw byte data representation of this object.inthashCode()Returns a hash code value for this object.doubleReturns the squared L2 normΣ_i this[i]^2.toString()Returns a string representation of the object.toString(int limitDimensions) Generates a string representation of the data array, with an option to limit the number of dimensions shown.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface com.apple.foundationdb.linear.RealVector
add, add, dot, isNearlyZeroNorm, l2Norm, l2SquaredDistance, multiply, normalize, subtract, subtract, toDoubleRealVector, toFloatRealVector, toHalfRealVector, toImmutable, toMutable, withData
-
Field Details
-
data
@Nonnull protected final double[] data -
hashCodeSupplier
-
-
Constructor Details
-
AbstractRealVector
protected AbstractRealVector(@Nonnull double[] data) Constructs a new RealVector with the given data.This constructor uses the provided array directly as the backing store for the vector. It does not create a defensive copy. Therefore, any subsequent modifications to the input array will be reflected in this vector's state. The contract of this constructor is that callers do not modify
dataafter calling the constructor. We do not want to copy the array here for performance reasons.- Parameters:
data- the components of this vector- Throws:
NullPointerException- if the provideddataarray is null.
-
-
Method Details
-
getNumDimensions
public int getNumDimensions()Returns the number of elements in the vector.- Specified by:
getNumDimensionsin interfaceRealVector- Returns:
- the number of elements
-
getComponent
public 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.
- Specified by:
getComponentin interfaceRealVector- Parameters:
dimension- the zero-based index of the component to retrieve.- Returns:
- the component at the specified dimension as a
double. - Throws:
IndexOutOfBoundsException- if thedimensionis negative or greater than or equal to the number of dimensions of this object.
-
getData
@Nonnull public double[] getData()Returns the underlyingdouble[]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 — callers must not mutate it unless the concrete subtype is
MutableDoubleRealVector.- Specified by:
getDatain interfaceRealVector- Returns:
- the data array, never
null.
-
getRawData
@Nonnull public 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.
- Specified by:
getRawDatain interfaceRealVector- Returns:
- a non-null byte array containing the raw data.
-
computeRawData
@Nonnull protected abstract byte[] computeRawData()Computes 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.
-
equals
Compares this vector to the specified object for equality.The result is
trueif and only if the argument is notnulland is aRealVectorobject that has the same data elements as this object. This method performs a deep equality check on the underlying data elements usingObjects.deepEquals(Object, Object). -
hashCode
public int hashCode()Returns a hash code value for this object. The hash code is computed once and memoized. -
computeHashCode
protected int computeHashCode()Computes a hash code based on the internaldataarray.- Returns:
- the computed hash code for this object.
-
toString
Returns a string representation of the object.This method provides a default string representation by calling
toString(int)with a predefined indentation level of 3. -
toString
Generates a string representation of the data array, with an option to limit the number of dimensions shown.If the specified
limitDimensionsis less than the actual number of dimensions in the data array, the resulting string will be a truncated view, ending with", ..."to indicate that more elements exist. Otherwise, the method returns a complete string representation of the entire array.- Parameters:
limitDimensions- The maximum number of array elements to include in the string. A non-positive value will cause anVerifyException.- Returns:
- A string representation of the data array, potentially truncated.
- Throws:
com.google.common.base.VerifyException- iflimitDimensionsis not positive
-
l2SquaredNorm
public double l2SquaredNorm()Returns the squared L2 normΣ_i this[i]^2. Implementations typically memoize this since the value is reused byRealVector.l2Norm()and several distance helpers.Memoized via the
l2SquaredNormSupplierso repeated calls — including the ones drivingRealVector.l2Norm()— share a single computation.- Specified by:
l2SquaredNormin interfaceRealVector- Returns:
- the squared L2 norm of this vector
-
computeL2SquaredNorm
protected double computeL2SquaredNorm()Computes the squared L2 norm from scratch asdot(this). Backs the memoizing supplier behindl2SquaredNorm(); subclasses normally don't need to call it directly.- Returns:
- the squared L2 norm of this vector
-
fromInts
@Nonnull protected static double[] fromInts(@Nonnull int[] ints) Widens anint[]to a freshly allocateddouble[]suitable for handing tothe array-by-reference constructor. Used by subclass int-constructors so the call site does not need to manage the widening copy.- Parameters:
ints- the source components- Returns:
- a new
double[]of the same length, each element widened from the correspondingints[i]
-
fromLongs
@Nonnull protected static double[] fromLongs(@Nonnull long[] longs) Widens along[]to a freshly allocateddouble[]suitable for handing tothe array-by-reference constructor. Used by subclass long-constructors so the call site does not need to manage the widening copy. Note thatlongvalues outside the 53-bit mantissa ofdoublemay lose precision in the widening.- Parameters:
longs- the source components- Returns:
- a new
double[]of the same length, each element widened from the correspondinglongs[i]
-