Skip to main content

Translate SymPy expressions to Google Highway SIMD expressions

Project description

sympy2hwy

sympy2hwy translates SymPy expressions into Google Highway SIMD function calls. It emits the explicit function API instead of overloaded C++ operators, so generated expressions remain usable with sizeless SIMD types such as SVE and RVV.

from sympy import symbols
from sympy2hwy import hwycode

a, b = symbols("a b")
hwycode(a + b)
# 'hn::Add(a, b)'

Security warning

Never pass untrusted strings to hwycode or the command-line interface.

String inputs are parsed with sympy.sympify. SymPy's string parsing uses Python evaluation mechanisms and is not a sandbox. A malicious expression can execute arbitrary Python code with the permissions of the running process.

For input originating outside your trust boundary, do not use this package's string interface or CLI.

Installation

python -m pip install -e .

The project requires Python 3.10 or newer and SymPy 1.14 or newer.

Python API

from sympy2hwy import HighwayPrinter, hwycode

hwycode("x - y / 3")
# 'hn::Sub(x, hn::Mul(hn::Set(d, 0.3333333333333333), y))'

hwycode("exp(x)", use_contrib=True)
# 'hn::Exp(d, x)'

hwycode("exp(x)", use_contrib=True, fast=True)
# 'hn::FastExp(d, x)'

hwycode("x + 2", namespace="project::", tag="df")
# 'project::Add(x, project::Set(df, 2))'

The public function is:

hwycode(
    expr,
    namespace="hn::",
    tag="d",
    fast=False,
    use_contrib=False,
)
  • expr may be a trusted string or a pre-built SymPy Expr.
  • namespace is emitted exactly as provided, including the trailing ::.
  • tag selects Highway overloads and broadcasts numeric constants.
  • use_contrib=True enables functions from hwy/contrib/math.
  • fast=True selects available Fast*, approximate reciprocal, and approximate reciprocal-square-root operations. Functions without a fast equivalent remain accurate when contrib support is enabled.

HighwayPrinter exposes the same configuration for direct printer use.

Command line

The CLI is intended only for trusted expressions:

sympy2hwy 'a + b'
sympy2hwy --contrib --fast 'exp(x) + cos(y)'
sympy2hwy --namespace 'project::' --tag df 'x / y'

Supported expressions

Core mode supports symbols, numeric broadcasts, arithmetic, integer powers, square roots, absolute value, min/max, modulo, floor, and ceiling. Contrib mode adds supported trigonometric, inverse, hyperbolic, exponential, logarithmic, error, gamma, cube-root, and general-power functions.

SymPy rationals are converted to binary64 decimal literals before being broadcast. This intentionally loses exact rational precision.

Expressions without a direct Highway translation raise UnsupportedExpressionError. This includes comparisons, boolean expressions, Piecewise, matrices, calculus objects, complex values, infinities, NaN, and unknown functions. The output is a single C++ expression; callers must provide the required Highway includes, namespace alias, tag, and vector declarations.

Development

python -m pytest

The test suite covers the printer API, core and contrib mappings, fast mode, strict rejection, configuration options, and CLI behavior.

Publishing

Pushing any Git tag, or manually running the Build and publish workflow in GitHub Actions, tests the package, builds both wheel and source distributions, checks their metadata, and publishes them to PyPI.

Publishing uses PyPI Trusted Publishing rather than a stored API token. Before the first release, configure a trusted publisher for the sympy2hwy PyPI project with these values:

  • Owner and repository: this GitHub repository's owner and name.
  • Workflow filename: publish.yml.
  • Environment: pypi.

Create a GitHub environment named pypi; adding required reviewers is strongly recommended, especially because the workflow supports manual publication. The version in pyproject.toml must be new on PyPI before triggering a release.

git tag v0.1.0
git push origin v0.1.0

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

sympy2hwy-0.1.0.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

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

sympy2hwy-0.1.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file sympy2hwy-0.1.0.tar.gz.

File metadata

  • Download URL: sympy2hwy-0.1.0.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sympy2hwy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5c14ed2fc435911c8d830dfade5854db901cb9d18af0c93c87f39feeca4a37e5
MD5 0f80d6e2822920745ee0ab2e48843650
BLAKE2b-256 5ab7751a530105c4dfe6ce913cbb16b8aa1fa5c1b5167c034b52887fdbe7a364

See more details on using hashes here.

Provenance

The following attestation bundles were made for sympy2hwy-0.1.0.tar.gz:

Publisher: publish.yml on LXYan2333/sympy2hwy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sympy2hwy-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: sympy2hwy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sympy2hwy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2e1f215b6d59b416761d58ed3030c5b57eac394cb0bc6e56bd6d13305f2923d1
MD5 3f87af862f4254673121045cd37955a3
BLAKE2b-256 2873736ca1e9b3ad3db95ab13027c07cd0d4b52991ec8f399643564a061830c1

See more details on using hashes here.

Provenance

The following attestation bundles were made for sympy2hwy-0.1.0-py3-none-any.whl:

Publisher: publish.yml on LXYan2333/sympy2hwy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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