Metadata-Version: 2.4
Name: sympy2hwy
Version: 0.1.0
Summary: Translate SymPy expressions to Google Highway SIMD expressions
Author: LXYan2333
License-Expression: MIT
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: sympy>=1.14
Dynamic: license-file

# 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.

```python
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

```bash
python -m pip install -e .
```

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

## Python API

```python
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:

```python
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:

```bash
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

```bash
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.

```bash
git tag v0.1.0
git push origin v0.1.0
```
