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
//! The `Real` trait — the unified *active*-scalar trait that abstracts
//! over every AD mode this crate ships (reverse via [`crate::AReal`],
//! forward first-order via [`crate::Jet1`], forward second-order via
//! [`crate::Jet2`]) plus the no-AD case (plain [`f64`]).
//!
//! Conceptually, `Real` is the seam that makes one body of numerical
//! code (a pricer, a loss, an ODE step) run under every AD mode without
//! duplication: the trait abstracts the *active scalar* and the chain
//! rule lives inside its operator impls.
//!
//! See also (theory): [`docs/theory/01-automatic-differentiation.md`](https://github.com/sercanatalik/xad-rs/blob/main/docs/theory/01-automatic-differentiation.md).
//!
//! Program your numerical code against `R: Real` once; instantiate
//! against the concrete mode that matches the problem shape at the call
//! site.
//!
//! # Why `Real`?
//!
//! Aligns with the QuantLib convention (`QuantLib::Real`,
//! `QuantLibAAD::Real`, `QuantLibAdjoint::Real`), where the same name
//! denotes the user-facing scalar type regardless of whether the build
//! is plain double or AD-enabled. Same idea here: code written against
//! `Real` reads the same whether it ultimately runs over `f64`,
//! `AReal<f64>`, `Jet1<f64>`, or `Jet2<f64>`.
//!
//! The complementary trait [`crate::Passive`] is the bound on the
//! *underlying* (storage) scalar — typically `f64`. The
//! `Real::Passive` associated type ties an active type back to the
//! passive type it wraps.
//!
//! # In-crate impls (v0.5.0)
//!
//! - `impl Real for f64` (this file) — the no-AD case; methods
//! delegate to inherent `f64::*`.
//! - `impl Real for AReal<f64>` (in `src/reverse/areal.rs`) —
//! delegates to `math::ad::*` so operations record on the active tape.
//! - `impl Real for Jet1<f64>` (in `src/forward/jet1.rs`) — delegates
//! to `math::fwd::*` so the tangent propagates correctly.
//! - `impl Real for Jet2<f64>` (in `src/forward/jet2.rs`) — delegates
//! to inherent `Jet2<T>::*` methods.
//!
//! Not in v0.5.0: `Jet1Vec`/`Jet2Vec` (length-of-partial-vector
//! incompatibility with the `Default` supertrait bound) and the f32
//! flavours (`From<f64>` is lossy for f32). Tracked as follow-up
//! changes.
use cratePassive;
use ;
use ;
/// Unified active-scalar trait — the seam between mode-agnostic
/// numerical code and the concrete AD type it eventually runs over.
///
/// See the module-level docs for the alignment with QuantLib's `Real`
/// and the rationale for not depending on [`num_traits::Float`].
// ----------------------------------------------------------------------------
// impl Real for f64 — the no-AD blanket so generic code over `R: Real`
// compiles for plain `f64` without special-casing.
// ----------------------------------------------------------------------------