# Python using the Forward Method. # # Copyright (C) 2008 Luis Zarrabeitia # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. import adolpy import adolpy.adolmath def log(x): return adolmath.log(x) dlog = adolpy.derivate(log) #print log(2.0), dlog(2.0) print(repr(dlog(0)), repr(dlog(-1)))
r *= a return r # A weird way of expressing x**5 + x*sqrt(y), x+y def f(x,y): # We can call other user defined functions z = pow(x,5) # and return more than one parameter return z + x*math.sqrt(y), x+y x,y = 3,6 print "value: ", f(x,y) der = adolpy.derivate(f) derivative = der(x,y) print print "Automatic derivatives" for i in derivative: print i.dot print "Manually computed jacobian" print [5 * x**4 + math.sqrt(y), x/(2*math.sqrt(y))] print [1,1] step = 1e-5 print "Numerically computed by central differences for step=", step
# Python using the Forward Method. # # Copyright (C) 2008 Luis Zarrabeitia # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. import adolpy import adolmath def log(x): return adolmath.log(x) dlog = adolpy.derivate(log) #print log(2.0), dlog(2.0) print repr(dlog(0)), repr(dlog(-1))