Dual2#

class rateslib.dual.Dual2(real, vars, dual, dual2)#

Dual number data type to perform first derivative automatic differentiation.

Parameters:
  • real (float, int) – The real coefficient of the dual number

  • vars (tuple of 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 of float) – First derivative information contained as coefficient of linear manifold. Defaults to an array of ones the length of vars if empty.

  • dual2 (list of float) – Second derivative information contained as coefficients of quadratic manifold. Defaults to a 2d array of zeros the size of vars if empty. These values represent a 2d array but must be given as a 1d list of values in row-major order.

Attributes

Variables:
  • real – float

  • vars – sequence of str

  • dual – 1d ndarray

  • dual2 – 2d ndarray

See also

Dual: Dual number data type to perform first derivative automatic differentiation.

Examples

In [1]: from rateslib.dual import Dual2, gradient

In [2]: def func(x, y):
   ...:     return 5 * x**2 + 10 * y**3
   ...: 

In [3]: x = Dual2(1.0, ["x"], [], [])

In [4]: y = Dual2(1.0, ["y"], [], [])

In [5]: gradient(func(x,y), ["x", "y"], order=2)
Out[5]: 
array([[10.,  0.],
       [ 0., 60.]])

Methods Summary

ptr_eq(other)

Evaluate if the ARC pointers of two Dual2 data types are equivalent.

to_json()

Create a JSON string representation of the object.

vars_from(other, real, vars, dual, dual2)

Create a Dual2 object with vars linked with another.

Methods Documentation

ptr_eq(other)#

Evaluate if the ARC pointers of two Dual2 data types are equivalent. See ptr_eq().

to_json()#

Create a JSON string representation of the object.

Return type:

str

static vars_from(other, real, vars, dual, dual2)#

Create a 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.

Return type:

Dual2

Notes

For examples see also…

See also

vars_from(): Create a Dual with vars linked to another.