libqe 0.2.6-dev.9001
QE C++ library
Loading...
Searching...
No Matches
nanobind.hpp
Go to the documentation of this file.
1
12#ifndef LIBQE_BIND_NANOBIND_HPP
13#define LIBQE_BIND_NANOBIND_HPP
14
15#include <nanobind/nanobind.h>
16#include <nanobind/ndarray.h>
17
18#include <armadillo>
19#include <algorithm>
20#include <cstddef>
21#include <cstdint>
22
23namespace qe {
24namespace bind {
25namespace py {
26
27using NpMat = ::nanobind::ndarray<double, ::nanobind::ndim<2>, ::nanobind::c_contig, ::nanobind::device::cpu>;
28using NpVec = ::nanobind::ndarray<double, ::nanobind::ndim<1>, ::nanobind::c_contig, ::nanobind::device::cpu>;
29
30// int32 matrix type for context_lookup (arma::imat)
31using NpIMat = ::nanobind::ndarray<int32_t, ::nanobind::ndim<2>, ::nanobind::c_contig, ::nanobind::device::cpu>;
32
33// numpy 2-D (rows × cols, C-order) → arma::mat (col-major)
34inline arma::mat to_mat(NpMat arr) {
35 arma::mat m(arr.shape(0), arr.shape(1));
36 for (size_t i = 0; i < arr.shape(0); ++i)
37 for (size_t j = 0; j < arr.shape(1); ++j)
38 m(i, j) = arr(i, j);
39 return m;
40}
41
42// numpy 1-D → arma::rowvec (copy)
43inline arma::rowvec to_rowvec(NpVec arr) {
44 return arma::rowvec(const_cast<double*>(arr.data()), arr.shape(0), /*copy=*/true);
45}
46
47// numpy 1-D → arma::vec (copy)
48inline arma::vec to_vec(NpVec arr) {
49 return arma::vec(const_cast<double*>(arr.data()), arr.shape(0), /*copy=*/true);
50}
51
52// numpy 2-D int32 (rows × cols, C-order) → arma::imat
53inline arma::imat to_imat(NpIMat arr) {
54 arma::imat m(arr.shape(0), arr.shape(1));
55 for (size_t i = 0; i < arr.shape(0); ++i)
56 for (size_t j = 0; j < arr.shape(1); ++j)
57 m(i, j) = arr(i, j);
58 return m;
59}
60
61// arma::mat → numpy 2-D (rows × cols, C-order, Python owns the copy)
62inline ::nanobind::ndarray<::nanobind::numpy, double, ::nanobind::ndim<2>> from_mat(const arma::mat& m) {
63 size_t shape[2] = {m.n_rows, m.n_cols};
64 double* data = new double[m.n_rows * m.n_cols];
65 for (size_t i = 0; i < m.n_rows; ++i)
66 for (size_t j = 0; j < m.n_cols; ++j)
67 data[i * m.n_cols + j] = m(i, j);
68 ::nanobind::capsule owner(data, [](void* p) noexcept { delete[] static_cast<double*>(p); });
69 return ::nanobind::ndarray<::nanobind::numpy, double, ::nanobind::ndim<2>>(data, 2, shape, owner);
70}
71
72// arma::rowvec → numpy 1-D (Python owns the copy)
73inline ::nanobind::ndarray<::nanobind::numpy, double, ::nanobind::ndim<1>> from_rowvec(const arma::rowvec& v) {
74 size_t shape[1] = {v.n_elem};
75 double* data = new double[v.n_elem];
76 std::copy(v.begin(), v.end(), data);
77 ::nanobind::capsule owner(data, [](void* p) noexcept { delete[] static_cast<double*>(p); });
78 return ::nanobind::ndarray<::nanobind::numpy, double, ::nanobind::ndim<1>>(data, 1, shape, owner);
79}
80
81// arma::vec (column vector) → numpy 1-D
82inline ::nanobind::ndarray<::nanobind::numpy, double, ::nanobind::ndim<1>> from_vec(const arma::vec& v) {
83 size_t shape[1] = {v.n_elem};
84 double* data = new double[v.n_elem];
85 std::copy(v.begin(), v.end(), data);
86 ::nanobind::capsule owner(data, [](void* p) noexcept { delete[] static_cast<double*>(p); });
87 return ::nanobind::ndarray<::nanobind::numpy, double, ::nanobind::ndim<1>>(data, 1, shape, owner);
88}
89
90// arma::umat → numpy 2-D int64 (Python owns the copy)
91inline ::nanobind::ndarray<::nanobind::numpy, int64_t, ::nanobind::ndim<2>> from_umat(const arma::umat& m) {
92 size_t shape[2] = {m.n_rows, m.n_cols};
93 int64_t* data = new int64_t[m.n_rows * m.n_cols];
94 for (size_t i = 0; i < m.n_rows; ++i)
95 for (size_t j = 0; j < m.n_cols; ++j)
96 data[i * m.n_cols + j] = static_cast<int64_t>(m(i, j));
97 ::nanobind::capsule owner(data, [](void* p) noexcept { delete[] static_cast<int64_t*>(p); });
98 return ::nanobind::ndarray<::nanobind::numpy, int64_t, ::nanobind::ndim<2>>(data, 2, shape, owner);
99}
100
101// arma::uvec → numpy 1-D int64
102inline ::nanobind::ndarray<::nanobind::numpy, int64_t, ::nanobind::ndim<1>> from_uvec(const arma::uvec& v) {
103 size_t shape[1] = {v.n_elem};
104 int64_t* data = new int64_t[v.n_elem];
105 for (size_t i = 0; i < v.n_elem; ++i) data[i] = static_cast<int64_t>(v[i]);
106 ::nanobind::capsule owner(data, [](void* p) noexcept { delete[] static_cast<int64_t*>(p); });
107 return ::nanobind::ndarray<::nanobind::numpy, int64_t, ::nanobind::ndim<1>>(data, 1, shape, owner);
108}
109
110} // namespace py
111} // namespace bind
112} // namespace qe
113
114#endif // LIBQE_BIND_NANOBIND_HPP
inline ::nanobind::ndarray<::nanobind::numpy, double, ::nanobind::ndim< 2 > > from_mat(const arma::mat &m)
Definition nanobind.hpp:62
inline ::nanobind::ndarray<::nanobind::numpy, double, ::nanobind::ndim< 1 > > from_rowvec(const arma::rowvec &v)
Definition nanobind.hpp:73
arma::mat to_mat(NpMat arr)
Definition nanobind.hpp:34
::nanobind::ndarray< double, ::nanobind::ndim< 2 >, ::nanobind::c_contig, ::nanobind::device::cpu > NpMat
Definition nanobind.hpp:27
::nanobind::ndarray< double, ::nanobind::ndim< 1 >, ::nanobind::c_contig, ::nanobind::device::cpu > NpVec
Definition nanobind.hpp:28
inline ::nanobind::ndarray<::nanobind::numpy, double, ::nanobind::ndim< 1 > > from_vec(const arma::vec &v)
Definition nanobind.hpp:82
::nanobind::ndarray< int32_t, ::nanobind::ndim< 2 >, ::nanobind::c_contig, ::nanobind::device::cpu > NpIMat
Definition nanobind.hpp:31
arma::imat to_imat(NpIMat arr)
Definition nanobind.hpp:53
inline ::nanobind::ndarray<::nanobind::numpy, int64_t, ::nanobind::ndim< 1 > > from_uvec(const arma::uvec &v)
Definition nanobind.hpp:102
arma::vec to_vec(NpVec arr)
Definition nanobind.hpp:48
inline ::nanobind::ndarray<::nanobind::numpy, int64_t, ::nanobind::ndim< 2 > > from_umat(const arma::umat &m)
Definition nanobind.hpp:91
arma::rowvec to_rowvec(NpVec arr)
Definition nanobind.hpp:43
Definition adjacency.hpp:16