Skip to content

Commit 1551bbd

Browse files
0xC000005claude
andcommitted
Update README, docs, and CLAUDE.md for v0.5.0 completeness
Add ChebyshevTT to README features list and code examples, docs landing page, getting-started class selection guide, API reference, and error estimation page. Update CLAUDE.md with tensor_train.py architecture, current test counts, and comparison script. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent dd2b9cf commit 1551bbd

6 files changed

Lines changed: 150 additions & 6 deletions

File tree

CLAUDE.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# CLAUDE.md
2+
3+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4+
5+
## Project Overview
6+
7+
PyChebyshev is a pip-installable Python library for multi-dimensional Chebyshev tensor interpolation with analytical derivatives. It uses barycentric interpolation with full weight pre-computation and BLAS GEMV for fast evaluation. Originally developed as a research project comparing numerical methods for Black-Scholes option pricing against the MoCaX C++ library.
8+
9+
## Commands
10+
11+
```bash
12+
# Setup
13+
uv sync
14+
15+
# Run tests (~100 tests, ~120s due to 5D Black-Scholes builds)
16+
uv run pytest tests/ -v
17+
18+
# Run a single test
19+
uv run pytest tests/test_barycentric.py::TestSimple3D::test_price_accuracy -v
20+
21+
# Build package
22+
uv build
23+
24+
# Build and preview docs locally
25+
uv run mkdocs serve # http://127.0.0.1:8000
26+
uv run mkdocs build --strict # Verify docs build
27+
28+
# Deploy docs to GitHub Pages
29+
uv run mkdocs gh-deploy --force
30+
31+
# Run benchmark scripts (not part of library)
32+
uv run python chebyshev_barycentric.py
33+
uv run python compare_methods_time_accuracy.py
34+
```
35+
36+
## Architecture
37+
38+
### Library (`src/pychebyshev/`)
39+
40+
The installable package. Public classes: `ChebyshevApproximation`, `ChebyshevSlider`, and `ChebyshevTT`.
41+
42+
- **`barycentric.py`** — Core implementation. `ChebyshevApproximation` class with `build()`, `eval()`, `vectorized_eval()`, `vectorized_eval_multi()`. Key insight: barycentric weights depend only on node positions (not function values), enabling full pre-computation. `vectorized_eval()` uses a reshape trick to route N-D tensor contractions through BLAS GEMV (~0.065ms/query). `vectorized_eval_multi()` shares barycentric weight computation across price + derivatives (~0.29ms for 6 outputs). `fast_eval()` exists but is deprecated (JIT path, ~150x slower than BLAS).
43+
- **`slider.py`**`ChebyshevSlider` class for high-dimensional approximation via the Sliding Technique.
44+
- **`tensor_train.py`**`ChebyshevTT` class for Tensor Train Chebyshev interpolation. TT-Cross builds from O(d·n·r²) evaluations with maxvol pivoting, eval caching, and SVD-based adaptive rank. Vectorized batch eval via numpy einsum. FD derivatives.
45+
- **`_jit.py`** — Deprecated Numba JIT kernel with pure NumPy fallback. Used only by deprecated `fast_eval()`.
46+
- **`_version.py`** — Single source of truth for version string.
47+
48+
### Benchmark Scripts (project root)
49+
50+
Not part of the library. Compare Chebyshev barycentric against alternative methods:
51+
52+
- `chebyshev_barycentric.py` — Standalone version with embedded test suite
53+
- `chebyshev_baseline.py` — NumPy polynomial coefficient approach
54+
- `fdm_baseline.py` — Finite difference PDE solver
55+
- `mocax_baseline.py`, `mocax_tt.py`, `mocax_sliding.py` — MoCaX C++ library tests (require `mocaxextend_lib/`)
56+
- `compare_methods_time_accuracy.py` — Fair time/accuracy comparison across all methods
57+
- `compare_tensor_train.py` — PyChebyshev TT vs MoCaX TT comparison (requires `mocaxextend_lib/`)
58+
59+
### Tests (`tests/`)
60+
61+
- `conftest.py` — Shared fixtures (`cheb_sin_3d`, `cheb_bs_3d`, `cheb_bs_5d`, `tt_sin_3d`, `tt_bs_5d`) and analytical Black-Scholes functions (reimplemented via `scipy.stats.norm` to avoid external deps). Helper functions are imported as `from conftest import ...` (pytest auto-import, NOT `from tests.conftest`).
62+
- `test_barycentric.py` — 35 tests: accuracy, derivatives, eval method consistency, node coincidence, error estimation, build-required guard.
63+
- `test_slider.py` — 36 tests: additive/coupled functions, 5D, cross-group derivatives, error estimation, serialization.
64+
- `test_tensor_train.py` — 23 tests: TT-Cross/TT-SVD accuracy, batch eval, FD derivatives, rank control, serialization, error estimation.
65+
66+
### CI/CD (`.github/workflows/`)
67+
68+
- `test.yml` — pytest on Python 3.10-3.13 (triggers on push/PR to main)
69+
- `publish.yml``uv build && uv publish` via OIDC trusted publishing (triggers on GitHub release creation)
70+
71+
### Docs (`docs/`)
72+
73+
MkDocs + Material theme, deployed to GitHub Pages. KaTeX for math rendering, mkdocstrings for API autodoc from NumPy-style docstrings.
74+
75+
## Key Technical Constraints
76+
77+
- **Numba/JIT is deprecated**: `fast_eval()` and `_jit.py` are deprecated. `vectorized_eval()` via BLAS GEMV is ~150x faster and needs no optional deps.
78+
- **Python >=3.10**: Uses `from __future__ import annotations` for modern type hints.
79+
- **Core deps are only numpy and scipy**: pytest, matplotlib, blackscholes etc. belong in dev/optional groups only.
80+
- **README images must use absolute GitHub URLs**: PyPI renders README but can't serve local files. Use `https://raw.githubusercontent.com/0xC000005/PyChebyshev/main/...` for all `<img src>`.
81+
- **Version in two places**: `pyproject.toml` and `src/pychebyshev/_version.py` must stay in sync.
82+
83+
## Release Process
84+
85+
1. Bump version in `pyproject.toml` and `src/pychebyshev/_version.py`
86+
2. Update `CHANGELOG.md`
87+
3. Commit, push to main
88+
4. `gh release create vX.Y.Z` — triggers publish workflow → PyPI
89+
5. `uv run mkdocs gh-deploy --force` — updates docs site

README.md

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ The convergence plots demonstrate exponential error decay as node count increase
6363

6464
## Features
6565

66-
- **Full tensor interpolation** via `ChebyshevApproximation` — spectral accuracy for up to ~5-6 dimensions
67-
- **Sliding technique** via `ChebyshevSlider` — additive decomposition for high-dimensional functions (10+ dims)
66+
- **Full tensor interpolation** via `ChebyshevApproximation` — spectral accuracy for up to ~5 dimensions
67+
- **Tensor Train decomposition** via `ChebyshevTT` — TT-Cross builds from O(d·n·r²) evaluations for 5+ dimensions
68+
- **Sliding technique** via `ChebyshevSlider` — additive decomposition for separable high-dimensional functions
6869
- **Analytical derivatives** via spectral differentiation matrices (no finite differences)
6970
- **Vectorized evaluation** using BLAS matrix-vector products (~0.065ms/query)
7071
- **Pure Python** — NumPy + SciPy only, no compiled extensions needed
@@ -114,9 +115,31 @@ results = cheb.vectorized_eval_multi(
114115
)
115116
```
116117

117-
### High-Dimensional Functions (Sliding Technique)
118+
### High-Dimensional Functions (Tensor Train)
118119

119-
For functions with more than ~6 dimensions, use `ChebyshevSlider` to decompose into low-dimensional slides:
120+
For 5+ dimensional functions where full tensor grids are too expensive, use `ChebyshevTT` with TT-Cross:
121+
122+
```python
123+
from pychebyshev import ChebyshevTT
124+
125+
tt = ChebyshevTT(
126+
my_func, num_dimensions=5,
127+
domain=[[-1, 1]] * 5,
128+
n_nodes=[11] * 5,
129+
max_rank=10,
130+
)
131+
tt.build()
132+
val = tt.eval([0.5] * 5)
133+
134+
# Batch evaluation (much faster per point)
135+
import numpy as np
136+
points = np.random.uniform(-1, 1, (1000, 5))
137+
vals = tt.eval_batch(points)
138+
```
139+
140+
### Separable Functions (Sliding Technique)
141+
142+
For functions that decompose additively, use `ChebyshevSlider`:
120143

121144
```python
122145
from pychebyshev import ChebyshevSlider

docs/api/reference.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
::: pychebyshev.ChebyshevSlider
1010

11+
## ChebyshevTT
12+
13+
::: pychebyshev.ChebyshevTT
14+
1115
## Module Functions
1216

1317
::: pychebyshev.barycentric.compute_barycentric_weights

docs/getting-started.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,11 @@ See [Saving & Loading](user-guide/serialization.md) for details.
103103
- More nodes = higher accuracy but more build-time evaluations ($n_1 \times n_2 \times \cdots$)
104104
- For 5D with 11 nodes: $11^5 = 161{,}051$ function evaluations at build time
105105
- Convergence is **exponential** for analytic functions — a few extra nodes can eliminate errors entirely
106+
107+
## Choosing the Right Class
108+
109+
| Class | Dimensions | Build Cost | Derivatives | Best For |
110+
|-------|-----------|-----------|-------------|----------|
111+
| [`ChebyshevApproximation`](user-guide/usage.md) | 1–5 | $n^d$ evals | Analytical | Full accuracy with spectral derivatives |
112+
| [`ChebyshevTT`](user-guide/tensor-train.md) | 5+ | $O(d \cdot n \cdot r^2)$ evals | Finite differences | High-dimensional problems where full grids are infeasible |
113+
| [`ChebyshevSlider`](user-guide/sliding.md) | 5+ | Sum of slide grids | Analytical (per slide) | Functions with additive/separable structure |

docs/index.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22

33
**Fast multi-dimensional Chebyshev tensor interpolation with analytical derivatives.**
44

5-
PyChebyshev builds a Chebyshev interpolant of any smooth function in up to N dimensions, then evaluates it and its derivatives in microseconds using vectorized NumPy operations.
5+
PyChebyshev builds a Chebyshev interpolant of any smooth function in up to N dimensions, then evaluates it and its derivatives in microseconds using vectorized NumPy operations. Three classes cover different use cases:
6+
7+
- **[`ChebyshevApproximation`](user-guide/usage.md)** — full tensor interpolation with analytical derivatives (up to ~5 dimensions)
8+
- **[`ChebyshevTT`](user-guide/tensor-train.md)** — Tensor Train format via TT-Cross for 5+ dimensions
9+
- **[`ChebyshevSlider`](user-guide/sliding.md)** — additive decomposition for separable high-dimensional functions
610

711
## Key Features
812

913
- **Spectral accuracy** — exponential error decay as node count increases
1014
- **Analytical derivatives** — via spectral differentiation matrices (no finite differences)
15+
- **Tensor Train** — TT-Cross builds from O(d·n·r²) evaluations instead of O(n^d)
1116
- **Fast evaluation**~0.065 ms per query (price), ~0.29 ms for price + 5 Greeks
12-
- **Minimal storage** — 55 floats (440 bytes) for a 5D interpolant with 11 nodes per dimension
1317
- **Save & load** — persist built interpolants to disk; rebuild-free deployment
1418
- **Pure Python** — NumPy + SciPy only, no compiled extensions needed
1519

docs/user-guide/error-estimation.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,25 @@ For $\sin(x)$, which is entire (analytic everywhere), the coefficients decay
149149
exponentially, so you should see the estimate drop by several orders of magnitude as
150150
$n$ grows.
151151

152+
## Tensor Train Error Estimation
153+
154+
`ChebyshevTT` also supports `error_estimate()`. The estimate is computed from the
155+
Chebyshev coefficient cores: for each dimension, the last coefficient slice of the
156+
core is extracted and its norm serves as the per-dimension error proxy. These are
157+
summed across dimensions, following the same logic as the full tensor case.
158+
159+
```python
160+
from pychebyshev import ChebyshevTT
161+
162+
tt = ChebyshevTT(my_func, 5, domain, [11]*5, max_rank=10)
163+
tt.build()
164+
print(f"TT error estimate: {tt.error_estimate():.2e}")
165+
```
166+
152167
## API Reference
153168

154169
For full method signatures and parameter details, see:
155170

156171
- [`ChebyshevApproximation.error_estimate()`](../api/reference.md#pychebyshev.ChebyshevApproximation.error_estimate) -- error estimate for standard tensor interpolation.
157172
- [`ChebyshevSlider.error_estimate()`](../api/reference.md#pychebyshev.ChebyshevSlider.error_estimate) -- error estimate for sliding approximation (sum of per-slide errors).
173+
- [`ChebyshevTT.error_estimate()`](../api/reference.md#pychebyshev.ChebyshevTT.error_estimate) -- error estimate for Tensor Train approximation (from coefficient cores).

0 commit comments

Comments
 (0)