rateslib/dual/
mod.rs

1//! Toolset for forward mode automatic differentiation (AD).
2//!
3//! # AD Architecture
4//!
5//! The entire *rateslib* library is built around three core numeric types: [f64],
6//! [Dual] and [Dual2]. Obviously [f64] allows for traditional computation, which benefits
7//! from efficient calculation leveraging BLAS, while [Dual] and [Dual2] reduce performance
8//! of traditional calculation but provide efficient calculation of first order and second order
9//! derivatives, respectively. Derivatives are calculated using forward mode AD,
10//! similar, but not identical, to the
11//! [Julia ForwardDiff library](https://github.com/JuliaDiff/ForwardDiff.jl).
12//!
13//! Mathematical operations are defined to give dual numbers the ability to combine, and
14//! flexibly reference different variables at any point during calculations.
15//!
16
17pub mod docs;
18
19mod dual;
20pub use crate::dual::dual::{
21    set_order, set_order_clone, Dual, Dual2, Gradient1, Gradient2, MathFuncs, NumberOps, Vars,
22    VarsRelationship,
23};
24
25mod dual_ops;
26pub(crate) mod dual_py;
27
28pub mod linalg;
29pub(crate) mod linalg_py;
30
31mod enums;
32pub use crate::dual::enums::{
33    ADOrder, Number, NumberArray1, NumberArray2, NumberMapping, NumberPPSpline, NumberVec,
34};
35
36/// Utility for creating an ordered list of variable tags from a string and enumerator
37pub(crate) fn get_variable_tags(name: &str, range: usize) -> Vec<String> {
38    Vec::from_iter((0..range).map(|i| name.to_string() + &i.to_string()))
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn test_get_variable_tags() {
47        let result = get_variable_tags("x", 3);
48        assert_eq!(
49            result,
50            vec!["x0".to_string(), "x1".to_string(), "x2".to_string()]
51        )
52    }
53}