multicalc 0.10.0

Math for real-time embedded systems, in stable no_std Rust: state estimation, control, kinematics, Lie groups, autodiff, and linear algebra — from 64-bit servers to bare-metal microcontrollers
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
//! Hyper-dual numbers for exact first and second derivatives.
//!
//! A [`HyperDual`] carries a value alongside first- and second-order derivative parts.
//! Evaluating a function on a [`HyperDual::variable`] returns `f(x)`, `f'(x)`, and `f''(x)`
//! in one pass, exact to rounding and with no allocation. Seeding two inputs along the two
//! `ε` directions yields an exact mixed partial — the building block of a Hessian. Because
//! `HyperDual` implements [`Numeric`], any function written generically over `Numeric` can be
//! differentiated by calling it with `HyperDual`.

use core::cmp::Ordering;
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};

use crate::scalar::Numeric;

/// A hyper-dual number `real + eps1·ε₁ + eps2·ε₂ + eps1eps2·ε₁ε₂`, where `ε₁² = ε₂² = 0`.
///
/// After evaluating a function, `real` holds the value, `eps1`/`eps2` hold the first
/// derivatives along each seeded direction, and `eps1eps2` holds the second (mixed) derivative.
#[derive(Debug, Clone, Copy)]
pub struct HyperDual<T: Numeric = f64> {
    /// The value, `f`.
    pub real: T,
    /// First derivative along direction 1.
    pub eps1: T,
    /// First derivative along direction 2.
    pub eps2: T,
    /// Second derivative across the two directions.
    pub eps1eps2: T,
}

impl<T: Numeric> HyperDual<T> {
    /// A hyper-dual number with explicit components.
    #[inline]
    #[must_use]
    pub fn new(real: T, eps1: T, eps2: T, eps1eps2: T) -> Self {
        HyperDual {
            real,
            eps1,
            eps2,
            eps1eps2,
        }
    }

    /// A constant, whose derivatives are all zero.
    #[inline]
    #[must_use]
    pub fn constant(real: T) -> Self {
        HyperDual {
            real,
            eps1: T::ZERO,
            eps2: T::ZERO,
            eps1eps2: T::ZERO,
        }
    }

    /// The independent variable, seeded along both directions to read `f`, `f'`, and `f''` of a
    /// single-variable function. For a mixed partial, seed two inputs on separate directions with
    /// `new(xᵢ, 1, 0, 0)` and `new(xⱼ, 0, 1, 0)`.
    #[inline]
    #[must_use]
    pub fn variable(real: T) -> Self {
        HyperDual {
            real,
            eps1: T::ONE,
            eps2: T::ONE,
            eps1eps2: T::ZERO,
        }
    }

    /// Applies a univariate function `φ` given its value and first two derivatives at `real`.
    ///
    /// With `val = φ(real)`, `d1 = φ'(real)`, `d2 = φ''(real)`, this carries the chain rule
    /// through to second order.
    #[inline]
    #[must_use]
    fn chain(self, val: T, d1: T, d2: T) -> Self {
        HyperDual {
            real: val,
            eps1: d1 * self.eps1,
            eps2: d1 * self.eps2,
            eps1eps2: d1 * self.eps1eps2 + d2 * self.eps1 * self.eps2,
        }
    }

    /// The reciprocal `1/self`, via `φ(x) = 1/x` (`φ' = -1/x²`, `φ'' = 2/x³`).
    #[inline]
    #[must_use]
    fn recip(self) -> Self {
        let t = T::ONE / self.real;
        self.chain(t, -(t * t), T::TWO * t * t * t)
    }
}

impl<T: Numeric> Add for HyperDual<T> {
    type Output = Self;
    #[inline]
    fn add(self, rhs: Self) -> Self {
        HyperDual {
            real: self.real + rhs.real,
            eps1: self.eps1 + rhs.eps1,
            eps2: self.eps2 + rhs.eps2,
            eps1eps2: self.eps1eps2 + rhs.eps1eps2,
        }
    }
}

impl<T: Numeric> Sub for HyperDual<T> {
    type Output = Self;
    #[inline]
    fn sub(self, rhs: Self) -> Self {
        HyperDual {
            real: self.real - rhs.real,
            eps1: self.eps1 - rhs.eps1,
            eps2: self.eps2 - rhs.eps2,
            eps1eps2: self.eps1eps2 - rhs.eps1eps2,
        }
    }
}

impl<T: Numeric> Mul for HyperDual<T> {
    type Output = Self;
    /// Second-order product rule: each derivative part collects the cross terms that survive
    /// `ε₁² = ε₂² = 0`.
    #[inline]
    fn mul(self, rhs: Self) -> Self {
        HyperDual {
            real: self.real * rhs.real,
            eps1: self.real * rhs.eps1 + self.eps1 * rhs.real,
            eps2: self.real * rhs.eps2 + self.eps2 * rhs.real,
            eps1eps2: self.real * rhs.eps1eps2
                + self.eps1 * rhs.eps2
                + self.eps2 * rhs.eps1
                + self.eps1eps2 * rhs.real,
        }
    }
}

impl<T: Numeric> Div for HyperDual<T> {
    type Output = Self;
    /// `self · (1/rhs)`. A zero divisor yields `inf`/`NaN`, as with plain floats.
    #[inline]
    #[allow(clippy::suspicious_arithmetic_impl)] // division is multiplication by the reciprocal
    fn div(self, rhs: Self) -> Self {
        self * rhs.recip()
    }
}

impl<T: Numeric> Neg for HyperDual<T> {
    type Output = Self;
    #[inline]
    fn neg(self) -> Self {
        HyperDual {
            real: -self.real,
            eps1: -self.eps1,
            eps2: -self.eps2,
            eps1eps2: -self.eps1eps2,
        }
    }
}

impl<T: Numeric> AddAssign for HyperDual<T> {
    #[inline]
    fn add_assign(&mut self, rhs: Self) {
        *self = *self + rhs;
    }
}

impl<T: Numeric> SubAssign for HyperDual<T> {
    #[inline]
    fn sub_assign(&mut self, rhs: Self) {
        *self = *self - rhs;
    }
}

impl<T: Numeric> MulAssign for HyperDual<T> {
    #[inline]
    fn mul_assign(&mut self, rhs: Self) {
        *self = *self * rhs;
    }
}

impl<T: Numeric> DivAssign for HyperDual<T> {
    #[inline]
    fn div_assign(&mut self, rhs: Self) {
        *self = *self / rhs;
    }
}

// Comparison uses only the real part, so ordering and equality match the underlying scalar; the
// derivatives do not take part. Two values with equal real but different derivatives therefore
// compare equal, so `HyperDual` is not suited as a map/set key.
impl<T: Numeric> PartialEq for HyperDual<T> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.real == other.real
    }
}

impl<T: Numeric> PartialOrd for HyperDual<T> {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.real.partial_cmp(&other.real)
    }
}

impl<T: Numeric> Numeric for HyperDual<T> {
    const ZERO: Self = HyperDual {
        real: T::ZERO,
        eps1: T::ZERO,
        eps2: T::ZERO,
        eps1eps2: T::ZERO,
    };
    const ONE: Self = HyperDual {
        real: T::ONE,
        eps1: T::ZERO,
        eps2: T::ZERO,
        eps1eps2: T::ZERO,
    };
    const TWO: Self = HyperDual {
        real: T::TWO,
        eps1: T::ZERO,
        eps2: T::ZERO,
        eps1eps2: T::ZERO,
    };
    const THREE: Self = HyperDual {
        real: T::THREE,
        eps1: T::ZERO,
        eps2: T::ZERO,
        eps1eps2: T::ZERO,
    };
    const HALF: Self = HyperDual {
        real: T::HALF,
        eps1: T::ZERO,
        eps2: T::ZERO,
        eps1eps2: T::ZERO,
    };
    const TEN: Self = HyperDual {
        real: T::TEN,
        eps1: T::ZERO,
        eps2: T::ZERO,
        eps1eps2: T::ZERO,
    };
    const HUNDRED: Self = HyperDual {
        real: T::HUNDRED,
        eps1: T::ZERO,
        eps2: T::ZERO,
        eps1eps2: T::ZERO,
    };
    const ONE_HUNDRED_EIGHTY: Self = HyperDual {
        real: T::ONE_HUNDRED_EIGHTY,
        eps1: T::ZERO,
        eps2: T::ZERO,
        eps1eps2: T::ZERO,
    };
    const PI: Self = HyperDual {
        real: T::PI,
        eps1: T::ZERO,
        eps2: T::ZERO,
        eps1eps2: T::ZERO,
    };
    const TWO_PI: Self = HyperDual {
        real: T::TWO_PI,
        eps1: T::ZERO,
        eps2: T::ZERO,
        eps1eps2: T::ZERO,
    };
    const EPSILON: Self = HyperDual {
        real: T::EPSILON,
        eps1: T::ZERO,
        eps2: T::ZERO,
        eps1eps2: T::ZERO,
    };
    const EPSILON_X4: Self = HyperDual {
        real: T::EPSILON_X4,
        eps1: T::ZERO,
        eps2: T::ZERO,
        eps1eps2: T::ZERO,
    };
    const EPSILON_X30: Self = HyperDual {
        real: T::EPSILON_X30,
        eps1: T::ZERO,
        eps2: T::ZERO,
        eps1eps2: T::ZERO,
    };
    const NAN: Self = HyperDual {
        real: T::NAN,
        eps1: T::ZERO,
        eps2: T::ZERO,
        eps1eps2: T::ZERO,
    };
    const INFINITY: Self = HyperDual {
        real: T::INFINITY,
        eps1: T::ZERO,
        eps2: T::ZERO,
        eps1eps2: T::ZERO,
    };
    const NEG_INFINITY: Self = HyperDual {
        real: T::NEG_INFINITY,
        eps1: T::ZERO,
        eps2: T::ZERO,
        eps1eps2: T::ZERO,
    };
    const MAX: Self = HyperDual {
        real: T::MAX,
        eps1: T::ZERO,
        eps2: T::ZERO,
        eps1eps2: T::ZERO,
    };
    const MIN_POSITIVE: Self = HyperDual {
        real: T::MIN_POSITIVE,
        eps1: T::ZERO,
        eps2: T::ZERO,
        eps1eps2: T::ZERO,
    };

    type Constant = T;

    #[inline]
    fn from_f64(value: f64) -> Self {
        HyperDual::constant(T::from_f64(value))
    }
    #[inline]
    fn from_u64(value: u64) -> Self {
        HyperDual::constant(T::from_u64(value))
    }
    #[inline]
    fn from_usize(value: usize) -> Self {
        HyperDual::constant(T::from_usize(value))
    }

    /// Derivative of `|x|` is its sign (the subgradient at zero is `+1`); its second derivative
    /// is zero.
    #[inline]
    fn abs(self) -> Self {
        let sign = if self.real < T::ZERO { -T::ONE } else { T::ONE };
        self.chain(self.real.abs(), sign, T::ZERO)
    }
    /// At `real == 0` the derivatives are unbounded and become `inf`/`NaN`.
    #[inline]
    fn sqrt(self) -> Self {
        let root = self.real.sqrt();
        let d1 = T::ONE / (T::TWO * root);
        let d2 = -(d1 / (T::TWO * self.real));
        self.chain(root, d1, d2)
    }
    /// At `real == 0` the derivatives are unbounded and become `inf`/`NaN`.
    #[inline]
    fn cbrt(self) -> Self {
        let root = self.real.cbrt();
        let d1 = T::ONE / (T::THREE * root * root);
        let d2 = -(T::TWO * d1 * d1 / root);
        self.chain(root, d1, d2)
    }
    #[inline]
    fn sin(self) -> Self {
        self.chain(self.real.sin(), self.real.cos(), -(self.real.sin()))
    }
    #[inline]
    fn cos(self) -> Self {
        self.chain(self.real.cos(), -(self.real.sin()), -(self.real.cos()))
    }
    #[inline]
    fn tan(self) -> Self {
        let t = self.real.tan();
        let sec2 = T::ONE + t * t;
        self.chain(t, sec2, T::TWO * t * sec2)
    }
    #[inline]
    fn exp(self) -> Self {
        let e = self.real.exp();
        self.chain(e, e, e)
    }
    #[inline]
    fn expm1(self) -> Self {
        let e = self.real.exp();
        let em1 = self.real.expm1();
        self.chain(em1, e, e)
    }
    /// Defined for `real > 0`; at `0` the value is `-inf` and the derivatives unbounded.
    #[inline]
    fn ln(self) -> Self {
        self.chain(
            self.real.ln(),
            T::ONE / self.real,
            -(T::ONE / (self.real * self.real)),
        )
    }

    #[inline]
    fn ln_1p(self) -> Self {
        let xp1 = self.real + T::ONE;
        self.chain(self.real.ln_1p(), T::ONE / xp1, -(T::ONE / (xp1 * xp1)))
    }

    #[inline]
    fn log2(self) -> Self {
        let ln2 = T::TWO.ln();
        self.chain(
            self.real.log2(),
            T::ONE / self.real / ln2,
            -(T::ONE / (self.real * self.real)) / ln2,
        )
    }

    #[inline]
    fn log10(self) -> Self {
        let ln10 = T::TEN.ln();
        self.chain(
            self.real.log10(),
            T::ONE / self.real / ln10,
            -(T::ONE / (self.real * self.real)) / ln10,
        )
    }

    /// Four-quadrant arctangent carried to second order. With `Y = self.real`,
    /// `X = other.real`, `r² = X² + Y²`, the partials are `f_Y = X/r²`, `f_X = −Y/r²`,
    /// `f_YY = −2XY/r⁴`, `f_XX = 2XY/r⁴`, `f_XY = (Y² − X²)/r⁴`. The `r²` denominator is
    /// nonzero off the origin, so the `x = 0` axis is handled; only `X = Y = 0` degenerates,
    /// as for float `atan2`.
    #[inline]
    fn atan2(self, other: Self) -> Self {
        let (y, x) = (self, other);
        let (yr, xr) = (y.real, x.real);
        let r2 = xr * xr + yr * yr;
        let r4 = r2 * r2;
        let f_y = xr / r2;
        let f_x = -yr / r2;
        let f_yy = -(T::TWO * xr * yr) / r4;
        let f_xx = (T::TWO * xr * yr) / r4;
        let f_xy = (yr * yr - xr * xr) / r4;
        HyperDual {
            real: yr.atan2(xr),
            eps1: f_y * y.eps1 + f_x * x.eps1,
            eps2: f_y * y.eps2 + f_x * x.eps2,
            eps1eps2: f_y * y.eps1eps2
                + f_x * x.eps1eps2
                + f_yy * y.eps1 * y.eps2
                + f_xx * x.eps1 * x.eps2
                + f_xy * (y.eps1 * x.eps2 + y.eps2 * x.eps1),
        }
    }
    /// Magnitude of `self` with the sign of `sign`: a linear scaling by `±1`, so it rides the
    /// univariate `chain` helper with slope `s` and zero curvature.
    #[inline]
    fn copysign(self, sign: Self) -> Self {
        let s = if (self.real < T::ZERO) == (sign.real < T::ZERO) {
            T::ONE
        } else {
            -T::ONE
        };
        self.chain(self.real.copysign(sign.real), s, T::ZERO)
    }
    /// Largest integer `<= self`; the derivatives of a step function are zero.
    #[inline]
    fn floor(self) -> Self {
        HyperDual {
            real: self.real.floor(),
            eps1: T::ZERO,
            eps2: T::ZERO,
            eps1eps2: T::ZERO,
        }
    }
    /// Largest integer `>= self`; the derivatives of a step function are zero.
    #[inline]
    fn ceil(self) -> Self {
        HyperDual {
            real: self.real.ceil(),
            eps1: T::ZERO,
            eps2: T::ZERO,
            eps1eps2: T::ZERO,
        }
    }

    /// Nearest integer, ties away from zero; the derivatives of a step function are zero.
    #[inline]
    fn round(self) -> Self {
        HyperDual {
            real: self.real.round(),
            eps1: T::ZERO,
            eps2: T::ZERO,
            eps1eps2: T::ZERO,
        }
    }

    /// Rounds towards zero, effectively removing the decimal part.
    /// A step function, so the derivative is zero.
    #[inline]
    fn trunc(self) -> Self {
        HyperDual {
            real: self.real.trunc(),
            eps1: T::ZERO,
            eps2: T::ZERO,
            eps1eps2: T::ZERO,
        }
    }

    /// Restrict a value to the interval `[min, max]`. Inside this range
    /// it is the identity function, so nothing changes. Outside this range
    /// it is a constant function (equal to either `min` or `max`), so the derivative
    /// is equal to zero.
    #[inline]
    fn clamp(self, min: Self::Constant, max: Self::Constant) -> Self {
        if self.real < min {
            HyperDual {
                real: min,
                eps1: T::ZERO,
                eps2: T::ZERO,
                eps1eps2: T::ZERO,
            }
        } else if max < self.real {
            HyperDual {
                real: max,
                eps1: T::ZERO,
                eps2: T::ZERO,
                eps1eps2: T::ZERO,
            }
        } else {
            self
        }
    }

    /// Reflects the real part only; the derivatives are not inspected.
    #[inline]
    fn is_nan(self) -> bool {
        self.real.is_nan()
    }
    /// Reflects the real part only; a finite value can still carry a non-finite derivative
    /// (e.g. from `sqrt(0)` or `ln(0)`).
    #[inline]
    fn is_finite(self) -> bool {
        self.real.is_finite()
    }
    /// Reflects the real part only; an infinite value can still carry a finite derivative.
    #[inline]
    fn is_infinite(self) -> bool {
        self.real.is_infinite()
    }
}