@@ -26,7 +26,7 @@ auto register_vector_type(nb::module_ &m, const char *name) {
2626 for (size_t i = 0 , size = std::min (Vector::Size, nb::len (arr)); i < size; ++i)
2727 (*v)[i] = nb::cast<Value>(arr[i]);
2828 })
29- .def (" __len__" , [](const Vector &) { return Size; })
29+ .def (" __len__" , [](const Vector &) -> size_t { return Vector:: Size; })
3030 .def (-nb::self)
3131 .def (nb::self == nb::self)
3232 .def (nb::self != nb::self)
@@ -107,19 +107,17 @@ auto register_matrix_type(nb::module_ &m, const char *name) {
107107 .def (nb::init<const Matrix &>());
108108
109109 type.def (" __init__" ,
110- [](Matrix *m, const nb::ndarray<Value, nb::f_contig, nb:: shape<Size, Size>, nb::ro, nb::device::cpu> &array) {
110+ [](Matrix *m, const nb::ndarray<Value, nb::shape<Size, Size>, nb::ro, nb::device::cpu> &array) {
111111 new (m) Matrix ();
112- memcpy (m->m , array.data (), sizeof (Value)*Size*Size);
113- })
114- .def (" __init__" ,
115- [](Matrix *m, const nb::ndarray<Value, nb::c_contig, nb::shape<Size, Size>, nb::ro, nb::device::cpu> &array) {
116- new (m) Matrix ();
117- memcpy (m->m , array.data (), sizeof (Value)*Size*Size);
118- *m = m->T ();
112+ for (size_t i = 0 ; i < Matrix::Size; ++i) {
113+ for (size_t j = 0 ; j < Matrix::Size; ++j) {
114+ m->m [j][i] = array (i, j); // Column-major storage
115+ }
116+ }
119117 })
120118 .def_prop_ro (" T" , &Matrix::T)
121119 .def (" __matmul__" , [](const Matrix &a, const Matrix &b) { return a * b; }, nb::is_operator ())
122- .def (" __len__" , [](const Matrix &) { return Size; })
120+ .def (" __len__" , [](const Matrix &) -> size_t { return Matrix:: Size; })
123121 .def (" __getitem__" , [](const Matrix &m, size_t index) -> const Vector& {
124122 if (index >= Vector::Size)
125123 throw nb::index_error ();
@@ -137,7 +135,7 @@ auto register_matrix_type(nb::module_ &m, const char *name) {
137135 delete (Matrix *) p;
138136 });
139137
140- return nb::ndarray<float >(&t->m , {Size, Size}, owner);
138+ return nb::ndarray<float >(&t->m , {Matrix:: Size, Matrix:: Size}, owner);
141139 })
142140 .def (" __repr__" , [](const Matrix &m) {
143141 std::ostringstream oss;
0 commit comments