Package com.apple.foundationdb.linear
Class FloatRealVector
java.lang.Object
com.apple.foundationdb.linear.AbstractRealVector
com.apple.foundationdb.linear.FloatRealVector
- All Implemented Interfaces:
RealVector
Immutable vector storing 32-bit single-precision (float) 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 to float precision so
the storage cost and round-trip behaviour match a true float vector. Conversion to
HalfRealVector is memoized; conversion to DoubleRealVector is cheap and not
memoized (it simply wraps the existing array).-
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
ConstructorsConstructorDescriptionFloatRealVector(double[] data) Constructs a single-precision vector from a primitivedouble[].FloatRealVector(float[] floatData) Constructs a single-precision vector from a primitivefloat[].FloatRealVector(int[] intData) Constructs a single-precision vector by widening each int component.FloatRealVector(long[] longData) Constructs a single-precision vector by widening each long component.FloatRealVector(Float[] floatData) Constructs a single-precision vector from a boxedFloat[]. -
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.Builds a fresh half-precision view of this vector by truncating each component into aHalfRealVector.protected byte[]Serializes this single-precision vector into a leading type byte followed by big-endian 32-bit IEEE-754 floats, one per component.static FloatRealVectorfromBytes(byte[] vectorBytes) Creates aFloatRealVectorfrom 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.Returnsthis— already a single-precision vector, so no conversion is needed.Returns the memoized half-precision projection of this vector.Returnsthis— instances of this class are already immutable.withData(double[] data) Returns a freshFloatRealVectorcarryingdata(after truncation to float precision inside the constructor).static FloatRealVectorzeroVector(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
-
FloatRealVector
Constructs a single-precision vector from a boxedFloat[]. The input is unboxed and widened into a freshdouble[]backing store; subsequent changes tofloatDatado not affect the returned vector.- Parameters:
floatData- the components of the new vector
-
FloatRealVector
public FloatRealVector(@Nonnull float[] floatData) Constructs a single-precision vector from a primitivefloat[]. The input is widened into a freshdouble[]backing store; subsequent changes tofloatDatado not affect the returned vector.- Parameters:
floatData- the components of the new vector
-
FloatRealVector
public FloatRealVector(@Nonnull double[] data) Constructs a single-precision vector from a primitivedouble[]. The input is truncated component-wise tofloatprecision (and re-widened todouble) so round-trip behaviour matches a native float vector. The originaldataarray is read but not retained.- Parameters:
data- the components of the new vector, indoubleprecision
-
FloatRealVector
public FloatRealVector(@Nonnull int[] intData) Constructs a single-precision vector by widening each int component. The new vector owns its backing array — the input is read but not retained.- Parameters:
intData- the components of the new vector
-
FloatRealVector
public FloatRealVector(@Nonnull long[] longData) Constructs a single-precision vector by widening each long component. The new vector owns its backing array — the input is read but not retained.longvalues outside the 24-bit float mantissa lose precision; values outside the 53-bit double mantissa lose precision in the intermediate widening as well.- 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().- Returns:
- a new
HalfRealVectorwith this vector's components
-
toFloatRealVector
Returnsthis— already a single-precision vector, so no conversion is needed.- 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 float-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 freshFloatRealVectorcarryingdata(after truncation to float precision inside the constructor).- Parameters:
data- the components of the new vector- Returns:
- a fresh immutable single-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 single-precision vector into a leading type byte followed by big-endian 32-bit IEEE-754 floats, one per component. The resulting byte array has length1 + 4 * 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
FloatRealVector.- 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
FloatRealVector.- 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
FloatRealVector.- 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
FloatRealVector.- 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
FloatRealVector.- 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
FloatRealVector.- 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
-
fromBytes
Creates aFloatRealVectorfrom a byte array produced byAbstractRealVector.getRawData().The input is interpreted as a leading type byte (which must match
VectorType.SINGLE) followed by a sequence of big-endian 32-bit IEEE-754 floats, one per component.- Parameters:
vectorBytes- the non-null byte array to convert- Returns:
- a new
FloatRealVectorinstance created from the byte array
-