File size: 9,427 Bytes
224e773
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//! Spin Factor Algebra
//!
//! A spin factor V_n is the Jordan algebra R + R^n with product:
//!   (alpha, v) . (beta, w) = (alpha*beta + <v,w>, alpha*w + beta*v)
//!
//! This product is commutative but NOT associative.

use hyperkitty_core::float_eq;

/// Spin factor element: x = (alpha, v) where alpha in R, v in R^n
#[derive(Debug, Clone, PartialEq)]
pub struct SpinFactor {
    pub alpha: f64,
    pub v: Vec<f64>,
}

impl SpinFactor {
    /// Create a new spin factor element.
    pub fn new(alpha: f64, v: Vec<f64>) -> Self {
        Self { alpha, v }
    }

    /// Dimension of the vector part.
    pub fn dim(&self) -> usize {
        self.v.len()
    }

    /// The identity element: (1, 0)
    pub fn identity(n: usize) -> Self {
        Self {
            alpha: 1.0,
            v: vec![0.0; n],
        }
    }

    /// The zero element: (0, 0)
    pub fn zero(n: usize) -> Self {
        Self {
            alpha: 0.0,
            v: vec![0.0; n],
        }
    }

    /// Norm of the vector part: |v|
    pub fn v_norm(&self) -> f64 {
        self.v.iter().map(|x| x * x).sum::<f64>().sqrt()
    }

    /// Unit vector in the direction of v (or zero if v = 0)
    pub fn v_hat(&self) -> Vec<f64> {
        let norm = self.v_norm();
        if norm < 1e-15 {
            vec![0.0; self.v.len()]
        } else {
            self.v.iter().map(|x| x / norm).collect()
        }
    }

    /// Jordan product: (alpha, v) . (beta, w) = (alpha*beta + <v,w>, alpha*w + beta*v)
    ///
    /// This product is:
    /// - Commutative: x . y = y . x
    /// - Power-associative: x^m . x^n = x^(m+n)
    /// - NOT associative in general: (x . y) . z != x . (y . z)
    pub fn product(&self, other: &Self) -> Self {
        assert_eq!(self.v.len(), other.v.len(), "Dimension mismatch in Jordan product");

        // Scalar part: alpha*beta + <v, w>
        let dot: f64 = self.v.iter().zip(&other.v).map(|(a, b)| a * b).sum();
        let alpha = self.alpha * other.alpha + dot;

        // Vector part: alpha*w + beta*v
        let v: Vec<f64> = self
            .v
            .iter()
            .zip(&other.v)
            .map(|(&vi, &wi)| self.alpha * wi + other.alpha * vi)
            .collect();

        Self { alpha, v }
    }

    /// Scalar multiplication: c * (alpha, v) = (c*alpha, c*v)
    pub fn scale(&self, c: f64) -> Self {
        Self {
            alpha: c * self.alpha,
            v: self.v.iter().map(|x| c * x).collect(),
        }
    }

    /// Addition: (alpha, v) + (beta, w) = (alpha+beta, v+w)
    pub fn add(&self, other: &Self) -> Self {
        assert_eq!(self.v.len(), other.v.len(), "Dimension mismatch in addition");
        Self {
            alpha: self.alpha + other.alpha,
            v: self.v.iter().zip(&other.v).map(|(a, b)| a + b).collect(),
        }
    }

    /// Subtraction
    pub fn sub(&self, other: &Self) -> Self {
        assert_eq!(self.v.len(), other.v.len(), "Dimension mismatch in subtraction");
        Self {
            alpha: self.alpha - other.alpha,
            v: self.v.iter().zip(&other.v).map(|(a, b)| a - b).collect(),
        }
    }

    /// Check commutativity: x . y == y . x
    pub fn is_commutative_with(&self, other: &Self) -> bool {
        let xy = self.product(other);
        let yx = other.product(self);
        float_eq(xy.alpha, yx.alpha, 1e-10)
            && xy
                .v
                .iter()
                .zip(&yx.v)
                .all(|(a, b)| float_eq(*a, *b, 1e-10))
    }

    /// Check whether (x . y) . z == x . (y . z) — associativity test
    pub fn is_associative_triple(&self, y: &Self, z: &Self) -> bool {
        let xy = self.product(y);
        let left = xy.product(z); // (x . y) . z

        let yz = y.product(z);
        let right = self.product(&yz); // x . (y . z)

        float_eq(left.alpha, right.alpha, 1e-10)
            && left
                .v
                .iter()
                .zip(&right.v)
                .all(|(a, b)| float_eq(*a, *b, 1e-10))
    }

    /// Check if this element is idempotent: e . e = e
    pub fn is_idempotent(&self) -> bool {
        let ee = self.product(self);
        float_eq(ee.alpha, self.alpha, 1e-10)
            && ee
                .v
                .iter()
                .zip(&self.v)
                .all(|(a, b)| float_eq(*a, *b, 1e-10))
    }

    /// Approximate equality check
    pub fn approx_eq(&self, other: &Self, epsilon: f64) -> bool {
        float_eq(self.alpha, other.alpha, epsilon)
            && self.v.len() == other.v.len()
            && self
                .v
                .iter()
                .zip(&other.v)
                .all(|(a, b)| float_eq(*a, *b, epsilon))
    }
}

/// Demonstrate non-associativity of spin factors.
///
/// Returns (left, right) where left = (x . y) . z, right = x . (y . z)
/// and left != right, proving the Jordan product is non-associative.
pub fn demonstrate_non_associativity() -> (SpinFactor, SpinFactor) {
    // Choose elements that will NOT associate:
    // x = (1, [1, 0]), y = (0, [0, 1]), z = (1, [1, 1])
    let x = SpinFactor::new(1.0, vec![1.0, 0.0]);
    let y = SpinFactor::new(0.0, vec![0.0, 1.0]);
    let z = SpinFactor::new(1.0, vec![1.0, 1.0]);

    // (x . y) . z
    // x . y = (1*0 + 1*0+0*1, 1*[0,1] + 0*[1,0]) = (0, [0, 1])
    // (x . y) . z = (0*1 + 0*1+1*1, 0*[1,1] + 1*[0,1]) = (1, [0, 1])
    let xy = x.product(&y);
    let left = xy.product(&z);

    // x . (y . z)
    // y . z = (0*1 + 0*1+1*1, 0*[1,1] + 1*[0,1]) = (1, [0, 1])
    // x . (y . z) = (1*1 + 1*0+0*1, 1*[0,1] + 1*[1,0]) = (1, [1, 1])
    let yz = y.product(&z);
    let right = x.product(&yz);

    (left, right)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_jordan_product_basic() {
        let x = SpinFactor::new(2.0, vec![1.0, 0.0, 0.0]);
        let y = SpinFactor::new(3.0, vec![0.0, 1.0, 0.0]);

        let xy = x.product(&y);
        // alpha = 2*3 + (1*0 + 0*1 + 0*0) = 6
        assert!(float_eq(xy.alpha, 6.0, 1e-10));
        // v = 2*[0,1,0] + 3*[1,0,0] = [3, 2, 0]
        assert!(float_eq(xy.v[0], 3.0, 1e-10));
        assert!(float_eq(xy.v[1], 2.0, 1e-10));
        assert!(float_eq(xy.v[2], 0.0, 1e-10));
    }

    #[test]
    fn test_commutativity() {
        let x = SpinFactor::new(1.5, vec![2.0, -1.0, 3.0]);
        let y = SpinFactor::new(-0.5, vec![1.0, 4.0, -2.0]);
        assert!(x.is_commutative_with(&y));
    }

    #[test]
    fn test_commutativity_many() {
        // Test with various random-ish elements
        let elements = vec![
            SpinFactor::new(1.0, vec![1.0, 0.0]),
            SpinFactor::new(0.0, vec![0.0, 1.0]),
            SpinFactor::new(-2.5, vec![3.14, -1.7]),
            SpinFactor::new(0.618, vec![-0.5, 0.5]),
        ];

        for i in 0..elements.len() {
            for j in (i + 1)..elements.len() {
                assert!(
                    elements[i].is_commutative_with(&elements[j]),
                    "Commutativity failed for elements {} and {}",
                    i,
                    j
                );
            }
        }
    }

    #[test]
    fn test_non_associativity() {
        let (left, right) = demonstrate_non_associativity();
        // They must differ
        assert!(
            !left.approx_eq(&right, 1e-10),
            "Expected non-associativity but got equal results"
        );
    }

    #[test]
    fn test_non_associativity_detailed() {
        let x = SpinFactor::new(1.0, vec![1.0, 0.0]);
        let y = SpinFactor::new(0.0, vec![0.0, 1.0]);
        let z = SpinFactor::new(1.0, vec![1.0, 1.0]);
        assert!(!x.is_associative_triple(&y, &z));
    }

    #[test]
    fn test_identity_is_identity() {
        let e = SpinFactor::identity(3);
        let x = SpinFactor::new(2.5, vec![1.0, -1.0, 0.5]);
        let ex = e.product(&x);
        assert!(ex.approx_eq(&x, 1e-10));
    }

    #[test]
    fn test_identity_is_idempotent() {
        let e = SpinFactor::identity(4);
        assert!(e.is_idempotent());
    }

    #[test]
    fn test_zero_product() {
        let z = SpinFactor::zero(3);
        let x = SpinFactor::new(5.0, vec![1.0, 2.0, 3.0]);
        let zx = z.product(&x);
        assert!(zx.approx_eq(&SpinFactor::zero(3), 1e-10));
    }

    #[test]
    fn test_scale() {
        let x = SpinFactor::new(2.0, vec![1.0, -1.0]);
        let scaled = x.scale(3.0);
        assert!(float_eq(scaled.alpha, 6.0, 1e-10));
        assert!(float_eq(scaled.v[0], 3.0, 1e-10));
        assert!(float_eq(scaled.v[1], -3.0, 1e-10));
    }

    #[test]
    fn test_add_sub() {
        let x = SpinFactor::new(1.0, vec![2.0, 3.0]);
        let y = SpinFactor::new(4.0, vec![5.0, 6.0]);
        let sum = x.add(&y);
        assert!(float_eq(sum.alpha, 5.0, 1e-10));
        assert!(float_eq(sum.v[0], 7.0, 1e-10));
        assert!(float_eq(sum.v[1], 9.0, 1e-10));

        let diff = x.sub(&y);
        assert!(float_eq(diff.alpha, -3.0, 1e-10));
        assert!(float_eq(diff.v[0], -3.0, 1e-10));
        assert!(float_eq(diff.v[1], -3.0, 1e-10));
    }

    #[test]
    fn test_v_norm() {
        let x = SpinFactor::new(0.0, vec![3.0, 4.0]);
        assert!(float_eq(x.v_norm(), 5.0, 1e-10));
    }

    #[test]
    fn test_v_hat() {
        let x = SpinFactor::new(0.0, vec![3.0, 4.0]);
        let hat = x.v_hat();
        assert!(float_eq(hat[0], 0.6, 1e-10));
        assert!(float_eq(hat[1], 0.8, 1e-10));
    }
}