|
| 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 | +**pocket_numpy** (repo: xtensor-numpy) is a C++ project that integrates the pocketpy lightweight Python interpreter with numpy-like array functionality powered by xtensor and Eigen. It produces: |
| 8 | +- A native CLI (`pocketpy.exe`) that runs Python scripts with numpy support |
| 9 | +- Python wheels via pybind11 (package name: `pocket_numpy`) |
| 10 | +- A WebAssembly build for browser deployment |
| 11 | + |
| 12 | +## Build & Test Commands |
| 13 | + |
| 14 | +```bash |
| 15 | +# Build (installs in editable mode with scikit-build-core) |
| 16 | +make build |
| 17 | + |
| 18 | +# Run numpy tests via pocketpy CLI |
| 19 | +make test # runs: build/pocketpy.exe tests/test_numpy.py |
| 20 | + |
| 21 | +# Run a single test file |
| 22 | +build/pocketpy.exe tests/test_numpy.py |
| 23 | + |
| 24 | +# Run tests via pytest (after pip install) |
| 25 | +python3 -m pytest tests/ |
| 26 | + |
| 27 | +# Install as pip package |
| 28 | +make python_install |
| 29 | + |
| 30 | +# Build WASM |
| 31 | +make build_wasm |
| 32 | + |
| 33 | +# Serve WASM demo locally |
| 34 | +make serve_wasm |
| 35 | +``` |
| 36 | + |
| 37 | +## Architecture |
| 38 | + |
| 39 | +### Core Type System (`include/numpy.hpp`) |
| 40 | + |
| 41 | +Defines C++ type aliases (int8–64, uint8–64, float32, float64, bool_, complex64/128) and `dtype_traits<T>` template for runtime dtype identification. Contains the `ndarray<T>` template backed by `xt::xarray<T>`. |
| 42 | + |
| 43 | +### Polymorphic ndarray (`include/ndarray_binding.hpp`) |
| 44 | + |
| 45 | +`ndarray_base` is an abstract base class with ~80+ virtual methods (shape, reductions, slicing, arithmetic, sorting, etc.). `ndarray<T>` is the concrete template implementation. This virtual dispatch pattern enables a single Python-facing interface that works across all numeric types via `std::unique_ptr<ndarray_base>`. |
| 46 | + |
| 47 | +### Module Bindings (`src/numpy.cpp`) |
| 48 | + |
| 49 | +Registers the `numpy` module into pocketpy's runtime. Binds all ndarray methods, array creation functions (ones, zeros, full, arange, linspace, identity), random number generation, and dtype attributes. This is the largest source file. |
| 50 | + |
| 51 | +### RDP Bindings (`src/pybind.cpp` + `src/rdp.hpp`) |
| 52 | + |
| 53 | +Ramer-Douglas-Peucker polyline simplification exposed via pybind11 as the `_core` module. Supports 2D and 3D point arrays using Eigen matrix types. |
| 54 | + |
| 55 | +### CLI Entry Point (`src/main.cpp`) |
| 56 | + |
| 57 | +Initializes pocketpy, registers the numpy module via `py_module_initialize()`, then executes a Python script or enters REPL mode. |
| 58 | + |
| 59 | +### Dependencies |
| 60 | + |
| 61 | +- **pocketpy** — git submodule at `pocketpy/` |
| 62 | +- **xtensor, xtl, Eigen** — vendored headers in `include/` |
| 63 | +- **pybind11** — required via pip/CMake for Python wheel builds |
| 64 | + |
| 65 | +### Build Targets (CMake) |
| 66 | + |
| 67 | +- `numpy` — static library (numpy.cpp + pocketpy) |
| 68 | +- `pocketpy_cli` — CLI executable linked against numpy |
| 69 | +- `_core` — pybind11 module for RDP bindings |
| 70 | + |
| 71 | +## Git Policy |
| 72 | + |
| 73 | +- **NEVER use `git commit --amend`**. Always create a new commit instead. Amending rewrites history and can destroy previous work, especially after a failed pre-commit hook where `--amend` would silently modify the wrong commit. |
| 74 | + |
| 75 | +## Key Conventions |
| 76 | + |
| 77 | +- C++17 required; C11 for pocketpy C sources |
| 78 | +- Default build type is Release with `-O3`; debug uses `-O0 -ggdb` |
| 79 | +- xtensor warnings are suppressed by default (`SHOW_XTENSOR_WARNINGS=OFF`) |
| 80 | +- Tests are standalone Python scripts (not pytest-based) so they can also run under pocketpy and WASM |
| 81 | +- macOS deployment target is 10.14+ (required for `std::visit`) |
0 commit comments