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
/*
* SPDX-License-Identifier: MIT
* Copyright (c) 2023 - 2026. The DeepCausality Authors and Contributors. All Rights Reserved.
*/
use crateReal;
/// A dual number `a + b·ε` where the infinitesimal `ε` satisfies `ε² = 0`.
///
/// Dual numbers are the type-based primitive for **forward-mode automatic
/// differentiation**. Evaluating any function composed from the arithmetic and
/// elementary operations on `Dual::variable(x0)` (which is `x0 + 1·ε`) yields
/// `f(x0)` in the real part and `f'(x0)` in the `ε` part, exact to machine
/// precision — the derivative falls out of the trait impls by the chain rule.
///
/// `Dual<T>` is built over `T: Real` (the analytic real-scalar trait), not
/// `RealField`: a dual's component needs the elementary functions but never a field
/// inverse. `Dual<T>` itself implements [`Real`](crate::Real) — so a dual is a
/// first-class analytic scalar that drops into any `Real`-generic code and **nests**
/// (`Dual<Dual<T>>` gives second derivatives) — but it does **not** implement
/// [`Field`](crate::Field)/[`RealField`](crate::RealField), because `ε` is a zero
/// divisor (`ε·ε = 0`) and has no multiplicative inverse.
///
/// # Examples
///
/// ```
/// use deep_causality_num::Dual;
///
/// // f(x) = x³ + 2x, evaluated with its derivative at x = 3.
/// let x = Dual::variable(3.0_f64);
/// let y = x * x * x + x + x;
/// assert_eq!(y.value(), 27.0 + 6.0); // 3³ + 2·3 = 33
/// assert_eq!(y.derivative(), 27.0 + 2.0); // 3·3² + 2 = 29
/// ```