rateslib/dual/
enums.rs

1use crate::dual::{Dual, Dual2};
2use crate::splines::{PPSplineDual, PPSplineDual2, PPSplineF64};
3use ndarray::{Array1, Array2};
4use pyo3::{pyclass, FromPyObject, IntoPyObject, PyErr};
5use serde::{Deserialize, Serialize};
6
7/// Defines the order of gradients available in a calculation with AD.
8#[pyclass(module = "rateslib.rs", eq, eq_int)]
9#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
10pub enum ADOrder {
11    /// Floating point arithmetic only.
12    Zero,
13    /// Derivatives available to first order.
14    One,
15    /// Derivatives available to second order.
16    Two,
17}
18
19/// Container for the three core numeric types; [f64], [Dual] and [Dual2].
20#[derive(Debug, Clone, FromPyObject, Serialize, Deserialize, IntoPyObject)]
21pub enum Number {
22    Dual(Dual),
23    Dual2(Dual2),
24    F64(f64),
25}
26
27/// Container for [Vec] of each core numeric type.
28#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
29pub enum NumberVec {
30    F64(Vec<f64>),
31    Dual(Vec<Dual>),
32    Dual2(Vec<Dual2>),
33}
34
35/// Container for [Array1] of each core numeric type.
36#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
37pub enum NumberArray1 {
38    F64(Array1<f64>),
39    Dual(Array1<Dual>),
40    Dual2(Array1<Dual2>),
41}
42
43/// Container for [Array2] of each core numeric type.
44#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
45pub enum NumberArray2 {
46    F64(Array2<f64>),
47    Dual(Array2<Dual>),
48    Dual2(Array2<Dual2>),
49}
50
51/// Container for [PPSpline](crate::splines::PPSpline) definitive type variants.
52#[derive(Clone, Serialize, Deserialize, PartialEq, IntoPyObject)]
53pub enum NumberPPSpline {
54    F64(PPSplineF64),
55    Dual(PPSplineDual),
56    Dual2(PPSplineDual2),
57}
58
59/// Generic trait indicating a function exists to map one [Number] to another.
60///
61/// An example of this trait is used by certain [PPSpline](crate::splines::PPSpline) indicating
62/// that an x-value as
63/// some [Number] can be mapped under spline interpolation to some y-value as another [Number].
64pub trait NumberMapping {
65    fn mapped_value(&self, x: &Number) -> Result<Number, PyErr>;
66}