AAD4SL

Example

SL Script

import numpy

def g(x): 
	x[0]=numpy.sin(x[0]*x[1])
	if x[0]<x[1]:
		x[1]=numpy.exp(x[0]/x[1])
	else :
		x[1]=x[1]+x[0]
	return x

def f(x): 
	x=g(x)
	x[1]=numpy.cos(x[0])/x[1]
	return x

Adjoint SL Script (generated by AAD4SL compiler)

import numpy
import tbr

v=[0]*4
a_v=[0]*4

def g(x):
        v[0]=x[0]
        v[1]=x[1]
        tbr.push_v(v[0])
        tbr.push_v(v[1])
        v[2]=v[0]*v[1]
        tbr.push_v(v[2])
        v[3]=numpy.sin(v[2])
        x[0]=v[3]
        if x[0]<x[1]:
                v[0]=x[0]
                v[1]=x[1]
                tbr.push_v(v[0])
                tbr.push_v(v[1])
                v[2]=v[0]/v[1]
                v[3]=numpy.exp(v[2])
                tbr.push_v(v[3])
                x[1]=v[3]
                tbr.push_b(1)
        else :
                v[0]=x[1]
                v[1]=x[0]
                v[2]=v[0]+v[1]
                x[1]=v[2]
                tbr.push_b(0)
        return x

def f(x):
        x=g(x)
        v[0]=x[0]
        tbr.push_v(v[0])
        v[1]=numpy.cos(v[0])
        v[2]=x[1]
        tbr.push_v(v[1])
        tbr.push_v(v[2])
        v[3]=v[1]/v[2]
        x[1]=v[3]
        return x

def a_f(a_x):
        a_v[3]+=a_x[1]
        a_x[1]=0.0
        v[2]=tbr.pop_v()
        v[1]=tbr.pop_v()
        a_x[1]+=a_v[2]
        a_v[2]=0.0
        v[0]=tbr.pop_v()
        a_v[0]+=-numpy.sin(v[0])*a_v[1]
        a_v[1]=0.0
        a_x[0]+=a_v[0]
        a_v[0]=0.0
        a_v[2]+=v[1]/(v[2]*v[2])*a_v[3]
        a_v[1]+=1.0/v[2]*a_v[3]
        a_v[3]=0.0
        a_x=a_g(a_x)
        return a_x

def a_g(a_x):
        if tbr.pop_b()==1:
                a_v[3]+=a_x[1]
                a_x[1]=0.0
                v[3]=tbr.pop_v()
                a_v[2]+=v[3]*a_v[3]
                a_v[3]=0.0
                v[1]=tbr.pop_v()
                v[0]=tbr.pop_v()
                a_x[1]+=a_v[1]
                a_v[1]=0.0
                a_x[0]+=a_v[0]
                a_v[0]=0.0
                a_v[1]+=v[0]/(v[1]*v[1])*a_v[2]
                a_v[0]+=1.0/v[1]*a_v[2]
                a_v[2]=0.0
        else :
                a_v[2]+=a_x[1]
                a_x[1]=0.0
                a_x[0]+=a_v[1]
                a_v[1]=0.0
                a_x[1]+=a_v[0]
                a_v[0]=0.0
                a_v[1]+=a_v[2]
                a_v[0]+=a_v[2]
                a_v[2]=0.0
        a_v[3]+=a_x[0]
        a_x[0]=0.0
        v[2]=tbr.pop_v()
        a_v[2]+=numpy.cos(v[2])*a_v[3]
        a_v[3]=0.0
        v[1]=tbr.pop_v()
        v[0]=tbr.pop_v()
        a_x[1]+=a_v[1]
        a_v[1]=0.0
        a_x[0]+=a_v[0]
        a_v[0]=0.0
        a_v[1]+=v[0]*a_v[2]
        a_v[0]+=v[1]*a_v[2]
        a_v[2]=0.0
        return a_x