Skip to content

Commit 862c59c

Browse files
committed
Add support for negative indices for ring variables & submatrices
We count from the right when the index is negative, just like lists.
1 parent 5b7c440 commit 862c59c

File tree

4 files changed

+12
-4
lines changed

4 files changed

+12
-4
lines changed

M2/Macaulay2/m2/enginering.m2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ Ring _ ZZ := RingElement => (R,i) -> (generators R)#i
448448
protect numallvars
449449

450450
EngineRing _ ZZ := (R,i) -> (
451+
if R.?numallvars and i < 0 then i += R.numallvars;
451452
if i < 0 or R.?numallvars and i >= R.numallvars then error("index ", toString i, " out of bounds 0 .. ", toString (R.numallvars-1));
452453
new R from R.RawRing_i
453454
)

M2/Macaulay2/m2/matrix.m2

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,10 +442,14 @@ 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-
submatrixFree = (m, rows, cols) -> map(ring m, if rows === null
446-
then rawSubmatrix(raw cover m, listZZ cols)
447-
else rawSubmatrix(raw cover m, listZZ rows,
448-
if cols =!= null then listZZ cols else 0 .. numgens source m - 1))
445+
adjustindex := (I, n) -> apply(I, i -> if i < 0 then n + i else i)
446+
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);
449+
map(ring m, if rows === null
450+
then rawSubmatrix(raw cover m, cols)
451+
else rawSubmatrix(raw cover m, rows,
452+
if cols =!= null then cols else 0 .. numgens source m - 1)))
449453
-- given a module, find a part of the ambient module
450454
-- along with corresponding generators and relations
451455
sliceModule = (M, rows) -> (

M2/Macaulay2/tests/normal/rings.m2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ assert( f == g )
77
assert( class f === R )
88
assert( class g === R )
99
assert( class h === S )
10+
assert( S_-1 === c )
1011

1112
--Date: Fri, 16 Apr 2004 11:43:07 +0000 (UTC)
1213
--From: Ben Richert <[email protected]>

M2/Macaulay2/tests/normal/submatrix.m2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ N = coker matrix{{x}}
1919
m = map(N++R^1, N++R^1, sub(M, R))
2020
assert(m^{0} == map(N, N++R^1, {{a, b}}))
2121
assert(m^{1} == map(R^1, N++R^1, {{c, d}}))
22+
assert(m^{-1} == map(R^1, N++R^1, {{c, d}}))
2223
assert(m_{0} == map(N++R^1, N, {{a}, {c}}))
2324
assert(m_{1} == map(N++R^1, R^1, {{b}, {d}}))
25+
assert(m_{-1} == map(N++R^1, R^1, {{b}, {d}}))
2426

2527
R = QQ[a..d];
2628
M = image vars R ++ coker vars R

0 commit comments

Comments
 (0)