Skip to content

Automatic differentiation of expressions in python3.

Notifications You must be signed in to change notification settings

markroxor/autodiff

Repository files navigation

Autodiff

Module for calculating gradient of any polynomial expression and most standard functions.

Usage documentation

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()

Sin

# range of plot
t = np.arange(0.0, 1.0, 0.01)
plot(Function.Sin(Constant(2*np.pi*t)))

Tan

# range of plot
t = np.arange(-0.45, 0.45, 0.01)
# Tan
plot(Function.Tan(Constant(np.pi*t)))

Exponential

# range of plot
t = np.arange(0.0, 1.0, 0.01)
# Exp function
plot(Function.Exp(Constant(2*2*np.pi*t)))

Nested misc functions

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)

TODO

  • Extend it for multi-variables.

About

Automatic differentiation of expressions in python3.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published