]> gitweb.michael.orlitzky.com - sage.d.git/commitdiff
mjo/matrix_algebra.py: alllow from_list() to take one long list
authorMichael Orlitzky <michael@orlitzky.com>
Sat, 24 Jan 2026 16:10:43 +0000 (11:10 -0500)
committerMichael Orlitzky <michael@orlitzky.com>
Sat, 24 Jan 2026 16:16:07 +0000 (11:16 -0500)
This makes its behavior more consistent with that of MatrixSpace.

mjo/matrix_algebra.py

index 5801f1d4e671647b0914e9d38bfb0a793a374545..6950b7bc4c7c7896822bfecb65b9d6449614909d 100644 (file)
@@ -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")