Hypervector

Struct Hypervector 

Source
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

Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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

Computes Hamming distance (number of differing bits)

§Performance

<50ns with SIMD popcount instruction and loop unrolling

§Example
use ruvector_nervous_system::hdc::Hypervector;

let a = Hypervector::random();
assert_eq!(a.hamming_distance(&a), 0);
Source

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);
Source

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);
Source

pub fn bundle_3(a: &Self, b: &Self, c: &Self) -> Self

Fast bundle for exactly 3 vectors using bitwise majority

§Performance

Single-pass bitwise operation: ~500ns for 10,000 bits

Source

pub fn bits(&self) -> &[u64; 157]

Returns the internal bit array (for advanced use cases)

Trait Implementations§

Source§

impl Clone for Hypervector

Source§

fn clone(&self) -> Hypervector

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Hypervector

Source§

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

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

impl Default for Hypervector

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for Hypervector

Source§

fn eq(&self, other: &Hypervector) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Hypervector

Source§

impl StructuralPartialEq for Hypervector

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
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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V