numdiff/automatic_differentiation/partial_derivative/scalar_valued.rs
1/// Get a function that returns the partial derivative of the provided multivariate, scalar-valued
2/// function.
3///
4/// The partial derivative is computed using forward-mode automatic differentiation.
5///
6/// # Arguments
7///
8/// * `f` - Multivariate, scalar-valued function, $f:\mathbb{R}^{n}\to\mathbb{R}$.
9/// * `func_name` - Name of the function that will return the partial derivative of $f(\mathbf{x})$
10/// with respect to $x_{k}$ at any point $\mathbf{x}\in\mathbb{R}^{n}$.
11/// * `param_type` (optional) - Type of the extra runtime parameter `p` that is passed to `f`.
12/// Defaults to `[f64]` (implying that `f` accepts `p: &[f64]`).
13///
14/// # Warning
15///
16/// `f` cannot be defined as closure. It must be defined as a function.
17///
18/// # Note
19///
20/// The function produced by this macro will perform 1 evaluation of $f(\mathbf{x})$ to evaluate its
21/// partial derivative with respect to $x_{k}$.
22///
23/// # Examples
24///
25/// ## Basic Example
26///
27/// Compute the partial derivative of
28///
29/// $$f(x)=x^{3}\sin{y}$$
30///
31/// with respect to $y$ at $(x,y)=(5,1)$, and compare the result to the true result of
32///
33/// $$\frac{\partial f}{\partial y}\bigg\rvert_{(x,y)=(5,1)}=5^{3}\cos{(1)}$$
34///
35/// First, note that we can rewrite this function as
36///
37/// $$f(\mathbf{x})=x_{0}^{3}\sin{x_{1}}$$
38///
39/// where $\mathbf{x}=(x_{0},x_{1})^{T}$ (note that we use 0-based indexing to aid with the
40/// computational implementation). We are then trying to find
41///
42/// $$\frac{\partial f}{\partial x_{1}}\bigg\rvert_{\mathbf{x}=\mathbf{x}_{0}}$$
43///
44/// where $\mathbf{x}_{0}=(5,1)^{T}$.
45///
46/// #### Using standard vectors
47///
48/// ```
49/// use linalg_traits::{Scalar, Vector};
50///
51/// use numdiff::{get_spartial_derivative, Dual, DualVector};
52///
53/// // Define the function, f(x).
54/// fn f<S: Scalar, V: Vector<S>>(x: &V, _p: &[f64]) -> S {
55/// x[0].powi(3) * x[1].sin()
56/// }
57///
58/// // Define the evaluation point.
59/// let x0 = vec![5.0, 1.0];
60///
61/// // Define the element of the vector (using 0-based indexing) we are differentiating with respect
62/// // to.
63/// let k = 1;
64///
65/// // Autogenerate the function "dfk" that can be used to compute the partial derivative of f(x)
66/// // with respect to xₖ at any point x.
67/// get_spartial_derivative!(f, dfk);
68///
69/// // Verify that the partial derivative function obtained using get_spartial_derivative! computes
70/// // the partial derivative correctly.
71/// assert_eq!(dfk(&x0, k, &[]), 5.0_f64.powi(3) * 1.0_f64.cos());
72/// ```
73///
74/// #### Using other vector types
75///
76/// The function produced by `get_spartial_derivative!` can accept _any_ type for `x0`, as long as
77/// it implements the `linalg_traits::Vector` trait.
78///
79/// ```
80/// use faer::Col;
81/// use linalg_traits::{Scalar, Vector};
82/// use nalgebra::{dvector, DVector, SVector};
83/// use ndarray::{array, Array1};
84///
85/// use numdiff::{get_spartial_derivative, Dual, DualVector};
86///
87/// // Define the function, f(x).
88/// fn f<S: Scalar, V: Vector<S>>(x: &V, _p: &[f64]) -> S {
89/// x[0].powi(3) * x[1].sin()
90/// }
91///
92/// // Define the element of the vector (using 0-based indexing) we are differentiating with respect
93/// // to.
94/// let k = 1;
95///
96/// // Autogenerate the function "dfk" that can be used to compute the partial derivative of f(x)
97/// // with respect to xₖ at any point x.
98/// get_spartial_derivative!(f, dfk);
99///
100/// // nalgebra::DVector
101/// let x0: DVector<f64> = dvector![5.0, 1.0];
102/// let dfk_eval: f64 = dfk(&x0, k, &[]);
103///
104/// // nalgebra::SVector
105/// let x0: SVector<f64, 2> = SVector::from_slice(&[5.0, 1.0]);
106/// let dfk_eval: f64 = dfk(&x0, k, &[]);
107///
108/// // ndarray::Array1
109/// let x0: Array1<f64> = array![5.0, 1.0];
110/// let dfk_eval: f64 = dfk(&x0, k, &[]);
111///
112/// // faer::Col
113/// let x0: Col<f64> = Col::from_slice(&[5.0, 1.0]);
114/// let dfk_eval: f64 = dfk(&x0, k, &[]);
115/// ```
116///
117/// ## Example Passing Runtime Parameters
118///
119/// Compute the partial derivative of a parameterized function
120///
121/// $$f(\mathbf{x})=ax_{0}^{2}+bx_{1}^{2}+cx_{0}x_{1}+d\sin(ex_{0})$$
122///
123/// where $a$, $b$, $c$, $d$, and $e$ are runtime parameters. The partial derivatives are:
124///
125/// * $\dfrac{\partial f}{\partial x_{0}}=2ax_{0}+cx_{1}+de\cos(ex_{0})$
126/// * $\dfrac{\partial f}{\partial x_{1}}=2bx_{1}+cx_{0}$
127///
128/// ```
129/// use linalg_traits::{Scalar, Vector};
130/// use numtest::*;
131///
132/// use numdiff::{get_spartial_derivative, Dual, DualVector};
133///
134/// // Define the function, f(x).
135/// fn f<S: Scalar, V: Vector<S>>(x: &V, p: &[f64]) -> S {
136/// let a = S::new(p[0]);
137/// let b = S::new(p[1]);
138/// let c = S::new(p[2]);
139/// let d = S::new(p[3]);
140/// let e = S::new(p[4]);
141/// a * x[0].powi(2)
142/// + b * x[1].powi(2)
143/// + c * x[0] * x[1]
144/// + d * (e * x[0]).sin()
145/// }
146///
147/// // Define individual parameters.
148/// let a = 1.5;
149/// let b = 2.0;
150/// let c = 0.8;
151/// let d = 3.0;
152/// let e = 0.5;
153///
154/// // Parameter vector.
155/// let p = [a, b, c, d, e];
156///
157/// // Evaluation point.
158/// let x0 = vec![1.0, -0.5];
159///
160/// // Autogenerate the partial derivative function.
161/// get_spartial_derivative!(f, dfk);
162///
163/// // True partial derivative functions.
164/// let df_dx0_true = |x: &[f64]| 2.0 * a * x[0] + c * x[1] + d * e * (e * x[0]).cos();
165/// let df_dx1_true = |x: &[f64]| 2.0 * b * x[1] + c * x[0];
166///
167/// // Compute ∂f/∂x₀ at x₀ and compare with true function.
168/// let df_dx0: f64 = dfk(&x0, 0, &p);
169/// let expected_df_dx0 = df_dx0_true(&x0);
170/// assert_equal_to_decimal!(df_dx0, expected_df_dx0, 14);
171///
172/// // Compute ∂f/∂x₁ at x0 and compare with true function.
173/// let df_dx1: f64 = dfk(&x0, 1, &p);
174/// let expected_df_dx1 = df_dx1_true(&x0);
175/// assert_equal_to_decimal!(df_dx1, expected_df_dx1, 15);
176/// ```
177///
178/// ## Example Passing Custom Parameter Types
179///
180/// Use a custom parameter struct instead of `f64` values.
181///
182/// ```
183/// use linalg_traits::{Scalar, Vector};
184/// use numtest::*;
185///
186/// use numdiff::{get_spartial_derivative, Dual, DualVector};
187///
188/// struct Data {
189/// a: f64,
190/// b: f64,
191/// c: f64,
192/// d: f64,
193/// e: f64,
194/// }
195///
196/// // Define the function, f(x).
197/// fn f<S: Scalar, V: Vector<S>>(x: &V, p: &Data) -> S {
198/// let a = S::new(p.a);
199/// let b = S::new(p.b);
200/// let c = S::new(p.c);
201/// let d = S::new(p.d);
202/// let e = S::new(p.e);
203/// a * x[0].powi(2)
204/// + b * x[1].powi(2)
205/// + c * x[0] * x[1]
206/// + d * (e * x[0]).sin()
207/// }
208///
209/// // Runtime parameter struct.
210/// let p = Data {
211/// a: 1.5,
212/// b: 2.0,
213/// c: 0.8,
214/// d: 3.0,
215/// e: 0.5,
216/// };
217///
218/// // Evaluation point.
219/// let x0 = vec![1.0, -0.5];
220///
221/// // Autogenerate the partial derivative function, telling the macro to expect a runtime parameter
222/// // of type &Data.
223/// get_spartial_derivative!(f, dfk, Data);
224///
225/// // True partial derivative functions.
226/// let df_dx0_true = |x: &[f64]| {
227/// 2.0 * p.a * x[0] + p.c * x[1] + p.d * p.e * (p.e * x[0]).cos()
228/// };
229/// let df_dx1_true = |x: &[f64]| 2.0 * p.b * x[1] + p.c * x[0];
230///
231/// // Compute the partial derivatives using both the automatically generated partial derivative
232/// // function and the true partial derivative functions, and compare the results.
233/// let df_dx0: f64 = dfk(&x0, 0, &p);
234/// let df_dx1: f64 = dfk(&x0, 1, &p);
235/// assert_equal_to_decimal!(df_dx0, df_dx0_true(&x0), 14);
236/// assert_equal_to_decimal!(df_dx1, df_dx1_true(&x0), 15);
237/// ```
238#[macro_export]
239macro_rules! get_spartial_derivative {
240 ($f:ident, $func_name:ident) => {
241 get_spartial_derivative!($f, $func_name, [f64]);
242 };
243 ($f:ident, $func_name:ident, $param_type:ty) => {
244 /// Partial derivative of a multivariate, scalar-valued function `f: ℝⁿ → ℝ`.
245 ///
246 /// This function is generated for a specific function `f` using the
247 /// `numdiff::get_spartial_derivative!` macro.
248 ///
249 /// # Arguments
250 ///
251 /// * `x0` - Evaluation point, `x₀ ∈ ℝⁿ`.
252 /// * `k` - Element of `x` to differentiate with respect to. Note that this uses 0-based
253 /// indexing (e.g. `x = (x₀,...,xₖ,...,xₙ₋₁)ᵀ`).
254 /// * `p` - Extra runtime parameter. This is a parameter (can be of any arbitrary type)
255 /// defined at runtime that the function may depend on but is not differentiated with
256 /// respect to.
257 ///
258 /// # Returns
259 ///
260 /// Partial derivative of `f` with respect to `xₖ`, evaluated at `x = x₀`.
261 ///
262 /// `(∂f/∂xₖ)|ₓ₌ₓ₀ ∈ ℝ`
263 fn $func_name<S, V>(x0: &V, k: usize, p: &$param_type) -> f64
264 where
265 S: Scalar,
266 V: Vector<S>,
267 {
268 // Promote the evaluation point to a vector of dual numbers.
269 let mut x0_dual = x0.clone().to_dual_vector();
270
271 // Take a unit step forward in the kth dual direction.
272 let original = x0_dual[k];
273 x0_dual[k] = Dual::new(original.get_real(), 1.0);
274
275 // Evaluate the function at the dual number.
276 let f_x0 = $f(&x0_dual, p);
277
278 // Partial derivative of f with respect to xₖ.
279 f_x0.get_dual()
280 }
281 };
282}
283
284#[cfg(test)]
285mod tests {
286 use crate::{Dual, DualVector};
287 use linalg_traits::{Scalar, Vector};
288 use nalgebra::SVector;
289 use numtest::*;
290
291 #[test]
292 fn test_spartial_derivative_1() {
293 // Function to take the partial derivative of.
294 fn f<S: Scalar, V: Vector<S>>(x: &V, _p: &[f64]) -> S {
295 x[0].powi(2)
296 }
297
298 // Evaluation point.
299 let x0 = vec![2.0];
300
301 // Element to differentiate with respect to.
302 let k = 0;
303
304 // True partial derivative function.
305 let dfk = |x: &Vec<f64>| 2.0 * x[0];
306
307 // Partial derivative function obtained via forward-mode automatic differentiation.
308 get_spartial_derivative!(f, dfk_autodiff);
309
310 // Evaluate the partial derivative using both functions.
311 let dfk_eval_autodiff: f64 = dfk_autodiff(&x0, k, &[]);
312 let dfk_eval: f64 = dfk(&x0);
313
314 // Test autodiff partial derivative against true partial derivative.
315 assert_eq!(dfk_eval_autodiff, dfk_eval);
316 }
317
318 #[test]
319 fn test_spartial_derivative_2() {
320 // Function to take the partial derivative of.
321 fn f<S: Scalar, V: Vector<S>>(x: &V, _p: &[f64]) -> S {
322 x[0].powi(3) * x[1].powi(3)
323 }
324
325 // Evaluation point.
326 let x0: SVector<f64, 2> = SVector::from_slice(&[3.0, 2.0]);
327
328 // Element to differentiate with respect to.
329 let k = 1;
330
331 // True partial derivative function.
332 let dfk = |x: &SVector<f64, 2>| 3.0 * x[0].powi(3) * x[1].powi(2);
333
334 // Partial derivative function obtained via forward-mode automatic differentiation.
335 get_spartial_derivative!(f, dfk_autodiff);
336
337 // Evaluate the partial derivative using both functions.
338 let dfk_eval_autodiff: f64 = dfk_autodiff(&x0, k, &[]);
339 let dfk_eval: f64 = dfk(&x0);
340
341 // Test autodiff partial derivative against true partial derivative.
342 assert_eq!(dfk_eval_autodiff, dfk_eval);
343 }
344
345 #[test]
346 fn test_spartial_derivative_3() {
347 // Function to take the partial derivative of.
348 #[allow(clippy::many_single_char_names)]
349 fn f<S: Scalar, V: Vector<S>>(x: &V, p: &[f64]) -> S {
350 let a = S::new(p[0]);
351 let b = S::new(p[1]);
352 let c = S::new(p[2]);
353 a * (b * x[0]).exp() + c * x[1].powi(3)
354 }
355
356 // Parameter vector.
357 let p = [2.5, 0.3, -1.2];
358
359 // Evaluation point.
360 let x0: SVector<f64, 2> = SVector::from_slice(&[1.5, 2.0]);
361
362 // Element to differentiate with respect to.
363 let k = 0;
364
365 // True partial derivative function.
366 let dfk = |x: &SVector<f64, 2>, p: &[f64]| p[0] * p[1] * (p[1] * x[0]).exp();
367
368 // Partial derivative function obtained via forward-mode automatic differentiation.
369 get_spartial_derivative!(f, dfk_autodiff);
370
371 // Evaluate the partial derivative using both functions.
372 let dfk_eval_autodiff: f64 = dfk_autodiff(&x0, k, &p);
373 let dfk_eval: f64 = dfk(&x0, &p);
374
375 // Test autodiff partial derivative against true partial derivative.
376 assert_eq!(dfk_eval_autodiff, dfk_eval);
377 }
378
379 #[test]
380 fn test_spartial_derivative_custom_params() {
381 struct Data {
382 a: f64,
383 b: f64,
384 c: f64,
385 d: f64,
386 e: f64,
387 }
388
389 // Function to take the partial derivative of.
390 #[allow(clippy::many_single_char_names)]
391 fn f<S: Scalar, V: Vector<S>>(x: &V, p: &Data) -> S {
392 let a = S::new(p.a);
393 let b = S::new(p.b);
394 let c = S::new(p.c);
395 let d = S::new(p.d);
396 let e = S::new(p.e);
397 a * x[0].powi(2) + b * x[1].powi(2) + c * x[0] * x[1] + d * (e * x[0]).sin()
398 }
399
400 // Runtime parameter struct.
401 let p = Data {
402 a: 1.5,
403 b: 2.0,
404 c: 0.8,
405 d: 3.0,
406 e: 0.5,
407 };
408
409 // Evaluation point.
410 let x0 = vec![1.0, -0.5];
411
412 // Partial derivative function obtained via forward-mode automatic differentiation.
413 get_spartial_derivative!(f, dfk, Data);
414
415 // True partial derivative functions.
416 let df_dx0_true =
417 |x: &[f64]| 2.0 * p.a * x[0] + p.c * x[1] + p.d * p.e * (p.e * x[0]).cos();
418 let df_dx1_true = |x: &[f64]| 2.0 * p.b * x[1] + p.c * x[0];
419
420 // Evaluate the partial derivatives using both functions.
421 let df_dx0: f64 = dfk(&x0, 0, &p);
422 let df_dx1: f64 = dfk(&x0, 1, &p);
423
424 // Test autodiff partial derivatives against true partial derivatives.
425 assert_equal_to_decimal!(df_dx0, df_dx0_true(&x0), 14);
426 assert_equal_to_decimal!(df_dx1, df_dx1_true(&x0), 15);
427 }
428}