DerApproximator

A Python module for finite-differences derivatives approximation
Download

DerApproximator Ranking & Summary

Advertisement

  • Rating:
  • License:
  • BSD License
  • Price:
  • FREE
  • Publisher Name:
  • Dmitrey Kroshko
  • Publisher web site:
  • http://openopt.org

DerApproximator Tags


DerApproximator Description

A Python module for finite-differences derivatives approximation DerApproximator is a Python library for finite-differences derivatives approximation.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.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^mExample:from DerApproximator import *print get_d1(lambda x: (x**2).sum(), )print get_d1(lambda x: x**2, )Expected output: ] * check_d1 checks user-provided routing for obtaining 1st derivatives of a functionExample:from numpy import *from DerApproximator import *func = lambda x: (x**4).sum()func_d = lambda x: 40 * x**3x = arange(1.0, 6.0)r = check_d1(func, func_d, x)func = lambda x: x**4func_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 RD0 +4.000e+01 +4.000e+00 31 +3.200e+02 +3.200e+01 32 +1.080e+03 +1.080e+02 33 +2.560e+03 +2.560e+02 34 +5.000e+03 +5.000e+02 3max(abs(d_user - d_numerical)) = 4499.9999861(is registered in func number 4)func num i,j: dfunc/dx user-supplied numerical RD0 0 / 0 +4.000e+01 +4.000e+00 36 1 / 1 +3.200e+02 +3.200e+01 312 2 / 2 +1.080e+03 +1.080e+02 318 3 / 3 +2.560e+03 +2.560e+02 324 4 / 4 +5.000e+03 +5.000e+02 3max(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_d1func = 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: * 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. Requirements: · Python · NumPy


DerApproximator Related Software