amari_holographic/lib.rs
1//! Holographic Reduced Representations and Vector Symbolic Architectures
2//!
3//! This crate provides implementations of various binding algebras for
4//! holographic memory and vector symbolic architectures (VSA).
5//!
6//! # Overview
7//!
8//! Holographic reduced representations (HRR) and vector symbolic architectures
9//! enable storing and retrieving associations in high-dimensional distributed
10//! representations through algebraic operations. These systems use vectors with
11//! thousands of dimensions to encode symbolic information in a distributed manner.
12//!
13//! ## Core Operations
14//!
15//! | Operation | Symbol | Purpose | Property |
16//! |-----------|--------|---------|----------|
17//! | **Binding** | `⊛` | Create associations | Result dissimilar to inputs |
18//! | **Bundling** | `⊕` | Superposition | Result similar to all inputs |
19//! | **Unbinding** | `⊛⁻¹` | Retrieve associations | Inverse of binding |
20//! | **Similarity** | `sim(a,b)` | Compare representations | Cosine similarity |
21//!
22//! ## How HRR Works
23//!
24//! HRR stores key-value associations in superposition:
25//!
26//! ```text
27//! memory = (key₁ ⊛ value₁) ⊕ (key₂ ⊛ value₂) ⊕ ... ⊕ (keyₙ ⊛ valueₙ)
28//! ```
29//!
30//! Retrieval uses unbinding with a query key:
31//!
32//! ```text
33//! retrieved_value ≈ key⁻¹ ⊛ memory
34//! ```
35//!
36//! The retrieved value is a noisy approximation of the original value, with
37//! signal-to-noise ratio decreasing as more items are stored.
38//!
39//! # Supported Algebras
40//!
41//! The [`algebra`] module provides multiple algebra implementations optimized
42//! for different use cases:
43//!
44//! | Algebra | Dimension | Compute | Use Case |
45//! |---------|-----------|---------|----------|
46//! | [`ProductCliffordAlgebra`] | 8K | O(64K) | **Recommended**: High-capacity with linear scaling |
47//! | [`Cl3`] | 8 | O(64) | Optimized building block for ProductClifford |
48//! | [`CliffordAlgebra`] | 2^n | O(4^n) | General Clifford algebras Cl(p,q,r) |
49//! | [`FHRRAlgebra`] | D | O(D) | Frequency domain, simple inverse |
50//! | [`MAPAlgebra`] | D | O(D) | Bipolar, self-inverse, hardware-friendly |
51//!
52//! ## ProductCliffordAlgebra (Recommended)
53//!
54//! The product Clifford algebra uses K copies of Cl(3,0,0) operating independently,
55//! providing O(64K) compute complexity for dimension 8K. This gives linear scaling
56//! instead of the exponential scaling of general Clifford algebras.
57//!
58//! ```ignore
59//! use amari_holographic::{ProductCliffordAlgebra, BindingAlgebra};
60//!
61//! type ProductCl3x32 = ProductCliffordAlgebra<32>; // 256-dimensional
62//!
63//! let key = ProductCl3x32::random_versor(2); // Product of 2 random vectors
64//! let value = ProductCl3x32::random_versor(2);
65//! let bound = key.bind(&value); // Geometric product
66//!
67//! // Retrieve using unbinding
68//! let retrieved = key.unbind(&bound).unwrap();
69//! assert!(retrieved.similarity(&value) > 0.9);
70//! ```
71//!
72//! ## FHRRAlgebra
73//!
74//! Fourier Holographic Reduced Representation uses frequency-domain operations.
75//! Binding is element-wise complex multiplication, making it very efficient.
76//!
77//! ```ignore
78//! use amari_holographic::{FHRRAlgebra, BindingAlgebra};
79//!
80//! type FHRR256 = FHRRAlgebra<256>;
81//!
82//! let key = FHRR256::random_unitary();
83//! let value = FHRR256::random_unitary();
84//! let bound = key.bind(&value); // Element-wise complex multiply
85//! ```
86//!
87//! ## MAPAlgebra
88//!
89//! Multiply-Add-Permute algebra uses bipolar vectors (±1 values) with XOR-like
90//! binding. Every element is its own inverse, simplifying retrieval.
91//!
92//! ```ignore
93//! use amari_holographic::{MAPAlgebra, BindingAlgebra};
94//!
95//! type MAP256 = MAPAlgebra<256>;
96//!
97//! let key = MAP256::random_bipolar();
98//! let value = MAP256::random_bipolar();
99//!
100//! // Self-inverse property: key.bind(key) ≈ identity
101//! let bound = key.bind(&value);
102//! let retrieved = key.bind(&bound); // Same as unbind!
103//! ```
104//!
105//! # Holographic Memory
106//!
107//! The [`memory`] module provides [`HolographicMemory`], a key-value store
108//! that uses holographic superposition. It's generic over any [`BindingAlgebra`].
109//!
110//! ```ignore
111//! use amari_holographic::{HolographicMemory, ProductCliffordAlgebra, BindingAlgebra, AlgebraConfig};
112//!
113//! type ProductCl3x32 = ProductCliffordAlgebra<32>;
114//!
115//! // Create memory with default configuration
116//! let mut memory = HolographicMemory::<ProductCl3x32>::new(AlgebraConfig::default());
117//!
118//! // Store associations
119//! let key1 = ProductCl3x32::random_versor(2);
120//! let value1 = ProductCl3x32::random_versor(2);
121//! memory.store(&key1, &value1);
122//!
123//! let key2 = ProductCl3x32::random_versor(2);
124//! let value2 = ProductCl3x32::random_versor(2);
125//! memory.store(&key2, &value2);
126//!
127//! // Retrieve - returns value with confidence score
128//! let result = memory.retrieve(&key1);
129//! println!("Confidence: {}", result.confidence);
130//! println!("Similarity to original: {}", result.value.similarity(&value1));
131//!
132//! // Check capacity status
133//! let info = memory.capacity_info();
134//! println!("Items: {} / {}", info.item_count, info.theoretical_capacity);
135//! ```
136//!
137//! # Resonator Networks
138//!
139//! [`Resonator`] networks clean up noisy retrievals by iteratively projecting
140//! toward valid codebook items. They implement an annealed softmax dynamics
141//! that converges to the nearest codebook entry.
142//!
143//! ```ignore
144//! use amari_holographic::{Resonator, ResonatorConfig, ProductCliffordAlgebra, BindingAlgebra};
145//!
146//! type ProductCl3x32 = ProductCliffordAlgebra<32>;
147//!
148//! // Create codebook of valid states
149//! let codebook: Vec<ProductCl3x32> = (0..10)
150//! .map(|_| ProductCl3x32::random_versor(2))
151//! .collect();
152//!
153//! // Create resonator with annealing schedule
154//! let config = ResonatorConfig {
155//! max_iterations: 50,
156//! convergence_threshold: 0.999,
157//! initial_beta: 1.0, // Low temperature = soft attention
158//! final_beta: 100.0, // High temperature = hard selection
159//! };
160//! let resonator = Resonator::new(codebook, config).unwrap();
161//!
162//! // Clean up noisy retrieval
163//! let noisy = memory.retrieve(&query).raw_value;
164//! let result = resonator.cleanup(&noisy);
165//! println!("Converged: {}, Best match: {}", result.converged, result.best_match_index);
166//! ```
167//!
168//! # Capacity and Performance
169//!
170//! ## Theoretical Capacity
171//!
172//! All algebras provide theoretical capacity of O(D / ln D) where D is dimension:
173//!
174//! | Configuration | Dimension | Capacity (~items) |
175//! |---------------|-----------|-------------------|
176//! | ProductCl3x32 | 256 | ~46 |
177//! | ProductCl3x64 | 512 | ~85 |
178//! | ProductCl3x128 | 1024 | ~147 |
179//! | FHRR1024 | 1024 | ~147 |
180//! | MAP2048 | 2048 | ~280 |
181//!
182//! ## Performance Guidelines
183//!
184//! - **Stay below 50% capacity** for reliable retrieval (SNR > 3dB)
185//! - **Use versors** (products of vectors) for better invertibility
186//! - **Monitor SNR**: Confidence drops as items are added
187//! - **Use resonators** to clean up noisy retrievals
188//! - **Batch operations** when storing many items
189//!
190//! # Features
191//!
192//! ```toml
193//! [dependencies]
194//! amari-holographic = { version = "0.12", features = ["parallel"] }
195//! ```
196//!
197//! | Feature | Description |
198//! |---------|-------------|
199//! | `std` | Standard library support (default) |
200//! | `parallel` | Parallel operations via rayon |
201//! | `serialize` | Serde serialization support |
202//!
203//! # Integration
204//!
205//! ## With amari-fusion
206//!
207//! The `TropicalDualClifford` type has built-in binding operations:
208//!
209//! ```ignore
210//! use amari_fusion::TropicalDualClifford;
211//!
212//! let tdc1 = TropicalDualClifford::<f64, 8>::random_with_scale(1.0);
213//! let tdc2 = TropicalDualClifford::<f64, 8>::random_with_scale(1.0);
214//!
215//! let bound = tdc1.bind(&tdc2);
216//! let similarity = tdc1.similarity(&tdc2);
217//! ```
218//!
219//! ## With amari-gpu
220//!
221//! GPU-accelerated batch operations are available:
222//!
223//! ```ignore
224//! use amari_gpu::GpuHolographic;
225//!
226//! let gpu = GpuHolographic::new(256).await?;
227//!
228//! // Batch bind thousands of pairs in parallel
229//! let results = gpu.batch_bind(&keys_flat, &values_flat).await?;
230//! let similarities = gpu.batch_similarity(&a_flat, &b_flat).await?;
231//! ```
232//!
233//! # Example: Semantic Memory
234//!
235//! ```ignore
236//! use amari_holographic::{HolographicMemory, ProductCliffordAlgebra, BindingAlgebra, AlgebraConfig};
237//!
238//! type ProductCl3x32 = ProductCliffordAlgebra<32>;
239//!
240//! let mut memory = HolographicMemory::<ProductCl3x32>::new(AlgebraConfig::default());
241//!
242//! // Create semantic symbols
243//! let dog = ProductCl3x32::random_versor(2);
244//! let cat = ProductCl3x32::random_versor(2);
245//! let animal = ProductCl3x32::random_versor(2);
246//! let bark = ProductCl3x32::random_versor(2);
247//! let meow = ProductCl3x32::random_versor(2);
248//!
249//! // Roles
250//! let is_a = ProductCl3x32::random_versor(2);
251//! let can = ProductCl3x32::random_versor(2);
252//!
253//! // Store relationships: dog IS-A animal, dog CAN bark
254//! memory.store(&dog.bind(&is_a), &animal);
255//! memory.store(&dog.bind(&can), &bark);
256//!
257//! // Store: cat IS-A animal, cat CAN meow
258//! memory.store(&cat.bind(&is_a), &animal);
259//! memory.store(&cat.bind(&can), &meow);
260//!
261//! // Query: what can dog do?
262//! let result = memory.retrieve(&dog.bind(&can));
263//! println!("Dog can: similarity to bark = {}", result.value.similarity(&bark));
264//!
265//! // Query: what is cat?
266//! let result = memory.retrieve(&cat.bind(&is_a));
267//! println!("Cat is-a: similarity to animal = {}", result.value.similarity(&animal));
268//! ```
269
270#![cfg_attr(not(feature = "std"), no_std)]
271// Allow index loops for clarity in algebra implementations
272#![allow(clippy::needless_range_loop)]
273#![allow(clippy::manual_range_contains)]
274
275extern crate alloc;
276
277// Core algebra module
278pub mod algebra;
279
280// Holographic memory module
281pub mod memory;
282
283// GA-native optical field operations for Lee hologram encoding
284pub mod optical;
285
286// Re-export core types from algebra
287pub use algebra::{AlgebraConfig, AlgebraError, AlgebraResult, BindingAlgebra, GeometricAlgebra};
288
289// Re-export specific algebras
290pub use algebra::{Cl3, CliffordAlgebra, FHRRAlgebra, MAPAlgebra, ProductCliffordAlgebra};
291
292// Re-export memory types
293pub use memory::{
294 Bindable, CapacityInfo, CleanupResult, FactorizationResult, HolographicError,
295 HolographicMemory, HolographicResult, Resonator, ResonatorConfig, RetrievalResult,
296};