62#ifndef snicholls_core_adapters_nanobind_ndarray_hpp
63#define snicholls_core_adapters_nanobind_ndarray_hpp
65#include <nanobind/nanobind.h>
66#include <nanobind/ndarray.h>
75namespace snicholls::python {
80using vector_in = ::nanobind::ndarray<const T, ::nanobind::ndim<1>, ::nanobind::c_contig, ::nanobind::device::cpu>;
85using matrix_in = ::nanobind::ndarray<const T, ::nanobind::ndim<2>, ::nanobind::c_contig, ::nanobind::device::cpu>;
90using vector_inout = ::nanobind::ndarray<T, ::nanobind::ndim<1>, ::nanobind::c_contig, ::nanobind::device::cpu>;
95using numpy_vector = ::nanobind::ndarray<::nanobind::numpy, T, ::nanobind::ndim<1>>;
100using numpy_matrix = ::nanobind::ndarray<::nanobind::numpy, T, ::nanobind::ndim<2>, ::nanobind::c_contig>;
107template <
class... Args>
108[[nodiscard]]
auto as_span(const ::nanobind::ndarray<Args...>& a)
noexcept
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());
121void delete_vector(
void* p)
noexcept
123 delete static_cast<std::vector<T>*
>(p);
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>);
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>);
::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.
::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.
::nanobind::ndarray<::nanobind::numpy, T, ::nanobind::ndim< 1 > > numpy_vector
A one-dimensional NumPy array of T, as a result.
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.
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.
::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.
::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.