echidna 0.9.0

A high-performance automatic differentiation library 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
//! `std::ops` implementations for `Taylor<F, K>` and `TaylorDyn<F>`.

use std::ops::{
    Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign,
};

use crate::float::Float;
use crate::taylor::Taylor;
use crate::taylor_ops;

// ══════════════════════════════════════════════
//  Taylor<F, K> ↔ Taylor<F, K>
// ══════════════════════════════════════════════

impl<F: Float, const K: usize> Add for Taylor<F, K> {
    type Output = Self;
    #[inline]
    fn add(self, rhs: Self) -> Self {
        let mut c = [F::zero(); K];
        taylor_ops::taylor_add(&self.coeffs, &rhs.coeffs, &mut c);
        Taylor { coeffs: c }
    }
}

impl<F: Float, const K: usize> Sub for Taylor<F, K> {
    type Output = Self;
    #[inline]
    fn sub(self, rhs: Self) -> Self {
        let mut c = [F::zero(); K];
        taylor_ops::taylor_sub(&self.coeffs, &rhs.coeffs, &mut c);
        Taylor { coeffs: c }
    }
}

// Truncated Taylor series multiplication uses the Cauchy product, which accumulates
// terms via addition — clippy flags the + inside a Mul impl, but this is correct for
// power-series coefficient propagation.
#[allow(clippy::suspicious_arithmetic_impl)]
impl<F: Float, const K: usize> Mul for Taylor<F, K> {
    type Output = Self;
    #[inline]
    fn mul(self, rhs: Self) -> Self {
        let mut c = [F::zero(); K];
        taylor_ops::taylor_mul(&self.coeffs, &rhs.coeffs, &mut c);
        Taylor { coeffs: c }
    }
}

// Truncated Taylor series division computes coefficients via recurrence that uses
// multiplication internally — clippy flags the * inside a Div impl, but this is correct
// for power-series coefficient propagation.
#[allow(clippy::suspicious_arithmetic_impl)]
impl<F: Float, const K: usize> Div for Taylor<F, K> {
    type Output = Self;
    #[inline]
    fn div(self, rhs: Self) -> Self {
        let mut c = [F::zero(); K];
        taylor_ops::taylor_div(&self.coeffs, &rhs.coeffs, &mut c);
        Taylor { coeffs: c }
    }
}

impl<F: Float, const K: usize> Neg for Taylor<F, K> {
    type Output = Self;
    #[inline]
    fn neg(self) -> Self {
        let mut c = [F::zero(); K];
        taylor_ops::taylor_neg(&self.coeffs, &mut c);
        Taylor { coeffs: c }
    }
}

impl<F: Float, const K: usize> Rem for Taylor<F, K> {
    type Output = Self;
    #[inline]
    #[allow(clippy::suspicious_arithmetic_impl)]
    fn rem(self, rhs: Self) -> Self {
        // Zero-divisor produces `Inf` from the division and `NaN` from the
        // modulo in the k=0 slot, then a silent tangent recurrence that mixes
        // finite and non-finite coefficients. Flag the whole series as NaN so
        // downstream consumers see a uniformly degenerate result.
        if rhs.coeffs[0] == F::zero() {
            return Taylor {
                coeffs: std::array::from_fn(|_| F::nan()),
            };
        }
        let q = (self.coeffs[0] / rhs.coeffs[0]).trunc();
        Taylor {
            coeffs: std::array::from_fn(|k| {
                if k == 0 {
                    self.coeffs[0] % rhs.coeffs[0]
                } else {
                    self.coeffs[k] - rhs.coeffs[k] * q
                }
            }),
        }
    }
}

impl<F: Float, const K: usize> AddAssign for Taylor<F, K> {
    #[inline]
    fn add_assign(&mut self, rhs: Self) {
        *self = *self + rhs;
    }
}

impl<F: Float, const K: usize> SubAssign for Taylor<F, K> {
    #[inline]
    fn sub_assign(&mut self, rhs: Self) {
        *self = *self - rhs;
    }
}

impl<F: Float, const K: usize> MulAssign for Taylor<F, K> {
    #[inline]
    fn mul_assign(&mut self, rhs: Self) {
        *self = *self * rhs;
    }
}

impl<F: Float, const K: usize> DivAssign for Taylor<F, K> {
    #[inline]
    fn div_assign(&mut self, rhs: Self) {
        *self = *self / rhs;
    }
}

impl<F: Float, const K: usize> RemAssign for Taylor<F, K> {
    #[inline]
    fn rem_assign(&mut self, rhs: Self) {
        *self = *self % rhs;
    }
}

// Mixed ops: Taylor<F, K> with primitive floats.
macro_rules! impl_taylor_scalar_ops {
    ($f:ty) => {
        impl<const K: usize> Add<$f> for Taylor<$f, K> {
            type Output = Taylor<$f, K>;
            #[inline]
            fn add(self, rhs: $f) -> Taylor<$f, K> {
                let mut coeffs = self.coeffs;
                coeffs[0] += rhs;
                Taylor { coeffs }
            }
        }

        impl<const K: usize> Add<Taylor<$f, K>> for $f {
            type Output = Taylor<$f, K>;
            #[inline]
            fn add(self, rhs: Taylor<$f, K>) -> Taylor<$f, K> {
                let mut coeffs = rhs.coeffs;
                coeffs[0] += self;
                Taylor { coeffs }
            }
        }

        impl<const K: usize> Sub<$f> for Taylor<$f, K> {
            type Output = Taylor<$f, K>;
            #[inline]
            fn sub(self, rhs: $f) -> Taylor<$f, K> {
                let mut coeffs = self.coeffs;
                coeffs[0] -= rhs;
                Taylor { coeffs }
            }
        }

        impl<const K: usize> Sub<Taylor<$f, K>> for $f {
            type Output = Taylor<$f, K>;
            #[inline]
            fn sub(self, rhs: Taylor<$f, K>) -> Taylor<$f, K> {
                Taylor {
                    coeffs: std::array::from_fn(|k| {
                        if k == 0 {
                            self - rhs.coeffs[0]
                        } else {
                            -rhs.coeffs[k]
                        }
                    }),
                }
            }
        }

        impl<const K: usize> Mul<$f> for Taylor<$f, K> {
            type Output = Taylor<$f, K>;
            #[inline]
            fn mul(self, rhs: $f) -> Taylor<$f, K> {
                Taylor {
                    coeffs: std::array::from_fn(|k| self.coeffs[k] * rhs),
                }
            }
        }

        impl<const K: usize> Mul<Taylor<$f, K>> for $f {
            type Output = Taylor<$f, K>;
            #[inline]
            fn mul(self, rhs: Taylor<$f, K>) -> Taylor<$f, K> {
                Taylor {
                    coeffs: std::array::from_fn(|k| self * rhs.coeffs[k]),
                }
            }
        }

        // Scalar division multiplies by the reciprocal for efficiency — clippy flags
        // the * inside a Div impl, but this is the standard reciprocal-multiply optimization.
        #[allow(clippy::suspicious_arithmetic_impl)]
        impl<const K: usize> Div<$f> for Taylor<$f, K> {
            type Output = Taylor<$f, K>;
            #[inline]
            fn div(self, rhs: $f) -> Taylor<$f, K> {
                let inv: $f = 1.0 / rhs;
                Taylor {
                    coeffs: std::array::from_fn(|k| self.coeffs[k] * inv),
                }
            }
        }

        impl<const K: usize> Div<Taylor<$f, K>> for $f {
            type Output = Taylor<$f, K>;
            #[inline]
            fn div(self, rhs: Taylor<$f, K>) -> Taylor<$f, K> {
                Taylor::constant(self) / rhs
            }
        }

        impl<const K: usize> Rem<$f> for Taylor<$f, K> {
            type Output = Taylor<$f, K>;
            #[inline]
            fn rem(self, rhs: $f) -> Taylor<$f, K> {
                let mut coeffs = self.coeffs;
                coeffs[0] %= rhs;
                Taylor { coeffs }
            }
        }

        impl<const K: usize> Rem<Taylor<$f, K>> for $f {
            type Output = Taylor<$f, K>;
            #[inline]
            #[allow(clippy::suspicious_arithmetic_impl)]
            fn rem(self, rhs: Taylor<$f, K>) -> Taylor<$f, K> {
                // scalar % b(t) = scalar - trunc(scalar/b[0]) * b(t)
                let q = (self / rhs.coeffs[0]).trunc();
                Taylor {
                    coeffs: std::array::from_fn(|k| {
                        if k == 0 {
                            self % rhs.coeffs[0]
                        } else {
                            -q * rhs.coeffs[k]
                        }
                    }),
                }
            }
        }
    };
}

impl_taylor_scalar_ops!(f32);
impl_taylor_scalar_ops!(f64);

impl<F: Float, const K: usize> PartialEq for Taylor<F, K> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.coeffs[0] == other.coeffs[0]
    }
}

impl<F: Float, const K: usize> PartialOrd for Taylor<F, K> {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.coeffs[0].partial_cmp(&other.coeffs[0])
    }
}

// ══════════════════════════════════════════════
//  TaylorDyn<F> operators
// ══════════════════════════════════════════════

use crate::taylor_dyn::{TaylorArenaLocal, TaylorDyn};

impl<F: Float + TaylorArenaLocal> Add for TaylorDyn<F> {
    type Output = Self;
    #[inline]
    fn add(self, rhs: Self) -> Self {
        TaylorDyn::binary_op(&self, &rhs, |a, b, c| taylor_ops::taylor_add(a, b, c))
    }
}

impl<F: Float + TaylorArenaLocal> Sub for TaylorDyn<F> {
    type Output = Self;
    #[inline]
    fn sub(self, rhs: Self) -> Self {
        TaylorDyn::binary_op(&self, &rhs, |a, b, c| taylor_ops::taylor_sub(a, b, c))
    }
}

// Truncated Taylor series multiplication uses the Cauchy product, which accumulates
// terms via addition — clippy flags the + inside a Mul impl, but this is correct for
// power-series coefficient propagation.
#[allow(clippy::suspicious_arithmetic_impl)]
impl<F: Float + TaylorArenaLocal> Mul for TaylorDyn<F> {
    type Output = Self;
    #[inline]
    fn mul(self, rhs: Self) -> Self {
        TaylorDyn::binary_op(&self, &rhs, |a, b, c| taylor_ops::taylor_mul(a, b, c))
    }
}

// Truncated Taylor series division computes coefficients via recurrence that uses
// multiplication internally — clippy flags the * inside a Div impl, but this is correct
// for power-series coefficient propagation.
#[allow(clippy::suspicious_arithmetic_impl)]
impl<F: Float + TaylorArenaLocal> Div for TaylorDyn<F> {
    type Output = Self;
    #[inline]
    fn div(self, rhs: Self) -> Self {
        TaylorDyn::binary_op(&self, &rhs, |a, b, c| taylor_ops::taylor_div(a, b, c))
    }
}

impl<F: Float + TaylorArenaLocal> Neg for TaylorDyn<F> {
    type Output = Self;
    #[inline]
    fn neg(self) -> Self {
        TaylorDyn::unary_op(&self, |a, c| taylor_ops::taylor_neg(a, c))
    }
}

impl<F: Float + TaylorArenaLocal> Rem for TaylorDyn<F> {
    type Output = Self;
    #[inline]
    fn rem(self, rhs: Self) -> Self {
        TaylorDyn::binary_op(&self, &rhs, |a, b, c| {
            // Mirror the `Taylor::rem` zero-divisor guard — without it the
            // k=0 slot holds `a[0] % 0 = NaN` while higher-order slots
            // compute from `(a[0]/0).trunc() = Inf` → `a[k] - b[k]*Inf`,
            // producing a mixed finite/NaN/Inf series that looks internally
            // inconsistent to downstream consumers.
            if b[0] == F::zero() {
                for ci in c.iter_mut() {
                    *ci = F::nan();
                }
                return;
            }
            c[0] = a[0] % b[0];
            let q = (a[0] / b[0]).trunc();
            for k in 1..c.len() {
                c[k] = a[k] - b[k] * q;
            }
        })
    }
}

impl<F: Float + TaylorArenaLocal> AddAssign for TaylorDyn<F> {
    #[inline]
    fn add_assign(&mut self, rhs: Self) {
        *self = *self + rhs;
    }
}

impl<F: Float + TaylorArenaLocal> SubAssign for TaylorDyn<F> {
    #[inline]
    fn sub_assign(&mut self, rhs: Self) {
        *self = *self - rhs;
    }
}

impl<F: Float + TaylorArenaLocal> MulAssign for TaylorDyn<F> {
    #[inline]
    fn mul_assign(&mut self, rhs: Self) {
        *self = *self * rhs;
    }
}

impl<F: Float + TaylorArenaLocal> DivAssign for TaylorDyn<F> {
    #[inline]
    fn div_assign(&mut self, rhs: Self) {
        *self = *self / rhs;
    }
}

impl<F: Float + TaylorArenaLocal> RemAssign for TaylorDyn<F> {
    #[inline]
    fn rem_assign(&mut self, rhs: Self) {
        *self = *self % rhs;
    }
}

// Mixed ops: TaylorDyn<F> with primitive floats.
macro_rules! impl_taylor_dyn_scalar_ops {
    ($f:ty) => {
        impl Add<$f> for TaylorDyn<$f> {
            type Output = TaylorDyn<$f>;
            #[inline]
            fn add(self, rhs: $f) -> TaylorDyn<$f> {
                self + TaylorDyn::constant(rhs)
            }
        }

        impl Add<TaylorDyn<$f>> for $f {
            type Output = TaylorDyn<$f>;
            #[inline]
            fn add(self, rhs: TaylorDyn<$f>) -> TaylorDyn<$f> {
                TaylorDyn::constant(self) + rhs
            }
        }

        impl Sub<$f> for TaylorDyn<$f> {
            type Output = TaylorDyn<$f>;
            #[inline]
            fn sub(self, rhs: $f) -> TaylorDyn<$f> {
                self - TaylorDyn::constant(rhs)
            }
        }

        impl Sub<TaylorDyn<$f>> for $f {
            type Output = TaylorDyn<$f>;
            #[inline]
            fn sub(self, rhs: TaylorDyn<$f>) -> TaylorDyn<$f> {
                TaylorDyn::constant(self) - rhs
            }
        }

        impl Mul<$f> for TaylorDyn<$f> {
            type Output = TaylorDyn<$f>;
            #[inline]
            fn mul(self, rhs: $f) -> TaylorDyn<$f> {
                self * TaylorDyn::constant(rhs)
            }
        }

        impl Mul<TaylorDyn<$f>> for $f {
            type Output = TaylorDyn<$f>;
            #[inline]
            fn mul(self, rhs: TaylorDyn<$f>) -> TaylorDyn<$f> {
                TaylorDyn::constant(self) * rhs
            }
        }

        impl Div<$f> for TaylorDyn<$f> {
            type Output = TaylorDyn<$f>;
            #[inline]
            fn div(self, rhs: $f) -> TaylorDyn<$f> {
                self / TaylorDyn::constant(rhs)
            }
        }

        impl Div<TaylorDyn<$f>> for $f {
            type Output = TaylorDyn<$f>;
            #[inline]
            fn div(self, rhs: TaylorDyn<$f>) -> TaylorDyn<$f> {
                TaylorDyn::constant(self) / rhs
            }
        }

        impl Rem<$f> for TaylorDyn<$f> {
            type Output = TaylorDyn<$f>;
            #[inline]
            fn rem(self, rhs: $f) -> TaylorDyn<$f> {
                self % TaylorDyn::constant(rhs)
            }
        }

        impl Rem<TaylorDyn<$f>> for $f {
            type Output = TaylorDyn<$f>;
            #[inline]
            fn rem(self, rhs: TaylorDyn<$f>) -> TaylorDyn<$f> {
                TaylorDyn::constant(self) % rhs
            }
        }
    };
}

impl_taylor_dyn_scalar_ops!(f32);
impl_taylor_dyn_scalar_ops!(f64);

impl<F: Float> PartialEq for TaylorDyn<F> {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.value == other.value
    }
}

impl<F: Float> PartialOrd for TaylorDyn<F> {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.value.partial_cmp(&other.value)
    }
}