core-adapters 0.1.3
Third-party adapters for snicholls-core: nanobind helpers, nlohmann/json hooks, the snicholls_core Python package
Loading...
Searching...
No Matches
ndarray.hpp
Go to the documentation of this file.
1//
2// ndarray.hpp
3// core-adapters
4//
5// Copyright 2010-2026 Saxon Herschel Nicholls
6//
7
61
62#ifndef snicholls_core_adapters_nanobind_ndarray_hpp
63#define snicholls_core_adapters_nanobind_ndarray_hpp
64
65#include <nanobind/nanobind.h>
66#include <nanobind/ndarray.h>
67
68#include <cstddef>
69#include <memory>
70#include <span>
71#include <stdexcept>
72#include <utility>
73#include <vector>
74
75namespace snicholls::python {
76
79template <class T>
80using vector_in = ::nanobind::ndarray<const T, ::nanobind::ndim<1>, ::nanobind::c_contig, ::nanobind::device::cpu>;
81
84template <class T>
85using matrix_in = ::nanobind::ndarray<const T, ::nanobind::ndim<2>, ::nanobind::c_contig, ::nanobind::device::cpu>;
86
89template <class T>
90using vector_inout = ::nanobind::ndarray<T, ::nanobind::ndim<1>, ::nanobind::c_contig, ::nanobind::device::cpu>;
91
94template <class T>
95using numpy_vector = ::nanobind::ndarray<::nanobind::numpy, T, ::nanobind::ndim<1>>;
96
99template <class T>
100using numpy_matrix = ::nanobind::ndarray<::nanobind::numpy, T, ::nanobind::ndim<2>, ::nanobind::c_contig>;
101
107template <class... Args>
108[[nodiscard]] auto as_span(const ::nanobind::ndarray<Args...>& a) noexcept
109{
110 using array = ::nanobind::ndarray<Args...>;
111 static_assert(array::Order == 'C', "snicholls::python::as_span: the array must be C-contiguous (nanobind::c_contig)");
112 static_assert(array::DeviceType == ::nanobind::device::cpu::value,
113 "snicholls::python::as_span: the array must be on the CPU (nanobind::device::cpu)");
114 using scalar = typename array::Scalar;
115 return std::span<scalar>(a.data(), a.size());
116}
117
118namespace detail {
119
120template <class T>
121void delete_vector(void* p) noexcept
122{
123 delete static_cast<std::vector<T>*>(p);
124}
125
126} // namespace detail
127
137template <class T>
138[[nodiscard]] numpy_matrix<T> to_numpy(std::vector<T>&& values, std::size_t rows, std::size_t columns)
139{
140 if (rows * columns != values.size()) [[unlikely]]
141 throw std::length_error("snicholls::python::to_numpy: rows * columns is not the number of values");
142 auto owned = std::make_unique<std::vector<T>>(std::move(values));
143 T* data = owned->data();
144 ::nanobind::capsule owner(owned.get(), &detail::delete_vector<T>);
145 owned.release();
146 return numpy_matrix<T>(data, {rows, columns}, owner);
147}
148
156template <class T>
157[[nodiscard]] numpy_vector<T> to_numpy(std::vector<T>&& values)
158{
159 const std::size_t n = values.size();
160 auto owned = std::make_unique<std::vector<T>>(std::move(values));
161 T* data = owned->data();
162 ::nanobind::capsule owner(owned.get(), &detail::delete_vector<T>);
163 owned.release();
164 return numpy_vector<T>(data, {n}, owner);
165}
166
167} // namespace snicholls::python
168
169#endif /* snicholls_core_adapters_nanobind_ndarray_hpp */
::nanobind::ndarray< const T, ::nanobind::ndim< 1 >, ::nanobind::c_contig, ::nanobind::device::cpu > vector_in
A read-only, C-contiguous, one-dimensional CPU array of T, as a function argument.
Definition ndarray.hpp:80
::nanobind::ndarray< const T, ::nanobind::ndim< 2 >, ::nanobind::c_contig, ::nanobind::device::cpu > matrix_in
A read-only, C-contiguous (row-major), two-dimensional CPU array of T, as a function argument.
Definition ndarray.hpp:85
::nanobind::ndarray<::nanobind::numpy, T, ::nanobind::ndim< 1 > > numpy_vector
A one-dimensional NumPy array of T, as a result.
Definition ndarray.hpp:95
auto as_span(const ::nanobind::ndarray< Args... > &a) noexcept
A span over a C-contiguous array's elements, all dimensions flattened in row-major order.
Definition ndarray.hpp:108
numpy_matrix< T > to_numpy(std::vector< T > &&values, std::size_t rows, std::size_t columns)
A NumPy array of shape (rows, columns) owning values' buffer, without copying it.
Definition ndarray.hpp:138
::nanobind::ndarray<::nanobind::numpy, T, ::nanobind::ndim< 2 >, ::nanobind::c_contig > numpy_matrix
A two-dimensional, row-major NumPy array of T, as a result.
Definition ndarray.hpp:100
::nanobind::ndarray< T, ::nanobind::ndim< 1 >, ::nanobind::c_contig, ::nanobind::device::cpu > vector_inout
A writable, C-contiguous, one-dimensional CPU array of T, for functions that work in place.
Definition ndarray.hpp:90