pub struct Dual2 { /* private fields */ }
Expand description
A dual number data type supporting second order derivatives.
Implementations§
Source§impl Dual2
impl Dual2
Sourcepub fn new(real: f64, vars: Vec<String>) -> Self
pub fn new(real: f64, vars: Vec<String>) -> Self
Constructs a new Dual2
.
vars
should be unique; duplicates will be removed by theIndexSet
.
Gradient values for each of the provided vars
is set to 1.0_f64.
Second order gradient values for each combination of provided vars
is set
to 0.0_f64.
§Examples
let x = Dual2::new(2.5, vec!["x".to_string()]);
// x: <Dual2: 2.5, (x), [1.0], [[0.0]]>
Sourcepub fn try_new(
real: f64,
vars: Vec<String>,
dual: Vec<f64>,
dual2: Vec<f64>,
) -> Result<Self, PyErr>
pub fn try_new( real: f64, vars: Vec<String>, dual: Vec<f64>, dual2: Vec<f64>, ) -> Result<Self, PyErr>
Constructs a new Dual2
.
vars
should be unique; duplicates will be removed by theIndexSet
.dual
can be empty; if so each gradient with respect to eachvars
is set to 1.0_f64.dual2
can be empty; if so each gradient with respect to eachvars
is set to 0.0_f64. Input as a flattened 2d-array in row major order.
§Errors
If the length of dual
and of vars
are not the same after parsing.
If the shape of two dimension dual2
does not match vars
after parsing.
§Examples
let x = Dual2::try_new(2.5, vec!["x".to_string()], vec![], vec![]).unwrap();
// x: <Dual2: 2.5, (x), [1.0], [[0.0]]>
Sourcepub fn new_from<T: Vars>(other: &T, real: f64, vars: Vec<String>) -> Self
pub fn new_from<T: Vars>(other: &T, real: f64, vars: Vec<String>) -> Self
Construct a new Dual2
cloning the vars
Arc pointer from another.
§Examples
let x = Dual2::try_new(2.5, vec!["x".to_string(), "y".to_string()], vec![1.0, 0.0], vec![]).unwrap();
let y1 = Dual2::new_from(&x, 1.5, vec!["y".to_string()]);
This is semantically the same as:
let y = Dual2::new(1.5, vec!["y".to_string()]).to_new_vars(x.vars(), None);
Sourcepub fn try_new_from<T: Vars>(
other: &T,
real: f64,
vars: Vec<String>,
dual: Vec<f64>,
dual2: Vec<f64>,
) -> Result<Self, PyErr>
pub fn try_new_from<T: Vars>( other: &T, real: f64, vars: Vec<String>, dual: Vec<f64>, dual2: Vec<f64>, ) -> Result<Self, PyErr>
Construct a new Dual2
cloning the vars
Arc pointer from another.
§Examples
let x = Dual2::try_new(2.5, vec!["x".to_string(), "y".to_string()], vec![1.0, 0.0], vec![]).unwrap();
let y1 = Dual2::new_from(&x, 1.5, vec!["y".to_string()]);
This is semantically the same as:
let y2 = Dual2::new(1.5, vec!["y".to_string()]).to_new_vars(x.vars(), None);
assert_eq!(y1, y2);
Source§impl Dual2
impl Dual2
Sourcepub fn new_py(
real: f64,
vars: Vec<String>,
dual: Vec<f64>,
dual2: Vec<f64>,
) -> PyResult<Self>
pub fn new_py( real: f64, vars: Vec<String>, dual: Vec<f64>, dual2: Vec<f64>, ) -> PyResult<Self>
Python wrapper to construct a new Dual2
.
Sourcepub fn vars_from(
other: &Dual2,
real: f64,
vars: Vec<String>,
dual: Vec<f64>,
dual2: Vec<f64>,
) -> PyResult<Self>
pub fn vars_from( other: &Dual2, real: f64, vars: Vec<String>, dual: Vec<f64>, dual2: Vec<f64>, ) -> PyResult<Self>
Create a :class:~rateslib.dual.Dual2
object with vars
linked with another.
§Parameters
other: Dual
The other Dual
from which vars
are linked.
real: float
The real coefficient of the dual number.
vars: list(str)
The labels of the variables for which to record derivatives. If empty,
the dual number represents a constant, equivalent to a float.
dual: list(float)
First derivative information contained as coefficient of linear manifold.
Defaults to an array of ones the length of vars
if empty.
dual2: list(float)
Second derivative information contained as coefficients of a quadratic manifold.
These values represent a 2d array but must be given as a 1d list of values in
row-major order.
Defaults to a 2-d array of zeros of size NxN where N is length of vars
if not
given.
§Returns
Dual2
§Notes
For examples see also…
.. seealso::
:meth:~rateslib.dual.Dual.vars_from
: Create a Dual with vars
linked to another.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Dual2
impl<'de> Deserialize<'de> for Dual2
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Element for Dual2
impl Element for Dual2
Source§const IS_COPY: bool = false
const IS_COPY: bool = false
Source§fn get_dtype(py: Python<'_>) -> Bound<'_, PyArrayDescr>
fn get_dtype(py: Python<'_>) -> Bound<'_, PyArrayDescr>
Source§fn clone_ref(&self, _py: Python<'_>) -> Self
fn clone_ref(&self, _py: Python<'_>) -> Self
§fn vec_from_slice(py: Python<'_>, slc: &[Self]) -> Vec<Self>
fn vec_from_slice(py: Python<'_>, slc: &[Self]) -> Vec<Self>
Source§impl Gradient2 for Dual2
impl Gradient2 for Dual2
Source§fn dual2(&self) -> &Array2<f64>
fn dual2(&self) -> &Array2<f64>
Source§fn gradient2(&self, vars: Vec<String>) -> Array2<f64>
fn gradient2(&self, vars: Vec<String>) -> Array2<f64>
fn gradient1_manifold(&self, vars: Vec<String>) -> Array1<Dual2>
Source§impl<'py> IntoPyObject<'py> for Dual2
impl<'py> IntoPyObject<'py> for Dual2
Source§impl MathFuncs for Dual2
impl MathFuncs for Dual2
Source§impl Num for Dual2
impl Num for Dual2
type FromStrRadixErr = String
Source§fn from_str_radix(
_src: &str,
_radix: u32,
) -> Result<Self, Self::FromStrRadixErr>
fn from_str_radix( _src: &str, _radix: u32, ) -> Result<Self, Self::FromStrRadixErr>
2..=36
). Read moreSource§impl PartialOrd<Dual2> for f64
impl PartialOrd<Dual2> for f64
Source§impl PartialOrd<f64> for Dual2
impl PartialOrd<f64> for Dual2
Source§impl PartialOrd for Dual2
impl PartialOrd for Dual2
Source§impl PyClassImpl for Dual2
impl PyClassImpl for Dual2
Source§const IS_BASETYPE: bool = false
const IS_BASETYPE: bool = false
Source§const IS_SUBCLASS: bool = false
const IS_SUBCLASS: bool = false
Source§const IS_MAPPING: bool = false
const IS_MAPPING: bool = false
Source§const IS_SEQUENCE: bool = false
const IS_SEQUENCE: bool = false
Source§const IS_IMMUTABLE_TYPE: bool = false
const IS_IMMUTABLE_TYPE: bool = false
Source§type ThreadChecker = SendablePyClass<Dual2>
type ThreadChecker = SendablePyClass<Dual2>
Source§type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::MutableChild
type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::MutableChild
Source§type BaseNativeType = PyAny
type BaseNativeType = PyAny
PyAny
by default, and when you declare
#[pyclass(extends=PyDict)]
, it’s PyDict
.fn items_iter() -> PyClassItemsIter
fn lazy_type_object() -> &'static LazyTypeObject<Self>
fn dict_offset() -> Option<isize>
fn weaklist_offset() -> Option<isize>
Source§impl PyClassNewTextSignature<Dual2> for PyClassImplCollector<Dual2>
impl PyClassNewTextSignature<Dual2> for PyClassImplCollector<Dual2>
fn new_text_signature(self) -> Option<&'static str>
Source§impl PyMethods<Dual2> for PyClassImplCollector<Dual2>
impl PyMethods<Dual2> for PyClassImplCollector<Dual2>
fn py_methods(self) -> &'static PyClassItems
Source§impl PyTypeInfo for Dual2
impl PyTypeInfo for Dual2
Source§fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
§fn type_object(py: Python<'_>) -> Bound<'_, PyType>
fn type_object(py: Python<'_>) -> Bound<'_, PyType>
§fn is_type_of(object: &Bound<'_, PyAny>) -> bool
fn is_type_of(object: &Bound<'_, PyAny>) -> bool
object
is an instance of this type or a subclass of this type.§fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool
fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool
object
is an instance of this type.Source§impl Signed for Dual2
impl Signed for Dual2
Source§fn is_positive(&self) -> bool
fn is_positive(&self) -> bool
Source§fn is_negative(&self) -> bool
fn is_negative(&self) -> bool
Source§impl Vars for Dual2
impl Vars for Dual2
Source§fn vars(&self) -> &Arc<IndexSet<String>>
fn vars(&self) -> &Arc<IndexSet<String>>
Get a reference to the Arc pointer for the IndexSet
containing the struct’s variables.
Source§fn to_new_vars(
&self,
arc_vars: &Arc<IndexSet<String>>,
state: Option<VarsRelationship>,
) -> Self
fn to_new_vars( &self, arc_vars: &Arc<IndexSet<String>>, state: Option<VarsRelationship>, ) -> Self
Construct a new Dual2
with vars
set as the given Arc pointer and gradients shuffled in memory.
Examples
let x = Dual2::new(1.5, vec!["x".to_string()]);
let xy = Dual2::new(2.5, vec!["x".to_string(), "y".to_string()]);
let x_y = x.to_new_vars(xy.vars(), None);
// x_y: <Dual2: 1.5, (x, y), [1.0, 0.0], [[0.0, 0.0], [0.0, 0.0]]>
assert_eq!(x_y, Dual2::try_new(1.5, vec!["x".to_string(), "y".to_string()], vec![1.0, 0.0], vec![]).unwrap());
Source§fn vars_cmp(&self, arc_vars: &Arc<IndexSet<String>>) -> VarsRelationship
fn vars_cmp(&self, arc_vars: &Arc<IndexSet<String>>) -> VarsRelationship
vars
on a Dual
with a given Arc pointer.Source§fn to_union_vars(
&self,
other: &Self,
state: Option<VarsRelationship>,
) -> (Self, Self)where
Self: Sized,
fn to_union_vars(
&self,
other: &Self,
state: Option<VarsRelationship>,
) -> (Self, Self)where
Self: Sized,
Source§fn to_combined_vars(&self, other: &Self) -> (Self, Self)where
Self: Sized,
fn to_combined_vars(&self, other: &Self) -> (Self, Self)where
Self: Sized,
Self
types whose vars
are linked by the explicit union
of their own variables. Read moreimpl DerefToPyAny for Dual2
impl NumberOps<Dual2> for Dual2
Auto Trait Implementations§
impl Freeze for Dual2
impl RefUnwindSafe for Dual2
impl Send for Dual2
impl Sync for Dual2
impl Unpin for Dual2
impl UnwindSafe for Dual2
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> FromPyObject<'_> for Twhere
T: PyClass + Clone,
impl<T> FromPyObject<'_> for Twhere
T: PyClass + Clone,
§fn extract_bound(obj: &Bound<'_, PyAny>) -> Result<T, PyErr>
fn extract_bound(obj: &Bound<'_, PyAny>) -> Result<T, PyErr>
§impl<'py, T> FromPyObjectBound<'_, 'py> for Twhere
T: FromPyObject<'py>,
impl<'py, T> FromPyObjectBound<'_, 'py> for Twhere
T: FromPyObject<'py>,
§fn from_py_object_bound(ob: Borrowed<'_, 'py, PyAny>) -> Result<T, PyErr>
fn from_py_object_bound(ob: Borrowed<'_, 'py, PyAny>) -> Result<T, PyErr>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more§impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
§fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>
fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>
self
into an owned Python object, dropping type information.§fn into_py_any(self, py: Python<'py>) -> Result<Py<PyAny>, PyErr>
fn into_py_any(self, py: Python<'py>) -> Result<Py<PyAny>, PyErr>
self
into an owned Python object, dropping type information and unbinding it
from the 'py
lifetime.§fn into_pyobject_or_pyerr(self, py: Python<'py>) -> Result<Self::Output, PyErr>
fn into_pyobject_or_pyerr(self, py: Python<'py>) -> Result<Self::Output, PyErr>
self
into a Python object. Read more§impl<T> PyErrArguments for T
impl<T> PyErrArguments for T
§impl<T> PyTypeCheck for Twhere
T: PyTypeInfo,
impl<T> PyTypeCheck for Twhere
T: PyTypeInfo,
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.