From 5c062f3caf7e751f5beb4d470724b3bb9260954a Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sat, 24 Jan 2026 11:10:43 -0500 Subject: [PATCH] mjo/matrix_algebra.py: alllow from_list() to take one long list This makes its behavior more consistent with that of MatrixSpace. --- mjo/matrix_algebra.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/mjo/matrix_algebra.py b/mjo/matrix_algebra.py index 5801f1d..6950b7b 100644 --- a/mjo/matrix_algebra.py +++ b/mjo/matrix_algebra.py @@ -394,8 +394,8 @@ class MatrixAlgebra(CombinatorialFreeModule): def from_list(self, entries): r""" - Construct an element of this algebra from a list of lists of - entries. + Construct an element of this algebra from a list (or list + of lists) of entries. SETUP:: @@ -414,11 +414,26 @@ class MatrixAlgebra(CombinatorialFreeModule): sage: M.to_vector() (0, 0, 0, 1, 0, -1, 0, 0) + One long list of entries will work, too: + + sage: A.from_list([0,I,-I,0]) + ┌────┬───┐ + │ 0 │ I │ + ├────┼───┤ + │ -I │ 0 │ + └────┴───┘ + """ nrows = len(entries) ncols = 0 if nrows > 0: - ncols = len(entries[0]) + if isinstance(entries[0], (list, tuple)): + ncols = len(entries[0]) + else: + # We were given one long list of entries, + # batch them into rows and then try again. + from itertools import batched + return self.from_list(tuple(batched(entries, self.ncols()))) if (not all( len(r) == ncols for r in entries )) or (ncols != nrows): raise ValueError("list must be square") -- 2.51.0