Skip to content

Commit 91223c7

Browse files
authored
Add pybind11/eigen.h type_caster for pkbind; simplify RDP bindings (#10)
* Add pybind11/eigen.h type_caster for pkbind; simplify RDP bindings Extract ndarray_base and ndarray<T> binding classes into include/ndarray_binding.hpp so that the new include/pybind11/eigen.h header can access them. eigen.h provides automatic Eigen ↔ pocketpy ndarray conversion via pkbind's type_caster mechanism: - type_caster for Eigen dense types (Matrix, Vector, Array) - type_caster for Eigen::Ref with persistent storage - Compile-time dimension checking enables Nx2 vs Nx3 overload resolution RDP bindings in numpy.cpp now use Eigen types directly (matching src/pybind.cpp interface), removing ~80 lines of manual conversion helpers. * update wasm * update version * Move eigen.h from pocketpy submodule to include/pkbind/ Avoid polluting the pocketpy submodule and prevent shadowing the real pybind11/eigen.h used by src/pybind.cpp. * add claude.md * no amend
1 parent 4b4ed81 commit 91223c7

File tree

7 files changed

+3068
-2829
lines changed

7 files changed

+3068
-2829
lines changed

CLAUDE.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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`)

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ python3 -m pocket_numpy --help
66
python3 -m pocket_numpy tests/test_numpy.py
77
```
88

9+
# Try is online
10+
11+
<https://cubao.github.io/xtensor-numpy>
12+
913
---
1014

1115
# How to build (for dev)

docs/lib/pocketpy.wasm

6.92 KB
Binary file not shown.

0 commit comments

Comments
 (0)