Math parser and evaluator capable of computing gradients and Hessians.
Project description
Mexpress
Math parser and evaluator in Python capable of computing gradients and Hessians. Mexpress uses the Rust crate Exmex.
Installation
pip install mexpress
Usage
import mexpress
import numpy as np
# parse function with parse_f64 or parse_f32
f = mexpress.parse_f64("(x - 1) ** 2 - y * x + 3 * y ** 2")
# evaluate function at (2, 4)
y = f(2, 4)
assert abs(y + 3) < 1e-12
# evaluate gradient at (2, 4)
grad_2_4 = f.grad(2, 4)
assert np.linalg.norm(grad_2_4 - [-2, 22]) < 1e-12
# evaluate Hessian at (2, 4)
hess_2_4 = f.hess(2, 4)
assert np.linalg.norm(hess_2_4 - [[2, -1], [-1, 6]]) < 1e-12
Besides **
you can also use ^
for exponentiation. Currently, a list of supported mathematical operators can be found in the documentation of Exmex.
Optimization Example
With gradients and Hessians one can at least locally optimize differentiable functions passed as strings, e.g., with scipy.optimize
.
from scipy.optimize import minimize
import mexpress
func_str = f"(1 - x) ** 2 + 100 * (y - x ** 2) ** 2 + (z - 7) ** 2 + (Ï + 5) ** 2"
f = mexpress.parse_f64(func_str)
res = minimize(f, x0=[0.0, 0.0, 0.0, 0.0], method="trust-ncg", jac=f.grad, hess=f.hess)
We have played around with different optimizers on func_str
, see the following output of py/demo/opt.py
. In the following table, #fails
is the number of fails out of 100 attempts with random x0
s. The iterations and elapsed seconds are medians.
CG #fails 0 #it 44 0.0049996 sec jac True hess False
CG #fails 23 #it 44 0.0179558 sec jac False hess False
Newton-CG #fails 0 #it 38 0.0049839 sec jac True hess True
Newton-CG #fails 5 #it 37 0.0059988 sec jac True hess False
trust-krylov #fails 0 #it 31 0.0255845 sec jac True hess True
trust-ncg #fails 0 #it 32 0.0030000 sec jac True hess True
trust-exact #fails 0 #it 30 0.0060000 sec jac True hess True
BFGS #fails 0 #it 72 0.0059998 sec jac True hess False
BFGS #fails 21 #it 74 0.0169995 sec jac False hess False
L-BFGS-B #fails 0 #it 43 0.0019979 sec jac True hess False
L-BFGS-B #fails 0 #it 42 0.0069985 sec jac False hess False
Nelder-Mead #fails 0 #it 441 0.0131288 sec jac False hess False
SLSQP #fails 0 #it 34 0.0029492 sec jac True hess False
dogleg #fails 17 #it 27 0.0027690 sec jac True hess True
TNC #fails 0 #it 29 0.0029995 sec jac True hess False
TNC #fails 0 #it 27 0.0110002 sec jac False hess False
COBYLA #fails 46 #it -1 0.0163412 sec jac False hess False
POWELL #fails 0 #it 22 0.0139999 sec jac False hess False
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
Built Distributions
Hashes for mexpress-0.1.4-pp37-pypy37_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a7cde1a6540a8c41b586e104378e75b967f7b41e7b359799342d4485fa061e8 |
|
MD5 | 18912c0b21609ee88da17c990af1cd72 |
|
BLAKE2b-256 | ff20bdea117c48d4732db1ec3b65e11c61a351bbe86d4255b2649c6e45c1d52b |
Hashes for mexpress-0.1.4-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e069701ab356a33aedbdbf7eddd5f53489b82149b66eea722f66960cad096e0a |
|
MD5 | cbb9f34af586dd88b4ca57c3b6b9f9ea |
|
BLAKE2b-256 | bee455b5047b0ce4cfccb2a08c7a1336b8a646dd2a1ea786057b7ae981e61da7 |
Hashes for mexpress-0.1.4-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40bb3bb2a52133fab1c3f39651a44cc193febbbaa7bdf78f7af709e3f0ad95cf |
|
MD5 | ae27a9ea8e06e3e35ae8bfb8ab93de3f |
|
BLAKE2b-256 | d5be7e1f9fe0fe6222b7fa6e226338031aea7d4fb071707c5b3714f160ad04df |
Hashes for mexpress-0.1.4-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 90bc1c8eceef99328762ba7572da3b6786fe869bde259e16c78d12b61e29e9ee |
|
MD5 | 2f3c0ee05fb018bad96d38f3e7a8ec17 |
|
BLAKE2b-256 | 6f391b915a6290c3d1bb21d81f9aad20f415ef08020c1c804d65805cdd9d8d22 |
Hashes for mexpress-0.1.4-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c8bb829ddea65e534d2b74969a502a111566a60fc66f28d4723842cfc0bc4c2d |
|
MD5 | 5aaf16df34e735d21b8df4fe693e0045 |
|
BLAKE2b-256 | c268ca442705d048390c9651a3cf4d590db52630ff3bdf2fecf3aedb9f568c3c |
Hashes for mexpress-0.1.4-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e169fe8b1352d5074370d2cf61199a126896613cb2f19365d45f2a88beb5722f |
|
MD5 | 01e5b849215f9a512187e652e5a93d7b |
|
BLAKE2b-256 | 27471643a7db203fe283f183d8e7fb6ca18faf8feefbc40a0fe28ffef2f7b29e |
Hashes for mexpress-0.1.4-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b405ef15a36ae72bdd6fdb8c52512ea7c6ad162b1b5af81b9025ede09cf93966 |
|
MD5 | 5b4bffcbb7a4ffbe42601953cc309fd8 |
|
BLAKE2b-256 | 822c54026caf953a77ebf70f259c13099eb078185bd318cefb53aa4f1f13ff25 |
Hashes for mexpress-0.1.4-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4058d4202dd31dc75ec43581feea2e7bcbba2c6cca1086af1b7992f3503785c9 |
|
MD5 | 889b9f028f36006a4fda223ed2d29087 |
|
BLAKE2b-256 | e75b2d9b60dc929939bb6f33039635ed9e4590c49dcbd8c9d912bbb9c20a18a9 |
Hashes for mexpress-0.1.4-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0821a5f3f317dd513c5fe0111c248c9a072437f4bf9a434702542e732939bd05 |
|
MD5 | 6b3c1d74d6b4fc592e219b249b4b04dc |
|
BLAKE2b-256 | 0524cbbf73604916b5880c961995e9bee9f8c5f82fd3073e82ba2a0d66e061fa |