pub struct Hypervector { /* private fields */ }Expand description
A binary hypervector with 10,000 bits packed into 156 u64 words
§Performance
- Memory: 156 * 8 = 1,248 bytes per vector
- XOR binding: <50ns (single CPU cycle per u64)
- Similarity: <100ns (SIMD popcount)
§Example
use ruvector_nervous_system::hdc::Hypervector;
let v1 = Hypervector::random();
let v2 = Hypervector::random();
let bound = v1.bind(&v2);
let sim = v1.similarity(&v2);Implementations§
Source§impl Hypervector
impl Hypervector
Sourcepub fn zero() -> Self
pub fn zero() -> Self
Creates a new hypervector with all bits set to zero
§Example
use ruvector_nervous_system::hdc::Hypervector;
let zero = Hypervector::zero();
assert_eq!(zero.popcount(), 0);Sourcepub fn random() -> Self
pub fn random() -> Self
Creates a random hypervector with ~50% bits set
Uses thread-local RNG for performance.
§Example
use ruvector_nervous_system::hdc::Hypervector;
let random = Hypervector::random();
let count = random.popcount();
// Should be around 5000 ± 150
assert!(count > 4500 && count < 5500);Sourcepub fn from_seed(seed: u64) -> Self
pub fn from_seed(seed: u64) -> Self
Creates a hypervector from a seed for reproducibility
§Example
use ruvector_nervous_system::hdc::Hypervector;
let v1 = Hypervector::from_seed(42);
let v2 = Hypervector::from_seed(42);
assert_eq!(v1, v2);Sourcepub fn bind(&self, other: &Self) -> Self
pub fn bind(&self, other: &Self) -> Self
Binds two hypervectors using XOR
Binding is associative, commutative, and self-inverse:
a.bind(b) == b.bind(a)a.bind(b).bind(b) == a
§Performance
<50ns on modern CPUs (single cycle XOR per u64)
§Example
use ruvector_nervous_system::hdc::Hypervector;
let a = Hypervector::random();
let b = Hypervector::random();
let bound = a.bind(&b);
// Self-inverse property
assert_eq!(bound.bind(&b), a);Sourcepub fn similarity(&self, other: &Self) -> f32
pub fn similarity(&self, other: &Self) -> f32
Computes similarity between two hypervectors
Returns a value in [0.0, 1.0] where:
- 1.0 = identical vectors
- 0.5 = random/orthogonal vectors
- 0.0 = completely opposite vectors
§Performance
<100ns with SIMD popcount
§Example
use ruvector_nervous_system::hdc::Hypervector;
let a = Hypervector::random();
let b = a.clone();
assert!((a.similarity(&b) - 1.0).abs() < 0.001);Sourcepub fn hamming_distance(&self, other: &Self) -> u32
pub fn hamming_distance(&self, other: &Self) -> u32
Sourcepub fn popcount(&self) -> u32
pub fn popcount(&self) -> u32
Counts the number of set bits (population count)
§Example
use ruvector_nervous_system::hdc::Hypervector;
let zero = Hypervector::zero();
assert_eq!(zero.popcount(), 0);
let random = Hypervector::random();
let count = random.popcount();
// Should be around 5000 for random vectors
assert!(count > 4500 && count < 5500);Sourcepub fn bundle(vectors: &[Self]) -> Result<Self, HdcError>
pub fn bundle(vectors: &[Self]) -> Result<Self, HdcError>
Bundles multiple vectors by majority voting on each bit
§Performance
Optimized word-level implementation: O(n * 157 words) instead of O(n * 10000 bits)
§Example
use ruvector_nervous_system::hdc::Hypervector;
let v1 = Hypervector::random();
let v2 = Hypervector::random();
let v3 = Hypervector::random();
let bundled = Hypervector::bundle(&[v1.clone(), v2, v3]).unwrap();
// Bundled vector is similar to all inputs
assert!(bundled.similarity(&v1) > 0.3);Trait Implementations§
Source§impl Clone for Hypervector
impl Clone for Hypervector
Source§fn clone(&self) -> Hypervector
fn clone(&self) -> Hypervector
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for Hypervector
impl Debug for Hypervector
Source§impl Default for Hypervector
impl Default for Hypervector
Source§impl PartialEq for Hypervector
impl PartialEq for Hypervector
impl Eq for Hypervector
impl StructuralPartialEq for Hypervector
Auto Trait Implementations§
impl Freeze for Hypervector
impl RefUnwindSafe for Hypervector
impl Send for Hypervector
impl Sync for Hypervector
impl Unpin for Hypervector
impl UnwindSafe for Hypervector
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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