Skip to main content

Python wrapper of Minpack (root finding) which can be called from within numba functions.

Project description

NumbaMinpack

NumbaMinpack is a python wrapper to Minpack, which is for solving systems of non-linear equations.

This package is very similar to scipy.optimize.root (see here), when you set method = 'lm' or method = 'hybr'. But, the problem with scipy.optimize.root, is that it can not be used within numba jit-compiled python functions. In contrast, NumbaMinpack can be used within a numba compiled function. For example, check out comparison2scipy.ipynb.

Right now, NumbaMinpack wraps the following Minpack algorithms

  • lmdif (Levenberg-Marquardt) with a finite-differenced, non-analytical, jacobian.
  • hybrd (modified Powell method) with a finite-differenced, non-analytical, jacobian.

Installation

NumbaMinpack will probably only work on MacOS or Linux. You must have a fortran compiler (On Mac install with brew install gcc).

After satisfying the dependencies, install with the pip

python -m pip install NumbaMinpack

Basic usage

from NumbaMinpack import lmdif, hybrd, minpack_sig
from numba import njit, cfunc
import numpy as np

# System of equations must look like this. Returns nothing!
@cfunc(minpack_sig)
def myfunc(x, fvec, args):
    fvec[0] = x[0]**2 - args[0]
    fvec[1] = x[1]**2 - args[1]
    
funcptr = myfunc.address # address in memory to myfunc

x_init = np.array([10.0,10.0]) # initial conditions
neqs = 2 # number of equations
args = np.array([30.0,8.0]) # data you want to pass to myfunc
xsol, fvec, success, info = lmdif(funcptr, x_init, neqs, args) # solve with lmdif
xsol, fvec, success, info = hybrd(funcptr, x_init, args) # OR solve with hybrd
# xsol = solution
# fvec = function evaluated at solution
# success = True/False
# info = an integer. See src/lmdif1.f for what it means.

Note, that either lmdif or hybrd can be called within a jit-compiled numba function:

@njit
def test()
    return hybrd(funcptr, x_init, args)
sol = test() # this works!!! :)

@njit
def test_sp():
    sol_sp = scipy.optimize.root(myfunc_scipy,x_init,method='hybr')
    return sol_sp
sol_sp = test_sp() # this DOES NOT WORK :(

Warning

Using NumbaMinpack is like using C or Fortran: You will not be notified if you write or read beyond an array. For example,

@cfunc(minpack_sig)
def myfunc(x, fvec, args):
    fvec[0] = x[0]**2 - args[0]
    fvec[1] = x[1]**2 - args[1]
funcptr = myfunc.address

x_init = np.array([10.0,10.0])
neqs = 2 
args = np.array([30.0]) # Array is too short!!!! 
sol = lmdif(funcptr, x_init, neqs, args) 

Notice, that args, is only length 1, but in myfunc we try to access args assuming it as 2 elements. No error will be thrown, and you will read from beyond the end of args, and the solution will be garbage. If you read far enough beyond then end an array, it will probably crash your program.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

NumbaMinpack-0.1.2.tar.gz (25.3 kB view details)

Uploaded Source

File details

Details for the file NumbaMinpack-0.1.2.tar.gz.

File metadata

  • Download URL: NumbaMinpack-0.1.2.tar.gz
  • Upload date:
  • Size: 25.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.6.1 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5

File hashes

Hashes for NumbaMinpack-0.1.2.tar.gz
Algorithm Hash digest
SHA256 8a3b79bf15e43bd14a49c4341235aaea524c6ad689010db145eecbf810a42512
MD5 4ee70029038332a0969a1df94ecc007a
BLAKE2b-256 12825afff35baa52c27332f96f9c0888ab8995bacd3753bd8d9fe362a91bd46b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page