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.0, 4.0)
assert abs(y - 41) < 1e-12
# evaluate gradient at (2, 4)
grad_2_4 = f.grad(2.0, 4.0)
assert np.linalg.norm(grad_2_4 - [-2, 22]) < 1e-12
# evaluate Hessian at (2, 4)
hess_2_4 = f.hess(2.0, 4.0)
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 methods to optimize 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.6-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9bf4bccd845e6917d98b1e3e84205f1a6f7c0a45bd048abb5484956aa1a58749 |
|
MD5 | c50d37ceed8381d4eadcaa4792f6323a |
|
BLAKE2b-256 | 6ac8405e3cfaa5a950d734a9b8663bef889558aad05f2636bc0138f09262d39d |
Hashes for mexpress-0.1.6-pp310-pypy310_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf63c03c5b0f2346f9b620c3458328aa83a574ec6e795da6bd3c42965df5b43b |
|
MD5 | 90f32518ece6c4d55c259f1ce79588d0 |
|
BLAKE2b-256 | 6832d6a9496a75f787cb636e1d798bfd63e247a750422d1a5abdc1465e47b413 |
Hashes for mexpress-0.1.6-pp310-pypy310_pp73-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8c27bcb8a5f548c868b8c2b1d71b6828f4eda28a9b55265d0b376da2c8117e6 |
|
MD5 | 6acdd3af35b2b8e80beed911c9050ba6 |
|
BLAKE2b-256 | 56dc49fc66a484ec6f97265071dd4178c53c7846a6ac94a2a5fe5de4830af8bd |
Hashes for mexpress-0.1.6-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8e384c837134677364b86107f3506ae7c87879413e90c9b2f8ba3b544782fbe |
|
MD5 | 17e80487e64cc41c9831004cd18c7a86 |
|
BLAKE2b-256 | 1068bf0dbe05e5910f4c24adaf088ecbca17b0db0da11d8efcffb5d3ac90b466 |
Hashes for mexpress-0.1.6-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d97e678146bfd9da117d4df9f2581fcf729c3aea103f4ada639decb214e63229 |
|
MD5 | c7d70302f99e69403669e95b7bed8b9c |
|
BLAKE2b-256 | 5f8f0c50f0f77ba42f7cb7c3e09202057cf651f1ee77034add7aced87afc7ba3 |
Hashes for mexpress-0.1.6-pp39-pypy39_pp73-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb0160116931dc6632696d67ff19c7863237c11a7ae9323931b2d35c5b481086 |
|
MD5 | 333958622ca7bfc6aa8ef66fb887441a |
|
BLAKE2b-256 | 577baa45ee9bd4ef8f270fabf94d356093290342f6f3615b33b5bf3c3008495e |
Hashes for mexpress-0.1.6-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf6699514402b5bc88bfa92fded55c113d9feed1f2e09f7bebf46999ad8ffcf6 |
|
MD5 | 00c2ad0be7dd8df77cd85ecbc115a122 |
|
BLAKE2b-256 | 575296735c2b53f2520227a44853f88fcf3a088d99a24e9f6459156ee1c5d6bb |
Hashes for mexpress-0.1.6-pp38-pypy38_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c7f8f439b4d576fd4fc61570a68073fd06c1ab7f6111c86c4d39646fcdb43cc1 |
|
MD5 | 36316679df3aceac866b5aac8da5fdb9 |
|
BLAKE2b-256 | 6994dcd9f2993fc0d7a88ef83f13cd729e8bc92b0c79884aa8a1679443dafa7e |
Hashes for mexpress-0.1.6-pp38-pypy38_pp73-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 58c659944bbbf4fde0a497212da3d8c369318daced549c6ba1f9812206ff196e |
|
MD5 | 4c985924deab4dfcbd7d15514571f069 |
|
BLAKE2b-256 | 890571dfde23a0c4e29fc268b94ce47c250b73a8f595f544c96aaa1d1672928c |
Hashes for mexpress-0.1.6-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8134bf68fba864bfc3835eb1a5d45e2eec534ffd1ed828b247e33affc69d8c6b |
|
MD5 | b690bdc7ae2d2a3f02711639154cf52a |
|
BLAKE2b-256 | 742d8fa1e2b06f9a2dd0f53123ce9e2a29cab2114e1b4aa37fafe77fbb5deba0 |
Hashes for mexpress-0.1.6-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee98c95a74e2e37abe7ea7877207da22f4ce0fc6c8866a4a01683c762ce5db2b |
|
MD5 | a0108729754ec3d65f7ec9142bf5c28c |
|
BLAKE2b-256 | 800960a08913a88d2064824386ba01e3822a727f5856fdf63903085a6a110d0d |
Hashes for mexpress-0.1.6-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0d58267349228910f820f51937b144eab6d034e7e47dcfcf08c7ae032bad02e1 |
|
MD5 | 56239fe48c52b96dec934666eae5db1d |
|
BLAKE2b-256 | c9893016e61a87f4d7165d386e78e32da2cb02d322796682198799cb18b600a3 |
Hashes for mexpress-0.1.6-cp312-cp312-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c8057210e4ecd8525acb22db3804f44ec18c2b82b3e3e34dd274f6398218550 |
|
MD5 | 95937b0ad7fb930a45ca1e29d13622a0 |
|
BLAKE2b-256 | e1564e1bee7b450b0826b8c3b1c3269164aa3dc130936ba631f0e44dcca9334e |
Hashes for mexpress-0.1.6-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1034c7ffde27c271c93965c7d160d4a3d2876a932c694259e3213b6dcedfe11 |
|
MD5 | 65e865165c2e01549bed2c776b8a48fd |
|
BLAKE2b-256 | cbaf22c8531f48e734fa5c045868b23973b7b33dbaf2247afe6eb27bd29c8245 |
Hashes for mexpress-0.1.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6626dcefe484a0194c904ecea27a2ffe37766c26034dec6bc4a18a83945c3d2c |
|
MD5 | 50f092fd7b988d84071d0183b79dc098 |
|
BLAKE2b-256 | d87f76e8510082e26d30c50e4e25a988fd68e71167eeae1374678b90c948b58a |
Hashes for mexpress-0.1.6-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 49f1f34abcb1396b73699db6bc0751c7228bff8494c7fcab7ed714d612c56247 |
|
MD5 | 22f9536d2fe0f0417ed99001203ee7d4 |
|
BLAKE2b-256 | 8e58e805f9a6d59a43c2db469c0b22c6ce99e3a39d826d2d0f1a4eca00981290 |
Hashes for mexpress-0.1.6-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dca98d6e1fd9f25644c73db15db4270380a0bcfc3601d1096826a0a1f82623ad |
|
MD5 | bd330b1f6639e86d81ef987d1ce40d2e |
|
BLAKE2b-256 | 7bf03a64845c129af42984f8b4e510238ff6fc0308b97a283ae18e6146d800d9 |
Hashes for mexpress-0.1.6-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4cd13c330bf850f211573cf7c278cfbdb2846efa95e35ee5dd864f27485eb462 |
|
MD5 | 35b0a50ad4ca40d7ee5b72f5feecc1d9 |
|
BLAKE2b-256 | 43d56b1ce389e4a51c45419892aa297bbedff1cab3a06644fb11c6abd7c0e1d5 |
Hashes for mexpress-0.1.6-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 447be62ad80c325cbd0204697beb20d193605439c2fab6408ae9b72c1d79bad6 |
|
MD5 | 45734e81976c5e2cb70e2cf5ad933e26 |
|
BLAKE2b-256 | c206bc924664bd5532d9c19adf7c174310c7db96b88a900ee2de2e9a6ad44f4f |
Hashes for mexpress-0.1.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0bef507449a9bcd36f9637d8e2d862b9e848c8ab0513e316eb21791f57aa5d90 |
|
MD5 | 47b0a89b8b662f55ebad7b59c4fb613b |
|
BLAKE2b-256 | 08b625b108232d5653623b1933bfa6a269bcc82a337049d5cc5b0eb88ecb6ba7 |
Hashes for mexpress-0.1.6-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd59c3896658ab8d5b4d15030bd1b738f0e50ca69f86ace01b1e18547653c421 |
|
MD5 | c9a6866daa79c829c655637470ce98ac |
|
BLAKE2b-256 | 990f67fd036cbd3f06cb1672a5474e178568194692fd649f4f1ae926d7b262de |
Hashes for mexpress-0.1.6-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4920df0340eb751d5fe851e6de7418aa8b75a2c5c677fc5627b5ffa0ed3375af |
|
MD5 | 5d6dcddfb1e7121caba2bf12fb22d92d |
|
BLAKE2b-256 | 85fbdf023d6a93aa65b6eeb53f8a4ea8b6fe886c623e35437a6b1a35b1a2f1b4 |
Hashes for mexpress-0.1.6-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8a4b097ea89ea2e853c254e8c2acf49546e7fbf5bbc18a1191e49c9f7554e13 |
|
MD5 | 8fb8f8fe3f378108d0e37dbb514f47ab |
|
BLAKE2b-256 | 473c2aed39b7492c8b0e2db903d96016032bd39e5a11473916001878d93e20a7 |
Hashes for mexpress-0.1.6-cp310-cp310-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 95aa0ce6a6eb2ba3ea386620e0fa0ee813ac2df179ee02f65fa0cb26c8a3d8a3 |
|
MD5 | 5682f581bd050803c80f9998c46ba02e |
|
BLAKE2b-256 | d579dd539167f4f7f451df4e8261ed5e78a3ac51d3261474238ea30c89825f91 |
Hashes for mexpress-0.1.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 225e4c1004b9b0730ef2790347738b4264afc28b30863b5c2f98db1b9dc40ca1 |
|
MD5 | 2597e3961108a04bc573de08f11caff7 |
|
BLAKE2b-256 | 8d5492b668057c98d8a53de41c1425d631407354462cb7b15dd9c6879173e08b |
Hashes for mexpress-0.1.6-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 55c143b879aa96dca2984c67acdb3c0d1746c640be4ce0efb8b4373e9b162bef |
|
MD5 | a2d44d926cf317ea191643fab6b19889 |
|
BLAKE2b-256 | 34b4bc6a51ec34964d61db97262fd1d7839e8aba6b93732f566adcd037dd7590 |
Hashes for mexpress-0.1.6-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7e19cb4f075522036f6d29d368f692f78a35349c1473fdb095a652891e46b0e1 |
|
MD5 | 9a12e0460f99f3cf204f016b26009e43 |
|
BLAKE2b-256 | f201d6324c9bb333c11ba652b81a8d40f64df92277aaf390dd7aa52325d4bcaa |
Hashes for mexpress-0.1.6-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4600ac6eed7cb69ae4b81ac4ffe36f9d73df1953328a9a74fd8459a5a54d30c9 |
|
MD5 | 045f4ae13ba5feb603825329e869d348 |
|
BLAKE2b-256 | f31d2ca4a0b8928c83c9c0e1f80a331ff8997702ee115e3582b1cb3d7b89d5e4 |
Hashes for mexpress-0.1.6-cp39-cp39-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7008d297a126370df92018b08c15c9b802ea1c23971d1ce35acf6d3367eca753 |
|
MD5 | ea56e663f0d7cdd470f9bef4927bb698 |
|
BLAKE2b-256 | f6a57d440c1670c767d51c101526e2be6cbdd71aab9b96d72d53e39cc4f2a0a4 |
Hashes for mexpress-0.1.6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c925ac5b9345f95112bd21d77d07e6fd1918063beab83c033c0aafd1d10cb98a |
|
MD5 | 6ed7094fed4e3c5b7238513ca3519ec3 |
|
BLAKE2b-256 | 91f2306498efd15207d3aa95d9f4ed65390d24faa5a12f48310e9f2b9bde1a3a |
Hashes for mexpress-0.1.6-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 00ad2f0a7711a36c9da570c0a23ac96d02d32c5e2032040455536d0b5e632385 |
|
MD5 | 498b49202c2afe6061d939617f5d1690 |
|
BLAKE2b-256 | dce05644c5f617245286cc600597a6b161a09287d460f5802479a5287eeeef5d |
Hashes for mexpress-0.1.6-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | daf3834bd38d3749017d0288e41c97f61e930e1111968aac37bdffaadd9e9472 |
|
MD5 | 6a248ea9cad6d5e96debfea5ef648e12 |
|
BLAKE2b-256 | 8934346b61f4cce0ec99a85d168aba2e8df538288427bd1e46faf2153f7155b1 |