sympytensor


Namesympytensor JSON
Version 1.3.1 PyPI version JSON
download
home_pageNone
SummaryPrint sympy expressions to pytensor graphs
upload_time2025-03-19 05:40:25
maintainerNone
docs_urlNone
authorNone
requires_python>=3.10
licenseMIT License
keywords computer algebra pytensor sympy
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Sympytensor

A tool for converting Sympy expressions to a Pytensor graph, with support for working with PyMC models.

# Installation

```text
pip install sympytensor
```

# Examples

## Writing expressions to pytensor
Two functions are provided to convert sympy expressions:

- `as_tensor` converts a sympy expression to a `pytensor` symbolic graph
- `pytensor_function` returns a compiled `pytensor.function` that computes the expression. Keyword arguments to
`pytensor.function` can be provided as `**kwargs`

Use sympy to compute 1d splines, then convert the splines to a symbolic pytensor variable:

```python
import pytensor
import sympy as sp
from sympytensor import as_tensor
from sympy.abc import x

x_data = [0, 1, 2, 3, 4, 5]
y_data = [3, 6, 5, 7, 9, 1]

s = sp.interpolating_spline(d=3, x=x, X=x_data, Y=y_data)
s_pt = as_tensor(s)
```

This generates the following function graph:
```python
pytensor.dprint(s_pt)

>>>Out: Elemwise{switch,no_inplace} [id A]
>>>      |Elemwise{and_,no_inplace} [id B]
>>>      | |Elemwise{ge,no_inplace} [id C]
>>>      | | |x [id D]
>>>      | | |TensorConstant{0} [id E]
>>>      | |Elemwise{le,no_inplace} [id F]
>>>      |   |x [id D]
>>>      |   |TensorConstant{2} [id G]
>>>      |Elemwise{add,no_inplace} [id H]
>>>      | |TensorConstant{3} [id I]
>>>      | |Elemwise{mul,no_inplace} [id J]
>>>      | | |Elemwise{true_div,no_inplace} [id K]
>>>      | | | |TensorConstant{-33} [id L]
>>>      | | | |TensorConstant{5} [id M]
>>>      | | |Elemwise{pow,no_inplace} [id N]
>>>      | |   |x [id D]
>>>      | |   |TensorConstant{2} [id O]
>>>      | |Elemwise{mul,no_inplace} [id P]
>>>      | | |Elemwise{true_div,no_inplace} [id Q]
>>>      | | | |TensorConstant{23} [id R]
>>>      | | | |TensorConstant{15} [id S]
>>>      | | |Elemwise{pow,no_inplace} [id T]
>>>      | |   |x [id D]
>>>      | |   |TensorConstant{3} [id U]
>>>      | |Elemwise{mul,no_inplace} [id V]
>>>      |   |Elemwise{true_div,no_inplace} [id W]
>>>      |   | |TensorConstant{121} [id X]
>>>      |   | |TensorConstant{15} [id Y]
>>>      |   |x [id D]
>>>      |Elemwise{switch,no_inplace} [id Z]
>>>        |Elemwise{and_,no_inplace} [id BA]
>>>        | |Elemwise{ge,no_inplace} [id BB]
>>>        | | |x [id D]
>>>        | | |TensorConstant{2} [id BC]
>>>        | |Elemwise{le,no_inplace} [id BD]
>>>        |   |x [id D]
>>>        |   |TensorConstant{3} [id BE]
>>>        |Elemwise{add,no_inplace} [id BF]
>>>        | |Elemwise{true_div,no_inplace} [id BG]
>>>        | | |TensorConstant{103} [id BH]
>>>        | | |TensorConstant{5} [id BI]
>>>        | |Elemwise{mul,no_inplace} [id BJ]
>>>        | | |Elemwise{true_div,no_inplace} [id BK]
>>>        | | | |TensorConstant{-55} [id BL]
>>>        | | | |TensorConstant{3} [id BM]
>>>        | | |x [id D]
>>>        | |Elemwise{mul,no_inplace} [id BN]
>>>        | | |Elemwise{true_div,no_inplace} [id BO]
>>>        | | | |TensorConstant{-2} [id BP]
>>>        | | | |TensorConstant{3} [id BQ]
>>>        | | |Elemwise{pow,no_inplace} [id BR]
>>>        | |   |x [id D]
>>>        | |   |TensorConstant{3} [id BS]
>>>        | |Elemwise{mul,no_inplace} [id BT]
>>>        |   |Elemwise{true_div,no_inplace} [id BU]
>>>        |   | |TensorConstant{33} [id BV]
>>>        |   | |TensorConstant{5} [id BW]
>>>        |   |Elemwise{pow,no_inplace} [id BX]
>>>        |     |x [id D]
>>>        |     |TensorConstant{2} [id BY]
>>>        |Elemwise{switch,no_inplace} [id BZ]
>>>          |Elemwise{and_,no_inplace} [id CA]
>>>          | |Elemwise{ge,no_inplace} [id CB]
>>>          | | |x [id D]
>>>          | | |TensorConstant{3} [id CC]
>>>          | |Elemwise{le,no_inplace} [id CD]
>>>          |   |x [id D]
>>>          |   |TensorConstant{5} [id CE]
>>>          |Elemwise{add,no_inplace} [id CF]
>>>          | |TensorConstant{53} [id CG]
>>>          | |Elemwise{mul,no_inplace} [id CH]
>>>          | | |Elemwise{true_div,no_inplace} [id CI]
>>>          | | | |TensorConstant{-761} [id CJ]
>>>          | | | |TensorConstant{15} [id CK]
>>>          | | |x [id D]
>>>          | |Elemwise{mul,no_inplace} [id CL]
>>>          | | |Elemwise{true_div,no_inplace} [id CM]
>>>          | | | |TensorConstant{-28} [id CN]
>>>          | | | |TensorConstant{15} [id CO]
>>>          | | |Elemwise{pow,no_inplace} [id CP]
>>>          | |   |x [id D]
>>>          | |   |TensorConstant{3} [id CQ]
>>>          | |Elemwise{mul,no_inplace} [id CR]
>>>          |   |Elemwise{true_div,no_inplace} [id CS]
>>>          |   | |TensorConstant{87} [id CT]
>>>          |   | |TensorConstant{5} [id CU]
>>>          |   |Elemwise{pow,no_inplace} [id CV]
>>>          |     |x [id D]
>>>          |     |TensorConstant{2} [id CW]
>>>          |TensorConstant{nan} [id CX]
```

## Inserting PyMC random variables into an expression

The `SympyDeterministic` function works as a drop-in replacement for pm.Deterministic, except a `sympy` expression is
expected. It will automatically search the active model context for random variables corresponding to symbols in the
expression and make substitutions.

Here is an example using sympy to symbolically compute the inverse of a matrix, which is then used in a model:

```python
from sympytensor import SympyDeterministic
import pymc as pm
import sympy as sp
from sympy.abc import a, b, c, d

A = sp.Matrix([[a, b],
               [c, d]])
A_inv = sp.matrices.Inverse(A).doit()

with pm.Model() as m:
    a_pm = pm.Normal('a')
    b_pm = pm.Normal('b')
    c_pm = pm.Normal('c')
    c_pm = pm.Normal('d')
    A_inv_pm = SympyDeterministic('A_inv', A_inv)
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "sympytensor",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "computer algebra, pytensor, sympy",
    "author": null,
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/39/6f/567e529ee4705f79e4ca363e44f2acfb4d9d299489a766fa50ffc4dccfe7/sympytensor-1.3.1.tar.gz",
    "platform": null,
    "description": "# Sympytensor\n\nA tool for converting Sympy expressions to a Pytensor graph, with support for working with PyMC models.\n\n# Installation\n\n```text\npip install sympytensor\n```\n\n# Examples\n\n## Writing expressions to pytensor\nTwo functions are provided to convert sympy expressions:\n\n- `as_tensor` converts a sympy expression to a `pytensor` symbolic graph\n- `pytensor_function` returns a compiled `pytensor.function` that computes the expression. Keyword arguments to\n`pytensor.function` can be provided as `**kwargs`\n\nUse sympy to compute 1d splines, then convert the splines to a symbolic pytensor variable:\n\n```python\nimport pytensor\nimport sympy as sp\nfrom sympytensor import as_tensor\nfrom sympy.abc import x\n\nx_data = [0, 1, 2, 3, 4, 5]\ny_data = [3, 6, 5, 7, 9, 1]\n\ns = sp.interpolating_spline(d=3, x=x, X=x_data, Y=y_data)\ns_pt = as_tensor(s)\n```\n\nThis generates the following function graph:\n```python\npytensor.dprint(s_pt)\n\n>>>Out: Elemwise{switch,no_inplace} [id A]\n>>>      |Elemwise{and_,no_inplace} [id B]\n>>>      | |Elemwise{ge,no_inplace} [id C]\n>>>      | | |x [id D]\n>>>      | | |TensorConstant{0} [id E]\n>>>      | |Elemwise{le,no_inplace} [id F]\n>>>      |   |x [id D]\n>>>      |   |TensorConstant{2} [id G]\n>>>      |Elemwise{add,no_inplace} [id H]\n>>>      | |TensorConstant{3} [id I]\n>>>      | |Elemwise{mul,no_inplace} [id J]\n>>>      | | |Elemwise{true_div,no_inplace} [id K]\n>>>      | | | |TensorConstant{-33} [id L]\n>>>      | | | |TensorConstant{5} [id M]\n>>>      | | |Elemwise{pow,no_inplace} [id N]\n>>>      | |   |x [id D]\n>>>      | |   |TensorConstant{2} [id O]\n>>>      | |Elemwise{mul,no_inplace} [id P]\n>>>      | | |Elemwise{true_div,no_inplace} [id Q]\n>>>      | | | |TensorConstant{23} [id R]\n>>>      | | | |TensorConstant{15} [id S]\n>>>      | | |Elemwise{pow,no_inplace} [id T]\n>>>      | |   |x [id D]\n>>>      | |   |TensorConstant{3} [id U]\n>>>      | |Elemwise{mul,no_inplace} [id V]\n>>>      |   |Elemwise{true_div,no_inplace} [id W]\n>>>      |   | |TensorConstant{121} [id X]\n>>>      |   | |TensorConstant{15} [id Y]\n>>>      |   |x [id D]\n>>>      |Elemwise{switch,no_inplace} [id Z]\n>>>        |Elemwise{and_,no_inplace} [id BA]\n>>>        | |Elemwise{ge,no_inplace} [id BB]\n>>>        | | |x [id D]\n>>>        | | |TensorConstant{2} [id BC]\n>>>        | |Elemwise{le,no_inplace} [id BD]\n>>>        |   |x [id D]\n>>>        |   |TensorConstant{3} [id BE]\n>>>        |Elemwise{add,no_inplace} [id BF]\n>>>        | |Elemwise{true_div,no_inplace} [id BG]\n>>>        | | |TensorConstant{103} [id BH]\n>>>        | | |TensorConstant{5} [id BI]\n>>>        | |Elemwise{mul,no_inplace} [id BJ]\n>>>        | | |Elemwise{true_div,no_inplace} [id BK]\n>>>        | | | |TensorConstant{-55} [id BL]\n>>>        | | | |TensorConstant{3} [id BM]\n>>>        | | |x [id D]\n>>>        | |Elemwise{mul,no_inplace} [id BN]\n>>>        | | |Elemwise{true_div,no_inplace} [id BO]\n>>>        | | | |TensorConstant{-2} [id BP]\n>>>        | | | |TensorConstant{3} [id BQ]\n>>>        | | |Elemwise{pow,no_inplace} [id BR]\n>>>        | |   |x [id D]\n>>>        | |   |TensorConstant{3} [id BS]\n>>>        | |Elemwise{mul,no_inplace} [id BT]\n>>>        |   |Elemwise{true_div,no_inplace} [id BU]\n>>>        |   | |TensorConstant{33} [id BV]\n>>>        |   | |TensorConstant{5} [id BW]\n>>>        |   |Elemwise{pow,no_inplace} [id BX]\n>>>        |     |x [id D]\n>>>        |     |TensorConstant{2} [id BY]\n>>>        |Elemwise{switch,no_inplace} [id BZ]\n>>>          |Elemwise{and_,no_inplace} [id CA]\n>>>          | |Elemwise{ge,no_inplace} [id CB]\n>>>          | | |x [id D]\n>>>          | | |TensorConstant{3} [id CC]\n>>>          | |Elemwise{le,no_inplace} [id CD]\n>>>          |   |x [id D]\n>>>          |   |TensorConstant{5} [id CE]\n>>>          |Elemwise{add,no_inplace} [id CF]\n>>>          | |TensorConstant{53} [id CG]\n>>>          | |Elemwise{mul,no_inplace} [id CH]\n>>>          | | |Elemwise{true_div,no_inplace} [id CI]\n>>>          | | | |TensorConstant{-761} [id CJ]\n>>>          | | | |TensorConstant{15} [id CK]\n>>>          | | |x [id D]\n>>>          | |Elemwise{mul,no_inplace} [id CL]\n>>>          | | |Elemwise{true_div,no_inplace} [id CM]\n>>>          | | | |TensorConstant{-28} [id CN]\n>>>          | | | |TensorConstant{15} [id CO]\n>>>          | | |Elemwise{pow,no_inplace} [id CP]\n>>>          | |   |x [id D]\n>>>          | |   |TensorConstant{3} [id CQ]\n>>>          | |Elemwise{mul,no_inplace} [id CR]\n>>>          |   |Elemwise{true_div,no_inplace} [id CS]\n>>>          |   | |TensorConstant{87} [id CT]\n>>>          |   | |TensorConstant{5} [id CU]\n>>>          |   |Elemwise{pow,no_inplace} [id CV]\n>>>          |     |x [id D]\n>>>          |     |TensorConstant{2} [id CW]\n>>>          |TensorConstant{nan} [id CX]\n```\n\n## Inserting PyMC random variables into an expression\n\nThe `SympyDeterministic` function works as a drop-in replacement for pm.Deterministic, except a `sympy` expression is\nexpected. It will automatically search the active model context for random variables corresponding to symbols in the\nexpression and make substitutions.\n\nHere is an example using sympy to symbolically compute the inverse of a matrix, which is then used in a model:\n\n```python\nfrom sympytensor import SympyDeterministic\nimport pymc as pm\nimport sympy as sp\nfrom sympy.abc import a, b, c, d\n\nA = sp.Matrix([[a, b],\n               [c, d]])\nA_inv = sp.matrices.Inverse(A).doit()\n\nwith pm.Model() as m:\n    a_pm = pm.Normal('a')\n    b_pm = pm.Normal('b')\n    c_pm = pm.Normal('c')\n    c_pm = pm.Normal('d')\n    A_inv_pm = SympyDeterministic('A_inv', A_inv)\n```\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "Print sympy expressions to pytensor graphs",
    "version": "1.3.1",
    "project_urls": null,
    "split_keywords": [
        "computer algebra",
        " pytensor",
        " sympy"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "c6975a97ff96168baccd461d71405f6a2e0d51eb1caf5674753f959b5508f4e5",
                "md5": "59020fccff70b814e49ce588a79a6c14",
                "sha256": "ef193fd67ed2c39e15b7a78c6d586caf28aaf5174f956830b25f6db30bbd8bbc"
            },
            "downloads": -1,
            "filename": "sympytensor-1.3.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "59020fccff70b814e49ce588a79a6c14",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 11246,
            "upload_time": "2025-03-19T05:40:24",
            "upload_time_iso_8601": "2025-03-19T05:40:24.185492Z",
            "url": "https://files.pythonhosted.org/packages/c6/97/5a97ff96168baccd461d71405f6a2e0d51eb1caf5674753f959b5508f4e5/sympytensor-1.3.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "396f567e529ee4705f79e4ca363e44f2acfb4d9d299489a766fa50ffc4dccfe7",
                "md5": "a56ee00bbfd4f5a57a5cd4dd28ec334e",
                "sha256": "6216bf662ccd33ee3ea1a18e6d61bc08b0a3a3f88f57682d8265bdf76994a793"
            },
            "downloads": -1,
            "filename": "sympytensor-1.3.1.tar.gz",
            "has_sig": false,
            "md5_digest": "a56ee00bbfd4f5a57a5cd4dd28ec334e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 12929,
            "upload_time": "2025-03-19T05:40:25",
            "upload_time_iso_8601": "2025-03-19T05:40:25.622416Z",
            "url": "https://files.pythonhosted.org/packages/39/6f/567e529ee4705f79e4ca363e44f2acfb4d9d299489a766fa50ffc4dccfe7/sympytensor-1.3.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-03-19 05:40:25",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "sympytensor"
}
        
Elapsed time: 0.99702s