Skip to content

Commit b7aa724

Browse files
committed
Allow negative indices when getting matrix entries
Also support using Matrix_Sequence to get submatrices, just like we've been doing with MutableMatrix_Sequence.
1 parent 0cfd17a commit b7aa724

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

M2/Macaulay2/m2/engine.m2

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,23 @@ RawMatrix.synonym = "raw matrix"
323323
setAttribute(RawMutableMatrix,ReverseDictionary,symbol RawMutableMatrix)
324324
RawMutableMatrix.synonym = "raw mutable matrix"
325325

326+
-- helper functions for negative indices
327+
adjustIndex = (i, n) -> if i < 0 then n + i else i
328+
adjustIndices = (I, n) -> apply(I, i -> adjustIndex(i, n))
329+
326330
rawExtract = method()
327331

328332
rawExtract(RawMatrix,ZZ,ZZ) :=
329-
rawExtract(RawMutableMatrix,ZZ,ZZ) := (m,r,c) -> rawMatrixEntry(m,r,c)
333+
rawExtract(RawMutableMatrix,ZZ,ZZ) := (m,r,c) -> (
334+
r = adjustIndex(r, rawNumberOfRows m);
335+
c = adjustIndex(c, rawNumberOfColumns m);
336+
rawMatrixEntry(m, r, c))
330337

331338
rawExtract(RawMatrix,Sequence,Sequence) :=
332-
rawExtract(RawMutableMatrix,Sequence,Sequence) := (m,r,c) -> rawSubmatrix(m,spliceInside r,spliceInside c)
339+
rawExtract(RawMutableMatrix,Sequence,Sequence) := (m,r,c) -> (
340+
r = adjustIndices(spliceInside r, rawNumberOfRows m);
341+
c = adjustIndices(spliceInside c, rawNumberOfColumns m);
342+
rawSubmatrix(m,spliceInside r,spliceInside c))
333343

334344
RawMatrix _ Sequence :=
335345
RawMutableMatrix _ Sequence := (m,rc) -> ((r,c) -> rawExtract(m,r,c)) rc
@@ -364,7 +374,13 @@ new RawMatrix from RawRingElement := (RawMatrix,f) -> rawMatrix1(rawFreeModule(r
364374
new RawMatrix from RawMutableMatrix := rawMatrix
365375
new RawMutableMatrix from RawMatrix := rawMutableMatrix
366376

367-
RawMutableMatrix _ Sequence = (M,ij,val) -> ((i,j) -> (rawSetMatrixEntry(M,i,j,val); val)) ij
377+
RawMutableMatrix _ Sequence = (M,ij,val) -> ((i,j) -> (
378+
rawSetMatrixEntry(
379+
M,
380+
adjustIndex(i, rawNumberOfRows M),
381+
adjustIndex(j, rawNumberOfColumns M),
382+
val);
383+
val)) ij
368384

369385
degree RawMatrix := rawMultiDegree
370386
degrees RawMatrix :=f -> {rawMultiDegree rawTarget f,rawMultiDegree rawSource f}

M2/Macaulay2/m2/matrix.m2

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ toSameRing = (m,n) -> (
9797
else (m,n))
9898

9999
Matrix _ Sequence := RingElement => (m,ind) -> (
100-
if # ind === 2
101-
then promote(rawMatrixEntry(m.RawMatrix, ind#0, ind#1), ring m)
102-
else error "expected a sequence of length two"
103-
)
100+
n := (raw m)_ind;
101+
if instance(n, RawRingElement) then promote(n, ring m)
102+
else if instance(n, RawMatrix) then map(ring m, n)
103+
else error "internal error")
104104

105105
Number == Matrix :=
106106
RingElement == Matrix :=
@@ -442,10 +442,9 @@ Matrix _ ZZ := Vector => (m,i) -> (
442442
new target h from {h})
443443

444444
-- given a map of free modules, find a submatrix of it
445-
adjustindex := (I, n) -> apply(I, i -> if i < 0 then n + i else i)
446445
submatrixFree = (m, rows, cols) -> (
447-
if rows =!= null then rows = adjustindex(listZZ rows, numRows m);
448-
if cols =!= null then cols = adjustindex(listZZ cols, numColumns m);
446+
if rows =!= null then rows = adjustIndices(listZZ rows, numRows m);
447+
if cols =!= null then cols = adjustIndices(listZZ cols, numColumns m);
449448
map(ring m, if rows === null
450449
then rawSubmatrix(raw cover m, cols)
451450
else rawSubmatrix(raw cover m, rows,

M2/Macaulay2/tests/normal/mutmat.m2

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ R = QQ
22
M = mutableMatrix(QQ,3,5)
33
M_(0,0) = 1_QQ
44
M_(2,4) = 3/4
5-
M
5+
M_(-1,-2) = 7
6+
assert(M == mutableMatrix {{1,0,0,0,0}, {0,0,0,0,0}, {0,0,0,7,3/4}})
67
debug Core
78
rawSubmatrix(raw M,(0,1),(0,1))
89

@@ -18,6 +19,7 @@ m
1819
assert(m_(0,2) == z)
1920
m_(1,2) = x+y
2021
assert(m_(1,2) == x+y)
22+
assert(m_(-1,-1) == z)
2123
assert(not (m == mutableMatrix f))
2224
m
2325
rowSwap(m,0,1)

M2/Macaulay2/tests/normal/submatrix.m2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ assert(m^{-1} == map(R^1, N++R^1, {{c, d}}))
2323
assert(m_{0} == map(N++R^1, N, {{a}, {c}}))
2424
assert(m_{1} == map(N++R^1, R^1, {{b}, {d}}))
2525
assert(m_{-1} == map(N++R^1, R^1, {{b}, {d}}))
26+
assert(m_(-1,-2) == c)
2627

2728
R = QQ[a..d];
2829
M = image vars R ++ coker vars R

0 commit comments

Comments
 (0)