From c99e0082c311df529411a0c54e9f4bd6db4a63df Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Mon, 24 Apr 2023 22:32:11 +0200 Subject: [PATCH] Implement `Matrix` constructor from an iterable of rows --- pymemesuite/common.pyx | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pymemesuite/common.pyx b/pymemesuite/common.pyx index e655283..11d9586 100644 --- a/pymemesuite/common.pyx +++ b/pymemesuite/common.pyx @@ -596,7 +596,20 @@ cdef class Matrix: Create a new matrix from the given iterable of values. """ - raise NotImplementedError("Matrix.__init__") + cdef size_t m = len(iterable) + cdef size_t n + + for i, row in enumerate(iterable): + if i == 0: + n = len(row) + self._owner = None + self._mx = libmeme.matrix.allocate_matrix(m, n) + if self._mx is NULL: + raise AllocationError("MATRIX_T", sizeof(MATRIX_T)) + elif len(row) != n: + raise ValueError(f"Invalid length for row {i}: {len(row)}") + for j, value in enumerate(row): + self._set_element(i, j, value) def __dealloc__(self): if self._owner is None: