{"id":15043612,"url":"https://github.com/matt-graham/symnum","last_synced_at":"2025-06-27T02:36:49.017Z","repository":{"id":57472866,"uuid":"247063908","full_name":"matt-graham/symnum","owner":"matt-graham","description":"Symbolically construct NumPy functions and their derivatives","archived":false,"fork":false,"pushed_at":"2024-10-05T22:27:42.000Z","size":12579,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-15T00:36:27.501Z","etag":null,"topics":["derivatives","numpy","sympy"],"latest_commit_sha":null,"homepage":"https://matt-graham.github.io/symnum/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/matt-graham.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":"CITATION.cff","codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-03-13T12:14:45.000Z","updated_at":"2024-10-16T07:34:39.000Z","dependencies_parsed_at":"2024-10-01T05:01:28.320Z","dependency_job_id":"dd783c5c-5ba3-42a2-9bb4-b252a6c9d81f","html_url":"https://github.com/matt-graham/symnum","commit_stats":{"total_commits":44,"total_committers":1,"mean_commits":44.0,"dds":0.0,"last_synced_commit":"c7238f436c748a0fe2677cc194f176071dfc912c"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/matt-graham/symnum","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matt-graham%2Fsymnum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matt-graham%2Fsymnum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matt-graham%2Fsymnum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matt-graham%2Fsymnum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/matt-graham","download_url":"https://codeload.github.com/matt-graham/symnum/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/matt-graham%2Fsymnum/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262179223,"owners_count":23271190,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["derivatives","numpy","sympy"],"created_at":"2024-09-24T20:49:20.530Z","updated_at":"2025-06-27T02:36:48.985Z","avatar_url":"https://github.com/matt-graham.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\u003cdiv style=\"text-align: center;\" align=\"center\"\u003e\n\n\u003cimg src=\"https://raw.githubusercontent.com/matt-graham/symnum/main/images/logomark-dark-background.svg\" alt=\"SymNum logo\" width=\"120\"/\u003e\n\n\u003ch1\u003eSymNum\u003c/h1\u003e\n\n\u003ca href=\"https://badge.fury.io/py/symnum\"\u003e\n  \u003cimg src=\"https://badge.fury.io/py/symnum.svg\" alt=\"PyPI version\"/\u003e\n\u003c/a\u003e\n\u003ca href=\"https://zenodo.org/badge/latestdoi/247063908\"\u003e\n  \u003cimg src=\"https://zenodo.org/badge/247063908.svg\" alt=\"Zenodo DOI badge\"\u003e\n\u003c/a\u003e\n\u003ca href=\"https://github.com/matt-graham/symnum/actions/workflows/tests.yml\"\u003e\n  \u003cimg src=\"https://github.com/matt-graham/symnum/actions/workflows/tests.yml/badge.svg\" alt=\"Test status\" /\u003e\n\u003c/a\u003e\n\u003ca href=\"https://matt-graham.github.io/symnum\"\u003e\n  \u003cimg src=\"https://github.com/matt-graham/symnum/actions/workflows/docs.yml/badge.svg\" alt=\"Documentation status\" /\u003e\n\u003c/a\u003e\n\u003c/div\u003e\n\n\n## What is SymNum?\n\nSymNum is a Python package that acts a bridge between\n[NumPy](https://numpy.org/) and [SymPy](https://www.sympy.org/), providing a\nNumPy-like interface that can be used to symbolically define functions which\ntake arrays as arguments and return arrays or scalars as values. A series of\n[Autograd](https://github.com/HIPS/autograd) style functional differential\noperators are also provided to construct derivatives of symbolic functions,\nwith the option to generate NumPy code to numerically evaluate these derivative\nfunctions.\n\n## Why use SymNum instead of Autograd or JAX?\n\nSymNum is intended for use in generating the derivatives of 'simple' functions\nwhich **compose a relatively small number of operations** and act on **small\narray inputs**. By reducing interpreter overheads it can produce code which is\ncheaper to evaluate than corresponding\n[Autograd](https://github.com/HIPS/autograd)  or\n[JAX](https://github.com/google/jax) functions (including those using  [JIT\ncompilation](https://jax.readthedocs.io/en/latest/notebooks/quickstart.html#Using-jit-to-speed-up-functions))\nin such cases, and which can be serialised with the inbuilt Python `pickle`\nlibrary allowing use for example in libraries which use `multiprocessing` to\nimplement parallelisation across multiple processes.\n\nThe original motivating use case for SymNum was to allow automatically\nconstructing the  derivatives of the sorts of functions of low dimensional\ninputs which are  commonly used as toy examples to demonstrate inference and\noptimisation algorithms. In these cases while manually deriving and\nimplementing derivatives is generally possible, this can still be labourious\nand error prone, and distract from the purpose of giving a simple show case of\nan algorithm. On the other hand the derivative functions produced by Autograd\nand JAX in such cases are often much slower than manual implementations. SymNum\ntries to fill this gap by providing the flexibility and ease of use that comes\nfrom automatic differentiation while still being efficient for small toy\nexamples.\n\n\n## Doesn't SymPy already have array support and allow export of NumPy functions?\n\nYes: SymNum is mainly a convenience wrapper around functionality already\nprovided by SymPy to make it easier to use for those already familiar with\nNumPy and Autograd / JAX. Specifically SymPy has several inbuilt array like\nclasses, which can be broadly split in to the [array\ntypes](https://docs.sympy.org/latest/modules/tensor/array.html) defined  in\n`sympy.tensor.array` and the  [matrix\ntypes](https://docs.sympy.org/latest/modules/matrices/matrices.html)  defined\nin `sympy.matrices`.\n\nEach of the inbuilt array and matrix classes supports some of the functionality\nof NumPy's core `ndarray` class, however both have some issues which means they\ndon't provide an easy drop-in replacement, with for example matrix classes\nbeing limited to two-dimensions, while both the inbuilt array and matrix\nclasses do not support the full broadcasting and operator overloading semantics\nof NumPy arrays. The `SymbolicArray` class in `symnum.array` aims to provide a\nmore `ndarray` like interface, supporting broadcasting of elementwise binary\narithmetic operations like `*`, `/`, `+` and `-`, elementwise NumPy ufunc-like\nmathematical functions like `numpy.log` via the `symnum.numpy` module, simple\narray contractions over potentially multiple axes with the `sum` and `prod` \nmethods and matrix multiplication with the `@` operator.\n\nSimilarly SymPy has extensive built in [code generation](https://docs.sympy.org/latest/modules/codegen.html) \nfeatures, including the\n[`lambdify`](https://docs.sympy.org/latest/modules/utilities/lambdify.html) \nfunction which supports generation of functions which operate on\nNumPy arrays. It can be non-trivial however to use these functions to generate\ncode which perform indexing operations on array inputs, or to construct higher\norder functions which return [closures](https://en.wikipedia.org/wiki/Closure_(computer_programming)). \nSymNum builds on top of the SymPy's code generation functionality to allow\nsimpler generation of NumPy functions using such features.\n\n\n## Example\n\n```Python\nimport numpy as np\nimport symnum.numpy as snp\nfrom symnum import named_array, numpify_func, jacobian\n\n# Define a function using the symnum.numpy interface.\ndef func(x):\n    return (snp.array([[1., -0.5], [-2., 3.]]) @ \n            snp.array([snp.cos(-x[1]**2 + 3 * x[0]), snp.sin(x[0] - 1)]))\n\n# Create a named symbolic array to act as input and evaluate func symbolically.\nx = named_array(name='x', shape=2)\ny = func(x)\n\n# Alternatively we can symbolically 'trace' func and use this to generate a\n# NumPy function which accepts ndarray arguments. To allow the tracing we\n# need to manually specify the shapes of the arguments to the function.\nx_np = np.array([0.2, 1.1])\nfunc_np = numpify_func(func, x.shape)\ny_np = func_np(x_np)\n\n# We can also use a similar approach to generate a NumPy function to evaluate\n# the Jacobian of func on ndarray arguments. The numpified function func_np \n# stores the symbolic function used to generate it and details of the argument\n# shapes and so we can pass it as a sole argument to jacobian without\n# specifying the argument shapes.\njacob_func_np = jacobian(func_np)\ndy_dx_np = jacob_func_np(x_np)\n```\n\nSee also the [demo Jupyter notebook](https://github.com/matt-graham/symnum/blob/main/Demo.ipynb).\n\n\n\n## Current limitations\n\nSymNum only supports a small subset of the NumPy API at the moment. A\nnon-exhaustive list of things that don't currently work\n\n  * Indexed / sliced assignment to arrays e.g. `a[i, j] = x` and `a[:, j] = y`\n  * Matrix multiplication with `@` of arrays with dimensions \u003e 2.\n  * Linear algebra operations in `numpy.linalg` and FFT functions in `numpy.fft`.\n  * All `scipy` functions such as the special functions in `scipy.special`.\n  * Similar to the limitations on using [Python control flow with the JIT\n    transformation in JAX](https://jax.readthedocs.io/en/latest/notebooks/Common_Gotchas_in_JAX.html#%F0%9F%94%AA-Control-Flow),\n    the symbolic tracing of functions with SymNum requires that only control\n    flows that does not depend on the value of array arguments is used.\n\nSome of these are not fundamental limitations and SymNum's coverage will \nimprove (pull requests are very welcome!), however as the focus is on \nallowing automatic generation of derivatives of simple functions of smallish\narrays if your use case uses more complex NumPy features you are likely to \nfind Autograd or JAX to be better bets.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatt-graham%2Fsymnum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmatt-graham%2Fsymnum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmatt-graham%2Fsymnum/lists"}