-
I am having some trouble returning a column major matrix with nanobind's m.def("get_matrix", []() {
float *data = new float[4]{1, 2, 3, 4};
nb::capsule owner(data, [](void *p) noexcept { delete[] (float *)p; });
return nb::ndarray<nb::numpy, const float, nb::f_contig,
nb::shape<2, 2>>(data, {2, 2}, owner);
}); Here is the python output: >>> import nanobind_example
>>> nanobind_example.nanobind_example_ext.get_matrix()
array([[1., 2.],
[3., 4.]], dtype=float32)
>>> nanobind_example.nanobind_example_ext.get_matrix().flags
C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : False
WRITEABLE : False
ALIGNED : True
WRITEBACKIFCOPY : False The result shows the numpy matrix has the right (non-owning, read only) raw data, but its order is c-contiguous instead f-continuous. It seems |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
When returning an array, the annotations like It would be an interesting possibility to compute |
Beta Was this translation helpful? Give feedback.
When returning an array, the annotations like
f_contig
orshape
are just decorations to get a nice type annotation. It's your task to give all the relevant info to thenb::ndarray
constructor (runtime values) so that the result indeed has these properties. In your case, I think that you need to specify thestrides
parameter.It would be an interesting possibility to compute
strides
automatically if the return value is annotated, but this is not implemented right now. These decorations just play a role on the input end.