A minimalistic symbolic package.
Project description
symbolite: a minimalistic symbolic python package
Symbolite allows you to create symbolic mathematical expressions. Just create a symbol (or more) and operate with them as you will normally do in Python.
>>> from symbolite import Symbol
>>> x = Symbol("x")
>>> y = Symbol("y")
>>> expr1 = x + 3 * y
>>> print(expr1)
x + 3 * y
An expression is just an unnamed Symbol. You can easily replace the symbols by the desired value.
>>> expr2 = expr1.subs_by_name(x=5, y=2)
>>> print(expr2)
5 + 3 * 2
The output is still a symbolic expression, which you can evaluate:
>>> expr2.eval()
11
Notice that we also got a warning (No libsl provided, defaulting to Python standard library.
).
This is because evaluating an expression requires a actual library implementation,
name usually as libsl
. The default one just uses python's math module.
You can avoid this warning by explicitely providing an libsl
implementation.
>>> from symbolite.impl import libstd
>>> expr2.eval(libstd)
11
You can also import it with the right name and it will be found
>>> from symbolite.impl import libstd as libsl
>>> expr2.eval()
11
In addition to the Symbol
class, there is also a Scalar
and Vector
classes
to represent integer, floats or complex numbers, and an array of those.
>>> from symbolite import Scalar, Vector
>>> x = Scalar("x")
>>> y = Scalar("y")
>>> v = Vector("v")
>>> expr1 = x + 3 * y
>>> print(expr1)
x + 3 * y
>>> print(2 * v)
2 * v
Mathematical functions that operate on scalars are available in the scalar
module.
>>> from symbolite import scalar
>>> expr3 = 3. * scalar.cos(0.5)
>>> print(expr3)
3.0 * scalar.cos(0.5)
Mathematical functions that operate on vectors are available in the vector
module.
>>> from symbolite import vector
>>> expr4 = 3. * vector.sum((1, 2, 3))
>>> print(expr4)
3.0 * vector.sum((1, 2, 3))
Notice that functions are named according to the python math module. Again, this is a symbolic expression until evaluated.
>>> expr3.eval()
2.6327476856711
>>> expr4.eval()
18.0
Three other implementations are provided: NumPy, SymPy, JAX.
>>> from symbolite.impl import libnumpy
>>> expr3.eval(libsl=libnumpy)
2.6327476856711
>>> from symbolite.impl import libsympy
>>> expr3.eval(libsl=libsympy)
2.6327476856711
(notice that the way that the different libraries round and display may vary)
In general, all symbols must be replaced by values in order to evaluate an expression. However, when using an implementation like SymPy that contains a Scalar object you can still evaluate.
>>> from symbolite.impl import libsympy as libsl
>>> (3. * scalar.cos(x).eval(libsl))
3.0*cos(x)
which is actually a SymPy expression with a SymPy symbol (x
).
And by the way, checkout vectorize
and auto_vectorize
functions
in the vector module.
Installing:
pip install -U symbolite
FAQ
Q: Is symbolite a replacement for SymPy?
A: No
Q: Does it aim to be a replacement for SymPy in the future?
A: No
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 Distribution
File details
Details for the file symbolite-0.5.0.tar.gz
.
File metadata
- Download URL: symbolite-0.5.0.tar.gz
- Upload date:
- Size: 24.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ae94f6a14aa13f39efff4d59fda0b8ba5e5e23aa1af2598fdb73a4e418f46e1 |
|
MD5 | c2abd37650f36d12726e549d9d55135a |
|
BLAKE2b-256 | dec4d9fd21d34bd02aab2d6b6c9503384fa70f58f8a1dfcad72882dbae85ae92 |
File details
Details for the file symbolite-0.5.0-py3-none-any.whl
.
File metadata
- Download URL: symbolite-0.5.0-py3-none-any.whl
- Upload date:
- Size: 31.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c906afafa0d33a9e9856196871e06c217313528813d7b5ee9e3b4fefbe145844 |
|
MD5 | 3c82641939a319d03df9b81953fdc69e |
|
BLAKE2b-256 | 53fca26b5ffbdcc58c59a0c96434a89e2f2990741be6d118cfb7917b2a093cdd |