autodiff package¶
Submodules¶
autodiff.operations module¶
Collection of tools to make new elementary operations
-
autodiff.operations.acos(x)¶ Arccos
Parameters: x (Number) – Value Returns: acos(x) Return type: Number
-
autodiff.operations.acos_deriv(x)¶ Derivative of arccos
Parameters: x (Number) – Value Returns: Partial derivatives w.r.t. everything x had a partial w.r.t. Return type: dict
-
autodiff.operations.add(x, y)¶ add two numbers together, one of x and y has to be a Number object
Parameters: - x – a Number object or an int/float
- y – a Number object or an int/float to be added
Returns: value of the sum
-
autodiff.operations.add_deriv(x, y)¶ Derivative of additions, one of x and y has to be a Number object
Parameters: - x – a Number
- y – a Number object or an int/float to be added
Returns: The derivative of the sum of x and y
-
autodiff.operations.asin(x)¶ Arcsin
Parameters: x (Number) – Value Returns: asin(x) Return type: Number
-
autodiff.operations.asin_deriv(x)¶ Arcsin derivative
Parameters: x (Number) – Value Returns: Partial derivatives w.r.t. everything x had a partial w.r.t. Return type: dict
-
autodiff.operations.atan(x)¶ Arctan
Parameters: x (Number) – Value Returns: atan(x) Return type: Number
-
autodiff.operations.atan_deriv(x)¶ Derivative of atan
Parameters: x (Number) – Value Returns: Partial derivatives w.r.t. everything x had a partial w.r.t. Return type: dict
-
autodiff.operations.cos(x)¶ Take the cos(x)
Parameters: x (Number) – Number to take the cos of Returns: cos(x.val) Return type: float
-
autodiff.operations.cos_deriv(x)¶ Derivative of cos(x)
Parameters: x (structures.Number()) – Number to take the cos of. Must have a derivattributeReturns: dictionary of partial derivatives Return type: dict
-
autodiff.operations.cosh(x)¶ Hyperbolic cosine
Parameters: x (Number) – Value Returns: cosh(x) Return type: Number
-
autodiff.operations.cosh_deriv(x)¶ Hyperbolic cosine
Parameters: x (Number) – Value Returns: Partial derivatives w.r.t. everything x had a partial w.r.t. Return type: dict
-
autodiff.operations.div(x, y)¶ Subtract one number from another, one of x and y has to be a Number object
Parameters: - x – a Number object
- y – a Number object or an int/float to be subtracted
Returns: value of the difference
-
autodiff.operations.div_deriv(x, y)¶ Derivative of division, one of x and y has to be a Number object
Parameters: - x – a Number
- y – a Number object or an int/float to be divided
Returns: The derivative of the quotient of x and y
-
autodiff.operations.elementary(deriv_func)¶ Decorator to create an elementary operation
This takes as an argument a function that calculates the derivative of the function the user is calculating. When the decorated function is called, @elementary also calls deriv_func and stores both the value and derivative in a new Number() object
Example
>>> import numpy as np >>> def sin_deriv(x): ... return {x: np.cos(x.val) * x.deriv[x]} >>> @elementary(sin_deriv) ... def sin(x): ... return np.sin(x.val) >>> a = Number(np.pi / 2) >>> sina = sin(a) >>> sina.val 1.0 >>> sina.deriv[a] 0.0
Parameters: deriv_func (function) – Function specifying the derivative of this function. Must return a dictionary where each key-value pair is the partial derivative of the decorated function Returns: Decorated function Return type: function
-
autodiff.operations.exp(x)¶ Take the exp(x)
Parameters: x (Number) – Number to take the exp of Returns: exp(x.val) Return type: float
-
autodiff.operations.exp_deriv(x)¶ Derivative of exp(x)
Parameters: x (structures.Number()) – Number to take the exp of. Must have a derivattributeReturns: dictionary of partial derivatives Return type: dict
-
autodiff.operations.log(x, y=2.718281828459045)¶ Take the log(x) at base y
Parameters: - x (Number) – Number to take the log of
- y (a Number object or an int/float) – Base of the logarithm.
Returns: log(x.val)
Return type: float
-
autodiff.operations.log_deriv(x, y=2.718281828459045)¶ Derivative of log(x) at base y
Parameters: - x (structures.Number()) – Number to take the log of. Must have a
derivattribute - y (a Number object or an int/float) – Base of the logarithm.
Returns: dictionary of partial derivatives
Return type: dict
- x (structures.Number()) – Number to take the log of. Must have a
-
autodiff.operations.logistic(x)¶ Take the logistic(x)
Parameters: x (Number) – Number to the natural exponential in the logistic Returns: logistic(x.val) Return type: float
-
autodiff.operations.logistic_deriv(x)¶ Derivative of logistic(x)
Parameters: - x (structures.Number()) – Number to the natural exponential in the logistic function.
- have a deriv attribute (Must) –
Returns: dictionary of partial derivatives
Return type: dict
-
autodiff.operations.mul(x, y)¶ Subtract one number from another, one of x and y has to be a Number object
Parameters: - x – a Number object
- y – a Number object or an int/float to be subtracted
Returns: value of the difference
-
autodiff.operations.mul_deriv(x, y)¶ Derivative of multiplication, one of x and y has to be a Number object
Parameters: - x – a Number
- y – a Number object or an int/float to be multiplied
Returns: The derivative of the product of x and y
-
autodiff.operations.negate(x)¶ Negate
Parameters: x (Number) – Negate a number Returns: -x Return type: Number
-
autodiff.operations.negate_deriv(x)¶ Derivatives of the elementary negation function
Parameters: x (Number) – Number to be negated Returns: the partial derivatives of the negated Number Return type: dictionary
-
autodiff.operations.pow_deriv(x, a)¶ Derivative of power of a Number
Parameters: - x – a Number
- a – a Number
Returns: The derivative of the power
-
autodiff.operations.power(x, y)¶ power of one number by another, one of x and y has to be a Number object
Parameters: - x – a Number object or an int/float to be powered
- y – a Number object or an int/float to be the exponential
Returns: value of the difference
-
autodiff.operations.sin(x)¶ Take the sin(x)
Parameters: x (Number) – Number to take the sin of Returns: sin(x.val) Return type: float
-
autodiff.operations.sin_deriv(x)¶ Derivative of sin(x)
Parameters: x (structures.Number()) – Number to take the sin of. Must have a derivattributeReturns: dictionary of partial derivatives Return type: dict
-
autodiff.operations.sinh(x)¶ Hyperbolic sine
Parameters: x (Number) – Value Returns: sinh(x) Return type: Number
-
autodiff.operations.sinh_deriv(x)¶ Hyperbolic sin derivative
Parameters: x (Number) – Value Returns: Partial derivatives w.r.t. everything x had a partial w.r.t. Return type: dict
-
autodiff.operations.sqrt(x)¶ Square root of a number
Parameters: x (Number) – Number to take the square root of Returns: the square root of Number x’s value Return type: float
-
autodiff.operations.sqrt_deriv(x)¶ Derivative of the square root function
Parameters: x (Number) – Number to take the square root of Returns: the partial derivatives of the square root of number Return type: dictionary
-
autodiff.operations.subtract(x, y)¶ Subtract one number from another, one of x and y has to be a Number object
Parameters: - x – a Number object
- y – a Number object or an int/float to be subtracted
Returns: value of the difference
-
autodiff.operations.subtract_deriv(x, y)¶ Derivative of subtractions, one of x and y has to be a Number object
Parameters: - x – a Number
- y – a Number object or an int/float to be subtracted
Returns: The derivative of the difference of x and y
-
autodiff.operations.tan(x)¶ Take the tan(x)
Parameters: x (Number) – Number to take the tan of Returns: tan(x.val) Return type: float
-
autodiff.operations.tan_deriv(x)¶ Derivative of tan(x)
Parameters: x (structures.Numbers()) – Number to take the tan of. Must have a derivattributeReturns: dictionary of partial derivatives Return type: dict
autodiff.structures module¶
Data structures for autodiff
-
class
autodiff.structures.Array(iterable)¶ Bases:
object-
cos()¶
-
exp()¶
-
jacobian(order)¶
-
logistic()¶
-
sin()¶
-
tan()¶
-
-
class
autodiff.structures.Number(val, deriv=None)¶ Bases:
objectNumber class is the core data structure for ‘autodiff’. It instantiates a Number object by specifying a value and a derivative. Derivative with respect to itself is automatically instantiated to 1.
Parameters: - val – value of the Number
- deriv – a dictionary of partial derivatives. It is automatically instantiated to {self: 1} unless otherwise specified.
Returns: Number, an object to perform automatic differentiation on.
Example
>>> import autodiff >>> x = autodiff.Number(3) >>> x.value 3 >>> x.deriv[x] 1 >>> a = autodiff.Number(3,2) >>> a.value 3 >>> a.deriv[a] 2
-
cos()¶ Calculates the cosine of the Number object.
Returns: another Number object, which is cosine of the original one.
-
exp()¶ Calculates the exponential of Number object.
Returns: another Number object, which is the exponential of the original one.
-
jacobian(order)¶ Returns the jacobian matrix by the order specified.
Parameters: order – the order to return the jacobian matrix in. Has to be not null Returns: a list of partial derivatives specified by the order.
-
logistic()¶ Calculates the logistic of Number object.
Returns: another Number object, which is the logistic of the original one.
-
sin()¶ Calculates the sin of the Number object.
Returns: another Number object, which is sin of the original one.
-
tan()¶ Calculates the tangent of the Number object.
Returns: another Number object, which is tangent of the original one.