SymEngine Python Bindings
SymEngine.py is a Python library providing high-performance wrappers to the SymEngine C++ symbolic manipulation library. It aims to offer a fast and efficient alternative for symbolic mathematics in Python, often used as a backend for SymPy or independently. The library is actively maintained, with frequent releases that typically follow updates to the core C++ SymEngine library.
Warnings
- breaking Python version support changes across major versions. For instance, v0.9.0 dropped Python 3.6 support, v0.11.0 required Python >=3.8, and v0.14.1 dropped Python 3.8 support. Always check `requires_python`.
- breaking The default backend for `lambdify` changed to LLVM when available in v0.10.0. This might alter performance or require LLVM dependencies if relying on `lambdify`'s default behavior.
- breaking Conversion of `ImmutableMatrix` to SymPy/Sage results in immutable matrices since v0.10.0. Also, `Mul` and `Add` operations of immutable and dense matrices result in immutable matrices since v0.9.0.
- gotcha Building `symengine` from source (e.g., via `pip install .` for development) requires `cmake` and often depends on a specific commit/tag of the core C++ SymEngine library. Mismatched versions can lead to build failures.
- gotcha Interoperability with `SymPy` can introduce performance overhead if SymEngine objects are frequently converted to SymPy objects and vice versa, as this might involve traversing expression trees and creating new objects.
Install
-
pip install symengine -
conda install python-symengine -c conda-forge
Imports
- symengine
import symengine
- Symbol
from symengine import Symbol
- sin
from symengine import sin
Quickstart
import symengine
from symengine import Symbol, sin
x = Symbol('x')
y = Symbol('y')
expr = x + y**2
print(f"Original expression: {expr}")
derivative = expr.diff(y)
print(f"Derivative with respect to y: {derivative}")
# Substitution
substituted_expr = expr.subs(x, sin(y))
print(f"Expression after substituting x=sin(y): {substituted_expr}")