frechet 0.1.0

basic autodifferentiation
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
//! `frechet` is a library providing simple dual number types with interfaces
//! similar to that of the standard floating point types. Additionally, provides
//! a small interface to abstract the computation of derivatives.
//!
//! # Refresher on dual numbers
//!
//! A dual number $z$ is represented as the sum of a "real part" $x$ and an "imaginary part" $y$,
//! and written $z = x + yj$, where $j$ is purely symbolic and follows the rule $j^2 = 0$.
//! As an example, evaluating the polynomial $P(X)=3X^2-X+1$ at the dual number $X + j$ yields
//! $$P(X+j)=3X^2-X+1 + (6X-1)j = P(X) + P^\prime(X)j.$$
//! This motivates the following extension of any (differentiable) real function $f$ to dual numbers:
//! $$f(x+yj) = f(x) + yf^\prime(x)j.$$
//!
//! # Example
//! ```
//! use frechet::*;
//!
//! fn p(x: dual32) -> dual32 { x.powf(2.5).atanh() + 1.0  }
//! fn p_derivative(x: f32) -> f32 { -2.5 * x.powf(1.5)/(x.powi(5) - 1.0) }
//!
//! // using the `derivative` function
//! let z1 = derivative(p, 2.0);
//!
//! // manually
//! let z2 = p(2.0.as_dual_variable()).d;
//!
//! // exact derivative
//! let z3 = p_derivative(2.0);
//!
//! assert!((z1 - z3).abs() < f32::EPSILON);
//! assert!((z2 - z3).abs() < f32::EPSILON);
//! ```

/// Type representing a simple 32-bit precision dual number.
#[allow(non_camel_case_types)]
#[derive(Clone, Copy, Debug)]
pub struct dual32 {
    pub x: f32,
    pub d: f32,
}

impl dual32 {
    pub const ZERO: dual32 = dual32 { x: 0.0, d: 0.0 };
    pub const ONE: dual32 = dual32 { x: 1.0, d: 0.0 };

    /// The unit "imaginary" dual number. Satisfies  `J*J == ZERO`.
    pub const J: dual32 = dual32 { x: 0.0, d: 1.0 };

    #[inline]
    pub const fn new(x: f32, d: f32) -> dual32 {
        Self { x, d }
    }
}

impl core::ops::Add<dual32> for dual32 {
    type Output = dual32;

    #[inline]
    fn add(self, rhs: dual32) -> Self::Output {
        Self::Output::new(self.x + rhs.x, self.d + rhs.d)
    }
}

impl core::ops::Add<f32> for dual32 {
    type Output = dual32;

    #[inline]
    fn add(self, rhs: f32) -> Self::Output {
        Self::Output::new(self.x + rhs, self.d)
    }
}

impl core::ops::Add<dual32> for f32 {
    type Output = dual32;

    #[inline]
    fn add(self, rhs: dual32) -> Self::Output {
        Self::Output::new(self + rhs.x, rhs.d)
    }
}

impl core::ops::Sub<dual32> for dual32 {
    type Output = dual32;

    #[inline]
    fn sub(self, rhs: dual32) -> Self::Output {
        Self::Output::new(self.x - rhs.x, self.d - rhs.d)
    }
}

impl core::ops::Sub<f32> for dual32 {
    type Output = dual32;

    #[inline]
    fn sub(self, rhs: f32) -> Self::Output {
        Self::Output::new(self.x - rhs, self.d)
    }
}

impl core::ops::Sub<dual32> for f32 {
    type Output = dual32;

    #[inline]
    fn sub(self, rhs: dual32) -> Self::Output {
        Self::Output::new(self - rhs.x, rhs.d)
    }
}

impl core::ops::Mul<dual32> for dual32 {
    type Output = dual32;

    #[inline]
    fn mul(self, rhs: dual32) -> Self::Output {
        Self::Output::new(self.x * rhs.x, self.x * rhs.d + self.d * rhs.x)
    }
}

impl core::ops::Mul<f32> for dual32 {
    type Output = dual32;

    #[inline]
    fn mul(self, rhs: f32) -> Self::Output {
        Self::Output::new(self.x * rhs, self.x * rhs)
    }
}

impl core::ops::Mul<dual32> for f32 {
    type Output = dual32;

    #[inline]
    fn mul(self, rhs: dual32) -> Self::Output {
        Self::Output::new(self * rhs.x, self * rhs.d)
    }
}

impl core::ops::Div<dual32> for dual32 {
    type Output = dual32;

    #[inline]
    fn div(self, rhs: dual32) -> Self::Output {
        Self::Output::new(
            self.x / rhs.x,
            (self.d * rhs.x - self.x * rhs.d) / (rhs.x * rhs.x),
        )
    }
}

impl core::ops::Div<f32> for dual32 {
    type Output = dual32;

    #[inline]
    fn div(self, rhs: f32) -> Self::Output {
        Self::Output::new(self.x / rhs, self.x / rhs)
    }
}

impl core::ops::Div<dual32> for f32 {
    type Output = dual32;

    #[inline]
    fn div(self, rhs: dual32) -> Self::Output {
        Self::Output::new(self / rhs.x, (-self * rhs.d) / (rhs.x * rhs.x))
    }
}

// Power functions
impl dual32 {
    #[inline]
    pub fn recip(self) -> dual32 {
        Self::new(self.x.recip(), -self.d * self.x.recip().powi(2))
    }

    #[inline]
    pub fn powf(self, n: f32) -> dual32 {
        Self::new(self.x.powf(n), self.d * n * self.x.powf(n - 1.0))
    }

    #[inline]
    pub fn powi(self, n: i32) -> dual32 {
        Self::new(self.x.powi(n), self.d * n as f32 * self.x.powi(n - 1))
    }

    #[inline]
    pub fn sqrt(self) -> dual32 {
        Self::new(self.x.sqrt(), self.d * 0.5 / self.x.sqrt())
    }

    #[inline]
    pub fn cbrt(self) -> dual32 {
        Self::new(self.x.cbrt(), self.d * (1.0 / 3.0) / self.x.cbrt().powi(2))
    }
}

// Exponentials and logarithms
impl dual32 {
    #[inline]
    pub fn exp(self) -> dual32 {
        Self::new(self.x.exp(), self.d * self.x.exp())
    }

    #[inline]
    pub fn exp2(self) -> dual32 {
        Self::new(
            self.x.exp2(),
            self.d * core::f32::consts::LN_2 * self.x.exp2(),
        )
    }

    #[inline]
    pub fn exp_m1(self) -> dual32 {
        Self::new(self.x.exp_m1(), self.d * self.x.exp())
    }

    #[inline]
    pub fn ln(self) -> dual32 {
        Self::new(self.x.ln(), self.d / self.x)
    }

    #[inline]
    pub fn ln_1p(self) -> dual32 {
        Self::new(self.x.ln_1p(), self.d / (1.0 + self.x))
    }

    #[inline]
    pub fn log(self, base: f32) -> dual32 {
        Self::new(self.x.log(base), self.d / (base.ln() * self.x))
    }

    #[inline]
    pub fn log10(self) -> dual32 {
        Self::new(self.x.log10(), self.d / (core::f32::consts::LN_10 * self.x))
    }

    #[inline]
    pub fn log2(self) -> dual32 {
        Self::new(self.x.log2(), self.d / (core::f32::consts::LN_2 * self.x))
    }
}

// Trigonometric functions
// atan2 ?
// sin_cos ?
impl dual32 {
    #[inline]
    pub fn cos(self) -> dual32 {
        Self::new(self.x.cos(), -self.d * self.x.sin())
    }

    #[inline]
    pub fn sin(self) -> dual32 {
        Self::new(self.x.sin(), self.d * self.x.cos())
    }

    #[inline]
    pub fn tan(self) -> dual32 {
        Self::new(self.x.tan(), self.d / self.x.cos().powi(2))
    }

    #[inline]
    pub fn acos(self) -> dual32 {
        Self::new(self.x.acos(), -self.d / (1.0 - self.x * self.x).sqrt())
    }

    #[inline]
    pub fn asin(self) -> dual32 {
        Self::new(self.x.asin(), self.d / (1.0 - self.x * self.x).sqrt())
    }

    #[inline]
    pub fn atan(self) -> dual32 {
        Self::new(self.x.tan(), self.d / (1.0 + self.x * self.x))
    }
}

// Hyperbolic functions
impl dual32 {
    #[inline]
    pub fn cosh(self) -> dual32 {
        Self::new(self.x.cosh(), self.d * self.x.sinh())
    }

    #[inline]
    pub fn sinh(self) -> dual32 {
        Self::new(self.x.sinh(), self.d * self.x.cosh())
    }

    #[inline]
    pub fn tanh(self) -> dual32 {
        Self::new(self.x.tanh(), self.d / self.x.cosh().powi(2))
    }

    #[inline]
    pub fn acosh(self) -> dual32 {
        Self::new(self.x.acosh(), self.d / (self.x * self.x - 1.0).sqrt())
    }

    #[inline]
    pub fn asinh(self) -> dual32 {
        Self::new(self.x.asinh(), self.d / (self.x * self.x + 1.0).sqrt())
    }

    #[inline]
    pub fn atanh(self) -> dual32 {
        Self::new(self.x.atanh(), self.d / (1.0 - self.x * self.x))
    }
}

// Continuous differentiable almost-everywhere functions
impl dual32 {
    #[inline]
    pub fn abs(self) -> dual32 {
        Self::new(self.x.abs(), self.d * self.x.signum())
    }

    #[inline]
    pub fn min(self, other: f32) -> dual32 {
        Self::new(
            self.x.min(other),
            if self.x < other { self.d * 1.0 } else { 0.0 },
        )
    }

    #[inline]
    pub fn max(self, other: f32) -> dual32 {
        Self::new(
            self.x.max(other),
            if other < self.x { self.d * 1.0 } else { 0.0 },
        )
    }

    #[inline]
    pub fn clamp(self, min: f32, max: f32) -> dual32 {
        Self::new(
            self.x.clamp(min, max),
            if min < self.x && self.x < max {
                self.d * 1.0
            } else {
                0.0
            },
        )
    }
}

/// Piece-wise constant functions
impl dual32 {
    #[inline]
    pub fn ceil(self) -> dual32 {
        Self::new(self.x.ceil(), 0.0)
    }

    #[inline]
    pub fn floor(self) -> dual32 {
        Self::new(self.x.floor(), 0.0)
    }

    // not constant but x.fract() == x - x.floor()
    #[inline]
    pub fn fract(self) -> dual32 {
        Self::new(self.x.fract(), self.d)
    }

    #[inline]
    pub fn round(self) -> dual32 {
        Self::new(self.x.round(), 0.0)
    }

    #[inline]
    pub fn signum(self) -> dual32 {
        Self::new(self.x.signum(), 0.0)
    }

    #[inline]
    pub fn trunc(self) -> dual32 {
        Self::new(self.x.trunc(), 0.0)
    }
}

/// Sporadic functions
impl dual32 {
    #[inline]
    pub fn mul_add(self, a: f32, b: f32) -> dual32 {
        Self::new(self.x.mul_add(a, b), self.d * a * self.x)
    }
}

/// Type representing a simple 64-bit precision dual number.
#[allow(non_camel_case_types)]
#[derive(Clone, Copy, Debug)]
pub struct dual64 {
    pub x: f64,
    pub d: f64,
}

impl dual64 {
    pub const ZERO: dual64 = dual64 { x: 0.0, d: 0.0 };
    pub const ONE: dual64 = dual64 { x: 1.0, d: 0.0 };

    /// The unit "imaginary" dual number. Satisfies  `J*J == ZERO`.
    pub const J: dual64 = dual64 { x: 0.0, d: 1.0 };

    #[inline]
    pub const fn new(x: f64, d: f64) -> dual64 {
        Self { x, d }
    }
}

impl core::ops::Add<dual64> for dual64 {
    type Output = dual64;

    #[inline]
    fn add(self, rhs: dual64) -> Self::Output {
        Self::Output::new(self.x + rhs.x, self.d + rhs.d)
    }
}

impl core::ops::Add<f64> for dual64 {
    type Output = dual64;

    #[inline]
    fn add(self, rhs: f64) -> Self::Output {
        Self::Output::new(self.x + rhs, self.d)
    }
}

impl core::ops::Add<dual64> for f64 {
    type Output = dual64;

    #[inline]
    fn add(self, rhs: dual64) -> Self::Output {
        Self::Output::new(self + rhs.x, rhs.d)
    }
}

impl core::ops::Sub<dual64> for dual64 {
    type Output = dual64;

    #[inline]
    fn sub(self, rhs: dual64) -> Self::Output {
        Self::Output::new(self.x - rhs.x, self.d - rhs.d)
    }
}

impl core::ops::Sub<f64> for dual64 {
    type Output = dual64;

    #[inline]
    fn sub(self, rhs: f64) -> Self::Output {
        Self::Output::new(self.x - rhs, self.d)
    }
}

impl core::ops::Sub<dual64> for f64 {
    type Output = dual64;

    #[inline]
    fn sub(self, rhs: dual64) -> Self::Output {
        Self::Output::new(self - rhs.x, rhs.d)
    }
}

impl core::ops::Mul<dual64> for dual64 {
    type Output = dual64;

    #[inline]
    fn mul(self, rhs: dual64) -> Self::Output {
        Self::Output::new(self.x * rhs.x, self.x * rhs.d + self.d * rhs.x)
    }
}

impl core::ops::Mul<f64> for dual64 {
    type Output = dual64;

    #[inline]
    fn mul(self, rhs: f64) -> Self::Output {
        Self::Output::new(self.x * rhs, self.x * rhs)
    }
}

impl core::ops::Mul<dual64> for f64 {
    type Output = dual64;

    #[inline]
    fn mul(self, rhs: dual64) -> Self::Output {
        Self::Output::new(self * rhs.x, self * rhs.d)
    }
}

impl core::ops::Div<dual64> for dual64 {
    type Output = dual64;

    #[inline]
    fn div(self, rhs: dual64) -> Self::Output {
        Self::Output::new(
            self.x / rhs.x,
            (self.d * rhs.x - self.x * rhs.d) / (rhs.x * rhs.x),
        )
    }
}

impl core::ops::Div<f64> for dual64 {
    type Output = dual64;

    #[inline]
    fn div(self, rhs: f64) -> Self::Output {
        Self::Output::new(self.x / rhs, self.x / rhs)
    }
}

impl core::ops::Div<dual64> for f64 {
    type Output = dual64;

    #[inline]
    fn div(self, rhs: dual64) -> Self::Output {
        Self::Output::new(self / rhs.x, (-self * rhs.d) / (rhs.x * rhs.x))
    }
}

// Power functions
impl dual64 {
    #[inline]
    pub fn recip(self) -> dual64 {
        Self::new(self.x.recip(), -self.d * self.x.recip().powi(2))
    }

    #[inline]
    pub fn powf(self, n: f64) -> dual64 {
        Self::new(self.x.powf(n), self.d * n * self.x.powf(n - 1.0))
    }

    #[inline]
    pub fn powi(self, n: i32) -> dual64 {
        Self::new(self.x.powi(n), self.d * n as f64 * self.x.powi(n - 1))
    }

    #[inline]
    pub fn sqrt(self) -> dual64 {
        Self::new(self.x.sqrt(), self.d * 0.5 / self.x.sqrt())
    }

    #[inline]
    pub fn cbrt(self) -> dual64 {
        Self::new(self.x.cbrt(), self.d * (1.0 / 3.0) / self.x.cbrt().powi(2))
    }
}

// Exponentials and logarithms
impl dual64 {
    #[inline]
    pub fn exp(self) -> dual64 {
        Self::new(self.x.exp(), self.d * self.x.exp())
    }

    #[inline]
    pub fn exp2(self) -> dual64 {
        Self::new(
            self.x.exp2(),
            self.d * core::f64::consts::LN_2 * self.x.exp2(),
        )
    }

    #[inline]
    pub fn exp_m1(self) -> dual64 {
        Self::new(self.x.exp_m1(), self.d * self.x.exp())
    }

    #[inline]
    pub fn ln(self) -> dual64 {
        Self::new(self.x.ln(), self.d / self.x)
    }

    #[inline]
    pub fn ln_1p(self) -> dual64 {
        Self::new(self.x.ln_1p(), self.d / (1.0 + self.x))
    }

    #[inline]
    pub fn log(self, base: f64) -> dual64 {
        Self::new(self.x.log(base), self.d / (base.ln() * self.x))
    }

    #[inline]
    pub fn log10(self) -> dual64 {
        Self::new(self.x.log10(), self.d / (core::f64::consts::LN_10 * self.x))
    }

    #[inline]
    pub fn log2(self) -> dual64 {
        Self::new(self.x.log2(), self.d / (core::f64::consts::LN_2 * self.x))
    }
}

// Trigonometric functions
// atan2 ?
// sin_cos ?
impl dual64 {
    #[inline]
    pub fn cos(self) -> dual64 {
        Self::new(self.x.cos(), -self.d * self.x.sin())
    }

    #[inline]
    pub fn sin(self) -> dual64 {
        Self::new(self.x.sin(), self.d * self.x.cos())
    }

    #[inline]
    pub fn tan(self) -> dual64 {
        Self::new(self.x.tan(), self.d / self.x.cos().powi(2))
    }

    #[inline]
    pub fn acos(self) -> dual64 {
        Self::new(self.x.acos(), -self.d / (1.0 - self.x * self.x).sqrt())
    }

    #[inline]
    pub fn asin(self) -> dual64 {
        Self::new(self.x.asin(), self.d / (1.0 - self.x * self.x).sqrt())
    }

    #[inline]
    pub fn atan(self) -> dual64 {
        Self::new(self.x.tan(), self.d / (1.0 + self.x * self.x))
    }
}

// Hyperbolic functions
impl dual64 {
    #[inline]
    pub fn cosh(self) -> dual64 {
        Self::new(self.x.cosh(), self.d * self.x.sinh())
    }

    #[inline]
    pub fn sinh(self) -> dual64 {
        Self::new(self.x.sinh(), self.d * self.x.cosh())
    }

    #[inline]
    pub fn tanh(self) -> dual64 {
        Self::new(self.x.tanh(), self.d / self.x.cosh().powi(2))
    }

    #[inline]
    pub fn acosh(self) -> dual64 {
        Self::new(self.x.acosh(), self.d / (self.x * self.x - 1.0).sqrt())
    }

    #[inline]
    pub fn asinh(self) -> dual64 {
        Self::new(self.x.asinh(), self.d / (self.x * self.x + 1.0).sqrt())
    }

    #[inline]
    pub fn atanh(self) -> dual64 {
        Self::new(self.x.atanh(), self.d / (1.0 - self.x * self.x))
    }
}

// Continuous differentiable almost-everywhere functions
impl dual64 {
    #[inline]
    pub fn abs(self) -> dual64 {
        Self::new(self.x.abs(), self.d * self.x.signum())
    }

    #[inline]
    pub fn min(self, other: f64) -> dual64 {
        Self::new(
            self.x.min(other),
            if self.x < other { self.d * 1.0 } else { 0.0 },
        )
    }

    #[inline]
    pub fn max(self, other: f64) -> dual64 {
        Self::new(
            self.x.max(other),
            if other < self.x { self.d * 1.0 } else { 0.0 },
        )
    }

    #[inline]
    pub fn clamp(self, min: f64, max: f64) -> dual64 {
        Self::new(
            self.x.clamp(min, max),
            if min < self.x && self.x < max {
                self.d * 1.0
            } else {
                0.0
            },
        )
    }
}

/// Piece-wise constant functions
impl dual64 {
    #[inline]
    pub fn ceil(self) -> dual64 {
        Self::new(self.x.ceil(), 0.0)
    }

    #[inline]
    pub fn floor(self) -> dual64 {
        Self::new(self.x.floor(), 0.0)
    }

    // not constant but x.fract() == x - x.floor()
    #[inline]
    pub fn fract(self) -> dual64 {
        Self::new(self.x.fract(), self.d)
    }

    #[inline]
    pub fn round(self) -> dual64 {
        Self::new(self.x.round(), 0.0)
    }

    #[inline]
    pub fn signum(self) -> dual64 {
        Self::new(self.x.signum(), 0.0)
    }

    #[inline]
    pub fn trunc(self) -> dual64 {
        Self::new(self.x.trunc(), 0.0)
    }
}

/// Sporadic functions
impl dual64 {
    #[inline]
    pub fn mul_add(self, a: f64, b: f64) -> dual64 {
        Self::new(self.x.mul_add(a, b), self.d * a * self.x)
    }
}

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

    #[test]
    fn identity32() {
        let dual32 { x, d } = dual32::J.powi(2);
        assert!(x.abs() < f32::EPSILON);
        assert!(d.abs() < f32::EPSILON);
    }

    #[test]
    fn identity64() {
        let dual64 { x, d } = dual64::J.powi(2);
        assert!(x.abs() < f64::EPSILON);
        assert!(d.abs() < f64::EPSILON);
    }

    #[test]
    fn polynomial32() {
        fn p(x: dual32) -> dual32 {
            4.0 * x * x - 3.0 * x + 3.0
        }
        let x = 3.0;
        let dual32 { x: px, d: pdx } = p(x + dual32::J);

        assert!((px - 30.0).abs() <= f32::EPSILON);
        assert!((pdx - 21.0).abs() <= f32::EPSILON);
    }

    #[test]
    fn polynomial64() {
        fn p(x: dual64) -> dual64 {
            4.0 * x * x - 3.0 * x + 3.0
        }
        let x = 3.0;
        let dual64 { x: px, d: pdx } = p(x + dual64::J);

        assert!((px - 30.0).abs() <= f64::EPSILON);
        assert!((pdx - 21.0).abs() <= f64::EPSILON);
    }
}

// --------------------

/// Trait providing generic getters for dual32 and dual64
pub trait Dual<F> {
    fn real(self) -> F;
    fn imag(self) -> F;
}

impl Dual<f32> for dual32 {
    fn real(self) -> f32 {
        self.x
    }

    fn imag(self) -> f32 {
        self.d
    }
}

impl Dual<f64> for dual64 {
    fn real(self) -> f64 {
        self.x
    }

    fn imag(self) -> f64 {
        self.d
    }
}

/// Trait used to convert a non-dual numeric type into a dual "variable",
/// i.e. a dual number with unit imaginary part.
pub trait AsDualVariable<Dual> {
    fn as_dual_variable(self) -> Dual;
    fn from_dual(z: Dual) -> Self;
}

impl AsDualVariable<dual32> for f32 {
    fn as_dual_variable(self) -> dual32 {
        dual32::new(self, 1.0)
    }

    fn from_dual(z: dual32) -> Self {
        z.x
    }
}

impl AsDualVariable<dual32> for dual32 {
    fn as_dual_variable(self) -> dual32 {
        self
    }

    fn from_dual(z: dual32) -> Self {
        z
    }
}

impl AsDualVariable<dual64> for f64 {
    fn as_dual_variable(self) -> dual64 {
        dual64::new(self, 1.0)
    }

    fn from_dual(z: dual64) -> Self {
        z.x
    }
}

impl AsDualVariable<dual64> for dual64 {
    fn as_dual_variable(self) -> dual64 {
        self
    }

    fn from_dual(z: dual64) -> Self {
        z
    }
}

/// Computes the derivative of a function `f` at a point `x`
pub fn derivative<F, D>(f: impl Fn(D) -> D, x: F) -> F
where
    D: Dual<F>,
    F: AsDualVariable<D>,
{
    f(x.as_dual_variable()).imag()
}