CyanDiff package — CyanDiff 0.1 documentation

# CyanDiff package

## Submodules

## CyanDiff.ad_helpers module

## CyanDiff.ad_overloads module

## CyanDiff.ad_types module

Class definitions for types used in Automatic Differentiation (AD).

In this module two classes are implemented `DualNumber` and `Function`. The `DualNumber` type represents dual numbers, which are a type of number with a real part and a dual part defined as a coefficient multiplied by a number \(\epsilon\). We have that \(\epsilon^2=0\). See `DualNumber` documentation for further explanation of how this is used in forward mode AD.

The `Function` class is used to build the complex functions which the user inputs for differentiation out of basic operators and functions. Functions can be combined using binary operators, which allows the user to define more complex functions with more parts. This class also implements the actual calculation of the Jacobian using forward mode AD.

class CyanDiff.ad_types.DualNumber(real, dual=1.0)

  Bases: `object`

  Implements dual numbers for AD, and the basic operators associated.

  In the CyanDiff AD implementation, we use dual numbers to implement AD due to their convenient properties. In particular, we have that we can use the real part of the dual number to track the primal trace of the function, while the dual part tracks the tangent trace. In particular, we have the nice property that this is preserved across basic operations and function on dual numbers, making dual numbers particularly useful for forward mode AD. In this class, we implement dual numbers and their associated operators: addition, multiplication, subtraction, division, and exponentiation. We also implement operations with int and float types so that operations between dual numbers and real number types is also supported.

  Parameters

    - real (`int` or `float`) – the value of the real part of the dual number

    - dual (`int` or `float`) – the coefficient of the dual part of the dual number, with default value 1.0.

  Example

      >>> z1 = DualNumber(2,1)
      >>> z2 = DualNumber(1,2)
      >>> z3 = z1 + z2
      >>> z4 = z1 + 2
      >>> print(z3.real)
      3
      >>> print(z3.dual)
      3
      >>> print(z4.real)
      3
      >>> print(z4.dual)
      1

  valid_types = (<class 'int'>, <class 'float'>)
  __init__(real, dual=1.0)

    Constructor method, defaults to dual part to be 1.0.

  __add__(other)

    Addition operator for two dual numbers.

    If second input is an ‘int’ or ‘float’, add the value to the real part of the dual number.

  __radd__(other)

    Reverse addition for dual numbers.

  __sub__(other)

    Subtract second dual number from first

    If second input is an ‘int’ or ‘float’, subtract the value from the real part of the dual number.

  __rsub__(other)

    Reverse addition for dual numbers.

  __mul__(other)

    Multiplication operator for two dual numbers.

    If the second input is an ‘int’ or ‘float’, multiply the value with the real and dual parts of the dual number.

  __rmul__(other)

    Reverse multiplication for dual numbers.

  __truediv__(other)

    Divide first dual number by second.

    If the second input is an ‘int’ or ‘float’, multiply the real and dual parts of the dual number by the value.

  __rtruediv__(other)

    Reverse division for dual numbers.

  __pow__(other)

    Raise first dual number to the power of the second

  __rpow__(other)
  __neg__()

    Negate a dual number

  __module__ = 'CyanDiff.ad_types'

class CyanDiff.ad_types.Function(evaluator=None)

  Bases: `object`

  Defines function objects on which differentiation is performed.

  The user defines variables (see `make_vars()` documentation under the `ad_helpers` module), which then can be combined into functions using math operations (basic operators defined in this class) and basic functions that we define (please see `ad_overloads` module for more information regarding functions). The `Function` objects that we define can then be differentiated using `Function.diff_at()` and :`Function.jacobian_at()` for single-variate and multi-variate functions respectively in order to obtain the derivative at a particular point.

  Parameters

    evaluator (`function`) – underlying function that evaluates the object at a particular value.

  valid_types = (<class 'int'>, <class 'float'>, <class 'CyanDiff.ad_types.DualNumber'>)
  __init__(evaluator=None)

    Constructor method.

  __call__(value_assignment)

    Evaluates the function at a given value

  diff_at(value_assignment)

    Evaluates the derivative of single-variate function at a point (scalar).

    Parameters

      value_assignment (`int`) – point at which derivative is evaluated

  jacobian_at(variable_order, value_assignment)
  __add__(other)
  __radd__(other)
  __sub__(other)
  __rsub__(other)
  __mul__(other)
  __rmul__(other)
  __truediv__(other)
  __rtruediv__(other)
  __pow__(other)
  __rpow__(other)
  __neg__()
  __module__ = 'CyanDiff.ad_types'

class CyanDiff.ad_types.VectorFunction(functions)

  Bases: `object`

  __init__(functions)
  __call__(value_assignment)

    Call self as a function.

  __module__ = 'CyanDiff.ad_types'
  jacobian_at(variable_order, value_assignment)

## Module contents