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
/// Get a function that returns the derivative of the provided univariate, vector-valued function.
///
/// The derivative is computed using forward-mode automatic differentiation.
///
/// # Arguments
///
/// * `f` - Univariate, vector-valued function, $\mathbb{f}:\mathbb{R}\to\mathbb{R}^{m}$.
/// * `func_name` - Name of the function that will return the derivative of $\mathbf{f}(x)$ at any
/// point $x\in\mathbb{R}$.
/// * `param_type` (optional) - Type of the extra runtime parameter `p` that is passed to `f`.
/// Defaults to `[f64]` (implying that `f` accepts `p: &[f64]`).
///
/// # Warning
///
/// `f` cannot be defined as closure. It must be defined as a function.
///
/// # Note
///
/// The function produced by this macro will perform 1 evaluation of $\mathbf{f}(x)$ to evaluate its
/// derivative.
///
/// # Examples
///
/// ## Basic Example
///
/// Compute the derivative of
///
/// $$f(t)=\begin{bmatrix}\sin{t}\\\\\cos{t}\end{bmatrix}$$
///
/// at $t=1$, and compare the result to the true result of
///
/// $$\frac{d\mathbf{f}}{dt}\bigg\rvert_{t=1}=\begin{bmatrix}\cos{(1)}\\\\-\sin{(1)}\end{bmatrix}$$
///
/// #### Using standard vectors
///
/// ```
/// use linalg_traits::{Scalar, Vector};
/// use numtest::*;
///
/// use numdiff::{get_vderivative, Dual};
///
/// // Define the function, f(t).
/// fn f<S: Scalar, V: Vector<S>>(t: S, _p: &[f64]) -> V {
/// V::from_slice(&[t.sin(), t.cos()])
/// }
///
/// // Autogenerate the function "df" that can be used to compute the derivative of f(t) at any
/// // point t.
/// get_vderivative!(f, df);
///
/// // Compute the derivative of f(t) at the evaluation point, t = 1.
/// let df_at_1 = df::<f64, Vec<f64>>(1.0, &[]);
///
/// // True derivative of f(t) at the evaluation point.
/// let df_at_1_true: Vec<f64> = vec![1.0_f64.cos(), -1.0_f64.sin()];
///
/// // Check the accuracy of the derivative.
/// assert_arrays_equal_to_decimal!(df_at_1, df_at_1_true, 16);
/// ```
///
/// #### Using other vector types
///
/// The function produced by `get_vderivative!` can accept _any_ type for `x0`, as long as it
/// implements the `linalg_traits::Vector` trait.
///
/// ```
/// use faer::Mat;
/// use linalg_traits::{Scalar, Vector};
/// use nalgebra::{dvector, DVector, SVector};
/// use ndarray::{array, Array1};
/// use numtest::*;
///
/// use numdiff::{get_vderivative, Dual};
///
/// // Define the function, f(t).
/// fn f<S: Scalar, V: Vector<S>>(t: S, _p: &[f64]) -> V {
/// V::from_slice(&[t.sin(), t.cos()])
/// }
///
/// // Autogenerate the function "df" that can be used to compute the derivative of f(t) at any
/// // point t.
/// get_vderivative!(f, df);
///
/// // True derivative of f(t) at the evaluation point.
/// let df_at_1_true: Vec<f64> = vec![1.0_f64.cos(), -1.0_f64.sin()];
///
/// // nalgebra::DVector
/// let df_at_1_dvector: DVector<f64> = df::<f64, DVector<f64>>(1.0, &[]);
/// assert_arrays_equal_to_decimal!(df_at_1_dvector, df_at_1_true, 16);
///
/// // nalgebra::SVector
/// let df_at_1_svector: SVector<f64, 2> = df::<f64, SVector<f64, 2>>(1.0, &[]);
/// assert_arrays_equal_to_decimal!(df_at_1_svector, df_at_1_true, 16);
///
/// // ndarray::Array1
/// let df_at_1_array1: Array1<f64> = df::<f64, Array1<f64>>(1.0, &[]);
/// assert_arrays_equal_to_decimal!(df_at_1_array1, df_at_1_true, 16);
///
/// // faer::Mat
/// let df_at_1_mat: Mat<f64> = df::<f64, Mat<f64>>(1.0, &[]);
/// assert_arrays_equal_to_decimal!(df_at_1_mat.as_slice(), df_at_1_true, 16);
/// ```
///
/// ## Example Passing Runtime Parameters
///
/// Compute the derivative of a parameterized vector function
///
/// $$f(t)=\begin{bmatrix}at^{2}+b\\\\ce^{t}+d\end{bmatrix}$$
///
/// where $a$, $b$, $c$, and $d$ are runtime parameters. Compare the result against the true
/// derivative of
///
/// $$f'(t)=\begin{bmatrix}2at\\\\ce^{t}\end{bmatrix}$$
///
/// ```
/// use linalg_traits::{Scalar, Vector};
/// use numtest::*;
///
/// use numdiff::{get_vderivative, Dual};
///
/// // Define the function, f(x).
/// fn f<S: Scalar, V: Vector<S>>(t: S, p: &[f64]) -> V {
/// let a = S::new(p[0]);
/// let b = S::new(p[1]);
/// let c = S::new(p[2]);
/// let d = S::new(p[3]);
/// V::from_slice(&[a * t.powi(2) + b, c * t.exp() + d])
/// }
///
/// // Parameter vector.
/// let a = 1.5;
/// let b = -2.0;
/// let c = 0.8;
/// let d = 3.0;
/// let p = [a, b, c, d];
///
/// // Autogenerate the derivative function.
/// get_vderivative!(f, df);
///
/// // True derivative function.
/// let df_true = |t: f64| vec![2.0 * a * t, c * t.exp()];
///
/// // Compute the derivative at t = 1.0 using both the automatically generated derivative function
/// // and the true derivative function, and compare the results.
/// let df_at_1: Vec<f64> = df::<f64, Vec<f64>>(1.0, &p);
/// let df_at_1_true: Vec<f64> = df_true(1.0);
/// assert_arrays_equal_to_decimal!(df_at_1, df_at_1_true, 15);
/// ```
///
/// ## Example Passing Custom Parameter Types
///
/// Use a custom parameter struct instead of `f64` values.
///
/// ```
/// use linalg_traits::{Scalar, Vector};
/// use numtest::*;
///
/// use numdiff::{get_vderivative, Dual};
///
/// struct Data {
/// a: f64,
/// b: f64,
/// c: f64,
/// d: f64,
/// }
///
/// // Define the function, f(t).
/// fn f<S: Scalar, V: Vector<S>>(t: S, p: &Data) -> V {
/// let a = S::new(p.a);
/// let b = S::new(p.b);
/// let c = S::new(p.c);
/// let d = S::new(p.d);
/// V::from_slice(&[a * t.powi(2) + b, c * t.exp() + d])
/// }
///
/// // Runtime parameter struct.
/// let p = Data {
/// a: 1.5,
/// b: -2.0,
/// c: 0.8,
/// d: 3.0,
/// };
///
/// // Autogenerate the derivative function, telling the macro to expect a runtime parameter of type
/// // &Data.
/// get_vderivative!(f, df, Data);
///
/// // True derivative function.
/// let df_true = |t: f64| vec![2.0 * p.a * t, p.c * t.exp()];
///
/// // Compute the derivative at t = 1.0 using both the automatically generated derivative function
/// //and the true derivative function, and compare the results.
/// let df_at_1: Vec<f64> = df::<f64, Vec<f64>>(1.0, &p);
/// let df_at_1_true: Vec<f64> = df_true(1.0);
/// assert_arrays_equal_to_decimal!(df_at_1, df_at_1_true, 15);
/// ```