Skip to content

Commit

Permalink
Implement Matrix constructor from an iterable of rows
Browse files Browse the repository at this point in the history
  • Loading branch information
althonos committed Apr 24, 2023
1 parent 49203f0 commit c99e008
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion pymemesuite/common.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit c99e008

Please sign in to comment.