Class MutableDoubleRealVector
- All Implemented Interfaces:
RealVector
DoubleRealVector with a set of explicit
in-place mutation methods suffixed *This (addToThis(RealVector),
subtractFromThis(RealVector), multiplyThisBy(double),
normalizeThis(), zeroThis(), setData(double[])). Each writes through
the receiver's existing storage and returns this, so they compose into chains without
allocating.
The non-*This arithmetic methods inherited from DoubleRealVector
(add, subtract, multiply, normalize, withData) are
deliberately not overridden here — they continue to allocate fresh
DoubleRealVector results and leave the receiver untouched. This split keeps the choice
of mutation visible at every call site: v.add(w) always produces a new value;
v.addToThis(w) always mutates v. A caller can therefore use the same
MutableDoubleRealVector reference under both idioms without surprise.
Typical use is short-lived hot loops that reuse a single storage buffer — most prominently
the centroid update in k-means, which zeros and then accumulates assigned
vectors into the same instance once per Lloyd iteration. For values that must remain stable
after construction, hand out toImmutable() (which copies into a fresh immutable
DoubleRealVector) rather than the mutable reference itself.
Implementation notes worth knowing:
- The parent class memoizes the hash code and the converted representations
(
DoubleRealVector.toHalfRealVector(),DoubleRealVector.toFloatRealVector(),AbstractRealVector.getRawData()). Mutation would invalidate those caches, so this class overrides each to recompute on every call. toMutable()returnsthis(no copy) since the receiver is already mutable. Callers who need an independent buffer must explicitly construct a new instance overgetData().clone().toDoubleRealVector()also returnsthis, typed asMutableDoubleRealVector. Code that captures the result under aDoubleRealVectorreference is still pointing at this mutable buffer; subsequent mutations will be visible through that reference.- Underlying storage is the same
double[]held byAbstractRealVector.data; thedouble[]constructor stores the array by reference, andAbstractRealVector.getData()returns the live array. Mutation methods write directly through it, so any external alias of that array sees the changes.
-
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
ConstructorsConstructorDescriptionMutableDoubleRealVector(double[] data) Constructs a mutable vector over the given primitive array.MutableDoubleRealVector(int[] intData) Constructs a mutable vector by widening each int component to adouble.MutableDoubleRealVector(long[] longData) Constructs a mutable vector by widening each long component to adouble.MutableDoubleRealVector(Double[] doubleData) Constructs a mutable vector from a boxedDouble[]. -
Method Summary
Modifier and TypeMethodDescriptionaddToThis(double scalar) Addsscalarto every component of this vector, in place.addToThis(RealVector other) Addsotherto this vector component-wise, in place.static MutableDoubleRealVectorfromBytes(byte[] vectorBytes) Creates aMutableDoubleRealVectorfrom the serialized form produced byAbstractRealVector.getRawData().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.multiplyThisBy(double scalar) Multiplies every component of this vector byscalar, in place.Normalizes this vector to unit L2 norm in place.setData(double[] data) Replaces this vector's components with those ofdata(element-wise copy through the existing backing array).subtractFromThis(double scalar) Subtractsscalarfrom every component of this vector, in place.subtractFromThis(RealVector other) Subtractsotherfrom this vector component-wise, in place.Returnsthis, typed asMutableDoubleRealVector.Returns the memoized single-precision projection of this vector.Returns the memoized half-precision projection of this vector.Returns a snapshot of the current contents as a fresh immutableDoubleRealVector.Returnsthis— the receiver is already mutable, so no copy is made.zeroThis()Sets every component of this vector to zero, in place.static MutableDoubleRealVectorzeroVector(int numDimensions) Returns a new mutable vector of the given dimensionality with all components set to zero.Methods inherited from class com.apple.foundationdb.linear.DoubleRealVector
add, add, computeDoubleData, computeFloatRealVector, computeHalfRealVector, computeRawData, decodeDoubleBytes, multiply, normalize, subtract, subtract, withDataMethods inherited from class com.apple.foundationdb.linear.AbstractRealVector
computeHashCode, computeL2SquaredNorm, equals, fromInts, fromLongs, getComponent, getData, getNumDimensions, 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
-
Constructor Details
-
MutableDoubleRealVector
Constructs a mutable 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
-
MutableDoubleRealVector
public MutableDoubleRealVector(@Nonnull double[] data) Constructs a mutable vector over the given primitive array. The array is stored by reference — the new vector reads and writes through it, and any external alias ofdatawill see every subsequent mutation. Usedata.clone()at the call site if you need an independent backing store.- Parameters:
data- the components of the new vector; ownership is shared with the caller per the aliasing contract above
-
MutableDoubleRealVector
public MutableDoubleRealVector(@Nonnull int[] intData) Constructs a mutable 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
-
MutableDoubleRealVector
public MutableDoubleRealVector(@Nonnull long[] longData) Constructs a mutable vector by widening each long component to adouble. The new vector owns its backing array — the input is read but not retained.- Parameters:
longData- the components of the new vector
-
-
Method Details
-
toHalfRealVector
Returns the memoized half-precision projection of this vector.Override note: the parent memoizes the half-precision projection because immutable vectors never change. This class's contents can change, so memoization would return stale values — every call recomputes from the current data.
- Specified by:
toHalfRealVectorin interfaceRealVector- Overrides:
toHalfRealVectorin classDoubleRealVector- Returns:
- a non-null
HalfRealVectorcontaining theHalfprecision floating-point representation of this object.
-
toFloatRealVector
Returns the memoized single-precision projection of this vector.Override note: same reasoning as
toHalfRealVector()— recomputed on every call because the underlying data may have changed since the last invocation.- Specified by:
toFloatRealVectorin interfaceRealVector- Overrides:
toFloatRealVectorin classDoubleRealVector- Returns:
- a non-null
FloatRealVectorcontaining the single precision floating-point representation of this object.
-
toDoubleRealVector
Returnsthis, typed asMutableDoubleRealVector. No copy is made; the result shares storage with the receiver, so any subsequent mutation is visible through both references.- Specified by:
toDoubleRealVectorin interfaceRealVector- Overrides:
toDoubleRealVectorin classDoubleRealVector- Returns:
- a non-null
DoubleRealVectorrepresentation of this vector.
-
toMutable
Returnsthis— the receiver is already mutable, so no copy is made. Callers that need an independent mutable buffer should explicitly construct one overgetData().clone().- Returns:
- a fresh (or in the case of
MutableDoubleRealVector, the same) mutable double-precision vector
-
toImmutable
Returns a snapshot of the current contents as a fresh immutableDoubleRealVector. The underlyingdouble[]is cloned, so subsequent mutations of the receiver do not affect the returned value.- Specified by:
toImmutablein interfaceRealVector- Overrides:
toImmutablein classDoubleRealVector- Returns:
- a non-null immutable vector with the same components as this vector
-
setData
Replaces this vector's components with those ofdata(element-wise copy through the existing backing array). The receiver's storage is reused — no allocation — anddatais read but not retained.- Parameters:
data- the new component values; must have the same length as this vector- Returns:
thisfor chaining- Throws:
IllegalArgumentException- ifdata.lengthdoes not match this vector's dimensionality
-
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.
Override note: the parent's memoized raw-bytes representation would be stale once any mutation happens; this override recomputes on every call.
- Specified by:
getRawDatain interfaceRealVector- Overrides:
getRawDatain classAbstractRealVector- Returns:
- a non-null byte array containing the raw data.
-
hashCode
public int hashCode()Returns a hash code value for this object. The hash code is computed once and memoized.Override note: hash codes are intentionally not memoized for mutable vectors — mutation changes equality and therefore the hash. Treat mutable instances as unsuitable keys for hash-based collections while they might still be mutated; snapshot via
toImmutable()before keying.- Overrides:
hashCodein classAbstractRealVector- Returns:
- a hash code value for this object.
-
l2SquaredNorm
public double l2SquaredNorm()Description copied from class:AbstractRealVectorReturns 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- Overrides:
l2SquaredNormin classAbstractRealVector- Returns:
- the squared L2 norm of this vector
-
normalizeThis
Normalizes this vector to unit L2 norm in place.- Returns:
thisfor chaining- Throws:
IllegalArgumentException- if this vector's L2 norm is zero, infinite, or NaN
-
addToThis
Addsotherto this vector component-wise, in place.- Parameters:
other- the vector to add; must have the same dimensionality as this vector- Returns:
thisfor chaining
-
addToThis
Addsscalarto every component of this vector, in place.- Parameters:
scalar- the value to add to each component- Returns:
thisfor chaining
-
subtractFromThis
Subtractsotherfrom this vector component-wise, in place.- Parameters:
other- the vector to subtract; must have the same dimensionality as this vector- Returns:
thisfor chaining
-
subtractFromThis
Subtractsscalarfrom every component of this vector, in place.- Parameters:
scalar- the value to subtract from each component- Returns:
thisfor chaining
-
multiplyThisBy
Multiplies every component of this vector byscalar, in place.- Parameters:
scalar- the factor to scale each component by- Returns:
thisfor chaining
-
zeroThis
Sets every component of this vector to zero, in place. The dimensionality is unchanged.- Returns:
thisfor chaining; the return is annotatedCanIgnoreReturnValuebecause callers commonly reset state without consuming the result
-
zeroVector
Returns a new mutable vector of the given dimensionality with all components set to zero.- Parameters:
numDimensions- number of dimensions; must be non-negative- Returns:
- a freshly allocated zero vector
-
fromBytes
Creates aMutableDoubleRealVectorfrom the serialized form produced byAbstractRealVector.getRawData().The input is interpreted as a leading type byte followed by a sequence of 64-bit big-endian IEEE-754 doubles; each 8-byte run becomes one component of the resulting vector. The returned instance owns a fresh backing array; the byte array is read but not retained.
- Parameters:
vectorBytes- the serialized form- Returns:
- a new mutable vector with the decoded components
-