libsymtorch 0.1.1__tar.gz → 0.1.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- libsymtorch-0.1.2/PKG-INFO +162 -0
- libsymtorch-0.1.2/README.md +134 -0
- {libsymtorch-0.1.1 → libsymtorch-0.1.2}/pyproject.toml +1 -1
- libsymtorch-0.1.2/src/libsymtorch.egg-info/PKG-INFO +162 -0
- {libsymtorch-0.1.1 → libsymtorch-0.1.2}/src/libsymtorch.egg-info/SOURCES.txt +1 -0
- libsymtorch-0.1.2/tests/test_benchmark_ultimate.py +264 -0
- libsymtorch-0.1.1/PKG-INFO +0 -58
- libsymtorch-0.1.1/README.md +0 -30
- libsymtorch-0.1.1/src/libsymtorch.egg-info/PKG-INFO +0 -58
- {libsymtorch-0.1.1 → libsymtorch-0.1.2}/LICENSE +0 -0
- {libsymtorch-0.1.1 → libsymtorch-0.1.2}/setup.cfg +0 -0
- {libsymtorch-0.1.1 → libsymtorch-0.1.2}/src/libsymtorch.egg-info/dependency_links.txt +0 -0
- {libsymtorch-0.1.1 → libsymtorch-0.1.2}/src/libsymtorch.egg-info/requires.txt +0 -0
- {libsymtorch-0.1.1 → libsymtorch-0.1.2}/src/libsymtorch.egg-info/top_level.txt +0 -0
- {libsymtorch-0.1.1 → libsymtorch-0.1.2}/src/symtorch/__init__.py +0 -0
- {libsymtorch-0.1.1 → libsymtorch-0.1.2}/src/symtorch/main.py +0 -0
- {libsymtorch-0.1.1 → libsymtorch-0.1.2}/tests/test_batching.py +0 -0
- {libsymtorch-0.1.1 → libsymtorch-0.1.2}/tests/test_conversion.py +0 -0
- {libsymtorch-0.1.1 → libsymtorch-0.1.2}/tests/test_numerical.py +0 -0
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: libsymtorch
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: SymPy-to-Torch conversion and numerical integration
|
|
5
|
+
Author-email: "Ibrahim H.I. Abushawish" <ibrahim.hamed2701@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/ibeuler/symtorch
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Classifier: Topic :: Scientific/Engineering :: Physics
|
|
11
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
12
|
+
Requires-Python: >=3.8
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: torch>=2.5.1
|
|
16
|
+
Requires-Dist: sympy>=1.13.1
|
|
17
|
+
Requires-Dist: torchquad>=0.5.0
|
|
18
|
+
Requires-Dist: numpy>=2.4.2
|
|
19
|
+
Requires-Dist: scipy>=1.17.0
|
|
20
|
+
Requires-Dist: loguru>=0.7.3
|
|
21
|
+
Provides-Extra: dev
|
|
22
|
+
Requires-Dist: pytest>=8; extra == "dev"
|
|
23
|
+
Requires-Dist: build>=1; extra == "dev"
|
|
24
|
+
Requires-Dist: twine>=5; extra == "dev"
|
|
25
|
+
Provides-Extra: examples
|
|
26
|
+
Requires-Dist: matplotlib>=3.8; extra == "examples"
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# symtorch (distribution: libsymtorch)
|
|
30
|
+
|
|
31
|
+
SymPy-to-Torch transcompilation and numerical integration library designed for extremely fast, batched evaluation on GPUs and CPUs.
|
|
32
|
+
|
|
33
|
+
Note: this module was first developed for [libphysics](https://github.com/ferhatpy/libphysics) — then it was split out into a standalone library to tackle generalized parallel computational bottlenecks.
|
|
34
|
+
|
|
35
|
+
## Install (dev)
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install -e .
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Install (PyPI)
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install libsymtorch
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Requirements
|
|
48
|
+
|
|
49
|
+
This project depends on the packages listed in `requirements.txt`. The primary runtime requirements are:
|
|
50
|
+
|
|
51
|
+
- Python 3.8+
|
|
52
|
+
- torch==2.5.1+cu121 (or another `torch` build appropriate for your platform)
|
|
53
|
+
- sympy==1.13.1
|
|
54
|
+
- torchquad==0.5.0
|
|
55
|
+
- numpy==2.4.2
|
|
56
|
+
- scipy==1.17.0
|
|
57
|
+
- loguru==0.7.3
|
|
58
|
+
|
|
59
|
+
To install the pinned packages, run:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
pip install -r requirements.txt
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
If you intend to use GPU acceleration, please install a `torch` wheel that matches your CUDA version (the example above is a CUDA-enabled wheel). For CPU-only environments, install the CPU `torch` wheel instead.
|
|
66
|
+
|
|
67
|
+
## Quick Usage
|
|
68
|
+
|
|
69
|
+
`symtorch` bridges the gap between SymPy's symbolic manipulation and PyTorch's highly optimized batched tensor operations. You can transcompile symbolic integrals directly into callable PyTorch engines.
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
import torch
|
|
73
|
+
import symtorch
|
|
74
|
+
import sympy as sp
|
|
75
|
+
|
|
76
|
+
# 1. Define your integrands symbolically
|
|
77
|
+
x = sp.Symbol("x", real=True)
|
|
78
|
+
p = sp.Symbol("p", real=True)
|
|
79
|
+
expr = sp.Integral(sp.exp(-p * x**2), (x, -sp.oo, sp.oo))
|
|
80
|
+
|
|
81
|
+
# 2. Compile to SymTorch engine
|
|
82
|
+
lt = symtorch.SymTorch()
|
|
83
|
+
texpr = lt.torchify(expr)
|
|
84
|
+
|
|
85
|
+
# 3. Evaluate massively batched parameter grids on accelerators
|
|
86
|
+
p_grid = torch.linspace(0.5, 100.0, 10000, dtype=torch.float64, device="cuda").unsqueeze(-1)
|
|
87
|
+
re, im = texpr.torch_integrate_batched(
|
|
88
|
+
params_values=p_grid,
|
|
89
|
+
method="gauss-legendre",
|
|
90
|
+
N=501, # Force high quadrature scaling for difficult integrands
|
|
91
|
+
device="cuda", # Target accelerator natively
|
|
92
|
+
dtype=torch.float64,
|
|
93
|
+
chunk_size_params=4096 # Automatically chunks massive batches to avoid OOM
|
|
94
|
+
)
|
|
95
|
+
print("Real part:", re)
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Parameter Reference
|
|
99
|
+
|
|
100
|
+
When evaluating continuous integrals dynamically using `torch_integrate_batched()`, you have granular control over numerical performance constraints:
|
|
101
|
+
|
|
102
|
+
- **`params_values`** (`torch.Tensor`): Array parameter map corresponding to all substituted free variables defining your symbolic integral.
|
|
103
|
+
- **`method`** (`str`): The mapped quadrature strategy (e.g. `"gauss-legendre"`).
|
|
104
|
+
- **`N`** (`int`): Limits Quadrature nodes. Higher N bounds push stability on deeply oscillatory configurations (allowing you to surpass typical recursion bottlenecks) but proportionately scales VRAM and FLOP demands.
|
|
105
|
+
- **`device`** (`str` or `torch.device`): Device mapping (e.g., `"cpu"`, `"cuda"`, `"mps"`).
|
|
106
|
+
- **`dtype`** (`torch.dtype`): Evaluative precision schema (use `torch.float64` for analytic comparisons or exponential limits).
|
|
107
|
+
- **`chunk_size_params`** (`int`): Sub-array partitioning for the grid parameters. Ex: With a $1,000,000$ point map and $N=2000$, setting this to `2048` iteratively offloads pressure allowing safe calculation without Out-Of-Memory hazards.
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
## ⚡ Universals Benchmarks & Capabilities
|
|
112
|
+
|
|
113
|
+
A major component of SymTorch is overcoming conventional scaling walls embedded heavily in dynamic Python CPU evaluators (e.g., SciPy's recursive `quad`).
|
|
114
|
+
|
|
115
|
+
**SymTorch evaluates integrals up to ~2,640× faster than SciPy with similar accuracy, and can uniquely utilize massive static grids to achieve mathematically narrower error bounds on notoriously difficult oscillatory problems.**
|
|
116
|
+
|
|
117
|
+
_Evaluation constraints_: Uniform sequential sweeps covering $p \in [0.5, 100.0]$ across a $10,000$ point grid resolving analytical ground truths. N represents quadrature scaling depth.
|
|
118
|
+
|
|
119
|
+
### Tabular Comparisons
|
|
120
|
+
|
|
121
|
+
| Case Name & Analytical Form | Grid | N | SymTorch ms/pt | SciPy ms/pt | Speedup | SymTorch Err | SciPy Err |
|
|
122
|
+
| :--- | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
|
|
123
|
+
| <br>**1_Gaussian**<br><br> $\int_{-\infty}^{\infty} e^{-p x^2}dx$<br><br> $= \sqrt{\frac{\pi}{p}}$<br><br> | 10000 | 121 | `0.00092` | `0.13262` | **144.0x** | `1.07e-14` | `3.72e-15` |
|
|
124
|
+
| | 10000 | 301 | `0.00332` | `0.14036` | **42.2x** | `2.13e-14` | `3.72e-15` |
|
|
125
|
+
| | 10000 | 501 | `0.00756` | `0.13877` | **18.3x** | `2.22e-14` | `3.72e-15` |
|
|
126
|
+
| | 10000 | 1001| `0.01996` | `0.13185` | **6.6x** | `2.27e-13` | `3.72e-15` |
|
|
127
|
+
| | 10000 | 2001| `0.06202` | `0.13026` | **2.1x** | `4.59e-13` | `3.72e-15` |
|
|
128
|
+
| <br>**2_Fourier_Gaussian**<br><br> $\int_{-\infty}^{\infty} e^{-x^2}e^{ipx}dx$<br><br> $= \sqrt{\pi}\,e^{-p^2/4}$<br><br> | 10000 | 121 | `0.00137` | `2.46700` | **1805.5x**| `2.65e-01` | `7.92e-10` |
|
|
129
|
+
| | 10000 | 301 | `0.00427` | `2.47832` | **580.4x** | `6.58e-03` | `7.92e-10` |
|
|
130
|
+
| | 10000 | 501 | `0.00949` | `2.42028` | **254.9x** | `4.58e-05` | `7.92e-10` |
|
|
131
|
+
| | 10000 | 1001| `0.02473` | `2.39489` | **96.8x** | `9.88e-12` | `7.92e-10` |
|
|
132
|
+
| | 10000 | 2001| `0.07130` | `2.49811` | **35.0x** | `3.05e-13` | `7.92e-10` |
|
|
133
|
+
| <br>**3_Damped_Cosine**<br><br> $\int_{0}^{\infty} e^{-x}\cos(px)dx$<br><br> $= \frac{1}{1+p^2}$<br><br> | 10000 | 121 | `0.00062` | `1.64998` | **2641.1x**| `2.07e-01` | `2.41e-09` |
|
|
134
|
+
| | 10000 | 301 | `0.00267` | `1.68845` | **632.9x** | `5.83e-02` | `2.41e-09` |
|
|
135
|
+
| | 10000 | 501 | `0.00657` | `1.67522` | **254.8x** | `2.20e-02` | `2.41e-09` |
|
|
136
|
+
| | 10000 | 1001| `0.01807` | `1.63858` | **90.7x** | `2.67e-03` | `2.41e-09` |
|
|
137
|
+
| | 10000 | 2001| `0.05752` | `1.63351` | **28.4x** | `5.72e-05` | `2.41e-09` |
|
|
138
|
+
|
|
139
|
+
_*To access raw LaTeX arrays, inspect: `tests/benchmarks/ultimate_universal_benchmark.tex`_
|
|
140
|
+
### Key Identifications:
|
|
141
|
+
* **Speedup Range:** Depending on the quadrature depth (N) and integrand complexity, speedups scale dynamically from $\sim2\times$ (at massive $N=2001$ limits) up to $\sim2641\times$ (at standard $N=121$ baselines).
|
|
142
|
+
* **Oscillatory Bypassing:** When SciPy encounters intense structural oscillations (`Case 2`), its adaptive iteration bottoms out (capping at $10^{-10}$ error margins). By structurally projecting massive arrays via SymTorch (`N=1001+`) we cleanly break past recursive thresholds achieving up to $10^{-13}$ true numerical accuracy while retaining ~ $40\times$ faster wall-times!
|
|
143
|
+
|
|
144
|
+
## Running Tests
|
|
145
|
+
|
|
146
|
+
To run the benchmarking suite locally across `symtorch` implementations:
|
|
147
|
+
|
|
148
|
+
```bash
|
|
149
|
+
pytest tests/ -v
|
|
150
|
+
pytest tests/test_benchmark_ultimate.py -s # Generates local .csv, .md, & .tex format drops safely
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Examples & Tutorials
|
|
154
|
+
|
|
155
|
+
For more extensive usage—including plotting workflows and physics applications—check the [`examples/notebooks/`](examples/notebooks/) directory which includes:
|
|
156
|
+
- `01_the_basics.ipynb`
|
|
157
|
+
- `02_parameter_sweeps.ipynb`
|
|
158
|
+
- `03_scattering_and_wigner.ipynb`
|
|
159
|
+
|
|
160
|
+
## License
|
|
161
|
+
|
|
162
|
+
Please see the [LICENSE](LICENSE) file in the root of the repository for usage and distribution terms.
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# symtorch (distribution: libsymtorch)
|
|
2
|
+
|
|
3
|
+
SymPy-to-Torch transcompilation and numerical integration library designed for extremely fast, batched evaluation on GPUs and CPUs.
|
|
4
|
+
|
|
5
|
+
Note: this module was first developed for [libphysics](https://github.com/ferhatpy/libphysics) — then it was split out into a standalone library to tackle generalized parallel computational bottlenecks.
|
|
6
|
+
|
|
7
|
+
## Install (dev)
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install -e .
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Install (PyPI)
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pip install libsymtorch
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Requirements
|
|
20
|
+
|
|
21
|
+
This project depends on the packages listed in `requirements.txt`. The primary runtime requirements are:
|
|
22
|
+
|
|
23
|
+
- Python 3.8+
|
|
24
|
+
- torch==2.5.1+cu121 (or another `torch` build appropriate for your platform)
|
|
25
|
+
- sympy==1.13.1
|
|
26
|
+
- torchquad==0.5.0
|
|
27
|
+
- numpy==2.4.2
|
|
28
|
+
- scipy==1.17.0
|
|
29
|
+
- loguru==0.7.3
|
|
30
|
+
|
|
31
|
+
To install the pinned packages, run:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pip install -r requirements.txt
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
If you intend to use GPU acceleration, please install a `torch` wheel that matches your CUDA version (the example above is a CUDA-enabled wheel). For CPU-only environments, install the CPU `torch` wheel instead.
|
|
38
|
+
|
|
39
|
+
## Quick Usage
|
|
40
|
+
|
|
41
|
+
`symtorch` bridges the gap between SymPy's symbolic manipulation and PyTorch's highly optimized batched tensor operations. You can transcompile symbolic integrals directly into callable PyTorch engines.
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
import torch
|
|
45
|
+
import symtorch
|
|
46
|
+
import sympy as sp
|
|
47
|
+
|
|
48
|
+
# 1. Define your integrands symbolically
|
|
49
|
+
x = sp.Symbol("x", real=True)
|
|
50
|
+
p = sp.Symbol("p", real=True)
|
|
51
|
+
expr = sp.Integral(sp.exp(-p * x**2), (x, -sp.oo, sp.oo))
|
|
52
|
+
|
|
53
|
+
# 2. Compile to SymTorch engine
|
|
54
|
+
lt = symtorch.SymTorch()
|
|
55
|
+
texpr = lt.torchify(expr)
|
|
56
|
+
|
|
57
|
+
# 3. Evaluate massively batched parameter grids on accelerators
|
|
58
|
+
p_grid = torch.linspace(0.5, 100.0, 10000, dtype=torch.float64, device="cuda").unsqueeze(-1)
|
|
59
|
+
re, im = texpr.torch_integrate_batched(
|
|
60
|
+
params_values=p_grid,
|
|
61
|
+
method="gauss-legendre",
|
|
62
|
+
N=501, # Force high quadrature scaling for difficult integrands
|
|
63
|
+
device="cuda", # Target accelerator natively
|
|
64
|
+
dtype=torch.float64,
|
|
65
|
+
chunk_size_params=4096 # Automatically chunks massive batches to avoid OOM
|
|
66
|
+
)
|
|
67
|
+
print("Real part:", re)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Parameter Reference
|
|
71
|
+
|
|
72
|
+
When evaluating continuous integrals dynamically using `torch_integrate_batched()`, you have granular control over numerical performance constraints:
|
|
73
|
+
|
|
74
|
+
- **`params_values`** (`torch.Tensor`): Array parameter map corresponding to all substituted free variables defining your symbolic integral.
|
|
75
|
+
- **`method`** (`str`): The mapped quadrature strategy (e.g. `"gauss-legendre"`).
|
|
76
|
+
- **`N`** (`int`): Limits Quadrature nodes. Higher N bounds push stability on deeply oscillatory configurations (allowing you to surpass typical recursion bottlenecks) but proportionately scales VRAM and FLOP demands.
|
|
77
|
+
- **`device`** (`str` or `torch.device`): Device mapping (e.g., `"cpu"`, `"cuda"`, `"mps"`).
|
|
78
|
+
- **`dtype`** (`torch.dtype`): Evaluative precision schema (use `torch.float64` for analytic comparisons or exponential limits).
|
|
79
|
+
- **`chunk_size_params`** (`int`): Sub-array partitioning for the grid parameters. Ex: With a $1,000,000$ point map and $N=2000$, setting this to `2048` iteratively offloads pressure allowing safe calculation without Out-Of-Memory hazards.
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
## ⚡ Universals Benchmarks & Capabilities
|
|
84
|
+
|
|
85
|
+
A major component of SymTorch is overcoming conventional scaling walls embedded heavily in dynamic Python CPU evaluators (e.g., SciPy's recursive `quad`).
|
|
86
|
+
|
|
87
|
+
**SymTorch evaluates integrals up to ~2,640× faster than SciPy with similar accuracy, and can uniquely utilize massive static grids to achieve mathematically narrower error bounds on notoriously difficult oscillatory problems.**
|
|
88
|
+
|
|
89
|
+
_Evaluation constraints_: Uniform sequential sweeps covering $p \in [0.5, 100.0]$ across a $10,000$ point grid resolving analytical ground truths. N represents quadrature scaling depth.
|
|
90
|
+
|
|
91
|
+
### Tabular Comparisons
|
|
92
|
+
|
|
93
|
+
| Case Name & Analytical Form | Grid | N | SymTorch ms/pt | SciPy ms/pt | Speedup | SymTorch Err | SciPy Err |
|
|
94
|
+
| :--- | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
|
|
95
|
+
| <br>**1_Gaussian**<br><br> $\int_{-\infty}^{\infty} e^{-p x^2}dx$<br><br> $= \sqrt{\frac{\pi}{p}}$<br><br> | 10000 | 121 | `0.00092` | `0.13262` | **144.0x** | `1.07e-14` | `3.72e-15` |
|
|
96
|
+
| | 10000 | 301 | `0.00332` | `0.14036` | **42.2x** | `2.13e-14` | `3.72e-15` |
|
|
97
|
+
| | 10000 | 501 | `0.00756` | `0.13877` | **18.3x** | `2.22e-14` | `3.72e-15` |
|
|
98
|
+
| | 10000 | 1001| `0.01996` | `0.13185` | **6.6x** | `2.27e-13` | `3.72e-15` |
|
|
99
|
+
| | 10000 | 2001| `0.06202` | `0.13026` | **2.1x** | `4.59e-13` | `3.72e-15` |
|
|
100
|
+
| <br>**2_Fourier_Gaussian**<br><br> $\int_{-\infty}^{\infty} e^{-x^2}e^{ipx}dx$<br><br> $= \sqrt{\pi}\,e^{-p^2/4}$<br><br> | 10000 | 121 | `0.00137` | `2.46700` | **1805.5x**| `2.65e-01` | `7.92e-10` |
|
|
101
|
+
| | 10000 | 301 | `0.00427` | `2.47832` | **580.4x** | `6.58e-03` | `7.92e-10` |
|
|
102
|
+
| | 10000 | 501 | `0.00949` | `2.42028` | **254.9x** | `4.58e-05` | `7.92e-10` |
|
|
103
|
+
| | 10000 | 1001| `0.02473` | `2.39489` | **96.8x** | `9.88e-12` | `7.92e-10` |
|
|
104
|
+
| | 10000 | 2001| `0.07130` | `2.49811` | **35.0x** | `3.05e-13` | `7.92e-10` |
|
|
105
|
+
| <br>**3_Damped_Cosine**<br><br> $\int_{0}^{\infty} e^{-x}\cos(px)dx$<br><br> $= \frac{1}{1+p^2}$<br><br> | 10000 | 121 | `0.00062` | `1.64998` | **2641.1x**| `2.07e-01` | `2.41e-09` |
|
|
106
|
+
| | 10000 | 301 | `0.00267` | `1.68845` | **632.9x** | `5.83e-02` | `2.41e-09` |
|
|
107
|
+
| | 10000 | 501 | `0.00657` | `1.67522` | **254.8x** | `2.20e-02` | `2.41e-09` |
|
|
108
|
+
| | 10000 | 1001| `0.01807` | `1.63858` | **90.7x** | `2.67e-03` | `2.41e-09` |
|
|
109
|
+
| | 10000 | 2001| `0.05752` | `1.63351` | **28.4x** | `5.72e-05` | `2.41e-09` |
|
|
110
|
+
|
|
111
|
+
_*To access raw LaTeX arrays, inspect: `tests/benchmarks/ultimate_universal_benchmark.tex`_
|
|
112
|
+
### Key Identifications:
|
|
113
|
+
* **Speedup Range:** Depending on the quadrature depth (N) and integrand complexity, speedups scale dynamically from $\sim2\times$ (at massive $N=2001$ limits) up to $\sim2641\times$ (at standard $N=121$ baselines).
|
|
114
|
+
* **Oscillatory Bypassing:** When SciPy encounters intense structural oscillations (`Case 2`), its adaptive iteration bottoms out (capping at $10^{-10}$ error margins). By structurally projecting massive arrays via SymTorch (`N=1001+`) we cleanly break past recursive thresholds achieving up to $10^{-13}$ true numerical accuracy while retaining ~ $40\times$ faster wall-times!
|
|
115
|
+
|
|
116
|
+
## Running Tests
|
|
117
|
+
|
|
118
|
+
To run the benchmarking suite locally across `symtorch` implementations:
|
|
119
|
+
|
|
120
|
+
```bash
|
|
121
|
+
pytest tests/ -v
|
|
122
|
+
pytest tests/test_benchmark_ultimate.py -s # Generates local .csv, .md, & .tex format drops safely
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Examples & Tutorials
|
|
126
|
+
|
|
127
|
+
For more extensive usage—including plotting workflows and physics applications—check the [`examples/notebooks/`](examples/notebooks/) directory which includes:
|
|
128
|
+
- `01_the_basics.ipynb`
|
|
129
|
+
- `02_parameter_sweeps.ipynb`
|
|
130
|
+
- `03_scattering_and_wigner.ipynb`
|
|
131
|
+
|
|
132
|
+
## License
|
|
133
|
+
|
|
134
|
+
Please see the [LICENSE](LICENSE) file in the root of the repository for usage and distribution terms.
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: libsymtorch
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: SymPy-to-Torch conversion and numerical integration
|
|
5
|
+
Author-email: "Ibrahim H.I. Abushawish" <ibrahim.hamed2701@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/ibeuler/symtorch
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Classifier: Topic :: Scientific/Engineering :: Physics
|
|
11
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
12
|
+
Requires-Python: >=3.8
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: torch>=2.5.1
|
|
16
|
+
Requires-Dist: sympy>=1.13.1
|
|
17
|
+
Requires-Dist: torchquad>=0.5.0
|
|
18
|
+
Requires-Dist: numpy>=2.4.2
|
|
19
|
+
Requires-Dist: scipy>=1.17.0
|
|
20
|
+
Requires-Dist: loguru>=0.7.3
|
|
21
|
+
Provides-Extra: dev
|
|
22
|
+
Requires-Dist: pytest>=8; extra == "dev"
|
|
23
|
+
Requires-Dist: build>=1; extra == "dev"
|
|
24
|
+
Requires-Dist: twine>=5; extra == "dev"
|
|
25
|
+
Provides-Extra: examples
|
|
26
|
+
Requires-Dist: matplotlib>=3.8; extra == "examples"
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# symtorch (distribution: libsymtorch)
|
|
30
|
+
|
|
31
|
+
SymPy-to-Torch transcompilation and numerical integration library designed for extremely fast, batched evaluation on GPUs and CPUs.
|
|
32
|
+
|
|
33
|
+
Note: this module was first developed for [libphysics](https://github.com/ferhatpy/libphysics) — then it was split out into a standalone library to tackle generalized parallel computational bottlenecks.
|
|
34
|
+
|
|
35
|
+
## Install (dev)
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install -e .
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Install (PyPI)
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install libsymtorch
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Requirements
|
|
48
|
+
|
|
49
|
+
This project depends on the packages listed in `requirements.txt`. The primary runtime requirements are:
|
|
50
|
+
|
|
51
|
+
- Python 3.8+
|
|
52
|
+
- torch==2.5.1+cu121 (or another `torch` build appropriate for your platform)
|
|
53
|
+
- sympy==1.13.1
|
|
54
|
+
- torchquad==0.5.0
|
|
55
|
+
- numpy==2.4.2
|
|
56
|
+
- scipy==1.17.0
|
|
57
|
+
- loguru==0.7.3
|
|
58
|
+
|
|
59
|
+
To install the pinned packages, run:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
pip install -r requirements.txt
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
If you intend to use GPU acceleration, please install a `torch` wheel that matches your CUDA version (the example above is a CUDA-enabled wheel). For CPU-only environments, install the CPU `torch` wheel instead.
|
|
66
|
+
|
|
67
|
+
## Quick Usage
|
|
68
|
+
|
|
69
|
+
`symtorch` bridges the gap between SymPy's symbolic manipulation and PyTorch's highly optimized batched tensor operations. You can transcompile symbolic integrals directly into callable PyTorch engines.
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
import torch
|
|
73
|
+
import symtorch
|
|
74
|
+
import sympy as sp
|
|
75
|
+
|
|
76
|
+
# 1. Define your integrands symbolically
|
|
77
|
+
x = sp.Symbol("x", real=True)
|
|
78
|
+
p = sp.Symbol("p", real=True)
|
|
79
|
+
expr = sp.Integral(sp.exp(-p * x**2), (x, -sp.oo, sp.oo))
|
|
80
|
+
|
|
81
|
+
# 2. Compile to SymTorch engine
|
|
82
|
+
lt = symtorch.SymTorch()
|
|
83
|
+
texpr = lt.torchify(expr)
|
|
84
|
+
|
|
85
|
+
# 3. Evaluate massively batched parameter grids on accelerators
|
|
86
|
+
p_grid = torch.linspace(0.5, 100.0, 10000, dtype=torch.float64, device="cuda").unsqueeze(-1)
|
|
87
|
+
re, im = texpr.torch_integrate_batched(
|
|
88
|
+
params_values=p_grid,
|
|
89
|
+
method="gauss-legendre",
|
|
90
|
+
N=501, # Force high quadrature scaling for difficult integrands
|
|
91
|
+
device="cuda", # Target accelerator natively
|
|
92
|
+
dtype=torch.float64,
|
|
93
|
+
chunk_size_params=4096 # Automatically chunks massive batches to avoid OOM
|
|
94
|
+
)
|
|
95
|
+
print("Real part:", re)
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Parameter Reference
|
|
99
|
+
|
|
100
|
+
When evaluating continuous integrals dynamically using `torch_integrate_batched()`, you have granular control over numerical performance constraints:
|
|
101
|
+
|
|
102
|
+
- **`params_values`** (`torch.Tensor`): Array parameter map corresponding to all substituted free variables defining your symbolic integral.
|
|
103
|
+
- **`method`** (`str`): The mapped quadrature strategy (e.g. `"gauss-legendre"`).
|
|
104
|
+
- **`N`** (`int`): Limits Quadrature nodes. Higher N bounds push stability on deeply oscillatory configurations (allowing you to surpass typical recursion bottlenecks) but proportionately scales VRAM and FLOP demands.
|
|
105
|
+
- **`device`** (`str` or `torch.device`): Device mapping (e.g., `"cpu"`, `"cuda"`, `"mps"`).
|
|
106
|
+
- **`dtype`** (`torch.dtype`): Evaluative precision schema (use `torch.float64` for analytic comparisons or exponential limits).
|
|
107
|
+
- **`chunk_size_params`** (`int`): Sub-array partitioning for the grid parameters. Ex: With a $1,000,000$ point map and $N=2000$, setting this to `2048` iteratively offloads pressure allowing safe calculation without Out-Of-Memory hazards.
|
|
108
|
+
|
|
109
|
+
---
|
|
110
|
+
|
|
111
|
+
## ⚡ Universals Benchmarks & Capabilities
|
|
112
|
+
|
|
113
|
+
A major component of SymTorch is overcoming conventional scaling walls embedded heavily in dynamic Python CPU evaluators (e.g., SciPy's recursive `quad`).
|
|
114
|
+
|
|
115
|
+
**SymTorch evaluates integrals up to ~2,640× faster than SciPy with similar accuracy, and can uniquely utilize massive static grids to achieve mathematically narrower error bounds on notoriously difficult oscillatory problems.**
|
|
116
|
+
|
|
117
|
+
_Evaluation constraints_: Uniform sequential sweeps covering $p \in [0.5, 100.0]$ across a $10,000$ point grid resolving analytical ground truths. N represents quadrature scaling depth.
|
|
118
|
+
|
|
119
|
+
### Tabular Comparisons
|
|
120
|
+
|
|
121
|
+
| Case Name & Analytical Form | Grid | N | SymTorch ms/pt | SciPy ms/pt | Speedup | SymTorch Err | SciPy Err |
|
|
122
|
+
| :--- | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
|
|
123
|
+
| <br>**1_Gaussian**<br><br> $\int_{-\infty}^{\infty} e^{-p x^2}dx$<br><br> $= \sqrt{\frac{\pi}{p}}$<br><br> | 10000 | 121 | `0.00092` | `0.13262` | **144.0x** | `1.07e-14` | `3.72e-15` |
|
|
124
|
+
| | 10000 | 301 | `0.00332` | `0.14036` | **42.2x** | `2.13e-14` | `3.72e-15` |
|
|
125
|
+
| | 10000 | 501 | `0.00756` | `0.13877` | **18.3x** | `2.22e-14` | `3.72e-15` |
|
|
126
|
+
| | 10000 | 1001| `0.01996` | `0.13185` | **6.6x** | `2.27e-13` | `3.72e-15` |
|
|
127
|
+
| | 10000 | 2001| `0.06202` | `0.13026` | **2.1x** | `4.59e-13` | `3.72e-15` |
|
|
128
|
+
| <br>**2_Fourier_Gaussian**<br><br> $\int_{-\infty}^{\infty} e^{-x^2}e^{ipx}dx$<br><br> $= \sqrt{\pi}\,e^{-p^2/4}$<br><br> | 10000 | 121 | `0.00137` | `2.46700` | **1805.5x**| `2.65e-01` | `7.92e-10` |
|
|
129
|
+
| | 10000 | 301 | `0.00427` | `2.47832` | **580.4x** | `6.58e-03` | `7.92e-10` |
|
|
130
|
+
| | 10000 | 501 | `0.00949` | `2.42028` | **254.9x** | `4.58e-05` | `7.92e-10` |
|
|
131
|
+
| | 10000 | 1001| `0.02473` | `2.39489` | **96.8x** | `9.88e-12` | `7.92e-10` |
|
|
132
|
+
| | 10000 | 2001| `0.07130` | `2.49811` | **35.0x** | `3.05e-13` | `7.92e-10` |
|
|
133
|
+
| <br>**3_Damped_Cosine**<br><br> $\int_{0}^{\infty} e^{-x}\cos(px)dx$<br><br> $= \frac{1}{1+p^2}$<br><br> | 10000 | 121 | `0.00062` | `1.64998` | **2641.1x**| `2.07e-01` | `2.41e-09` |
|
|
134
|
+
| | 10000 | 301 | `0.00267` | `1.68845` | **632.9x** | `5.83e-02` | `2.41e-09` |
|
|
135
|
+
| | 10000 | 501 | `0.00657` | `1.67522` | **254.8x** | `2.20e-02` | `2.41e-09` |
|
|
136
|
+
| | 10000 | 1001| `0.01807` | `1.63858` | **90.7x** | `2.67e-03` | `2.41e-09` |
|
|
137
|
+
| | 10000 | 2001| `0.05752` | `1.63351` | **28.4x** | `5.72e-05` | `2.41e-09` |
|
|
138
|
+
|
|
139
|
+
_*To access raw LaTeX arrays, inspect: `tests/benchmarks/ultimate_universal_benchmark.tex`_
|
|
140
|
+
### Key Identifications:
|
|
141
|
+
* **Speedup Range:** Depending on the quadrature depth (N) and integrand complexity, speedups scale dynamically from $\sim2\times$ (at massive $N=2001$ limits) up to $\sim2641\times$ (at standard $N=121$ baselines).
|
|
142
|
+
* **Oscillatory Bypassing:** When SciPy encounters intense structural oscillations (`Case 2`), its adaptive iteration bottoms out (capping at $10^{-10}$ error margins). By structurally projecting massive arrays via SymTorch (`N=1001+`) we cleanly break past recursive thresholds achieving up to $10^{-13}$ true numerical accuracy while retaining ~ $40\times$ faster wall-times!
|
|
143
|
+
|
|
144
|
+
## Running Tests
|
|
145
|
+
|
|
146
|
+
To run the benchmarking suite locally across `symtorch` implementations:
|
|
147
|
+
|
|
148
|
+
```bash
|
|
149
|
+
pytest tests/ -v
|
|
150
|
+
pytest tests/test_benchmark_ultimate.py -s # Generates local .csv, .md, & .tex format drops safely
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Examples & Tutorials
|
|
154
|
+
|
|
155
|
+
For more extensive usage—including plotting workflows and physics applications—check the [`examples/notebooks/`](examples/notebooks/) directory which includes:
|
|
156
|
+
- `01_the_basics.ipynb`
|
|
157
|
+
- `02_parameter_sweeps.ipynb`
|
|
158
|
+
- `03_scattering_and_wigner.ipynb`
|
|
159
|
+
|
|
160
|
+
## License
|
|
161
|
+
|
|
162
|
+
Please see the [LICENSE](LICENSE) file in the root of the repository for usage and distribution terms.
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import math
|
|
4
|
+
import time
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
import pytest
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass
|
|
13
|
+
class UltimateConfig:
|
|
14
|
+
N: int # Quadrature nodes (impacts accuracy and GPU work)
|
|
15
|
+
grid_points: int # Total parameters to evaluate (impacts batching efficiency)
|
|
16
|
+
chunk_params: int # Batching chunk size for memory control
|
|
17
|
+
k_min: float # Parameter range lower bound
|
|
18
|
+
k_max: float # Parameter range upper bound (high k = high oscillation)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass
|
|
22
|
+
class UltimateCase:
|
|
23
|
+
name: str
|
|
24
|
+
expr: Any
|
|
25
|
+
variables: list
|
|
26
|
+
limits: list
|
|
27
|
+
params: list
|
|
28
|
+
ref_fn: Any
|
|
29
|
+
scipy_re: Any
|
|
30
|
+
scipy_im: Any
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _perf_best_of(func, repeats: int = 3):
|
|
34
|
+
best = float("inf")
|
|
35
|
+
for _ in range(repeats):
|
|
36
|
+
t0 = time.perf_counter()
|
|
37
|
+
func()
|
|
38
|
+
t = time.perf_counter() - t0
|
|
39
|
+
best = min(best, t)
|
|
40
|
+
return best
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def test_ultimate_universal_benchmark(symtorch_instance, sp, torch, device, tmp_path):
|
|
44
|
+
"""
|
|
45
|
+
The Ultimate Universal Benchmark Suite for SymTorch.
|
|
46
|
+
|
|
47
|
+
Reads extremely simple and iterates over multiple challenging mathematical regimes:
|
|
48
|
+
1) Gaussian (Decay, infinite domain)
|
|
49
|
+
2) Fourier-Gaussian (Oscillatory, infinite domain)
|
|
50
|
+
3) Lorentzian / Cosine (High oscillatory, semi-infinite domain)
|
|
51
|
+
|
|
52
|
+
Sweeps over different batched settings (Grid sizes) and quadrature resolutions (N)
|
|
53
|
+
to find the "best case speed per point".
|
|
54
|
+
"""
|
|
55
|
+
dtype = torch.float64
|
|
56
|
+
scipy = pytest.importorskip("scipy")
|
|
57
|
+
import numpy as np
|
|
58
|
+
from scipy import integrate as scipy_integrate
|
|
59
|
+
|
|
60
|
+
# -------------------------------------------------------------
|
|
61
|
+
# 1. Define Universal Cases
|
|
62
|
+
# -------------------------------------------------------------
|
|
63
|
+
x = sp.Symbol("x", real=True)
|
|
64
|
+
p = sp.Symbol("p", real=True)
|
|
65
|
+
|
|
66
|
+
# Note: lambdify functions for SciPy
|
|
67
|
+
gauss_np = sp.lambdify((x, p), sp.exp(-p * x**2), modules="numpy")
|
|
68
|
+
fourier_np = sp.lambdify((x, p), sp.exp(-x**2) * sp.exp(sp.I * p * x), modules="numpy")
|
|
69
|
+
lorent_np = sp.lambdify((x, p), sp.exp(-x) * sp.cos(p * x), modules="numpy")
|
|
70
|
+
|
|
71
|
+
cases = [
|
|
72
|
+
# Case 1: Standard Infinite Domain
|
|
73
|
+
UltimateCase(
|
|
74
|
+
name="1_Gaussian",
|
|
75
|
+
expr=sp.exp(-p * x**2),
|
|
76
|
+
variables=[x],
|
|
77
|
+
limits=[(x, -sp.oo, sp.oo)],
|
|
78
|
+
params=[p],
|
|
79
|
+
ref_fn=lambda p_tensor: torch.sqrt(torch.tensor(torch.pi, device=device, dtype=dtype) / p_tensor),
|
|
80
|
+
scipy_re=lambda x_val, p_val: gauss_np(x_val, p_val),
|
|
81
|
+
scipy_im=lambda x_val, p_val: 0.0,
|
|
82
|
+
),
|
|
83
|
+
# Case 2: Oscillatory Infinite Domain
|
|
84
|
+
UltimateCase(
|
|
85
|
+
name="2_Fourier_Gaussian",
|
|
86
|
+
expr=sp.exp(-x**2) * sp.exp(sp.I * p * x),
|
|
87
|
+
variables=[x],
|
|
88
|
+
limits=[(x, -sp.oo, sp.oo)],
|
|
89
|
+
params=[p],
|
|
90
|
+
ref_fn=lambda p_tensor: torch.sqrt(torch.tensor(torch.pi, device=device, dtype=dtype)) * torch.exp(-(p_tensor**2)/4.0),
|
|
91
|
+
scipy_re=lambda x_val, p_val: np.real(fourier_np(x_val, p_val)),
|
|
92
|
+
scipy_im=lambda x_val, p_val: np.imag(fourier_np(x_val, p_val)),
|
|
93
|
+
),
|
|
94
|
+
# Case 3: High Oscillatory Semi-Infinite Domain
|
|
95
|
+
UltimateCase(
|
|
96
|
+
name="3_Damped_Cosine",
|
|
97
|
+
expr=sp.exp(-x) * sp.cos(p * x),
|
|
98
|
+
variables=[x],
|
|
99
|
+
limits=[(x, 0, sp.oo)],
|
|
100
|
+
params=[p],
|
|
101
|
+
ref_fn=lambda p_tensor: 1.0 / (1.0 + p_tensor**2),
|
|
102
|
+
scipy_re=lambda x_val, p_val: lorent_np(x_val, p_val),
|
|
103
|
+
scipy_im=lambda x_val, p_val: 0.0,
|
|
104
|
+
),
|
|
105
|
+
]
|
|
106
|
+
|
|
107
|
+
# -------------------------------------------------------------
|
|
108
|
+
# 2. Define Universal Configurations to Sweep
|
|
109
|
+
# -------------------------------------------------------------
|
|
110
|
+
# We sweep (Grid Points, N) combinations to see scaling behavior.
|
|
111
|
+
configs = [
|
|
112
|
+
UltimateConfig(N=121, grid_points=10000, chunk_params=4096, k_min=0.5, k_max=100.0),
|
|
113
|
+
UltimateConfig(N=301, grid_points=10000, chunk_params=4096, k_min=0.5, k_max=100.0),
|
|
114
|
+
UltimateConfig(N=501, grid_points=10000, chunk_params=4096, k_min=0.5, k_max=100.0),
|
|
115
|
+
UltimateConfig(N=1001, grid_points=10000, chunk_params=4096, k_min=0.5, k_max=100.0),
|
|
116
|
+
UltimateConfig(N=2001, grid_points=10000, chunk_params=4096, k_min=0.5, k_max=100.0),
|
|
117
|
+
]
|
|
118
|
+
|
|
119
|
+
method = "gauss-legendre"
|
|
120
|
+
scipy_limit = 1000
|
|
121
|
+
|
|
122
|
+
results = []
|
|
123
|
+
|
|
124
|
+
with torch.no_grad():
|
|
125
|
+
for case in cases:
|
|
126
|
+
# 1. Compile Integrand once per case
|
|
127
|
+
integral = sp.Integral(case.expr, *case.limits)
|
|
128
|
+
texpr = symtorch_instance.torchify(integral)
|
|
129
|
+
|
|
130
|
+
for cfg in configs:
|
|
131
|
+
# 2. Setup grid and analytical answers
|
|
132
|
+
p_grid = torch.linspace(cfg.k_min, cfg.k_max, cfg.grid_points, device=device, dtype=dtype).unsqueeze(-1)
|
|
133
|
+
ref_tensor = case.ref_fn(p_grid.squeeze(-1))
|
|
134
|
+
|
|
135
|
+
# 3. Warm-up GPU cache
|
|
136
|
+
_ = texpr.torch_integrate_batched(
|
|
137
|
+
params_values=p_grid[:2],
|
|
138
|
+
method=method, N=cfg.N, device=device, dtype=dtype, chunk_size_params=2
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
# 4. Measure SymTorch Batched Speed
|
|
142
|
+
def _run_symtorch():
|
|
143
|
+
return texpr.torch_integrate_batched(
|
|
144
|
+
params_values=p_grid,
|
|
145
|
+
method=method,
|
|
146
|
+
N=cfg.N,
|
|
147
|
+
device=device,
|
|
148
|
+
dtype=dtype,
|
|
149
|
+
chunk_size_params=cfg.chunk_params,
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
t_symtorch = _perf_best_of(_run_symtorch, repeats=3)
|
|
153
|
+
re_sym, im_sym = _run_symtorch()
|
|
154
|
+
|
|
155
|
+
err_sym = float((re_sym - ref_tensor).abs().max().item())
|
|
156
|
+
symtorch_ms_per_point = (t_symtorch / float(cfg.grid_points)) * 1000.0
|
|
157
|
+
|
|
158
|
+
# 5. Measure SciPy baseline (sub-sampled for extreme grids to save patience)
|
|
159
|
+
scipy_subset_points = min(cfg.grid_points, 20)
|
|
160
|
+
sub_indices = np.linspace(0, cfg.grid_points - 1, scipy_subset_points, dtype=int)
|
|
161
|
+
p_cpu = p_grid.squeeze(-1).detach().cpu().numpy()[sub_indices]
|
|
162
|
+
ref_cpu = ref_tensor.detach().cpu().numpy()[sub_indices]
|
|
163
|
+
|
|
164
|
+
scipy_res = []
|
|
165
|
+
import warnings
|
|
166
|
+
from scipy.integrate import IntegrationWarning
|
|
167
|
+
with warnings.catch_warnings():
|
|
168
|
+
warnings.simplefilter("ignore", IntegrationWarning)
|
|
169
|
+
for p_val in p_cpu:
|
|
170
|
+
re_val, _ = scipy_integrate.quad(case.scipy_re, case.limits[0][1], case.limits[0][2], args=(float(p_val),), limit=scipy_limit)
|
|
171
|
+
im_val, _ = scipy_integrate.quad(case.scipy_im, case.limits[0][1], case.limits[0][2], args=(float(p_val),), limit=scipy_limit)
|
|
172
|
+
scipy_res.append(re_val + 1j * im_val if np.abs(im_val) > 1e-15 else re_val)
|
|
173
|
+
err_scipy = float(np.abs(np.array(scipy_res) - ref_cpu).max())
|
|
174
|
+
|
|
175
|
+
# SciPy integration mapping
|
|
176
|
+
def _run_scipy_subset():
|
|
177
|
+
with warnings.catch_warnings():
|
|
178
|
+
warnings.simplefilter("ignore", IntegrationWarning)
|
|
179
|
+
for p_val in p_cpu:
|
|
180
|
+
scipy_integrate.quad(case.scipy_re, case.limits[0][1], case.limits[0][2], args=(float(p_val),), limit=scipy_limit)
|
|
181
|
+
scipy_integrate.quad(case.scipy_im, case.limits[0][1], case.limits[0][2], args=(float(p_val),), limit=scipy_limit)
|
|
182
|
+
|
|
183
|
+
t_scipy_subset = _perf_best_of(_run_scipy_subset, repeats=1)
|
|
184
|
+
scipy_ms_per_point = (t_scipy_subset / float(scipy_subset_points)) * 1000.0
|
|
185
|
+
|
|
186
|
+
# 6. Verdict Calculation
|
|
187
|
+
speedup = scipy_ms_per_point / symtorch_ms_per_point if symtorch_ms_per_point > 0 else 0.0
|
|
188
|
+
|
|
189
|
+
results.append({
|
|
190
|
+
"case": case.name,
|
|
191
|
+
"grid": cfg.grid_points,
|
|
192
|
+
"N": cfg.N,
|
|
193
|
+
"symtorch_ms": symtorch_ms_per_point,
|
|
194
|
+
"scipy_ms": scipy_ms_per_point,
|
|
195
|
+
"speedup": speedup,
|
|
196
|
+
"err_sym": err_sym,
|
|
197
|
+
"err_scipy": err_scipy
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
# Save format processing
|
|
201
|
+
project_root = Path(__file__).resolve().parents[1]
|
|
202
|
+
bench_dir = project_root / "tests" / "benchmarks"
|
|
203
|
+
bench_dir.mkdir(parents=True, exist_ok=True)
|
|
204
|
+
|
|
205
|
+
# 1. Output CSV
|
|
206
|
+
import csv
|
|
207
|
+
with open(bench_dir / "ultimate_universal_benchmark.csv", "w", newline='') as f:
|
|
208
|
+
writer = csv.writer(f)
|
|
209
|
+
writer.writerow(["Case Name", "Grid", "N", "SymTorch ms/pt", "SciPy ms/pt", "Speedup", "SymTorch Error", "SciPy Error"])
|
|
210
|
+
for r in results:
|
|
211
|
+
writer.writerow([r["case"], r["grid"], r["N"], f"{r['symtorch_ms']:.5f}", f"{r['scipy_ms']:.5f}", f"{r['speedup']:.1f}", f"{r['err_sym']:.2e}", f"{r['err_scipy']:.2e}"])
|
|
212
|
+
|
|
213
|
+
# 2. Output LaTeX Table
|
|
214
|
+
case_latex = {
|
|
215
|
+
"1_Gaussian": "\\makecell{1\\_Gaussian: \\\\ $\\bigint_{-\\infty}^{\\infty} e^{-p x^2}\\,dx = \\sqrt{\\frac{\\pi}{p}}$}",
|
|
216
|
+
"2_Fourier_Gaussian": "\\makecell{2\\_Fourier\\_Gaussian: \\\\ $\\bigint_{-\\infty}^{\\infty} e^{-x^2}e^{ipx}\\,dx$ \\\\ $= \\sqrt{\\pi}\\,e^{-p^2/4}$}",
|
|
217
|
+
"3_Damped_Cosine": "\\makecell{3\\_Damped\\_Cosine: \\\\ $\\bigint_{0}^{\\infty} e^{-x}\\cos(px)\\,dx$ \\\\ $= \\frac{1}{1+p^2}$}",
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
tex_lines = [
|
|
221
|
+
"\\begin{table}[h]",
|
|
222
|
+
"\\centering",
|
|
223
|
+
"\\resizebox{\\columnwidth}{!}{%",
|
|
224
|
+
"\\begin{tabular}{l c c c c c c c}",
|
|
225
|
+
"\\toprule",
|
|
226
|
+
"\\textbf{Case} & \\textbf{Grid} & \\textbf{N} & \\textbf{SymTorch (ms)} & \\textbf{SciPy (ms)} & \\textbf{Speedup} & \\textbf{SymTorch Err} & \\textbf{SciPy Err} \\\\",
|
|
227
|
+
"\\midrule"
|
|
228
|
+
]
|
|
229
|
+
# Group results by case for multirow formatting
|
|
230
|
+
from collections import defaultdict
|
|
231
|
+
grouped_results = defaultdict(list)
|
|
232
|
+
for r in results:
|
|
233
|
+
grouped_results[r['case']].append(r)
|
|
234
|
+
|
|
235
|
+
for i, (cname, rlist) in enumerate(grouped_results.items()):
|
|
236
|
+
safe_case = case_latex.get(cname, cname.replace('_', '\\_'))
|
|
237
|
+
num_rows = len(rlist)
|
|
238
|
+
for j, r in enumerate(rlist):
|
|
239
|
+
case_str = f"\\multirow{{{num_rows}}}{{*}}{{{safe_case}}}" if j == 0 else ""
|
|
240
|
+
tex_lines.append(f"{case_str} & {r['grid']} & {r['N']} & {r['symtorch_ms']:.5f} & {r['scipy_ms']:.5f} & {r['speedup']:.1f}$\\times$ & {r['err_sym']:.2e} & {r['err_scipy']:.2e} \\\\")
|
|
241
|
+
|
|
242
|
+
if i < len(grouped_results) - 1:
|
|
243
|
+
tex_lines.append("\\hline")
|
|
244
|
+
|
|
245
|
+
tex_lines.extend([
|
|
246
|
+
"\\bottomrule",
|
|
247
|
+
"\\end{tabular}",
|
|
248
|
+
"}",
|
|
249
|
+
"\\caption{Universal Benchmark Results: SymTorch vs SciPy (ms/point)}",
|
|
250
|
+
"\\label{tab:universal_benchmark}",
|
|
251
|
+
"\\end{table}"
|
|
252
|
+
])
|
|
253
|
+
(bench_dir / "ultimate_universal_benchmark.tex").write_text("\n".join(tex_lines), encoding="utf-8")
|
|
254
|
+
|
|
255
|
+
# 3. Output Markdown (for easy console read and GitHub)
|
|
256
|
+
md_lines = ["| Case Name | Grid | N | SymTorch ms/pt | SciPy ms/pt | Speedup | SymTorch Err | SciPy Err |", "| :--- | :--- | :--- | :--- | :--- | :--- | :--- | :--- |"]
|
|
257
|
+
for r in results:
|
|
258
|
+
md_lines.append(f"| {r['case']} | {r['grid']} | {r['N']} | `{r['symtorch_ms']:.5f}` | `{r['scipy_ms']:.5f}` | **{r['speedup']:.1f}x** | `{r['err_sym']:.2e}` | `{r['err_scipy']:.2e}` |")
|
|
259
|
+
|
|
260
|
+
md_text = "\n".join(md_lines)
|
|
261
|
+
(bench_dir / "ultimate_universal_benchmark.md").write_text(md_text, encoding="utf-8")
|
|
262
|
+
|
|
263
|
+
print("\n# Ultimate Benchmark Complete. Generated .csv, .tex, and .md files.")
|
|
264
|
+
print(md_text)
|
libsymtorch-0.1.1/PKG-INFO
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: libsymtorch
|
|
3
|
-
Version: 0.1.1
|
|
4
|
-
Summary: SymPy-to-Torch conversion and numerical integration
|
|
5
|
-
Author-email: "Ibrahim H.I. Abushawish" <ibrahim.hamed2701@gmail.com>
|
|
6
|
-
License-Expression: MIT
|
|
7
|
-
Project-URL: Homepage, https://github.com/ibeuler/symtorch
|
|
8
|
-
Classifier: Programming Language :: Python :: 3
|
|
9
|
-
Classifier: Operating System :: OS Independent
|
|
10
|
-
Classifier: Topic :: Scientific/Engineering :: Physics
|
|
11
|
-
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
12
|
-
Requires-Python: >=3.8
|
|
13
|
-
Description-Content-Type: text/markdown
|
|
14
|
-
License-File: LICENSE
|
|
15
|
-
Requires-Dist: torch>=2.5.1
|
|
16
|
-
Requires-Dist: sympy>=1.13.1
|
|
17
|
-
Requires-Dist: torchquad>=0.5.0
|
|
18
|
-
Requires-Dist: numpy>=2.4.2
|
|
19
|
-
Requires-Dist: scipy>=1.17.0
|
|
20
|
-
Requires-Dist: loguru>=0.7.3
|
|
21
|
-
Provides-Extra: dev
|
|
22
|
-
Requires-Dist: pytest>=8; extra == "dev"
|
|
23
|
-
Requires-Dist: build>=1; extra == "dev"
|
|
24
|
-
Requires-Dist: twine>=5; extra == "dev"
|
|
25
|
-
Provides-Extra: examples
|
|
26
|
-
Requires-Dist: matplotlib>=3.8; extra == "examples"
|
|
27
|
-
Dynamic: license-file
|
|
28
|
-
|
|
29
|
-
# symtorch (distribution: libsymtorch)
|
|
30
|
-
|
|
31
|
-
SymPy-to-Torch conversion and numerical integration helpers.
|
|
32
|
-
|
|
33
|
-
Note: this module was first developed for libphysics (and remains compatible): https://github.com/ferhatpy/libphysics — then it was split out into a standalone library.
|
|
34
|
-
|
|
35
|
-
## Install (dev)
|
|
36
|
-
|
|
37
|
-
pip install -e .
|
|
38
|
-
|
|
39
|
-
## Install (PyPI)
|
|
40
|
-
|
|
41
|
-
```bash
|
|
42
|
-
pip install libsymtorch
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
## Quick usage
|
|
46
|
-
|
|
47
|
-
```python
|
|
48
|
-
import symtorch
|
|
49
|
-
import sympy as sp
|
|
50
|
-
|
|
51
|
-
x = sp.Symbol("x", real=True)
|
|
52
|
-
expr = sp.Integral(sp.exp(-x**2), (x, -sp.oo, sp.oo))
|
|
53
|
-
|
|
54
|
-
lt = symtorch.SymTorch()
|
|
55
|
-
texpr = lt.torchify(expr)
|
|
56
|
-
re, im = texpr.torchquad_integrate(N=121)
|
|
57
|
-
print(re, im)
|
|
58
|
-
```
|
libsymtorch-0.1.1/README.md
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
# symtorch (distribution: libsymtorch)
|
|
2
|
-
|
|
3
|
-
SymPy-to-Torch conversion and numerical integration helpers.
|
|
4
|
-
|
|
5
|
-
Note: this module was first developed for libphysics (and remains compatible): https://github.com/ferhatpy/libphysics — then it was split out into a standalone library.
|
|
6
|
-
|
|
7
|
-
## Install (dev)
|
|
8
|
-
|
|
9
|
-
pip install -e .
|
|
10
|
-
|
|
11
|
-
## Install (PyPI)
|
|
12
|
-
|
|
13
|
-
```bash
|
|
14
|
-
pip install libsymtorch
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
## Quick usage
|
|
18
|
-
|
|
19
|
-
```python
|
|
20
|
-
import symtorch
|
|
21
|
-
import sympy as sp
|
|
22
|
-
|
|
23
|
-
x = sp.Symbol("x", real=True)
|
|
24
|
-
expr = sp.Integral(sp.exp(-x**2), (x, -sp.oo, sp.oo))
|
|
25
|
-
|
|
26
|
-
lt = symtorch.SymTorch()
|
|
27
|
-
texpr = lt.torchify(expr)
|
|
28
|
-
re, im = texpr.torchquad_integrate(N=121)
|
|
29
|
-
print(re, im)
|
|
30
|
-
```
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: libsymtorch
|
|
3
|
-
Version: 0.1.1
|
|
4
|
-
Summary: SymPy-to-Torch conversion and numerical integration
|
|
5
|
-
Author-email: "Ibrahim H.I. Abushawish" <ibrahim.hamed2701@gmail.com>
|
|
6
|
-
License-Expression: MIT
|
|
7
|
-
Project-URL: Homepage, https://github.com/ibeuler/symtorch
|
|
8
|
-
Classifier: Programming Language :: Python :: 3
|
|
9
|
-
Classifier: Operating System :: OS Independent
|
|
10
|
-
Classifier: Topic :: Scientific/Engineering :: Physics
|
|
11
|
-
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
12
|
-
Requires-Python: >=3.8
|
|
13
|
-
Description-Content-Type: text/markdown
|
|
14
|
-
License-File: LICENSE
|
|
15
|
-
Requires-Dist: torch>=2.5.1
|
|
16
|
-
Requires-Dist: sympy>=1.13.1
|
|
17
|
-
Requires-Dist: torchquad>=0.5.0
|
|
18
|
-
Requires-Dist: numpy>=2.4.2
|
|
19
|
-
Requires-Dist: scipy>=1.17.0
|
|
20
|
-
Requires-Dist: loguru>=0.7.3
|
|
21
|
-
Provides-Extra: dev
|
|
22
|
-
Requires-Dist: pytest>=8; extra == "dev"
|
|
23
|
-
Requires-Dist: build>=1; extra == "dev"
|
|
24
|
-
Requires-Dist: twine>=5; extra == "dev"
|
|
25
|
-
Provides-Extra: examples
|
|
26
|
-
Requires-Dist: matplotlib>=3.8; extra == "examples"
|
|
27
|
-
Dynamic: license-file
|
|
28
|
-
|
|
29
|
-
# symtorch (distribution: libsymtorch)
|
|
30
|
-
|
|
31
|
-
SymPy-to-Torch conversion and numerical integration helpers.
|
|
32
|
-
|
|
33
|
-
Note: this module was first developed for libphysics (and remains compatible): https://github.com/ferhatpy/libphysics — then it was split out into a standalone library.
|
|
34
|
-
|
|
35
|
-
## Install (dev)
|
|
36
|
-
|
|
37
|
-
pip install -e .
|
|
38
|
-
|
|
39
|
-
## Install (PyPI)
|
|
40
|
-
|
|
41
|
-
```bash
|
|
42
|
-
pip install libsymtorch
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
## Quick usage
|
|
46
|
-
|
|
47
|
-
```python
|
|
48
|
-
import symtorch
|
|
49
|
-
import sympy as sp
|
|
50
|
-
|
|
51
|
-
x = sp.Symbol("x", real=True)
|
|
52
|
-
expr = sp.Integral(sp.exp(-x**2), (x, -sp.oo, sp.oo))
|
|
53
|
-
|
|
54
|
-
lt = symtorch.SymTorch()
|
|
55
|
-
texpr = lt.torchify(expr)
|
|
56
|
-
re, im = texpr.torchquad_integrate(N=121)
|
|
57
|
-
print(re, im)
|
|
58
|
-
```
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|