Skip to main content

pyMuParser

This is a Python binding for MuParser (https://github.com/beltoforion/muparser). It is based on the project by Ayuto (https://github.com/Ayuto/Py-MuParser), adapted for the modern MuParser project, and with additional helper classes.

Installation

The easiest way to install this project on your machine, is to use pip:

python -m pip install pymuparser

Getting started

Examples for scalar and vector expressions (using the ScalarExpression and VectorExpression classes in pymuparser.expression) are given in the examples directory (https://github.com/bobmyhill/pymuparser/tree/main/examples). A vector example with added functions (those provided by deal.II) is reproduced below:

from pymuparser.expression import VectorExpression
import numpy as np
from scipy.special import erfc
import matplotlib.pyplot as plt

# In this example, we evaluate the following vector expression
# at a user-defined set of positions.
# The expressions are taken from the tests convection_box_phase_function.prm
# and vof_linear_2f.prm in ASPECT.
expr = "500 + 500*(erfc(z/(h*delta)) - erfc((1-z/h)/delta)) + A*cos(pi*x/h)*sin(pi*z/h) + A*cos(2*pi*x/h)*sin(pi*z/h) + A*cos(pi*x/h)*sin(2*pi*z/h) + A*cos(2*pi*x/h)*sin(2*pi*z/h);cos(init*pi)*(x-x0_1-xv*t)+sin(init*pi)*(z-z0_1-zv*t)"

# These are the deal.II functions we want to add to muparser processing.
# They must all take only one variable as argument.
extra_functions = [["int", lambda x: np.round(x, 0)],
                   ["ceil", np.ceil],
                   ["floor", np.floor],
                   ["cot", lambda x: 1./np.tan(x)],
                   ["csc", lambda x: 1/np.sin(x)],
                   ["sec", lambda x: 1/np.cos(x)],
                   ["erfc", erfc],
                   ["rand", np.random.rand],
                   ["rand_seed", np.random.seed]]


# Here, the variables are defined as x and y (spatial coordinates)
# and time.
variables = ["x", "z", "t"]

# The constants are defined as a dictionary.
constants = {
    "delta": 0.1,
    "A": 10,
    "h": 1350000,
    "init": 1.25,
    "x0_1": 0.75,
    "z0_1": 0.0,
    "xv": -0.25,
    "zv": -0.25,
    "pi": np.pi,
}

# We now define the positions over which we would like
# to evaluate the scalar expression. In this case
# we want to create a 2D grid of spatial positions
# at two distinct times.
xs = np.linspace(0, 1350000, 11)
ys = np.linspace(0, 1350000, 11)
ts = np.linspace(0, 2.e8, 2)

# To evaluate the function in the minimal number of lines,
# we use the meshgrid function of numpy to create equally
# spaced grids over x, y and t.
grid = np.array(np.meshgrid(xs, ys, ts))

# In the pymuparser functions, the loop over variables
# must be the innermost loop, but numpy meshgrid returns
# an array where the outermost loop is over the variables.
# We can deal with this by moving the first axis to the last
# position.
values = np.moveaxis(grid, 0, -1)

# Here's where we define our parser.
parser = VectorExpression(expr, variables, constants, extra_functions)

# And here we evaluate the vector expression
# at the given values.
compositions = parser.evaluate(values)

# Finally, let's plot the values.
# Finally, let's plot the values.
fig = plt.figure(figsize=(12, 8))
ax = [fig.add_subplot(2, 2, i) for i in range(1, 5)]

i = 0
for idx_c in range(2):
    for idx_t in range(2):

        ax[i].set_title(f"Composition {idx_c}, time {ts[idx_t]}")
        cset = ax[i].contourf(xs, ys, compositions[idx_c, :, :, idx_t])
        ax[i].set_xlabel("x")
        ax[i].set_ylabel("y")
        fig.colorbar(cset, ax=ax[i])
        i += 1

fig.set_tight_layout(True)
plt.show()

Metadata

Release files for pymuparser 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pymuparser 1.1.0
File Size Uploaded Provenance
pymuparser-1.1.0.tar.gz 5.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pymuparser 1.1.0
File Interpreter ABI Platform
pymuparser-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.3 kB

Release files / pymuparser-1.1.0.tar.gz

Download URL pymuparser-1.1.0.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
b842524d8b6d14ada4a9c484059a93233b490dc399330fbd36d86d37d15f5493
BLAKE2b-256 checksum
How to use checksums
83e6cdb608c0026d8e2f93b6ed14c49e15a08f84432ceef00463aa29a6d28367
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.9.19

Release files / pymuparser-1.1.0-py3-none-any.whl

Download URL pymuparser-1.1.0-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e861a730b77944fea026744110cee5ef5a32649c3bc996aa4c395be9a8e4e0d7
BLAKE2b-256 checksum
How to use checksums
709bcf9a34d337467e9db966e4545b9d7d70ca1d279b01f6173d3bc69a093de6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.9.19

Security for pymuparser 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page