Package com.apple.foundationdb.linear
Class HalfRealVector
java.lang.Object
com.apple.foundationdb.linear.AbstractRealVector
com.apple.foundationdb.linear.HalfRealVector
- All Implemented Interfaces:
RealVector
Immutable vector storing 16-bit
Half-precision components. Internally the data is
widened to a double[] for arithmetic compatibility with the rest of the
RealVector hierarchy, but every value is first truncated through
Half.valueOf(double) so storage cost and round-trip behaviour match a true half-precision
vector. Conversions to the higher-precision FloatRealVector and DoubleRealVector
are not memoized (each call wraps the already-widened backing array in a fresh instance —
cheap enough that caching wouldn't pay off).-
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
ConstructorsConstructorDescriptionHalfRealVector(double[] data) Constructs a half-precision vector from a primitivedouble[].HalfRealVector(int[] intData) Constructs a half-precision vector by widening each int component.HalfRealVector(long[] longData) Constructs a half-precision vector by widening each long component.HalfRealVector(Half[] halfData) Constructs a half-precision vector from a boxedHalf[]. -
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 byte[]Serializes this half-precision vector into a leading type byte followed by big-endian 16-bitHalf-encoded components, one per dimension.static HalfRealVectorfromBytes(byte[] vectorBytes) Creates aHalfRealVectorfrom a byte array produced byAbstractRealVector.getRawData().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.Returns a freshDoubleRealVectorcarrying this vector's already-widened components.Returns a freshFloatRealVectorcarrying this vector's already-widened components.Returnsthis— already a half-precision vector, so no conversion is needed.Returnsthis— instances of this class are already immutable.withData(double[] data) Returns a freshHalfRealVectorcarryingdata(after truncation to half precision inside the constructor).static HalfRealVectorzeroVector(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
-
HalfRealVector
Constructs a half-precision vector from a boxedHalf[]. The input is unboxed and widened into a freshdouble[]backing store; subsequent changes tohalfDatado not affect the returned vector.- Parameters:
halfData- the components of the new vector
-
HalfRealVector
public HalfRealVector(@Nonnull double[] data) Constructs a half-precision vector from a primitivedouble[]. The input is truncated component-wise throughHalf.valueOf(double)(and re-widened todouble) so round-trip behaviour matches a native half-precision vector. The originaldataarray is read but not retained.- Parameters:
data- the components of the new vector, indoubleprecision
-
HalfRealVector
public HalfRealVector(@Nonnull int[] intData) Constructs a half-precision vector by widening each int component. The new vector owns its backing array — the input is read but not retained.intvalues outside the ~11-bit half-precision mantissa lose precision after the truncation in the constructor delegate.- Parameters:
intData- the components of the new vector
-
HalfRealVector
public HalfRealVector(@Nonnull long[] longData) Constructs a half-precision vector by widening each long component. The new vector owns its backing array — the input is read but not retained. Same precision caveats asHalfRealVector(int[]).- Parameters:
longData- the components of the new vector
-
-
Method Details
-
toHalfRealVector
Returnsthis— already a half-precision vector, so no conversion is needed.- Returns:
- a non-null
HalfRealVectorcontaining theHalfprecision floating-point representation of this object.
-
toFloatRealVector
Returns a freshFloatRealVectorcarrying this vector's already-widened components. Note that the underlying values are still half-truncated; the conversion only changes the runtime type, not the precision of the data.- Returns:
- a non-null
FloatRealVectorcontaining the single precision floating-point representation of this object.
-
toDoubleRealVector
Returns a freshDoubleRealVectorcarrying this vector's already-widened components. Note that the underlying values are still half-truncated; the conversion only changes the runtime type, not the precision of the data.- Returns:
- a non-null
DoubleRealVectorrepresentation of this vector.
-
withData
Returns a freshHalfRealVectorcarryingdata(after truncation to half precision inside the constructor).- Parameters:
data- the components of the new vector- Returns:
- a fresh immutable half-precision vector
-
computeRawData
@Nonnull protected byte[] computeRawData()Serializes this half-precision vector into a leading type byte followed by big-endian 16-bitHalf-encoded components, one per dimension. The resulting byte array has length1 + 2 * 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
HalfRealVector.- 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
HalfRealVector.- 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
HalfRealVector.- 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
HalfRealVector.- 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
HalfRealVector.- 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
HalfRealVector.- 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
-
toImmutable
Returnsthis— instances of this class are already immutable.- Returns:
- a non-null immutable vector with the same components as this vector
-
fromBytes
Creates aHalfRealVectorfrom a byte array produced byAbstractRealVector.getRawData().The input is interpreted as a leading type byte (which must match
VectorType.HALF) followed by a sequence of big-endian 16-bitHalf-encoded components, one per dimension.- Parameters:
vectorBytes- the non-null byte array to convert- Returns:
- a new
HalfRealVectorinstance created from the byte array
-