hisab 1.4.0

Higher mathematics library — linear algebra, geometry, calculus, and numerical methods for Rust
Documentation
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
//! Storage-efficient symmetric and antisymmetric tensors.
//!
//! [`SymmetricTensor`] stores only the independent components of a fully
//! symmetric rank-k tensor in n dimensions, reducing the Riemann tensor's
//! 256 components to 20, for example.
//!
//! [`AntisymmetricTensor`] stores only the independent components of a fully
//! antisymmetric (alternating) tensor.

use crate::HisabError;

// ---------------------------------------------------------------------------
// SymmetricTensor
// ---------------------------------------------------------------------------

/// A fully symmetric tensor of given rank and dimension.
///
/// Stores only the independent components using the multiset coefficient
/// formula: C(n + k - 1, k) independent entries for rank k in n dimensions.
///
/// # Examples
///
/// ```
/// use hisab::tensor::SymmetricTensor;
///
/// // Symmetric 2-tensor in 4D (metric tensor) = 10 independent components
/// let mut g = SymmetricTensor::zeros(4, 2);
/// g.set(&[0, 0], 1.0).unwrap();
/// g.set(&[1, 1], -1.0).unwrap();
/// assert_eq!(g.num_independent(), 10);
/// assert!((g.get(&[0, 0]).unwrap() - 1.0).abs() < 1e-12);
/// ```
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct SymmetricTensor {
    /// Dimension of each index.
    dim: usize,
    /// Number of indices (rank).
    rank: usize,
    /// Independent components in canonical (sorted) index order.
    data: Vec<f64>,
}

impl SymmetricTensor {
    /// Create a zero-filled symmetric tensor.
    #[must_use]
    pub fn zeros(dim: usize, rank: usize) -> Self {
        let count = multiset_coeff(dim, rank);
        Self {
            dim,
            rank,
            data: vec![0.0; count],
        }
    }

    /// Number of independent components.
    #[must_use]
    #[inline]
    pub fn num_independent(&self) -> usize {
        self.data.len()
    }

    /// Dimension of each index.
    #[must_use]
    #[inline]
    pub fn dim(&self) -> usize {
        self.dim
    }

    /// Rank of the tensor.
    #[must_use]
    #[inline]
    pub fn rank(&self) -> usize {
        self.rank
    }

    /// Get element at multi-index (order doesn't matter — indices are sorted).
    ///
    /// # Errors
    ///
    /// Returns error if index count or values are wrong.
    pub fn get(&self, indices: &[usize]) -> Result<f64, HisabError> {
        let flat = self.canonical_index(indices)?;
        Ok(self.data[flat])
    }

    /// Set element at multi-index (order doesn't matter — indices are sorted).
    ///
    /// # Errors
    ///
    /// Returns error if index count or values are wrong.
    pub fn set(&mut self, indices: &[usize], value: f64) -> Result<(), HisabError> {
        let flat = self.canonical_index(indices)?;
        self.data[flat] = value;
        Ok(())
    }

    /// Raw data slice (canonical ordering).
    #[must_use]
    #[inline]
    pub fn data(&self) -> &[f64] {
        &self.data
    }

    /// Expand to a full dense tensor (all permutations filled).
    #[must_use]
    #[allow(clippy::needless_range_loop)]
    pub fn to_dense(&self) -> Vec<f64> {
        let total = self.dim.pow(self.rank as u32);
        let mut dense = vec![0.0; total];
        let mut idx = vec![0usize; self.rank];

        for flat in 0..total {
            // Convert flat to multi-index
            let mut remainder = flat;
            for i in (0..self.rank).rev() {
                idx[i] = remainder % self.dim;
                remainder /= self.dim;
            }
            // Look up using canonical (sorted) form
            if let Ok(val) = self.get(&idx) {
                dense[flat] = val;
            }
        }
        dense
    }

    /// Map a multi-index to canonical (sorted) flat index.
    fn canonical_index(&self, indices: &[usize]) -> Result<usize, HisabError> {
        if indices.len() != self.rank {
            return Err(HisabError::InvalidInput(format!(
                "expected {} indices, got {}",
                self.rank,
                indices.len()
            )));
        }
        for &i in indices {
            if i >= self.dim {
                return Err(HisabError::OutOfRange(format!(
                    "index {i} out of range for dimension {}",
                    self.dim
                )));
            }
        }

        // Sort indices for canonical form
        let mut sorted = indices.to_vec();
        sorted.sort_unstable();

        // Compute combinatorial index for sorted multiset
        // Using the combinatorial number system for multisets
        multiset_to_flat(&sorted, self.dim)
    }
}

impl std::fmt::Display for SymmetricTensor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "SymmetricTensor(dim={}, rank={}, {} independent)",
            self.dim,
            self.rank,
            self.data.len()
        )
    }
}

// ---------------------------------------------------------------------------
// AntisymmetricTensor
// ---------------------------------------------------------------------------

/// A fully antisymmetric tensor of given rank and dimension.
///
/// Stores only C(n, k) independent components. Accessing with permuted indices
/// returns the value with the appropriate sign.
///
/// # Examples
///
/// ```
/// use hisab::tensor::AntisymmetricTensor;
///
/// // Antisymmetric 2-tensor in 4D (electromagnetic field tensor) = 6 independent
/// let mut f = AntisymmetricTensor::zeros(4, 2);
/// f.set(&[0, 1], 1.0).unwrap();
/// assert!((f.get(&[1, 0]).unwrap() + 1.0).abs() < 1e-12); // antisymmetric
/// assert_eq!(f.num_independent(), 6);
/// ```
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct AntisymmetricTensor {
    dim: usize,
    rank: usize,
    data: Vec<f64>,
}

impl AntisymmetricTensor {
    /// Create a zero-filled antisymmetric tensor.
    #[must_use]
    pub fn zeros(dim: usize, rank: usize) -> Self {
        let count = binomial(dim, rank);
        Self {
            dim,
            rank,
            data: vec![0.0; count],
        }
    }

    /// Number of independent components.
    #[must_use]
    #[inline]
    pub fn num_independent(&self) -> usize {
        self.data.len()
    }

    /// Dimension of each index.
    #[must_use]
    #[inline]
    pub fn dim(&self) -> usize {
        self.dim
    }

    /// Rank of the tensor.
    #[must_use]
    #[inline]
    pub fn rank(&self) -> usize {
        self.rank
    }

    /// Get element at multi-index with sign from permutation parity.
    ///
    /// Returns 0 if any indices are repeated.
    ///
    /// # Errors
    ///
    /// Returns error if index count or values are wrong.
    pub fn get(&self, indices: &[usize]) -> Result<f64, HisabError> {
        if indices.len() != self.rank {
            return Err(HisabError::InvalidInput(format!(
                "expected {} indices, got {}",
                self.rank,
                indices.len()
            )));
        }
        for &i in indices {
            if i >= self.dim {
                return Err(HisabError::OutOfRange(format!(
                    "index {i} out of range for dimension {}",
                    self.dim
                )));
            }
        }

        // Check for repeated indices
        let mut sorted = indices.to_vec();
        let sign = sort_and_count_sign(&mut sorted);
        if sign == 0 {
            return Ok(0.0);
        }

        let flat = strictly_increasing_to_flat(&sorted, self.dim)?;
        Ok(self.data[flat] * sign as f64)
    }

    /// Set element at canonical (strictly increasing) multi-index.
    ///
    /// For antisymmetric tensors, you should only set the canonical ordering
    /// where `indices[0] < indices[1] < ...`. The sign is handled automatically
    /// on read.
    ///
    /// # Errors
    ///
    /// Returns error if index count or values are wrong.
    pub fn set(&mut self, indices: &[usize], value: f64) -> Result<(), HisabError> {
        if indices.len() != self.rank {
            return Err(HisabError::InvalidInput(format!(
                "expected {} indices, got {}",
                self.rank,
                indices.len()
            )));
        }
        for &i in indices {
            if i >= self.dim {
                return Err(HisabError::OutOfRange(format!(
                    "index {i} out of range for dimension {}",
                    self.dim
                )));
            }
        }

        let mut sorted = indices.to_vec();
        let sign = sort_and_count_sign(&mut sorted);
        if sign == 0 {
            // Repeated indices — antisymmetric component is forced to 0
            return Ok(());
        }

        let flat = strictly_increasing_to_flat(&sorted, self.dim)?;
        self.data[flat] = value * sign as f64;
        Ok(())
    }

    /// Raw data slice (canonical ordering).
    #[must_use]
    #[inline]
    pub fn data(&self) -> &[f64] {
        &self.data
    }
}

impl std::fmt::Display for AntisymmetricTensor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "AntisymmetricTensor(dim={}, rank={}, {} independent)",
            self.dim,
            self.rank,
            self.data.len()
        )
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Multiset coefficient: C(n + k - 1, k) = number of independent components
/// of a symmetric rank-k tensor in n dimensions.
#[must_use]
fn multiset_coeff(n: usize, k: usize) -> usize {
    if k == 0 {
        return 1;
    }
    binomial(n + k - 1, k)
}

/// Binomial coefficient C(n, k).
#[must_use]
fn binomial(n: usize, k: usize) -> usize {
    if k > n {
        return 0;
    }
    let k = k.min(n - k);
    let mut result = 1usize;
    for i in 0..k {
        result = result * (n - i) / (i + 1);
    }
    result
}

/// Convert a sorted multiset index to a flat position.
///
/// For a symmetric tensor, the canonical form has sorted indices.
/// We enumerate sorted tuples in lexicographic order.
fn multiset_to_flat(sorted: &[usize], dim: usize) -> Result<usize, HisabError> {
    let k = sorted.len();
    if k == 0 {
        return Ok(0);
    }

    // Enumerate using combinatorial number system for multisets
    let mut flat = 0;
    let mut prev = 0;
    for (pos, &idx) in sorted.iter().enumerate() {
        // Count how many canonical tuples come before this one at position `pos`
        for v in prev..idx {
            flat += multiset_coeff(dim - v, k - pos - 1);
        }
        // For multisets (not strictly increasing), the minimum next value can equal current
        prev = idx;
    }
    Ok(flat)
}

/// Sort indices and return the permutation sign (+1 or -1), or 0 if repeated.
fn sort_and_count_sign(indices: &mut [usize]) -> i32 {
    let n = indices.len();
    let mut sign = 1i32;
    // Bubble sort to count transpositions
    for i in 0..n {
        for j in 0..n - 1 - i {
            if indices[j] > indices[j + 1] {
                indices.swap(j, j + 1);
                sign = -sign;
            } else if indices[j] == indices[j + 1] {
                return 0;
            }
        }
    }
    sign
}

/// Convert a strictly increasing index list to flat position (for antisymmetric).
fn strictly_increasing_to_flat(sorted: &[usize], dim: usize) -> Result<usize, HisabError> {
    let k = sorted.len();
    if k == 0 {
        return Ok(0);
    }

    // Combinatorial number system: flat = Σ C(sorted[i], i+1) doesn't work
    // directly, use enumeration of k-subsets of {0..n-1}
    let mut flat = 0;
    let mut prev = 0;
    for (pos, &idx) in sorted.iter().enumerate() {
        for v in prev..idx {
            flat += binomial(dim - 1 - v, k - 1 - pos);
        }
        prev = idx + 1;
    }
    Ok(flat)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    const TOL: f64 = 1e-12;

    fn approx(a: f64, b: f64) -> bool {
        (a - b).abs() < TOL
    }

    // -- SymmetricTensor --

    #[test]
    fn symmetric_2_tensor_4d() {
        // 4D symmetric rank-2 = C(5, 2) = 10 independent components
        let g = SymmetricTensor::zeros(4, 2);
        assert_eq!(g.num_independent(), 10);
    }

    #[test]
    fn symmetric_get_set() {
        let mut t = SymmetricTensor::zeros(3, 2);
        t.set(&[0, 1], 5.0).unwrap();
        // Symmetric: t[0,1] == t[1,0]
        assert!(approx(t.get(&[0, 1]).unwrap(), 5.0));
        assert!(approx(t.get(&[1, 0]).unwrap(), 5.0));
    }

    #[test]
    fn symmetric_diagonal() {
        let mut t = SymmetricTensor::zeros(4, 2);
        t.set(&[2, 2], std::f64::consts::PI).unwrap();
        assert!(approx(t.get(&[2, 2]).unwrap(), std::f64::consts::PI));
    }

    #[test]
    fn symmetric_riemann_storage() {
        // Rank-4 symmetric tensor pair in 4D has C(7, 4) = 35 components
        // But actual Riemann has more structure. A fully symmetric rank-4 in 4D:
        // C(4 + 4 - 1, 4) = C(7, 4) = 35
        let r = SymmetricTensor::zeros(4, 4);
        assert_eq!(r.num_independent(), 35);
    }

    #[test]
    fn symmetric_to_dense() {
        let mut t = SymmetricTensor::zeros(2, 2);
        t.set(&[0, 0], 1.0).unwrap();
        t.set(&[0, 1], 2.0).unwrap();
        t.set(&[1, 1], 3.0).unwrap();
        let dense = t.to_dense();
        // Should be: [[1, 2], [2, 3]]
        assert_eq!(dense.len(), 4);
        assert!(approx(dense[0], 1.0)); // [0,0]
        assert!(approx(dense[1], 2.0)); // [0,1]
        assert!(approx(dense[2], 2.0)); // [1,0]
        assert!(approx(dense[3], 3.0)); // [1,1]
    }

    #[test]
    fn symmetric_rank0() {
        let t = SymmetricTensor::zeros(4, 0);
        assert_eq!(t.num_independent(), 1);
    }

    // -- AntisymmetricTensor --

    #[test]
    fn antisymmetric_2_tensor_4d() {
        // 4D antisymmetric rank-2 = C(4, 2) = 6 independent components
        let f = AntisymmetricTensor::zeros(4, 2);
        assert_eq!(f.num_independent(), 6);
    }

    #[test]
    fn antisymmetric_sign() {
        let mut f = AntisymmetricTensor::zeros(4, 2);
        f.set(&[0, 1], 5.0).unwrap();
        assert!(approx(f.get(&[0, 1]).unwrap(), 5.0));
        assert!(approx(f.get(&[1, 0]).unwrap(), -5.0)); // antisymmetric
    }

    #[test]
    fn antisymmetric_repeated_zero() {
        let mut f = AntisymmetricTensor::zeros(3, 2);
        f.set(&[0, 0], 999.0).unwrap(); // should be forced to 0
        assert!(approx(f.get(&[0, 0]).unwrap(), 0.0));
    }

    #[test]
    fn antisymmetric_em_tensor() {
        // Electromagnetic field tensor F_μν, 4D antisymmetric rank-2
        let mut f = AntisymmetricTensor::zeros(4, 2);
        // Set E_x = F_{01}
        f.set(&[0, 1], 1.0).unwrap();
        // F_{10} = -F_{01}
        assert!(approx(f.get(&[1, 0]).unwrap(), -1.0));
    }

    #[test]
    fn antisymmetric_rank3() {
        // 4D rank-3 antisymmetric = C(4, 3) = 4
        let t = AntisymmetricTensor::zeros(4, 3);
        assert_eq!(t.num_independent(), 4);
    }

    #[test]
    fn binomial_known() {
        assert_eq!(binomial(4, 2), 6);
        assert_eq!(binomial(10, 3), 120);
        assert_eq!(binomial(5, 0), 1);
        assert_eq!(binomial(5, 5), 1);
        assert_eq!(binomial(0, 0), 1);
    }

    #[test]
    fn multiset_coeff_known() {
        // C(n+k-1, k)
        assert_eq!(multiset_coeff(4, 2), 10); // C(5,2)
        assert_eq!(multiset_coeff(3, 3), 10); // C(5,3)
        assert_eq!(multiset_coeff(4, 0), 1);
    }

    // -- Error paths --

    #[test]
    fn symmetric_get_wrong_index_count() {
        let t = SymmetricTensor::zeros(3, 2);
        assert!(t.get(&[0, 1, 2]).is_err());
    }

    #[test]
    fn symmetric_set_wrong_index_count() {
        let mut t = SymmetricTensor::zeros(3, 2);
        assert!(t.set(&[0], 1.0).is_err());
    }

    #[test]
    fn symmetric_get_out_of_range() {
        let t = SymmetricTensor::zeros(3, 2);
        assert!(t.get(&[0, 5]).is_err());
    }

    #[test]
    fn symmetric_set_out_of_range() {
        let mut t = SymmetricTensor::zeros(3, 2);
        assert!(t.set(&[3, 0], 1.0).is_err());
    }

    #[test]
    fn antisymmetric_get_wrong_index_count() {
        let t = AntisymmetricTensor::zeros(4, 2);
        assert!(t.get(&[0]).is_err());
    }

    #[test]
    fn antisymmetric_set_wrong_index_count() {
        let mut t = AntisymmetricTensor::zeros(4, 2);
        assert!(t.set(&[0, 1, 2], 1.0).is_err());
    }

    #[test]
    fn antisymmetric_get_out_of_range() {
        let t = AntisymmetricTensor::zeros(4, 2);
        assert!(t.get(&[0, 7]).is_err());
    }

    #[test]
    fn antisymmetric_set_out_of_range() {
        let mut t = AntisymmetricTensor::zeros(4, 2);
        assert!(t.set(&[5, 0], 1.0).is_err());
    }
}