Class FloatRealVector

java.lang.Object
com.apple.foundationdb.linear.AbstractRealVector
com.apple.foundationdb.linear.FloatRealVector
All Implemented Interfaces:
RealVector

public class FloatRealVector extends AbstractRealVector
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).
  • Constructor Details

    • FloatRealVector

      public FloatRealVector(@Nonnull Float[] floatData)
      Constructs a single-precision vector from a boxed Float[]. The input is unboxed and widened into a fresh double[] backing store; subsequent changes to floatData do 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 primitive float[]. The input is widened into a fresh double[] backing store; subsequent changes to floatData do 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 primitive double[]. The input is truncated component-wise to float precision (and re-widened to double) so round-trip behaviour matches a native float vector. The original data array is read but not retained.
      Parameters:
      data - the components of the new vector, in double precision
    • 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. long values 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

      @Nonnull public HalfRealVector toHalfRealVector()
      Returns the memoized half-precision projection of this vector.
      Returns:
      a non-null HalfRealVector containing the Half precision floating-point representation of this object.
    • computeHalfRealVector

      @Nonnull public HalfRealVector computeHalfRealVector()
      Builds a fresh half-precision view of this vector by truncating each component into a HalfRealVector. Used as the supplier behind toHalfRealVector().
      Returns:
      a new HalfRealVector with this vector's components
    • toFloatRealVector

      @Nonnull public FloatRealVector toFloatRealVector()
      Returns this — already a single-precision vector, so no conversion is needed.
      Returns:
      a non-null FloatRealVector containing the single precision floating-point representation of this object.
    • toDoubleRealVector

      @Nonnull public DoubleRealVector toDoubleRealVector()
      Returns a fresh DoubleRealVector carrying 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 DoubleRealVector representation of this vector.
    • withData

      @Nonnull public FloatRealVector withData(@Nonnull double[] data)
      Returns a fresh FloatRealVector carrying data (after truncation to float precision inside the constructor).
      Parameters:
      data - the components of the new vector
      Returns:
      a fresh immutable single-precision vector
    • toImmutable

      @Nonnull public FloatRealVector toImmutable()
      Returns this — 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 length 1 + 4 * getNumDimensions() and round-trips through fromBytes(byte[]).
      Specified by:
      computeRawData in class AbstractRealVector
      Returns:
      a new byte array representing the serialized vector data; never null
    • normalize

      @Nonnull public FloatRealVector 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

      @Nonnull public FloatRealVector add(@Nonnull RealVector other)
      Returns a new vector whose components are the element-wise sum of this vector and other. 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

      @Nonnull public FloatRealVector add(double scalar)
      Returns a new vector with scalar added 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

      @Nonnull public FloatRealVector subtract(@Nonnull RealVector other)
      Returns a new vector whose components are the element-wise difference of this vector and other. 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

      @Nonnull public FloatRealVector subtract(double scalar)
      Returns a new vector with scalar subtracted 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

      @Nonnull public FloatRealVector multiply(double scalar)
      Returns a new vector with every component of this vector scaled by scalar. 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

      @Nonnull public static FloatRealVector zeroVector(int numDimensions)
      Returns a vector whose components are all zero.
      Parameters:
      numDimensions - number of dimensions
      Returns:
      a vector whose components are all zero
    • fromBytes

      @Nonnull public static FloatRealVector fromBytes(@Nonnull byte[] vectorBytes)
      Creates a FloatRealVector from a byte array produced by AbstractRealVector.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 FloatRealVector instance created from the byte array