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::
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")