Skip to content

Commit 6d72119

Browse files
authored
Merge pull request #1798 from passagemath/matrix_keys_fix-2
Improved handling of `matrix(row_keys=..., column_keys=...)` for callables
2 parents bc76523 + d115afb commit 6d72119

File tree

1 file changed

+64
-6
lines changed

1 file changed

+64
-6
lines changed

src/sage/matrix/args.pyx

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,42 @@ cdef class MatrixArgs:
11391139
to Free module generated by {0, 1, 2, 3, 4} over Integer Ring
11401140
in Category of finite dimensional modules with basis over Integer Ring;
11411141
typ=SEQ_FLAT; entries=[0, 1, 1, 2, 2, 3, 3, 4, 4, 5]>
1142+
sage: MatrixArgs(lambda i, j: i + j,
1143+
....: ncols=3, row_keys=[10, 20, 30, 40]).finalized()
1144+
<MatrixArgs for Set of Morphisms
1145+
from Ambient free module of rank 3 over the principal ideal domain Integer Ring
1146+
to Free module generated by {10, 20, 30, 40} over Integer Ring
1147+
in Category of finite dimensional modules with basis over
1148+
(Dedekind domains and euclidean domains and noetherian rings
1149+
and infinite enumerated sets and metric spaces);
1150+
typ=SEQ_FLAT; entries=[10, 11, 12, 20, 21, 22, 30, 31, 32, 40, 41, 42]>
1151+
sage: MatrixArgs(lambda i, j: i + j,
1152+
....: column_keys=[0, 2, 5], row_keys=[10, 20, 30, 40]).finalized()
1153+
<MatrixArgs for Set of Morphisms
1154+
from Free module generated by {0, 2, 5} over Integer Ring
1155+
to Free module generated by {10, 20, 30, 40} over Integer Ring
1156+
in Category of finite dimensional modules with basis over Integer Ring;
1157+
typ=SEQ_FLAT; entries=[10, 12, 15, 20, 22, 25, 30, 32, 35, 40, 42, 45]>
1158+
sage: MatrixArgs(lambda i, j: i + j,
1159+
....: column_keys=[0, 2, 5], nrows=4).finalized()
1160+
<MatrixArgs for Set of Morphisms
1161+
from Free module generated by {0, 2, 5} over Integer Ring
1162+
to Ambient free module of rank 4 over the principal ideal domain Integer Ring
1163+
in Category of finite dimensional modules with basis over
1164+
(Dedekind domains and euclidean domains and noetherian rings
1165+
and infinite enumerated sets and metric spaces);
1166+
typ=SEQ_FLAT; entries=[0, 2, 5, 1, 3, 6, 2, 4, 7, 3, 5, 8]>
1167+
sage: MatrixArgs(lambda i, j: i + j,
1168+
....: ncols=4, nrows=3).finalized()
1169+
<MatrixArgs for Full MatrixSpace of 3 by 4 dense matrices over Integer Ring;
1170+
typ=SEQ_FLAT; entries=[0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5]>
1171+
sage: MatrixArgs(ZZ, lambda i, j: i + j,
1172+
....: column_keys=[0, 2, 5], row_keys=[10, 20, 30, 40]).finalized()
1173+
<MatrixArgs for Set of Morphisms
1174+
from Free module generated by {0, 2, 5} over Integer Ring
1175+
to Free module generated by {10, 20, 30, 40} over Integer Ring
1176+
in Category of finite dimensional modules with basis over Integer Ring;
1177+
typ=SEQ_FLAT; entries=[10, 12, 15, 20, 22, 25, 30, 32, 35, 40, 42, 45]>
11421178
11431179
Check :issue:`36065`::
11441180
@@ -1241,6 +1277,9 @@ cdef class MatrixArgs:
12411277
else:
12421278
raise AssertionError(f"nrows={self.nrows} ncols={self.ncols} base={self.base} type={self.typ}")
12431279

1280+
if self.typ == MA_ENTRIES_CALLABLE and (self.row_keys is not None or self.column_keys is not None):
1281+
self.finalize_callable()
1282+
12441283
# Non-zero scalar matrices must be square
12451284
# also ensure type is MA_ENTRIES_ZERO for scalar zero matrices
12461285
if self.typ == MA_ENTRIES_SCALAR:
@@ -1518,14 +1557,33 @@ cdef class MatrixArgs:
15181557
self._ensure_nrows_ncols()
15191558
# Dimensions are required, so we must determine the base ring.
15201559
# We do this by converting to the SEQ_FLAT type
1521-
f = self.entries
15221560
cdef list entries = []
15231561
cdef long i, j
1524-
for i in range(self.nrows):
1525-
row = <object>i
1526-
for j in range(self.ncols):
1527-
sig_check()
1528-
entries.append(f(row, j))
1562+
1563+
f = self.entries
1564+
1565+
if self.row_keys is None:
1566+
if self.column_keys is None:
1567+
for i in range(self.nrows):
1568+
for j in range(self.ncols):
1569+
sig_check()
1570+
entries.append(f(i, j))
1571+
else:
1572+
for i in range(self.nrows):
1573+
for col in self.column_keys:
1574+
sig_check()
1575+
entries.append(f(i, col))
1576+
else:
1577+
if self.column_keys is None:
1578+
for row in self.row_keys:
1579+
for j in range(self.ncols):
1580+
sig_check()
1581+
entries.append(f(row, j))
1582+
else:
1583+
for row in self.row_keys:
1584+
for col in self.column_keys:
1585+
sig_check()
1586+
entries.append(f(row, col))
15291587

15301588
self.set_seq_flat(entries)
15311589

0 commit comments

Comments
 (0)