|
| 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 |
0 commit comments