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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
//! Index-aware tensor algebra for theoretical physics.
//!
//! Provides [`IndexedTensor`] with covariant/contravariant index tracking,
//! Einstein summation convention, tensor contraction, outer product,
//! and index raising/lowering via a metric tensor.

use super::Tensor;
use crate::HisabError;
use serde::{Deserialize, Serialize};

// ---------------------------------------------------------------------------
// Index variance
// ---------------------------------------------------------------------------

/// Whether a tensor index is upper (contravariant) or lower (covariant).
///
/// In Einstein notation:
/// - **Contravariant** (upper): `Tᵘ` — transforms inversely to basis vectors
/// - **Covariant** (lower): `Tᵤ` — transforms like basis vectors
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum IndexVariance {
    /// Contravariant (upper) index.
    Contravariant,
    /// Covariant (lower) index.
    Covariant,
}

impl IndexVariance {
    /// Flip the variance: upper ↔ lower.
    #[must_use]
    #[inline]
    pub fn flip(self) -> Self {
        match self {
            Self::Contravariant => Self::Covariant,
            Self::Covariant => Self::Contravariant,
        }
    }
}

/// A labeled tensor index with a name, variance, and dimension.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct TensorIndex {
    /// Human-readable label (e.g. "μ", "ν", "α").
    pub label: String,
    /// Upper or lower index.
    pub variance: IndexVariance,
    /// Dimension of this index (e.g. 4 for spacetime).
    pub dim: usize,
}

impl TensorIndex {
    /// Create a new tensor index.
    #[must_use]
    pub fn new(label: impl Into<String>, variance: IndexVariance, dim: usize) -> Self {
        Self {
            label: label.into(),
            variance,
            dim,
        }
    }

    /// Create a contravariant (upper) index.
    #[must_use]
    pub fn upper(label: impl Into<String>, dim: usize) -> Self {
        Self::new(label, IndexVariance::Contravariant, dim)
    }

    /// Create a covariant (lower) index.
    #[must_use]
    pub fn lower(label: impl Into<String>, dim: usize) -> Self {
        Self::new(label, IndexVariance::Covariant, dim)
    }
}

// ---------------------------------------------------------------------------
// IndexedTensor
// ---------------------------------------------------------------------------

/// A tensor with named, typed indices for physics calculations.
///
/// Tracks covariant/contravariant structure and supports Einstein summation,
/// contraction, outer products, and index raising/lowering.
///
/// # Examples
///
/// ```
/// use hisab::tensor::{IndexedTensor, TensorIndex, IndexVariance};
///
/// // Create a rank-2 tensor Tᵘᵥ (one upper, one lower index) in 4D spacetime
/// let indices = vec![
///     TensorIndex::upper("μ", 4),
///     TensorIndex::lower("ν", 4),
/// ];
/// let data = vec![0.0; 16]; // 4×4
/// let t = IndexedTensor::new(indices, data).unwrap();
/// assert_eq!(t.rank(), 2);
/// ```
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct IndexedTensor {
    /// The indices with variance and dimension metadata.
    indices: Vec<TensorIndex>,
    /// Underlying dense storage.
    data: Tensor,
}

impl IndexedTensor {
    /// Create a new indexed tensor.
    ///
    /// The shape is derived from the index dimensions.
    ///
    /// # Errors
    ///
    /// Returns error if `data.len()` doesn't match the product of index dimensions.
    pub fn new(indices: Vec<TensorIndex>, data: Vec<f64>) -> Result<Self, HisabError> {
        let shape: Vec<usize> = indices.iter().map(|i| i.dim).collect();
        let tensor = Tensor::new(shape, data)?;
        Ok(Self {
            indices,
            data: tensor,
        })
    }

    /// Create a zero-filled indexed tensor.
    #[must_use]
    pub fn zeros(indices: Vec<TensorIndex>) -> Self {
        let shape: Vec<usize> = indices.iter().map(|i| i.dim).collect();
        Self {
            data: Tensor::zeros(shape),
            indices,
        }
    }

    /// Create the Kronecker delta `δᵘᵥ` (identity tensor).
    ///
    /// # Errors
    ///
    /// Returns error if `dim` is zero.
    pub fn kronecker_delta(
        upper_label: impl Into<String>,
        lower_label: impl Into<String>,
        dim: usize,
    ) -> Result<Self, HisabError> {
        if dim == 0 {
            return Err(HisabError::InvalidInput(
                "dimension must be positive".into(),
            ));
        }
        let indices = vec![
            TensorIndex::upper(upper_label, dim),
            TensorIndex::lower(lower_label, dim),
        ];
        let mut data = vec![0.0; dim * dim];
        for i in 0..dim {
            data[i * dim + i] = 1.0;
        }
        Self::new(indices, data)
    }

    /// Create the Minkowski metric `ηᵤᵥ = diag(+1, −1, −1, −1)`.
    #[must_use]
    pub fn minkowski(label_a: impl Into<String>, label_b: impl Into<String>) -> Self {
        let indices = vec![
            TensorIndex::lower(label_a, 4),
            TensorIndex::lower(label_b, 4),
        ];
        let mut data = vec![0.0; 16];
        data[0] = 1.0; // η₀₀ = +1
        data[5] = -1.0; // η₁₁ = -1
        data[10] = -1.0; // η₂₂ = -1
        data[15] = -1.0; // η₃₃ = -1
        Self {
            indices,
            data: Tensor::from_raw(vec![4, 4], data),
        }
    }

    /// Create the inverse Minkowski metric `ηᵘᵛ = diag(+1, −1, −1, −1)`.
    #[must_use]
    pub fn minkowski_inverse(label_a: impl Into<String>, label_b: impl Into<String>) -> Self {
        let indices = vec![
            TensorIndex::upper(label_a, 4),
            TensorIndex::upper(label_b, 4),
        ];
        let mut data = vec![0.0; 16];
        data[0] = 1.0;
        data[5] = -1.0;
        data[10] = -1.0;
        data[15] = -1.0;
        Self {
            indices,
            data: Tensor::from_raw(vec![4, 4], data),
        }
    }

    /// The Levi-Civita symbol `εᵘᵛᵖˢ` in n dimensions.
    ///
    /// # Errors
    ///
    /// Returns error if `dim` is zero.
    pub fn levi_civita(dim: usize, variance: IndexVariance) -> Result<Self, HisabError> {
        if dim == 0 {
            return Err(HisabError::InvalidInput(
                "dimension must be positive".into(),
            ));
        }
        let indices: Vec<TensorIndex> = (0..dim)
            .map(|i| TensorIndex::new(format!("i{i}"), variance, dim))
            .collect();
        let total: usize = dim.pow(dim as u32);
        let mut data = vec![0.0; total];
        let shape: Vec<usize> = vec![dim; dim];

        // Iterate over all index combinations
        let mut combo = vec![0usize; dim];
        'outer: loop {
            // Check if all indices are distinct
            let mut distinct = true;
            'check: for a in 0..dim {
                for b in (a + 1)..dim {
                    if combo[a] == combo[b] {
                        distinct = false;
                        break 'check;
                    }
                }
            }

            if distinct {
                // Count inversions (parity of permutation)
                let mut inversions = 0;
                for a in 0..dim {
                    for b in (a + 1)..dim {
                        if combo[a] > combo[b] {
                            inversions += 1;
                        }
                    }
                }
                let sign = if inversions % 2 == 0 { 1.0 } else { -1.0 };

                // Compute flat index
                let mut flat = 0;
                let mut stride = 1;
                for i in (0..dim).rev() {
                    flat += combo[i] * stride;
                    stride *= shape[i];
                }
                data[flat] = sign;
            }

            // Increment combo (odometer style)
            let mut carry = true;
            for i in (0..dim).rev() {
                if carry {
                    combo[i] += 1;
                    if combo[i] >= dim {
                        combo[i] = 0;
                    } else {
                        carry = false;
                    }
                }
            }
            if carry {
                break 'outer;
            }
        }

        Ok(Self {
            indices,
            data: Tensor::from_raw(shape, data),
        })
    }

    // -----------------------------------------------------------------------
    // Accessors

    /// Tensor rank (number of indices).
    #[must_use]
    #[inline]
    pub fn rank(&self) -> usize {
        self.indices.len()
    }

    /// The index metadata.
    #[must_use]
    #[inline]
    pub fn indices(&self) -> &[TensorIndex] {
        &self.indices
    }

    /// Shape derived from index dimensions.
    #[must_use]
    #[inline]
    pub fn shape(&self) -> &[usize] {
        self.data.shape()
    }

    /// Raw data in row-major order.
    #[must_use]
    #[inline]
    pub fn data(&self) -> &[f64] {
        self.data.data()
    }

    /// Mutable raw data.
    #[inline]
    pub fn data_mut(&mut self) -> &mut [f64] {
        self.data.data_mut()
    }

    /// Get element by multi-dimensional index.
    ///
    /// # Errors
    ///
    /// Returns error if indices are out of range.
    pub fn get(&self, idx: &[usize]) -> Result<f64, HisabError> {
        self.data.get(idx)
    }

    /// Set element by multi-dimensional index.
    ///
    /// # Errors
    ///
    /// Returns error if indices are out of range.
    pub fn set(&mut self, idx: &[usize], value: f64) -> Result<(), HisabError> {
        self.data.set(idx, value)
    }

    /// Reference to the underlying dense tensor.
    #[must_use]
    #[inline]
    pub fn as_tensor(&self) -> &Tensor {
        &self.data
    }

    // -----------------------------------------------------------------------
    // Arithmetic

    /// Element-wise addition (indices must match exactly).
    ///
    /// # Errors
    ///
    /// Returns error if index structure doesn't match.
    pub fn add(&self, other: &Self) -> Result<Self, HisabError> {
        if self.indices != other.indices {
            return Err(HisabError::InvalidInput(
                "index structure mismatch for addition".into(),
            ));
        }
        Ok(Self {
            indices: self.indices.clone(),
            data: self.data.add(&other.data)?,
        })
    }

    /// Element-wise subtraction (indices must match exactly).
    ///
    /// # Errors
    ///
    /// Returns error if index structure doesn't match.
    pub fn sub(&self, other: &Self) -> Result<Self, HisabError> {
        if self.indices != other.indices {
            return Err(HisabError::InvalidInput(
                "index structure mismatch for subtraction".into(),
            ));
        }
        Ok(Self {
            indices: self.indices.clone(),
            data: self.data.sub(&other.data)?,
        })
    }

    /// Scalar multiplication.
    #[must_use]
    pub fn scale(&self, scalar: f64) -> Self {
        Self {
            indices: self.indices.clone(),
            data: self.data.scale(scalar),
        }
    }

    /// Negate all elements.
    #[must_use]
    pub fn neg(&self) -> Self {
        self.scale(-1.0)
    }

    // -----------------------------------------------------------------------
    // Tensor operations

    /// Outer (tensor) product: `C^{a...b...} = A^{a...} ⊗ B^{b...}`.
    ///
    /// Result has rank = `self.rank() + other.rank()`, with indices concatenated.
    #[must_use]
    pub fn outer(&self, other: &Self) -> Self {
        let mut new_indices = self.indices.clone();
        new_indices.extend_from_slice(&other.indices);

        let self_data = self.data.data();
        let other_data = other.data.data();
        let mut data = Vec::with_capacity(self_data.len() * other_data.len());
        for &a in self_data {
            for &b in other_data {
                data.push(a * b);
            }
        }

        let shape: Vec<usize> = new_indices.iter().map(|i| i.dim).collect();
        Self {
            indices: new_indices,
            data: Tensor::from_raw(shape, data),
        }
    }

    /// Contract (trace) over two indices by position.
    ///
    /// The two indices must have the same dimension. One should be upper and
    /// one lower for physical correctness, but this is not enforced.
    ///
    /// # Errors
    ///
    /// Returns error if positions are invalid or dimensions don't match.
    #[allow(clippy::needless_range_loop)]
    pub fn contract(&self, pos_a: usize, pos_b: usize) -> Result<Self, HisabError> {
        let rank = self.rank();
        if pos_a >= rank || pos_b >= rank || pos_a == pos_b {
            return Err(HisabError::InvalidInput(format!(
                "invalid contraction positions {pos_a}, {pos_b} for rank-{rank} tensor"
            )));
        }
        let dim = self.indices[pos_a].dim;
        if self.indices[pos_b].dim != dim {
            return Err(HisabError::InvalidInput(format!(
                "dimension mismatch for contraction: {} vs {}",
                dim, self.indices[pos_b].dim
            )));
        }
        tracing::trace!(
            pos_a,
            pos_b,
            label_a = %self.indices[pos_a].label,
            label_b = %self.indices[pos_b].label,
            "contracting indices"
        );

        // Build new index list (remove pos_a and pos_b)
        let (lo, hi) = if pos_a < pos_b {
            (pos_a, pos_b)
        } else {
            (pos_b, pos_a)
        };
        let mut new_indices: Vec<TensorIndex> = Vec::with_capacity(rank - 2);
        for (i, idx) in self.indices.iter().enumerate() {
            if i != lo && i != hi {
                new_indices.push(idx.clone());
            }
        }

        let new_shape: Vec<usize> = new_indices.iter().map(|i| i.dim).collect();
        let new_total: usize = if new_shape.is_empty() {
            1
        } else {
            new_shape.iter().product()
        };
        let mut new_data = vec![0.0; new_total];

        let old_shape = self.data.shape();
        let old_rank = old_shape.len();

        // Compute strides for old tensor
        let mut strides = vec![1usize; old_rank];
        for i in (0..old_rank - 1).rev() {
            strides[i] = strides[i + 1] * old_shape[i + 1];
        }

        // Iterate over all elements of the new tensor
        let mut new_idx = vec![0usize; new_indices.len()];
        for flat_new in 0..new_total {
            // Convert flat_new to multi-index
            if !new_indices.is_empty() {
                let mut remainder = flat_new;
                for i in (0..new_indices.len()).rev() {
                    new_idx[i] = remainder % new_shape[i];
                    remainder /= new_shape[i];
                }
            }

            // Sum over the contracted index
            let mut sum = 0.0;
            for k in 0..dim {
                // Build old multi-index
                let mut old_flat = 0;
                let mut new_pos = 0;
                for i in 0..old_rank {
                    let val = if i == lo || i == hi {
                        k
                    } else {
                        let v = new_idx[new_pos];
                        new_pos += 1;
                        v
                    };
                    old_flat += val * strides[i];
                }
                sum += self.data.data()[old_flat];
            }
            new_data[flat_new] = sum;
        }

        if new_indices.is_empty() {
            // Scalar result — return rank-0 tensor stored as [1]
            Ok(Self {
                indices: new_indices,
                data: Tensor::new(vec![1], new_data)?,
            })
        } else {
            Ok(Self {
                indices: new_indices,
                data: Tensor::new(new_shape, new_data)?,
            })
        }
    }

    /// Contract with another tensor (Einstein summation on matching label pairs).
    ///
    /// Finds pairs where one tensor has an upper index and the other has a lower
    /// index with the same label. Performs the outer product then contracts all
    /// matching pairs.
    ///
    /// # Errors
    ///
    /// Returns error on dimension mismatches.
    pub fn contract_with(&self, other: &Self) -> Result<Self, HisabError> {
        // Find matching index pairs (one upper, one lower, same label)
        let mut pairs: Vec<(usize, usize)> = Vec::new();
        for (i, idx_a) in self.indices.iter().enumerate() {
            for (j, idx_b) in other.indices.iter().enumerate() {
                if idx_a.label == idx_b.label
                    && idx_a.variance != idx_b.variance
                    && idx_a.dim == idx_b.dim
                {
                    pairs.push((i, self.rank() + j));
                }
            }
        }

        tracing::trace!(matched_pairs = pairs.len(), "contract_with");

        if pairs.is_empty() {
            // No matching indices — return outer product
            return Ok(self.outer(other));
        }

        // Build outer product first
        let mut result = self.outer(other);

        // Contract pairs (adjust positions as earlier contractions reduce rank)
        // Sort pairs in reverse order of position to avoid index shifting issues
        let mut sorted_pairs = pairs;
        sorted_pairs.sort_by(|a, b| b.0.cmp(&a.0).then(b.1.cmp(&a.1)));

        for (a, b) in sorted_pairs {
            result = result.contract(a.min(b), a.max(b))?;
        }

        Ok(result)
    }

    /// Raise an index using a metric tensor.
    ///
    /// Contracts `metric^{new_label, idx_label} * self_{..., idx_label, ...}`.
    ///
    /// # Errors
    ///
    /// Returns error if the index position is invalid or metric dimensions mismatch.
    #[allow(clippy::needless_range_loop)]
    pub fn raise_index(&self, pos: usize, metric_inverse: &Self) -> Result<Self, HisabError> {
        if pos >= self.rank() {
            return Err(HisabError::InvalidInput(format!(
                "index position {pos} out of range for rank-{} tensor",
                self.rank()
            )));
        }
        if self.indices[pos].variance == IndexVariance::Contravariant {
            return Err(HisabError::InvalidInput(
                "index is already contravariant (upper)".into(),
            ));
        }
        tracing::trace!(pos, label = %self.indices[pos].label, "raising index");

        // The metric inverse should have two upper indices
        if metric_inverse.rank() != 2 {
            return Err(HisabError::InvalidInput("metric must be rank-2".into()));
        }

        let dim = self.indices[pos].dim;

        // Build result by contracting metric with tensor
        // T^...μ... = g^{μν} T_{...ν...}
        let old_shape = self.data.shape();
        let rank = self.rank();

        let mut new_indices = self.indices.clone();
        new_indices[pos].variance = IndexVariance::Contravariant;

        let new_shape: Vec<usize> = new_indices.iter().map(|i| i.dim).collect();
        let total: usize = new_shape.iter().product();
        let mut new_data = vec![0.0; total];

        // Compute strides
        let mut strides = vec![1usize; rank];
        for i in (0..rank - 1).rev() {
            strides[i] = strides[i + 1] * old_shape[i + 1];
        }

        let mut new_strides = vec![1usize; rank];
        for i in (0..rank - 1).rev() {
            new_strides[i] = new_strides[i + 1] * new_shape[i + 1];
        }

        // For each element of the result
        let mut idx = vec![0usize; rank];
        for flat in 0..total {
            // Convert flat to multi-index
            let mut remainder = flat;
            for i in (0..rank).rev() {
                idx[i] = remainder % new_shape[i];
                remainder /= new_shape[i];
            }

            let mu = idx[pos]; // The new (raised) index value

            let mut sum = 0.0;
            for nu in 0..dim {
                // g^{μν}
                let g_val = metric_inverse.data.get(&[mu, nu]).unwrap_or(0.0);
                if g_val.abs() < 1e-300 {
                    continue;
                }

                // T_{...ν...} (replace idx[pos] with nu)
                let mut old_flat = 0;
                for (i, &s) in strides.iter().enumerate() {
                    let v = if i == pos { nu } else { idx[i] };
                    old_flat += v * s;
                }
                sum += g_val * self.data.data()[old_flat];
            }
            new_data[flat] = sum;
        }

        Ok(Self {
            indices: new_indices,
            data: Tensor::new(new_shape, new_data)?,
        })
    }

    /// Lower an index using a metric tensor.
    ///
    /// # Errors
    ///
    /// Returns error if the index position is invalid or metric dimensions mismatch.
    #[allow(clippy::needless_range_loop)]
    pub fn lower_index(&self, pos: usize, metric: &Self) -> Result<Self, HisabError> {
        if pos >= self.rank() {
            return Err(HisabError::InvalidInput(format!(
                "index position {pos} out of range for rank-{} tensor",
                self.rank()
            )));
        }
        if self.indices[pos].variance == IndexVariance::Covariant {
            return Err(HisabError::InvalidInput(
                "index is already covariant (lower)".into(),
            ));
        }
        tracing::trace!(pos, label = %self.indices[pos].label, "lowering index");

        if metric.rank() != 2 {
            return Err(HisabError::InvalidInput("metric must be rank-2".into()));
        }

        let dim = self.indices[pos].dim;
        let old_shape = self.data.shape();
        let rank = self.rank();

        let mut new_indices = self.indices.clone();
        new_indices[pos].variance = IndexVariance::Covariant;

        let new_shape: Vec<usize> = new_indices.iter().map(|i| i.dim).collect();
        let total: usize = new_shape.iter().product();
        let mut new_data = vec![0.0; total];

        let mut strides = vec![1usize; rank];
        for i in (0..rank - 1).rev() {
            strides[i] = strides[i + 1] * old_shape[i + 1];
        }

        let mut new_strides = vec![1usize; rank];
        for i in (0..rank - 1).rev() {
            new_strides[i] = new_strides[i + 1] * new_shape[i + 1];
        }

        let mut idx = vec![0usize; rank];
        for flat in 0..total {
            let mut remainder = flat;
            for i in (0..rank).rev() {
                idx[i] = remainder % new_shape[i];
                remainder /= new_shape[i];
            }

            let mu = idx[pos];

            let mut sum = 0.0;
            for nu in 0..dim {
                let g_val = metric.data.get(&[mu, nu]).unwrap_or(0.0);
                if g_val.abs() < 1e-300 {
                    continue;
                }
                let mut old_flat = 0;
                for (i, &s) in strides.iter().enumerate() {
                    let v = if i == pos { nu } else { idx[i] };
                    old_flat += v * s;
                }
                sum += g_val * self.data.data()[old_flat];
            }
            new_data[flat] = sum;
        }

        Ok(Self {
            indices: new_indices,
            data: Tensor::new(new_shape, new_data)?,
        })
    }

    /// Permute indices to a new order.
    ///
    /// `perm[new_pos] = old_pos`
    ///
    /// # Errors
    ///
    /// Returns error if `perm` is not a valid permutation of `0..rank`.
    #[allow(clippy::needless_range_loop)]
    pub fn permute(&self, perm: &[usize]) -> Result<Self, HisabError> {
        let rank = self.rank();
        if perm.len() != rank {
            return Err(HisabError::InvalidInput(format!(
                "permutation length {} != rank {rank}",
                perm.len()
            )));
        }
        // Verify it's a valid permutation
        let mut seen = vec![false; rank];
        for &p in perm {
            if p >= rank || seen[p] {
                return Err(HisabError::InvalidInput("invalid permutation".into()));
            }
            seen[p] = true;
        }

        let old_shape = self.data.shape();
        let new_indices: Vec<TensorIndex> = perm.iter().map(|&p| self.indices[p].clone()).collect();
        let new_shape: Vec<usize> = new_indices.iter().map(|i| i.dim).collect();
        let total: usize = new_shape.iter().product();

        // Compute old strides
        let mut old_strides = vec![1usize; rank];
        for i in (0..rank - 1).rev() {
            old_strides[i] = old_strides[i + 1] * old_shape[i + 1];
        }

        let mut new_data = vec![0.0; total];
        let mut new_idx = vec![0usize; rank];
        for flat in 0..total {
            let mut remainder = flat;
            for i in (0..rank).rev() {
                new_idx[i] = remainder % new_shape[i];
                remainder /= new_shape[i];
            }

            // Map new indices back to old positions
            let mut old_flat = 0;
            for (new_pos, &old_pos) in perm.iter().enumerate() {
                old_flat += new_idx[new_pos] * old_strides[old_pos];
            }
            new_data[flat] = self.data.data()[old_flat];
        }

        Ok(Self {
            indices: new_indices,
            data: Tensor::new(new_shape, new_data)?,
        })
    }
}

impl std::fmt::Display for IndexedTensor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "IndexedTensor(")?;
        for (i, idx) in self.indices.iter().enumerate() {
            if i > 0 {
                write!(f, ", ")?;
            }
            let arrow = match idx.variance {
                IndexVariance::Contravariant => "",
                IndexVariance::Covariant => "",
            };
            write!(f, "{}{}{}", idx.label, arrow, idx.dim)?;
        }
        write!(f, ")")
    }
}

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

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

    const TOL: f64 = 1e-12;

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

    #[test]
    fn create_indexed_tensor() {
        let t = IndexedTensor::new(
            vec![TensorIndex::upper("μ", 4), TensorIndex::lower("ν", 4)],
            vec![0.0; 16],
        )
        .unwrap();
        assert_eq!(t.rank(), 2);
        assert_eq!(t.shape(), &[4, 4]);
    }

    #[test]
    fn kronecker_delta_trace() {
        // δᵘᵤ contracted = dimension
        let delta = IndexedTensor::kronecker_delta("μ", "ν", 4).unwrap();
        let trace = delta.contract(0, 1).unwrap();
        assert!(approx(trace.data()[0], 4.0));
    }

    #[test]
    fn minkowski_signature() {
        let eta = IndexedTensor::minkowski("μ", "ν");
        assert!(approx(eta.get(&[0, 0]).unwrap(), 1.0));
        assert!(approx(eta.get(&[1, 1]).unwrap(), -1.0));
        assert!(approx(eta.get(&[2, 2]).unwrap(), -1.0));
        assert!(approx(eta.get(&[3, 3]).unwrap(), -1.0));
        assert!(approx(eta.get(&[0, 1]).unwrap(), 0.0));
    }

    #[test]
    fn minkowski_trace() {
        let eta = IndexedTensor::minkowski("μ", "ν");
        let trace = eta.contract(0, 1).unwrap();
        // η_μμ = 1 + (-1) + (-1) + (-1) = -2
        assert!(approx(trace.data()[0], -2.0));
    }

    #[test]
    fn outer_product_shape() {
        let v =
            IndexedTensor::new(vec![TensorIndex::upper("μ", 4)], vec![1.0, 0.0, 0.0, 0.0]).unwrap();
        let w =
            IndexedTensor::new(vec![TensorIndex::lower("ν", 4)], vec![0.0, 1.0, 0.0, 0.0]).unwrap();
        let outer = v.outer(&w);
        assert_eq!(outer.rank(), 2);
        assert_eq!(outer.shape(), &[4, 4]);
        assert!(approx(outer.get(&[0, 1]).unwrap(), 1.0));
        assert!(approx(outer.get(&[0, 0]).unwrap(), 0.0));
    }

    #[test]
    fn contract_with_einstein() {
        // v^μ η_{μν} = v_ν (lowering)
        let v =
            IndexedTensor::new(vec![TensorIndex::upper("μ", 4)], vec![1.0, 2.0, 3.0, 4.0]).unwrap();
        let eta = IndexedTensor::minkowski("μ", "ν");
        let result = v.contract_with(&eta).unwrap();
        // v_0 = η_{0μ} v^μ = v^0 = 1
        // v_1 = η_{1μ} v^μ = -v^1 = -2
        // v_2 = η_{2μ} v^μ = -v^2 = -3
        // v_3 = η_{3μ} v^μ = -v^3 = -4
        assert_eq!(result.rank(), 1);
        assert!(approx(result.get(&[0]).unwrap(), 1.0));
        assert!(approx(result.get(&[1]).unwrap(), -2.0));
        assert!(approx(result.get(&[2]).unwrap(), -3.0));
        assert!(approx(result.get(&[3]).unwrap(), -4.0));
    }

    #[test]
    fn raise_lower_roundtrip() {
        let eta = IndexedTensor::minkowski("μ", "ν");
        let eta_inv = IndexedTensor::minkowski_inverse("μ", "ν");

        // Start with v_μ = (1, -2, -3, -4) (covariant)
        let v = IndexedTensor::new(
            vec![TensorIndex::lower("α", 4)],
            vec![1.0, -2.0, -3.0, -4.0],
        )
        .unwrap();

        // Raise: v^α = g^{αβ} v_β
        let v_up = v.raise_index(0, &eta_inv).unwrap();
        assert_eq!(v_up.indices()[0].variance, IndexVariance::Contravariant);
        // v^0 = η^{00} v_0 = 1
        // v^1 = η^{11} v_1 = -(-2) = 2
        assert!(approx(v_up.get(&[0]).unwrap(), 1.0));
        assert!(approx(v_up.get(&[1]).unwrap(), 2.0));
        assert!(approx(v_up.get(&[2]).unwrap(), 3.0));
        assert!(approx(v_up.get(&[3]).unwrap(), 4.0));

        // Lower again: should get back original
        let v_back = v_up.lower_index(0, &eta).unwrap();
        for i in 0..4 {
            assert!(approx(v_back.get(&[i]).unwrap(), v.get(&[i]).unwrap()));
        }
    }

    #[test]
    fn levi_civita_3d() {
        let eps = IndexedTensor::levi_civita(3, IndexVariance::Contravariant).unwrap();
        // ε^{012} = +1
        assert!(approx(eps.get(&[0, 1, 2]).unwrap(), 1.0));
        // ε^{021} = -1
        assert!(approx(eps.get(&[0, 2, 1]).unwrap(), -1.0));
        // ε^{011} = 0
        assert!(approx(eps.get(&[0, 1, 1]).unwrap(), 0.0));
    }

    #[test]
    fn levi_civita_4d_antisymmetric() {
        let eps = IndexedTensor::levi_civita(4, IndexVariance::Covariant).unwrap();
        // ε_{0123} = +1
        assert!(approx(eps.get(&[0, 1, 2, 3]).unwrap(), 1.0));
        // ε_{1023} = -1
        assert!(approx(eps.get(&[1, 0, 2, 3]).unwrap(), -1.0));
        // ε_{1032} = +1
        assert!(approx(eps.get(&[1, 0, 3, 2]).unwrap(), 1.0));
    }

    #[test]
    fn permute_transpose() {
        let t = IndexedTensor::new(
            vec![TensorIndex::upper("μ", 3), TensorIndex::lower("ν", 3)],
            vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0],
        )
        .unwrap();
        let tp = t.permute(&[1, 0]).unwrap();
        // Transposed: tp[i][j] = t[j][i]
        assert!(approx(tp.get(&[0, 0]).unwrap(), 1.0));
        assert!(approx(tp.get(&[0, 1]).unwrap(), 4.0));
        assert!(approx(tp.get(&[1, 0]).unwrap(), 2.0));
    }

    #[test]
    fn index_variance_flip() {
        assert_eq!(
            IndexVariance::Contravariant.flip(),
            IndexVariance::Covariant
        );
        assert_eq!(
            IndexVariance::Covariant.flip(),
            IndexVariance::Contravariant
        );
    }

    #[test]
    fn tensor_display() {
        let t = IndexedTensor::zeros(vec![TensorIndex::upper("μ", 4), TensorIndex::lower("ν", 4)]);
        let s = format!("{t}");
        assert!(s.contains("μ↑4"));
        assert!(s.contains("ν↓4"));
    }

    #[test]
    fn add_sub() {
        let a = IndexedTensor::new(vec![TensorIndex::upper("μ", 3)], vec![1.0, 2.0, 3.0]).unwrap();
        let b =
            IndexedTensor::new(vec![TensorIndex::upper("μ", 3)], vec![10.0, 20.0, 30.0]).unwrap();
        let c = a.add(&b).unwrap();
        assert!(approx(c.get(&[0]).unwrap(), 11.0));
        let d = a.sub(&b).unwrap();
        assert!(approx(d.get(&[0]).unwrap(), -9.0));
    }

    #[test]
    fn add_index_mismatch() {
        let a = IndexedTensor::new(vec![TensorIndex::upper("μ", 4)], vec![1.0; 4]).unwrap();
        let b = IndexedTensor::new(vec![TensorIndex::lower("μ", 4)], vec![1.0; 4]).unwrap();
        assert!(a.add(&b).is_err());
    }

    #[test]
    fn new_size_mismatch() {
        assert!(IndexedTensor::new(vec![TensorIndex::upper("μ", 4)], vec![1.0; 3]).is_err());
    }

    #[test]
    fn contract_invalid_positions() {
        let t = IndexedTensor::zeros(vec![TensorIndex::upper("μ", 4), TensorIndex::lower("ν", 4)]);
        assert!(t.contract(0, 5).is_err());
    }

    #[test]
    fn contract_same_position() {
        let t = IndexedTensor::zeros(vec![TensorIndex::upper("μ", 4), TensorIndex::lower("ν", 4)]);
        assert!(t.contract(1, 1).is_err());
    }

    #[test]
    fn raise_index_already_contravariant() {
        let t = IndexedTensor::zeros(vec![TensorIndex::upper("μ", 4)]);
        let metric = IndexedTensor::minkowski_inverse("α", "β");
        assert!(t.raise_index(0, &metric).is_err());
    }

    #[test]
    fn lower_index_already_covariant() {
        let t = IndexedTensor::zeros(vec![TensorIndex::lower("μ", 4)]);
        let metric = IndexedTensor::minkowski("α", "β");
        assert!(t.lower_index(0, &metric).is_err());
    }

    #[test]
    fn permute_invalid() {
        let t = IndexedTensor::zeros(vec![TensorIndex::upper("μ", 3), TensorIndex::lower("ν", 3)]);
        assert!(t.permute(&[0, 0]).is_err());
    }

    #[test]
    fn permute_wrong_length() {
        let t = IndexedTensor::zeros(vec![TensorIndex::upper("μ", 3), TensorIndex::lower("ν", 3)]);
        assert!(t.permute(&[0]).is_err());
    }

    #[test]
    fn sub_index_mismatch() {
        let a = IndexedTensor::new(vec![TensorIndex::upper("μ", 4)], vec![1.0; 4]).unwrap();
        let b = IndexedTensor::new(vec![TensorIndex::lower("μ", 4)], vec![1.0; 4]).unwrap();
        assert!(a.sub(&b).is_err());
    }
}