Skip to main content

EntangledHVec

Struct EntangledHVec 

Source
pub struct EntangledHVec { /* private fields */ }
Expand description

Entangled Sparse Hypervector.

Instead of a dense bitvec (~50% active), stores only sorted active indices. Sparsity rho = 1/256 means ~D/256 active bits out of D total. Indices are grouped into NUM_ENTANGLEMENTS entanglement groups, each generated from a seeded hash.

Benefits:

  • 256x less memory per vector at D=16384
  • Hamming via sorted merge (O(k) where k = active count)

Implementations§

Source§

impl EntangledHVec

Source

pub fn new_deterministic(dim: usize, seed: u64) -> Self

Create a random entangled sparse hypervector. Uses entanglement groups when dim is large enough, otherwise direct hash generation. After dedup, if birthday collisions reduced the count below active_count, deterministic backfill slots are generated until the exact count is reached.

Source

pub fn new_with_density(dim: usize, density_denom: usize, seed: u64) -> Self

Source

pub fn from_indices(indices: Vec<u32>, dim: usize) -> Self

Create directly from a set of active indices. Indices must be sorted, deduplicated, and within [0, dim).

Source

pub fn from_dense(dense: &[f32], target_dim: usize) -> Self

Create from a dense float vector via sparse ternary sketching. Selects top rho*dim indices by Achlioptas ternary projection magnitude.

Source

pub fn from_scalar( value: f64, min_val: f64, max_val: f64, target_dim: usize, ) -> Self

Encode a scalar into a sparse hypervector via permuted sliding window.

The normalized value selects a contiguous window of active_count logical slots in [0, target_dim - active_count]. Each logical slot is then scattered across the index space by multiplying with a prime constant modulo target_dim (golden-ratio permutation). This gives:

  • Nearby values share most window slots → high Jaccard
  • Distant values share few slots → low Jaccard
  • Monotonic: similarity decreases with scalar distance
Source

pub fn indices(&self) -> &[u32]

Active bit indices (sorted).

Source

pub fn to_deltas(&self) -> Vec<u32>

Convert sorted indices to deltas (gaps between consecutive indices). First element is the absolute value.

Source

pub fn from_deltas(deltas: &[u32], dim: usize) -> Self

Restore absolute indices from deltas.

Source

pub fn hamming(&self, other: &Self) -> u32

Hamming distance via sorted-merge intersection count on sparse index sets.

Source

pub fn similarity(&self, other: &Self) -> f64

Jaccard similarity: |A∩B| / |A∪B|. Proper metric for sparse index sets (ρ=1/256). The old 1−hamming/dim collapsed to ~0.992 for all sparse pairs.

Source

pub fn bind(&self, other: &Self) -> Self

Entangled bind: symmetric XOR of index sets. For sparse vectors, XOR of active sets = symmetric difference.

Source

pub fn permute(&self, shifts: usize) -> Self

Permute by shifting all indices by shifts modulo dim.

Source

pub fn hash_permute(&self, seed: u64) -> Self

Nonlinear index permutation via keyed hash.

Maps each active index through hash(idx, seed) % dim, producing a completely scrambled view. Unlike circular shift (permute), this destroys spatial correlation between indices, making containment values independent across different seeds.

Nearly density-preserving: hash collisions reduce count by ~0.1% at density 1/256.

Source

pub fn bundle<V: Borrow<Self>>(vectors: &[V]) -> Self

Bundle multiple sparse vectors with threshold at rho. Counts per-index frequency, keeps only those above threshold.

Source

pub fn bundle_bloom<V: Borrow<Self>>(vectors: &[V]) -> Self

Bloom-filter bundling: set UNION of all input vectors.

Instead of majority vote (which fails at K≥3 for sparse vectors with density 1/256), takes the union of all active indices. The bundle grows denser with more items. Use containment_similarity to query.

Validated by HyperCam (arxiv 2501.10547): Bloom filter bundling achieves 2 orders of magnitude reduction in encoding operations for sparse HDC.

Source

pub fn containment_similarity(&self, other: &Self) -> f64

Containment similarity: |A∩B| / |A|.

Measures what fraction of self’s active indices appear in other. The correct metric for Bloom-filter bundles: if A was bundled into B, containment(A, B) ≈ 1.0 regardless of how many other items are in B.

Source

pub fn corrected_containment(&self, bloom_bundle: &Self) -> f64

Density-corrected containment: (raw - density) / (1 - density).

Subtracts the expected noise floor (bundle density) and normalizes. Members: raw=1.0, corrected=1.0. Non-members: raw≈density, corrected≈0. This is the correct metric for large Bloom bundles where raw containment has a high floor due to bundle density.

Source

pub fn bind_ordered(&self, other: &Self) -> Self

Non-commutative binding: permute(self, 1) XOR other.

Captures order: bind_ordered(A, B) ≠ bind_ordered(B, A). ~2x cost of plain XOR (one permute + one XOR). Inverse: unbind B first (XOR), then inverse-permute to recover A.

Source

pub fn unbind_ordered_left(&self, other: &Self) -> Self

Inverse of bind_ordered: given C = bind_ordered(A, B), recover A. A = permute_inv(C XOR B, 1)

Source

pub fn bundle_dp<V: Borrow<Self>>(vectors: &[V], epsilon: f64) -> Self

Fixed-domain, contribution-clipped histogram followed by noisy top-k. The dimension must be public; prefer HmsCore::bundle for empty datasets.

Trait Implementations§

Source§

impl Clone for EntangledHVec

Source§

fn clone(&self) -> EntangledHVec

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for EntangledHVec

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for EntangledHVec

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for EntangledHVec

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more