DerApproximator
From OpenOpt
- DerApproximator is a small yet important package for getting/checking derivatives (currently only 1st ones), extracted from OpenOpt framework to be standalone Python module. It is required by FuncDesigner (for obtaining derivatives of oofuns beyond standard set without routines to yield them directly) and some OpenOpt solvers (when there are some functions without user-supplied derivatives).
- Requirements for the package (as well as for OpenOpt and FuncDesigner) are NumPy and python-setuptools; OS - any where Python and numpy work (Linux, Windows, Mac OS X etc).
- Currently it provides only 2 functions - get_d1 and check_d1. Two required arguments for get_d1 and check_d1 are the func involved and the point where derivatives are to be got / checked. If user provides Python list (as start point), it is automatically casted to NumPy array.
- get_d1 returns 1st derivatives of a func f : R^n -> R^m Example
from DerApproximator import *
print get_d1(lambda x: (x**2).sum(), [1,2,3])
print get_d1(lambda x: x**2, [1,2,3])
Expected output: [ 1.99999993 3.99999998 5.99999996] [[ 2. 0. 0. ] [ 0. 3.99999996 0. ] [ 0. 0. 5.99999996]]
- check_d1 checks user-provided routing for obtaining 1st derivatives of a function Example
from numpy import *
from DerApproximator import *
func = lambda x: (x**4).sum()
func_d = lambda x: 40 * x**3
x = arange(1.0, 6.0)
r = check_d1(func, func_d, x)
func = lambda x: x**4
func_d = lambda x: 40 * diag(x**3)
x = arange(1.0, 6.0)
r = check_d1(func, func_d, x)
Expected output: func num user-supplied numerical RD 0 +4.000e+01 +4.000e+00 3 1 +3.200e+02 +3.200e+01 3 2 +1.080e+03 +1.080e+02 3 3 +2.560e+03 +2.560e+02 3 4 +5.000e+03 +5.000e+02 3 max(abs(d_user - d_numerical)) = 4499.9999861 (is registered in func number 4) func num i,j: dfunc[i]/dx[j] user-supplied numerical RD 0 0 / 0 +4.000e+01 +4.000e+00 3 6 1 / 1 +3.200e+02 +3.200e+01 3 12 2 / 2 +1.080e+03 +1.080e+02 3 18 3 / 3 +2.560e+03 +2.560e+02 3 24 4 / 4 +5.000e+03 +5.000e+02 3 max(abs(d_user - d_numerical)) = 4499.9999861 (is registered in func number 24)
- Default diffInt is 1.5e-8, you can overwrite it by "diffInt" argument for get_d1 and check_d1. Another one argument is stencil, default value 2 for DerApproximator, FuncDesigner and OpenOpt NSP is 2, i.e. (f(x+diffInt)-f(x-diffInt)) / (2*diffInt), for OpenOpt NLP default is 1, i.e. (f(x+diffInt)-f(x)) / diffInt. Example
from numpy import *
from DerApproximator import get_d1
func = lambda x: (x**4).sum()
x = arange(1.0, 6.0)
r1 = get_d1(func, x, stencil = 1, diffInt = 1e-5)
print(r1)
r2 = get_d1(func, x, stencil = 2, diffInt = 1e-5)
print(r2)
Expected output: [ 4.00005999 32.00024 108.00054 256.00095998 500.00149998] [ 4. 32. 108. 256. 499.99999998]
- If it turns out that f(x+diffInt) is NaN (not a number) or f(x-diffInt) is NaN, than only one side will be involved into calculations. BTW this is a typical situation for lots of numerical optimization problems, and currently functions approx_fprime and check_grad from scipy.optimize are even more primitive - they have only one stencil and no handling of NaNs.
- In future five-point_stencil is intended to be provided, mb along with some stencils for 2nd derivatives.
| Made by Dmitrey |
See also:
- Download and installation instructions at our Install webpage
- DerApproximator linux.softpedia.com entry
- FuncDesigner - for most cases it can provide more exact derivatives via Automatic differentiation


