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
//! Comprehensive functional automatic differentiation API for scirs2-autograd
//!
//! This module provides a self-contained, graph-free automatic differentiation
//! API built on three complementary foundations:
//!
//! | Submodule | Mechanism | Best for |
//! |-----------|-----------|----------|
//! | [`dual`] | Dual-number forward-mode AD | Exact derivatives, small n |
//! | [`tape`] | Wengert-list reverse-mode AD | Gradient of scalar losses |
//! | [`transforms`] | JAX-style functional transforms (FD) | Composable closures |
//! | [`higher_order`] | Higher-order derivatives and specialised operators | Hessians, Laplacians, Taylor series |
//! | [`legacy`] | Original point-wise API returning `Result` | Backward compat |
//!
//! # Quick Start
//!
//! ## Gradient via transform closure
//!
//! ```rust
//! use scirs2_autograd::functional::transforms::grad;
//!
//! let grad_f = grad(|xs: &[f64]| xs[0].powi(2) + xs[1].powi(2));
//! let g = grad_f(&[3.0, 4.0]);
//! assert!((g[0] - 6.0).abs() < 1e-3);
//! assert!((g[1] - 8.0).abs() < 1e-3);
//! ```
//!
//! ## Gradient via dual numbers (exact)
//!
//! ```rust
//! use scirs2_autograd::functional::dual::{Dual, eval_gradient};
//!
//! let (val, grad) = eval_gradient(
//! |xs: &[scirs2_autograd::functional::dual::Dual]| xs[0] * xs[0] + xs[1] * xs[1],
//! &[3.0, 4.0],
//! );
//! assert!((val - 25.0).abs() < 1e-12);
//! assert!((grad[0] - 6.0).abs() < 1e-12);
//! ```
//!
//! ## Gradient via Wengert tape (reverse-mode)
//!
//! ```rust
//! use scirs2_autograd::functional::tape::tape_grad;
//!
//! let g = tape_grad(
//! |tape, xs| {
//! let x2 = tape.powi(xs[0], 2);
//! let y2 = tape.powi(xs[1], 2);
//! tape.add(x2, y2)
//! },
//! &[3.0, 4.0],
//! ).expect("tape gradient");
//! assert!((g[0] - 6.0).abs() < 1e-12);
//! assert!((g[1] - 8.0).abs() < 1e-12);
//! ```
//!
//! ## JVP and VJP
//!
//! ```rust
//! use scirs2_autograd::functional::transforms::{jvp, vjp};
//!
//! let f = |xs: &[f64]| vec![xs[0]*xs[0], xs[0]*xs[1]];
//!
//! // Forward-mode JVP
//! let (fx, jvp_val) = jvp(f, &[2.0, 3.0], &[1.0, 0.0]);
//! assert!((jvp_val[0] - 4.0).abs() < 1e-4); // d(x^2)/dx * 1 = 4
//!
//! // Reverse-mode VJP
//! let f2 = |xs: &[f64]| vec![xs[0]*xs[0], xs[0]*xs[1]];
//! let (_, g) = vjp(f2, &[2.0, 3.0], &[1.0, 0.0]);
//! assert!((g[0] - 4.0).abs() < 1e-4);
//! ```
//!
//! ## Hessian
//!
//! ```rust
//! use scirs2_autograd::functional::transforms::hessian;
//!
//! let hess = hessian(|xs: &[f64]| xs[0]*xs[0] + 3.0*xs[0]*xs[1] + 2.0*xs[1]*xs[1]);
//! let h = hess(&[1.0, 1.0]);
//! assert!((h[0][0] - 2.0).abs() < 1e-2);
//! assert!((h[0][1] - 3.0).abs() < 1e-2);
//! ```
//!
//! ## Laplacian
//!
//! ```rust
//! use scirs2_autograd::functional::higher_order::laplacian;
//!
//! // f = x^2 + y^2 + z^2; Δf = 6
//! let lap = laplacian(
//! |xs: &[f64]| xs.iter().map(|v| v*v).sum::<f64>(),
//! &[1.0, 2.0, 3.0],
//! ).expect("laplacian");
//! assert!((lap - 6.0).abs() < 0.5);
//! ```
//!
//! # Design Notes
//!
//! ## Three AD backends
//!
//! This module deliberately provides **three distinct backends** for gradient
//! computation, each with different trade-offs:
//!
//! 1. **Dual numbers** (`dual` module): Forward-mode AD with *exact* chain-rule
//! propagation. Cost: `n` forward passes for the full gradient, one pass per
//! partial. Best for small `n` where exactness matters (e.g. inside numerical
//! solvers, verification tests).
//!
//! 2. **Wengert tape** (`tape` module): Reverse-mode AD that records the forward
//! computation and replays it backwards. Cost: one forward pass + one backward
//! sweep (O(n) in graph size). Best for large `n` (many inputs, one output
//! loss), as in machine learning.
//!
//! 3. **Finite differences** (`transforms` module): Numerical approximation via
//! central differences. Cost: `2n` evaluations per gradient; `O(n²)` for
//! Hessian. Best for *any* function (no instrumentation required) when `n`
//! is small and accuracy requirements are modest.
//!
//! ## JAX compatibility
//!
//! The function signatures in [`transforms`] deliberately match JAX conventions:
//!
//! | JAX | scirs2 |
//! |-----|--------|
//! | `jax.grad(f)` | `transforms::grad(f)` |
//! | `jax.jacobian(f)` | `transforms::jacobian(f, m)` |
//! | `jax.hessian(f)` | `transforms::hessian(f)` |
//! | `jax.vmap(f)` | `transforms::vmap(f)` |
//! | `jax.jvp(f, x, v)` | `transforms::jvp(f, x, v)` |
//! | `jax.vjp(f, x)` | `transforms::vjp(f, x, cotangent)` |
// New advanced submodules
// Backward-compatible legacy API (preserved from functional.rs)
// ============================================================================
// Convenience re-exports — new advanced API
// ============================================================================
// Dual number types and helpers
pub use ;
// Tape-based reverse-mode AD
pub use ;
// Functional transforms (JAX-style)
pub use ;
// Higher-order derivatives
pub use ;
// ============================================================================
// Module-level tests (integration tests spanning multiple submodules)
// ============================================================================