Skip to main content

A minimalistic symbolic package.

Project description

Package CodeStyle License PyVersion CI Lint

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, substitute, translate
>>> 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 = substitute(expr1, {x: 5, y: 2})
>>> print(expr2)
5 + 3 * 2

The output is still a symbolic expression, which you can translate:

>>> translate(expr2)
11

Notice that we also got a warning (No libsl provided, defaulting to Python standard library.). This is because translating an expression requires an actual library implementation, named 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
>>> translate(expr2, libstd)
11

You can also import it with the right name and it will be found

>>> from symbolite.impl import libstd as libsl
>>> translate(expr2)
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 real, 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 real
>>> 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 translated.

>>> translate(expr3)
2.6327476856711
>>> translate(expr4)
18.0

Three other implementations are provided: NumPy, SymPy, JAX.

>>> from symbolite.impl import libnumpy
>>> translate(expr3, libsl=libnumpy)
np.float64(2.6327476856711183)
>>> from symbolite.impl import libsympy
>>> translate(expr3, 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 translate an expression. However, when using an implementation like SymPy that contains a Scalar object you can still translate.

>>> from symbolite.impl import libsympy as libsl
>>> translate(3. * scalar.cos(x), 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.

We provide a simple way to call user defined functions.

>>> from symbolite import UserFunction
>>> def abs_times_two(x: float) -> float:
...     return 2 * abs(x)
>>> uf = UserFunction.from_function(abs_times_two)
>>> uf
UserFunction(name='abs_times_two', namespace='user')
>>> translate(uf(-1))
2

and you can register implementations for other backends:

>>> def np_abs_times_two(x: float) -> float:
...     return 2 * np.abs(x)
>>> uf.register_impl(libnumpy, np_abs_times_two)
>>> translate(uf(-1), libnumpy)
2

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

symbolite-1.0.1.tar.gz (29.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

symbolite-1.0.1-py3-none-any.whl (56.9 kB view details)

Uploaded Python 3

File details

Details for the file symbolite-1.0.1.tar.gz.

File metadata

  • Download URL: symbolite-1.0.1.tar.gz
  • Upload date:
  • Size: 29.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for symbolite-1.0.1.tar.gz
Algorithm Hash digest
SHA256 2b109c6f8ca22055bd2dcdcb4fca732b959c506d5d4cd1187fd539147e4f33c4
MD5 c836e1f73698f5b68f63604032bbab9e
BLAKE2b-256 41d432f736fd3f5aa02a55d79f6490dcefc4b6e9046a70b7529d8fc15a99603f

See more details on using hashes here.

File details

Details for the file symbolite-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: symbolite-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 56.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for symbolite-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 713c9a22b22913f389420e66d0cd7ee0ae3160d0c96b6f969d2c70171fcc13a2
MD5 ae6eb98dfaf2aa2330be24c9ff54307c
BLAKE2b-256 928815c493d8bef048fdc86100192302a16ab1a706622eeb2f22e82823f03b1a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page