This module provides automatic differentiation for functions with any number of variables. Instances of the class DerivVar represent the values of a function and its partial derivatives with respect to a list of variables. All common mathematical operations and functions are available for these numbers. There is no restriction on the type of the numbers fed into the code; it works for real and complex numbers as well as for any Python type that implements the necessary operations.
This module is as far as possible compatible with the n-th order derivatives module Derivatives. If only first-order derivatives are required, this module is faster than the general one.
Example:print sin(DerivVar(2))
produces the output
(0.909297426826, [-0.416146836547])
The first number is the value of sin(2); the number in the following list is the value of the derivative of sin(x) at x=2, i.e. cos(2).
When there is more than one variable, DerivVar must be called with an integer second argument that specifies the number of the variable.
Example:x = DerivVar(7., 0) y = DerivVar(42., 1) z = DerivVar(pi, 2) print (sqrt(pow(x,2)+pow(y,2)+pow(z,2)))
produces the output
(42.6950770511, [0.163953328662, 0.98371997197, 0.0735820818365])
The numbers in the list are the partial derivatives with respect to x, y, and z, respectively.
Note: It doesn't make sense to use DerivVar with different values for the same variable index in one calculation, but there is no check for this. I.e.print DerivVar(3, 0)+DerivVar(5, 0)
produces
(8, [2])
but this result is meaningless.
isDerivVar()
Returns 1 if x is a DerivVar object.
DerivVector()
Returns a vector whose components are DerivVar objects.
vector components (numbers)
the DerivVar index for the x component. The y and z components receive consecutive indices.
Constructor: DerivVar(value, index = 0)
the numerical value of the variable
the variable index (an integer), which serves to distinguish between variables and as an index for the derivative lists. Each explicitly created instance of DerivVar must have a unique index.
Indexing with an integer yields the derivatives of the corresponding order.