33#include < nanobind/stl/string.h>
44#include < nanobind/stl/vector.h>
55
6+ #include < cstdint>
67#include < cstddef>
78#include < stdexcept>
89#include < string>
@@ -16,6 +17,7 @@ namespace {
1617
1718using Vector = nb::ndarray<const double , nb::ndim<1 >, nb::c_contig>;
1819using IntVector = nb::ndarray<const int , nb::ndim<1 >, nb::c_contig>;
20+ using Matrix = nb::ndarray<nb::numpy, double , nb::shape<-1 , -1 >, nb::f_contig>;
1921
2022std::size_t checked_size (const Vector& x, const char * name) {
2123 const std::size_t n = x.shape (0 );
@@ -25,6 +27,14 @@ std::size_t checked_size(const Vector& x, const char* name) {
2527 return n;
2628}
2729
30+ Matrix matrix_from_column_major_vector (std::vector<double >&& values, std::size_t dim) {
31+ auto * storage = new std::vector<double >(std::move (values));
32+ nb::capsule owner (storage, [](void * p) noexcept {
33+ delete static_cast <std::vector<double >*>(p);
34+ });
35+ return Matrix (storage->data (), {dim, dim}, owner, {1 , static_cast <int64_t >(dim)});
36+ }
37+
2838void check_same_size (const Vector& x, const Vector& y, const char * x_name, const char * y_name) {
2939 if (checked_size (x, x_name) != checked_size (y, y_name)) {
3040 throw std::invalid_argument (std::string (x_name) + " and " + y_name + " must have the same length." );
@@ -136,14 +146,14 @@ nb::dict pm_matrix_dict(double degree_lpm,
136146 throw std::invalid_argument (" target length must equal d." );
137147 }
138148 checked_flat_matrix_size (variable, n, d, " variable" );
139- const nns::PMMatrixResult result = nns::pm_matrix (degree_lpm, degree_upm, target.data (),
140- variable.data (), n, d, pop_adj, norm);
149+ nns::PMMatrixResult result = nns::pm_matrix (degree_lpm, degree_upm, target.data (),
150+ variable.data (), n, d, pop_adj, norm);
141151 nb::dict out;
142- out[" cupm" ] = result.cupm ;
143- out[" dupm" ] = result.dupm ;
144- out[" dlpm" ] = result.dlpm ;
145- out[" clpm" ] = result.clpm ;
146- out[" cov.matrix" ] = result.cov ;
152+ out[" cupm" ] = matrix_from_column_major_vector ( std::move ( result.cupm ), result. dim ) ;
153+ out[" dupm" ] = matrix_from_column_major_vector ( std::move ( result.dupm ), result. dim ) ;
154+ out[" dlpm" ] = matrix_from_column_major_vector ( std::move ( result.dlpm ), result. dim ) ;
155+ out[" clpm" ] = matrix_from_column_major_vector ( std::move ( result.clpm ), result. dim ) ;
156+ out[" cov.matrix" ] = matrix_from_column_major_vector ( std::move ( result.cov ), result. dim ) ;
147157 out[" dim" ] = result.dim ;
148158 return out;
149159}
0 commit comments