use std::f64;
pub fn hidden_dim_count(epsilon: f64) -> usize {
if epsilon <= 0.0 {
return usize::MAX;
}
if epsilon >= 1.0 {
return 0;
}
(1.0 / epsilon).log2().ceil() as usize
}
pub fn precision_from_hidden_dims(k: usize) -> f64 {
2.0_f64.powi(-(k as i32))
}
pub fn holographic_accuracy(k: usize, n: usize) -> f64 {
if n == 0 {
return 0.0;
}
let base_accuracy = k as f64 / n as f64;
let correction = if n > 1 {
1.0 / (n as f64).ln()
} else {
0.0
};
(base_accuracy + correction).min(1.0)
}
pub fn lift_to_hidden(point: &[f64], k: usize) -> Vec<f64> {
let n = point.len();
let mut lifted = Vec::with_capacity(n + k);
lifted.extend_from_slice(point);
for i in 0..k {
let hidden_val = 2.0_f64.powi(-(i as i32 + 1));
lifted.push(hidden_val);
}
lifted
}
pub fn project_to_visible(lifted: &[f64], n: usize) -> Vec<f64> {
lifted.iter().take(n).copied().collect()
}
pub fn encode_with_hidden_dims(point: &[f64], epsilon: f64) -> Vec<f64> {
let n = point.len();
let k = hidden_dim_count(epsilon);
let lifted = lift_to_hidden(point, k);
let snapped = snap_to_lattice(&lifted);
project_to_visible(&snapped, n)
}
fn snap_to_lattice(point: &[f64]) -> Vec<f64> {
let norm: f64 = point.iter().map(|x| x * x).sum::<f64>().sqrt();
if norm < 1e-10 {
return point.to_vec();
}
point.iter().map(|&x| snap_to_rational(x / norm) * norm).collect()
}
fn snap_to_rational(value: f64) -> f64 {
let pythagorean_ratios: &[f64] = &[
0.0, 1.0,
3.0/5.0, 4.0/5.0, 5.0/13.0, 12.0/13.0, 8.0/17.0, 15.0/17.0, 7.0/25.0, 24.0/25.0, 20.0/29.0, 21.0/29.0, 9.0/41.0, 40.0/41.0, 12.0/37.0, 35.0/37.0, 11.0/61.0, 60.0/61.0, 28.0/53.0, 45.0/53.0, 33.0/65.0, 56.0/65.0, 16.0/65.0, 63.0/65.0, 0.5, 0.7071067811865476, ];
let mut best = value;
let mut min_dist = f64::MAX;
for &ratio in pythagorean_ratios {
let dist = (value - ratio).abs();
if dist < min_dist {
min_dist = dist;
best = ratio;
}
}
for &ratio in pythagorean_ratios {
let dist = (value - (-ratio)).abs();
if dist < min_dist {
min_dist = dist;
best = -ratio;
}
}
best
}
#[derive(Clone, Debug)]
pub struct HiddenDimensionConfig {
pub epsilon: f64,
pub hidden_dims: usize,
pub cross_plane_optimization: bool,
}
impl HiddenDimensionConfig {
pub fn new(epsilon: f64) -> Self {
Self {
epsilon,
hidden_dims: hidden_dim_count(epsilon),
cross_plane_optimization: true,
}
}
pub fn with_hidden_dims(hidden_dims: usize) -> Self {
Self {
epsilon: precision_from_hidden_dims(hidden_dims),
hidden_dims,
cross_plane_optimization: true,
}
}
pub fn encode(&self, point: &[f64]) -> Vec<f64> {
encode_with_hidden_dims(point, self.epsilon)
}
}
pub fn cross_plane_finetune(point: &[f64], planes: &[[usize; 2]]) -> Vec<f64> {
if planes.is_empty() {
return point.to_vec();
}
let mut best_point = point.to_vec();
let mut best_error = constraint_error(&best_point);
for plane in planes {
let mut snapped = point.to_vec();
if plane[0] < point.len() && plane[1] < point.len() {
let (a, b) = (point[plane[0]], point[plane[1]]);
let norm = (a * a + b * b).sqrt().max(1e-10);
snapped[plane[0]] = a / norm;
snapped[plane[1]] = b / norm;
}
let error = constraint_error(&snapped);
if error < best_error {
best_error = error;
best_point = snapped;
}
}
best_point
}
fn constraint_error(point: &[f64]) -> f64 {
let norm_sq: f64 = point.iter().map(|x| x * x).sum();
(norm_sq - 1.0).abs()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_hidden_dim_count() {
assert_eq!(hidden_dim_count(0.1), 4);
assert_eq!(hidden_dim_count(0.01), 7);
assert_eq!(hidden_dim_count(0.001), 10);
assert_eq!(hidden_dim_count(0.0001), 14);
assert_eq!(hidden_dim_count(1e-10), 34);
assert_eq!(hidden_dim_count(1e-20), 67);
}
#[test]
fn test_precision_inverse() {
for &eps in &[0.1, 0.01, 0.001, 1e-10] {
let k = hidden_dim_count(eps);
let computed_eps = precision_from_hidden_dims(k);
assert!(computed_eps <= eps, "Precision {} should be <= {}", computed_eps, eps);
}
}
#[test]
fn test_holographic_accuracy() {
let acc = holographic_accuracy(10, 10);
println!("acc(10,10) = {}", acc);
assert!((acc - 1.0).abs() < 0.05, "acc = {}", acc);
let acc = holographic_accuracy(0, 10);
println!("acc(0,10) = {}", acc);
assert!(acc < 0.5, "acc = {}", acc);
let acc = holographic_accuracy(5, 10); println!("acc(5,10) = {}", acc);
assert!(acc > 0.7 && acc < 1.0);
}
#[test]
fn test_lift_and_project() {
let point = vec![0.6, 0.8];
let lifted = lift_to_hidden(&point, 3);
assert_eq!(lifted.len(), 5);
let projected = project_to_visible(&lifted, 2);
assert_eq!(projected.len(), 2);
assert!((projected[0] - 0.6).abs() < 1e-10);
assert!((projected[1] - 0.8).abs() < 1e-10);
}
#[test]
fn test_encode_with_hidden_dims() {
let point = [0.6, 0.8];
let encoded = encode_with_hidden_dims(&point, 1e-6);
assert_eq!(encoded.len(), 2);
assert!((encoded[0] - 0.6).abs() < 0.2);
assert!((encoded[1] - 0.8).abs() < 0.2);
}
#[test]
fn test_config() {
let config = HiddenDimensionConfig::new(1e-6);
assert_eq!(config.hidden_dims, 20);
assert!((config.epsilon - 1e-6).abs() < 1e-10);
let config = HiddenDimensionConfig::with_hidden_dims(10);
assert_eq!(config.hidden_dims, 10);
}
#[test]
fn test_cross_plane_finetune() {
let point = vec![0.577, 0.816]; let planes = [[0, 1], [0, 1]]; let optimized = cross_plane_finetune(&point, &planes);
let norm: f64 = optimized.iter().map(|x| x * x).sum::<f64>().sqrt();
assert!((norm - 1.0).abs() < 0.1);
}
#[test]
fn test_snap_to_rational() {
assert!((snap_to_rational(0.6) - 0.6).abs() < 0.1);
assert!((snap_to_rational(0.8) - 0.8).abs() < 0.1);
let snapped = snap_to_rational(0.71);
assert!((snapped - 0.707).abs() < 0.1);
}
#[test]
fn test_edge_cases() {
assert_eq!(hidden_dim_count(0.0), usize::MAX);
assert_eq!(hidden_dim_count(-1.0), usize::MAX);
assert_eq!(hidden_dim_count(1.0), 0);
assert_eq!(hidden_dim_count(2.0), 0);
}
}