Package com.apple.foundationdb.linear
Class DoubleRealVector
java.lang.Object
com.apple.foundationdb.linear.AbstractRealVector
com.apple.foundationdb.linear.DoubleRealVector
- All Implemented Interfaces:
RealVector
- Direct Known Subclasses:
MutableDoubleRealVector
Immutable vector storing 64-bit double-precision components. Conversions to the lower-precision
HalfRealVector and FloatRealVector representations are memoized, so callers
that round-trip a vector through several precisions repeatedly only pay the conversion cost
once per direction. The mutable counterpart with the same storage layout is
MutableDoubleRealVector.-
Field Summary
Fields inherited from class com.apple.foundationdb.linear.AbstractRealVector
data, hashCodeSupplierFields inherited from interface com.apple.foundationdb.linear.RealVector
EPS, VECTOR_TYPES -
Constructor Summary
ConstructorsConstructorDescriptionDoubleRealVector(double[] data) Constructs a double-precision vector over the given primitive array.DoubleRealVector(int[] intData) Constructs a double-precision vector by widening each int component to adouble.DoubleRealVector(long[] longData) Constructs a double-precision vector by widening each long component to adouble.DoubleRealVector(Double[] doubleData) Constructs a double-precision vector from a boxedDouble[]. -
Method Summary
Modifier and TypeMethodDescriptionadd(double scalar) Returns a new vector withscalaradded to every component of this vector.add(RealVector other) Returns a new vector whose components are the element-wise sum of this vector andother.protected static double[]computeDoubleData(Double[] doubleData) Unboxes aDouble[]into a freshly allocateddouble[]suitable for handing to the array-by-reference constructor.protected FloatRealVectorBuilds a fresh single-precision view of this vector by truncating each component into aFloatRealVector.protected HalfRealVectorBuilds a fresh half-precision view of this vector by truncating each component into aHalfRealVector.protected byte[]Serializes this double-precision vector into a leading type byte followed by big-endian 64-bit IEEE-754 doubles, one per component.protected static double[]decodeDoubleBytes(byte[] vectorBytes) Decodes a serialized double-precision vector byte array into its rawdouble[]components, shared betweenfromBytes(byte[])andMutableDoubleRealVector.fromBytes(byte[]).static DoubleRealVectorfromBytes(byte[] vectorBytes) Creates aDoubleRealVectorfrom a byte array.multiply(double scalar) Returns a new vector with every component of this vector scaled byscalar.Returns a new vector pointing in the same direction as this vector but scaled to unit L2 norm.subtract(double scalar) Returns a new vector withscalarsubtracted from every component of this vector.subtract(RealVector other) Returns a new vector whose components are the element-wise difference of this vector andother.Returnsthis— already a double-precision vector, so no conversion is needed.Returns the memoized single-precision projection of this vector.Returns the memoized half-precision projection of this vector.Returnsthis— instances of this class are already immutable.withData(double[] data) Returns a freshDoubleRealVectorbacked bydata.static DoubleRealVectorzeroVector(int numDimensions) Returns a vector whose components are all zero.Methods inherited from class com.apple.foundationdb.linear.AbstractRealVector
computeHashCode, computeL2SquaredNorm, equals, fromInts, fromLongs, getComponent, getData, getNumDimensions, getRawData, hashCode, l2SquaredNorm, toString, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface com.apple.foundationdb.linear.RealVector
dot, isNearlyZeroNorm, l2Norm, l2SquaredDistance, toMutable
-
Constructor Details
-
DoubleRealVector
Constructs a double-precision vector from a boxedDouble[]. The input is defensively copied into a fresh primitive array, so subsequent changes todoubleDatado not affect the returned vector.- Parameters:
doubleData- the components of the new vector
-
DoubleRealVector
public DoubleRealVector(@Nonnull double[] data) Constructs a double-precision vector over the given primitive array. The array is stored by reference per thebase contract— callers must not mutatedataafter construction. Usedata.clone()at the call site if you need an independent backing store.- Parameters:
data- the components of the new vector; ownership transfers to this vector
-
DoubleRealVector
public DoubleRealVector(@Nonnull int[] intData) Constructs a double-precision vector by widening each int component to adouble. The new vector owns its backing array — the input is read but not retained.- Parameters:
intData- the components of the new vector
-
DoubleRealVector
public DoubleRealVector(@Nonnull long[] longData) Constructs a double-precision vector by widening each long component to adouble. The new vector owns its backing array — the input is read but not retained.longvalues outside the 53-bit double mantissa may lose precision.- Parameters:
longData- the components of the new vector
-
-
Method Details
-
toHalfRealVector
Returns the memoized half-precision projection of this vector.- Returns:
- a non-null
HalfRealVectorcontaining theHalfprecision floating-point representation of this object.
-
computeHalfRealVector
Builds a fresh half-precision view of this vector by truncating each component into aHalfRealVector. Used as the supplier behindtoHalfRealVector(); subclasses may override to plug in alternative half-precision representations.- Returns:
- a new
HalfRealVectorwith this vector's components
-
toFloatRealVector
Returns the memoized single-precision projection of this vector.- Returns:
- a non-null
FloatRealVectorcontaining the single precision floating-point representation of this object.
-
computeFloatRealVector
Builds a fresh single-precision view of this vector by truncating each component into aFloatRealVector. Used as the supplier behindtoFloatRealVector(); subclasses may override to plug in alternative single-precision representations.- Returns:
- a new
FloatRealVectorwith this vector's components
-
toDoubleRealVector
Returnsthis— already a double-precision vector, so no conversion is needed.- Returns:
- a non-null
DoubleRealVectorrepresentation of this vector.
-
withData
Returns a freshDoubleRealVectorbacked bydata. Likethe array-by-reference constructor, ownership ofdatatransfers to the returned vector.- Parameters:
data- the components of the new vector- Returns:
- a fresh immutable double-precision vector
-
toImmutable
Returnsthis— instances of this class are already immutable.- Returns:
- a non-null immutable vector with the same components as this vector
-
computeRawData
@Nonnull protected byte[] computeRawData()Serializes this double-precision vector into a leading type byte followed by big-endian 64-bit IEEE-754 doubles, one per component. The resulting byte array has length1 + 8 * getNumDimensions()and round-trips throughfromBytes(byte[]).- Specified by:
computeRawDatain classAbstractRealVector- Returns:
- a new byte array representing the serialized vector data; never
null
-
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.Narrows the return type to
DoubleRealVector.- Returns:
- a non-null unit-norm vector
-
add
Returns a new vector whose components are the element-wise sum of this vector andother. The receiver is not mutated.Narrows the return type to
DoubleRealVector.- 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]
-
add
Returns a new vector withscalaradded to every component of this vector. The receiver is not mutated.Narrows the return type to
DoubleRealVector.- 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.Narrows the return type to
DoubleRealVector.- 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]
-
subtract
Returns a new vector withscalarsubtracted from every component of this vector. The receiver is not mutated.Narrows the return type to
DoubleRealVector.- 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.Narrows the return type to
DoubleRealVector.- Parameters:
scalar- the factor to scale each component by- Returns:
- a non-null vector with
result[i] = this[i] * scalar
-
zeroVector
Returns a vector whose components are all zero.- Parameters:
numDimensions- number of dimensions- Returns:
- a vector whose components are all zero
-
computeDoubleData
Unboxes aDouble[]into a freshly allocateddouble[]suitable for handing to the array-by-reference constructor. Shared between this class andMutableDoubleRealVector's boxed-array constructor.- Parameters:
doubleData- the boxed components- Returns:
- a new primitive array of the same length and values
-
fromBytes
Creates aDoubleRealVectorfrom a byte array.This method interprets the input byte array as a sequence of 64-bit double-precision floating-point numbers. Each run of eight bytes is converted into a
doublevalue, which then becomes a component of the resulting vector.- Parameters:
vectorBytes- the non-null byte array to convert- Returns:
- a new
DoubleRealVectorinstance created from the byte array
-
decodeDoubleBytes
@Nonnull protected static double[] decodeDoubleBytes(@Nonnull byte[] vectorBytes) Decodes a serialized double-precision vector byte array into its rawdouble[]components, shared betweenfromBytes(byte[])andMutableDoubleRealVector.fromBytes(byte[]). The first byte must be theVectorType.DOUBLEordinal; the remainder is read as big-endian 64-bit doubles.- Parameters:
vectorBytes- the non-null byte array to decode- Returns:
- a freshly allocated
double[]with the decoded components
-