oxieml 0.1.2

EML operator: all elementary functions from exp(x) - ln(y)
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
//! Dimensional analysis for unit-aware symbolic regression.
//!
//! Provides [`Units`] — a 7-element exponent vector representing SI base units —
//! and [`UnitError`] for dimensional consistency violations. Integrates with
//! [`crate::lower::LoweredOp::check_units`] to enable hard pruning of
//! dimensionally-inadmissible topologies during symbolic regression.
//!
//! # SI base dimensions
//!
//! Index | Symbol | Quantity
//! ------|--------|----------
//! 0     | m      | Length
//! 1     | kg     | Mass
//! 2     | s      | Time
//! 3     | A      | Electric current
//! 4     | K      | Thermodynamic temperature
//! 5     | mol    | Amount of substance
//! 6     | cd     | Luminous intensity
//!
//! # Example
//!
//! ```
//! use oxieml::units::Units;
//!
//! // velocity: m/s
//! let velocity = Units::METER.div(&Units::SECOND);
//! assert_eq!(velocity.try_into_int_exps(), Some([1i8, 0, -1, 0, 0, 0, 0]));
//!
//! // kinetic energy: kg·m²/s²  ≡ JOULE
//! let ke = Units::KILOGRAM.mul(&Units::METER).mul(&Units::METER).div(&Units::SECOND).div(&Units::SECOND);
//! assert_eq!(ke, Units::JOULE);
//! ```

use std::fmt;

// -----------------------------------------------------------------------
// gcd helpers (needed for Rexp normalization)
// -----------------------------------------------------------------------

fn gcd_u16(mut a: u16, mut b: u16) -> u16 {
    while b != 0 {
        let t = b;
        b = a % b;
        a = t;
    }
    if a == 0 { 1 } else { a }
}

fn gcd_u32(mut a: u32, mut b: u32) -> u32 {
    while b != 0 {
        let t = b;
        b = a % b;
        a = t;
    }
    if a == 0 { 1 } else { a }
}

// -----------------------------------------------------------------------
// Rexp: rational exponent
// -----------------------------------------------------------------------

/// A rational exponent for SI base dimensions.
/// Normalized: `gcd(num.abs(), den) == 1`, `den > 0`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Rexp {
    /// Numerator of the rational exponent.
    pub num: i16,
    /// Denominator of the rational exponent (always positive, ≥ 1).
    pub den: i16,
}

impl Rexp {
    /// The additive identity: `0/1`.
    pub const ZERO: Self = Rexp { num: 0, den: 1 };
    /// The multiplicative identity: `1/1`.
    pub const ONE: Self = Rexp { num: 1, den: 1 };

    /// Construct a normalized `Rexp`. Returns `None` if `den <= 0`.
    pub fn new(num: i16, den: i16) -> Option<Self> {
        if den <= 0 {
            return None;
        }
        if num == 0 {
            return Some(Self::ZERO);
        }
        let g = gcd_u16(num.unsigned_abs(), den.unsigned_abs()) as i16;
        Some(Rexp {
            num: num / g,
            den: den / g,
        })
    }

    /// Construct from a known-valid ratio (panics only if den <= 0, which
    /// is only called from `rationalize_f64` with controlled inputs).
    pub fn from_ratio(num: i16, den: i16) -> Self {
        Self::new(num, den).expect("Rexp::from_ratio: den must be positive")
    }

    /// Construct from an integer exponent.
    pub fn from_int(n: i8) -> Self {
        Rexp {
            num: n as i16,
            den: 1,
        }
    }

    /// Convert to `f64` (numerator / denominator).
    pub fn to_f64(self) -> f64 {
        self.num as f64 / self.den as f64
    }

    /// Return `true` if the denominator is 1 (i.e. an integer exponent).
    pub fn is_integer(self) -> bool {
        self.den == 1
    }

    /// Try to convert to `i8`. Returns `None` if the denominator is not 1 or the value is out of `i8` range.
    pub fn to_i8(self) -> Option<i8> {
        if self.den == 1 && self.num >= -128 && self.num <= 127 {
            Some(self.num as i8)
        } else {
            None
        }
    }

    fn add_raw(self, rhs: Self) -> Self {
        let num = self.num as i32 * rhs.den as i32 + rhs.num as i32 * self.den as i32;
        let den = self.den as i32 * rhs.den as i32;
        if num == 0 {
            return Self::ZERO;
        }
        let g = gcd_u32(num.unsigned_abs(), den.unsigned_abs()) as i32;
        let g = if g == 0 { 1 } else { g };
        Rexp {
            num: (num / g).clamp(-32767, 32767) as i16,
            den: (den / g).clamp(1, 32767) as i16,
        }
    }

    fn rexp_add(self, rhs: Self) -> Self {
        self.add_raw(rhs)
    }

    fn rexp_sub(self, rhs: Self) -> Self {
        self.add_raw(Rexp {
            num: -rhs.num,
            den: rhs.den,
        })
    }

    /// Multiply by an integer scalar: `self * s`.
    pub fn mul_int(self, s: i32) -> Self {
        if s == 0 {
            return Self::ZERO;
        }
        let num = self.num as i32 * s;
        let g = gcd_u32(num.unsigned_abs(), self.den as u32) as i32;
        let g = if g == 0 { 1 } else { g };
        Rexp {
            num: (num / g).clamp(-32767, 32767) as i16,
            den: (self.den as i32 / g) as i16,
        }
    }

    /// Multiply two rational exponents: `self * rhs`.
    pub fn mul_rexp(self, rhs: Self) -> Self {
        let num = self.num as i32 * rhs.num as i32;
        let den = self.den as i32 * rhs.den as i32;
        if num == 0 {
            return Self::ZERO;
        }
        let g = gcd_u32(num.unsigned_abs(), den.unsigned_abs()) as i32;
        let g = if g == 0 { 1 } else { g };
        Rexp {
            num: (num / g).clamp(-32767, 32767) as i16,
            den: (den / g).clamp(1, 32767) as i16,
        }
    }
}

impl std::ops::Add for Rexp {
    type Output = Self;
    fn add(self, rhs: Self) -> Self {
        self.rexp_add(rhs)
    }
}

impl std::ops::Sub for Rexp {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self {
        self.rexp_sub(rhs)
    }
}

impl fmt::Display for Rexp {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.den == 1 {
            write!(f, "{}", self.num)
        } else {
            write!(f, "({}/{})", self.num, self.den)
        }
    }
}

// -----------------------------------------------------------------------
// const helper for building Units constants
// -----------------------------------------------------------------------

const fn rexp_int(n: i8) -> Rexp {
    Rexp {
        num: n as i16,
        den: 1,
    }
}

// -----------------------------------------------------------------------
// Units
// -----------------------------------------------------------------------

/// SI unit represented as a 7-element rational exponent vector `[m, kg, s, A, K, mol, cd]`.
///
/// Positive exponents appear in the numerator, negative in the denominator.
/// The arithmetic follows the standard rules:
///
/// - Multiplication: element-wise addition of exponents (`m·s → [1,0,1,0,0,0,0]`).
/// - Division: element-wise subtraction of exponents (`m/s → [1,0,-1,0,0,0,0]`).
/// - Integer power: element-wise scaling (`m² → [2,0,0,0,0,0,0]`).
/// - Rational power: element-wise rational scaling (`m^(1/2)` via `sqrt()`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Units(pub [Rexp; 7]);

impl Units {
    // -----------------------------------------------------------------------
    // SI base units
    // -----------------------------------------------------------------------

    /// Dimensionless quantity (no physical unit).
    pub const DIMENSIONLESS: Self = Self([
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
    ]);
    /// Metre (length).
    pub const METER: Self = Self([
        Rexp::ONE,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
    ]);
    /// Kilogram (mass).
    pub const KILOGRAM: Self = Self([
        Rexp::ZERO,
        Rexp::ONE,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
    ]);
    /// Second (time).
    pub const SECOND: Self = Self([
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ONE,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
    ]);
    /// Ampere (electric current).
    pub const AMPERE: Self = Self([
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ONE,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
    ]);
    /// Kelvin (thermodynamic temperature).
    pub const KELVIN: Self = Self([
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ONE,
        Rexp::ZERO,
        Rexp::ZERO,
    ]);
    /// Mole (amount of substance).
    pub const MOL: Self = Self([
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ONE,
        Rexp::ZERO,
    ]);
    /// Candela (luminous intensity).
    pub const CANDELA: Self = Self([
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ONE,
    ]);

    // -----------------------------------------------------------------------
    // Derived units
    // -----------------------------------------------------------------------

    /// Newton (force): kg·m·s⁻².
    pub const NEWTON: Self = Self([
        rexp_int(1),
        rexp_int(1),
        rexp_int(-2),
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
    ]);
    /// Joule (energy): kg·m²·s⁻².
    pub const JOULE: Self = Self([
        rexp_int(2),
        rexp_int(1),
        rexp_int(-2),
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
    ]);
    /// Watt (power): kg·m²·s⁻³.
    pub const WATT: Self = Self([
        rexp_int(2),
        rexp_int(1),
        rexp_int(-3),
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
    ]);
    /// Pascal (pressure): kg·m⁻¹·s⁻².
    pub const PASCAL: Self = Self([
        rexp_int(-1),
        rexp_int(1),
        rexp_int(-2),
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
    ]);
    /// Hertz (frequency): s⁻¹.
    pub const HERTZ: Self = Self([
        Rexp::ZERO,
        Rexp::ZERO,
        rexp_int(-1),
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
    ]);
    /// Coulomb (electric charge): A·s.
    pub const COULOMB: Self = Self([
        Rexp::ZERO,
        Rexp::ZERO,
        rexp_int(1),
        rexp_int(1),
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
    ]);
    /// Volt (electric potential): kg·m²·s⁻³·A⁻¹.
    pub const VOLT: Self = Self([
        rexp_int(2),
        rexp_int(1),
        rexp_int(-3),
        rexp_int(-1),
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
    ]);
    /// Ohm (electric resistance): kg·m²·s⁻³·A⁻².
    pub const OHM: Self = Self([
        rexp_int(2),
        rexp_int(1),
        rexp_int(-3),
        rexp_int(-2),
        Rexp::ZERO,
        Rexp::ZERO,
        Rexp::ZERO,
    ]);

    // -----------------------------------------------------------------------
    // Constructors
    // -----------------------------------------------------------------------

    /// Construct a `Units` value from a raw 7-element integer exponent array.
    #[inline]
    pub fn new(exps: [i8; 7]) -> Self {
        Self([
            Rexp::from_int(exps[0]),
            Rexp::from_int(exps[1]),
            Rexp::from_int(exps[2]),
            Rexp::from_int(exps[3]),
            Rexp::from_int(exps[4]),
            Rexp::from_int(exps[5]),
            Rexp::from_int(exps[6]),
        ])
    }

    /// Construct from integer exponents array (alias for `new`).
    #[inline]
    pub fn from_int_exps(exps: [i8; 7]) -> Self {
        Self::new(exps)
    }

    /// Try to convert back to integer exponents (for backward-compat checks).
    /// Returns `None` if any exponent is non-integer.
    pub fn try_into_int_exps(&self) -> Option<[i8; 7]> {
        let mut result = [0i8; 7];
        for (i, r) in self.0.iter().enumerate() {
            result[i] = r.to_i8()?;
        }
        Some(result)
    }

    /// Return `true` when all exponents are zero (i.e. dimensionless).
    #[inline]
    pub fn is_dimensionless(&self) -> bool {
        self.0.iter().all(|r| *r == Rexp::ZERO)
    }

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

    /// Multiply units: element-wise addition of exponents.
    ///
    /// Equivalent to multiplying two quantities with these units.
    #[inline]
    pub fn mul(&self, other: &Self) -> Self {
        let mut result = [Rexp::ZERO; 7];
        for (dst, (a, b)) in result.iter_mut().zip(self.0.iter().zip(other.0.iter())) {
            *dst = *a + *b;
        }
        Self(result)
    }

    /// Divide units: element-wise subtraction of exponents.
    ///
    /// Equivalent to dividing a quantity with `self` units by one with `other` units.
    #[inline]
    pub fn div(&self, other: &Self) -> Self {
        let mut result = [Rexp::ZERO; 7];
        for (dst, (a, b)) in result.iter_mut().zip(self.0.iter().zip(other.0.iter())) {
            *dst = *a - *b;
        }
        Self(result)
    }

    /// Raise to an integer power: multiply each exponent by `n`.
    ///
    /// Returns `Err` when exponent overflow occurs.
    ///
    /// # Errors
    ///
    /// Returns [`UnitError::ExponentOverflow`] when any exponent would overflow.
    pub fn pow_int(&self, n: i32) -> Result<Self, UnitError> {
        let mut result = [Rexp::ZERO; 7];
        for (i, (dst, r)) in result.iter_mut().zip(self.0.iter()).enumerate() {
            let scaled_num = r.num as i64 * n as i64;
            // For integer base exponents (den==1), enforce i8 range for backward compatibility.
            // For rational base exponents (den!=1), enforce i16 range.
            let overflow = if r.den == 1 {
                !(-128..=127_i64).contains(&scaled_num)
            } else {
                !(-32767..=32767_i64).contains(&scaled_num)
            };
            if overflow {
                return Err(UnitError::ExponentOverflow {
                    dimension: i,
                    base_exp: r.to_i8().unwrap_or(0),
                    power: n,
                });
            }
            *dst = r.mul_int(n);
        }
        Ok(Self(result))
    }

    /// Raise to a rational power: each exponent multiplied by `num/den`.
    ///
    /// # Errors
    ///
    /// Returns [`UnitError::ExponentOverflow`] if `den <= 0`.
    pub fn pow_rational(&self, num: i16, den: i16) -> Result<Self, UnitError> {
        if den <= 0 {
            return Err(UnitError::ExponentOverflow {
                dimension: 0,
                base_exp: 0,
                power: num as i32,
            });
        }
        let r = Rexp::from_ratio(num, den);
        let mut out = [Rexp::ZERO; 7];
        for (dst, src) in out.iter_mut().zip(self.0.iter()) {
            *dst = src.mul_rexp(r);
        }
        Ok(Self(out))
    }

    /// Square root: each exponent multiplied by 1/2.
    pub fn sqrt(&self) -> Self {
        self.pow_rational(1, 2).unwrap_or(*self)
    }

    // -----------------------------------------------------------------------
    // Symbol helpers
    // -----------------------------------------------------------------------

    /// Symbol string for dimension `idx` (0 = m, 1 = kg, 2 = s, …).
    fn dim_symbol(idx: usize) -> &'static str {
        match idx {
            0 => "m",
            1 => "kg",
            2 => "s",
            3 => "A",
            4 => "K",
            5 => "mol",
            6 => "cd",
            _ => "?",
        }
    }
}

// -----------------------------------------------------------------------
// Display — produces strings like "m¹·kg²·s⁻²" or "m^(1/2)" for rationals
// -----------------------------------------------------------------------

fn format_superscript(n: i16) -> String {
    match n {
        1 => String::new(),
        -1 => "\u{207B}\u{00B9}".to_string(),
        2 => "\u{00B2}".to_string(),
        3 => "\u{00B3}".to_string(),
        -2 => "\u{207B}\u{00B2}".to_string(),
        -3 => "\u{207B}\u{00B3}".to_string(),
        _ => {
            let sign = if n < 0 { "\u{207B}" } else { "" };
            let digits: String = n
                .unsigned_abs()
                .to_string()
                .chars()
                .map(|c| match c {
                    '0' => '\u{2070}',
                    '1' => '\u{00B9}',
                    '2' => '\u{00B2}',
                    '3' => '\u{00B3}',
                    '4' => '\u{2074}',
                    '5' => '\u{2075}',
                    '6' => '\u{2076}',
                    '7' => '\u{2077}',
                    '8' => '\u{2078}',
                    '9' => '\u{2079}',
                    other => other,
                })
                .collect();
            format!("{sign}{digits}")
        }
    }
}

impl fmt::Display for Units {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.is_dimensionless() {
            return write!(f, "1");
        }

        let mut first = true;
        for (i, r) in self.0.iter().enumerate() {
            if *r == Rexp::ZERO {
                continue;
            }
            if !first {
                write!(f, "\u{00B7}")?; // · middle dot
            }
            first = false;
            let sym = Self::dim_symbol(i);
            if r.den == 1 {
                // Integer exponent: use superscripts like before
                write!(f, "{}{}", sym, format_superscript(r.num))?;
            } else {
                // Rational exponent
                write!(f, "{}^({}/{})", sym, r.num, r.den)?;
            }
        }
        Ok(())
    }
}

// -----------------------------------------------------------------------
// Error type
// -----------------------------------------------------------------------

/// Errors that can occur during dimensional analysis.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum UnitError {
    /// `Add` or `Sub` applied to operands with incompatible units.
    IncompatibleAddSub {
        /// Units of the left operand.
        left: Units,
        /// Units of the right operand.
        right: Units,
    },
    /// Transcendental function (`exp`, `ln`, `sin`, …) applied to a
    /// non-dimensionless argument.
    NonDimensionlessArgument {
        /// Name of the offending operation.
        op: &'static str,
        /// Actual units of the argument.
        got: Units,
    },
    /// `Pow` with a non-dimensionless base and a non-rational exponent.
    NonRationalPower {
        /// Units of the base expression.
        base_units: Units,
    },
    /// Variable index `index` was out of range for a `var_units` slice of
    /// length `n_vars`.
    VarIndexOutOfRange {
        /// The offending variable index.
        index: usize,
        /// Length of the `var_units` slice.
        n_vars: usize,
    },
    /// Integer exponent scaling would overflow the representable range.
    ExponentOverflow {
        /// Zero-based dimension index.
        dimension: usize,
        /// Base exponent value.
        base_exp: i8,
        /// Requested integer power.
        power: i32,
    },
}

impl fmt::Display for UnitError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::IncompatibleAddSub { left, right } => {
                write!(f, "add/sub unit mismatch: {left}{right}")
            }
            Self::NonDimensionlessArgument { op, got } => {
                write!(f, "{op} requires a dimensionless argument, got {got}")
            }
            Self::NonRationalPower { base_units } => {
                write!(
                    f,
                    "Pow with dimensioned base ({base_units}) requires a rational-constant exponent"
                )
            }
            Self::VarIndexOutOfRange { index, n_vars } => {
                write!(
                    f,
                    "variable index {index} out of range (var_units has {n_vars} entries)"
                )
            }
            Self::ExponentOverflow {
                dimension,
                base_exp,
                power,
            } => {
                write!(
                    f,
                    "exponent overflow for dimension {dimension}: {base_exp} × {power} exceeds range"
                )
            }
        }
    }
}

impl std::error::Error for UnitError {}

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

    #[test]
    fn dimensionless_is_zero_vector() {
        assert!(Units::DIMENSIONLESS.is_dimensionless());
        assert_eq!(Units::DIMENSIONLESS.try_into_int_exps(), Some([0i8; 7]));
    }

    #[test]
    fn named_units_correct() {
        assert_eq!(
            Units::METER.try_into_int_exps(),
            Some([1i8, 0, 0, 0, 0, 0, 0])
        );
        assert_eq!(
            Units::KILOGRAM.try_into_int_exps(),
            Some([0i8, 1, 0, 0, 0, 0, 0])
        );
        assert_eq!(
            Units::SECOND.try_into_int_exps(),
            Some([0i8, 0, 1, 0, 0, 0, 0])
        );
    }

    #[test]
    fn mul_adds_exponents() {
        let result = Units::METER.mul(&Units::SECOND);
        assert_eq!(result.try_into_int_exps(), Some([1i8, 0, 1, 0, 0, 0, 0]));
    }

    #[test]
    fn div_subtracts_exponents() {
        let result = Units::METER.div(&Units::SECOND);
        assert_eq!(result.try_into_int_exps(), Some([1i8, 0, -1, 0, 0, 0, 0]));
    }

    #[test]
    fn pow_int_scales_exponents() {
        let result = Units::METER.pow_int(3).expect("no overflow");
        assert_eq!(result.try_into_int_exps(), Some([3i8, 0, 0, 0, 0, 0, 0]));
    }

    #[test]
    fn pow_int_overflow_returns_err() {
        let result = Units::METER.pow_int(200);
        assert!(result.is_err());
    }

    #[test]
    fn display_meter_per_second() {
        let v = Units::METER.div(&Units::SECOND);
        let s = v.to_string();
        assert!(s.contains('m'), "expected 'm' in '{s}'");
        assert!(s.contains('s'), "expected 's' in '{s}'");
    }

    #[test]
    fn display_dimensionless() {
        assert_eq!(Units::DIMENSIONLESS.to_string(), "1");
    }

    #[test]
    fn derived_newton() {
        // F = kg·m·s⁻²
        let newton = Units::KILOGRAM.mul(&Units::METER).mul(
            &Units::SECOND
                .pow_int(-2)
                .expect("pow_int(-2) does not overflow"),
        );
        assert_eq!(newton, Units::NEWTON);
    }

    #[test]
    fn rexp_from_int_roundtrip() {
        let r = Rexp::from_int(3);
        assert_eq!(r.num, 3);
        assert_eq!(r.den, 1);
        assert!(r.is_integer());
        assert_eq!(r.to_i8(), Some(3));
    }

    #[test]
    fn sqrt_of_m2_is_m() {
        let m2 = Units::METER.pow_int(2).expect("no overflow");
        let m = m2.sqrt();
        assert_eq!(m.0[0].to_i8(), Some(1), "sqrt(m²) should give m^1");
    }

    #[test]
    fn sqrt_of_meter_is_half_power() {
        let m_half = Units::METER.sqrt();
        assert_eq!(
            m_half.0[0],
            Rexp::from_ratio(1, 2),
            "sqrt(m) should be m^(1/2)"
        );
    }

    #[test]
    fn mul_half_plus_half_is_one() {
        let m_half = Units::METER.sqrt();
        let m = m_half.mul(&m_half);
        assert_eq!(m.0[0].to_i8(), Some(1), "m^(1/2) * m^(1/2) = m");
    }

    #[test]
    fn legacy_integer_ops_preserved() {
        let m = Units::METER;
        let m2 = m.pow_int(2).expect("no overflow");
        assert_eq!(m2.0[0].to_i8(), Some(2));
        let m_back = m2.div(&m);
        assert_eq!(m_back.0[0].to_i8(), Some(1));
    }

    #[test]
    fn display_rational_exponent() {
        let m_half = Units::METER.sqrt();
        let s = m_half.to_string();
        assert!(
            s.contains("(1/2)"),
            "rational display should show (1/2), got: {s}"
        );
    }

    #[test]
    fn new_from_int_array() {
        let v = Units::new([1, 0, -1, 0, 0, 0, 0]);
        assert_eq!(v.try_into_int_exps(), Some([1i8, 0, -1, 0, 0, 0, 0]));
    }
}