12#ifndef LIBQE_BIND_NANOBIND_HPP
13#define LIBQE_BIND_NANOBIND_HPP
15#include <nanobind/nanobind.h>
16#include <nanobind/ndarray.h>
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>;
31using NpIMat = ::nanobind::ndarray<int32_t, ::nanobind::ndim<2>, ::nanobind::c_contig, ::nanobind::device::cpu>;
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)
44 return arma::rowvec(
const_cast<double*
>(arr.data()), arr.shape(0),
true);
49 return arma::vec(
const_cast<double*
>(arr.data()), arr.shape(0),
true);
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)
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);
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);
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);
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);
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);
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