Module for calculating gradient of any polynomial expression and most standard functions.
Constants -
zero = Constant(0)
one = Constant(1)
two = Constant(2)
three = Constant(3)
pi = Constant(np.pi)
Variables -
x = Variable("x")
y = Variable("y")
Functions -
tan = Function.Sin(zero) / Function.Cos(zero)
exp = Function.Exp(y)
tan = Function.Tan(x)
Initialize the variables -
tan.update({'x':5, 'y':np.array([1,2,3])})
Evaluate the expression-
tan.evaluate()
Calulate gradient of the expression-
tan.grad()
Complete examples -
from autodiff import *
import matplotlib.pyplot as plt
import numpy as np
def plot(fun):
# plot the function
plt.plot(t, fun.evaluate())
# plt the gradient
plt.plot(t, fun.grad())
plt.grid(True)
plt.show()
# range of plot
t = np.arange(0.0, 1.0, 0.01)
plot(Function.Sin(Constant(2*np.pi*t)))
# range of plot
t = np.arange(-0.45, 0.45, 0.01)
# Tan
plot(Function.Tan(Constant(np.pi*t)))
# range of plot
t = np.arange(0.0, 1.0, 0.01)
# Exp function
plot(Function.Exp(Constant(2*2*np.pi*t)))
t = np.arange(-1, 1, 0.01)
exp = x**two + two*x + three * Function.Cos(Function.Exp(Function.Tan(x)))
exp.update({'x':t, 'y':t})
plot(exp)
- Extend it for multi-variables.