Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions M2/Macaulay2/m2/engine.m2
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ isSmall := i -> class i === ZZ and i < 2^15 and i > -2^15
isCount := i -> class i === ZZ and i >= 0 and i < 2^15
isListOfIntegers = x -> instance(x, List) and all(x,i -> class i === ZZ)
isListOfListsOfIntegers = x -> instance(x, List) and all(x,isListOfIntegers)
listZ = listZZ = v -> if isListOfIntegers(v = toList splice v) then v else error "expected a list of integers"
listZZ = v -> if isListOfIntegers(v = toList splice v) then v else error "expected a list of integers"
checkCount := i -> if not isCount i then error "expected a small positive integer"

fixup1 := method(Dispatch => Thing) -- stage 1, everything except Tiny and Small
Expand Down Expand Up @@ -323,13 +323,23 @@ RawMatrix.synonym = "raw matrix"
setAttribute(RawMutableMatrix,ReverseDictionary,symbol RawMutableMatrix)
RawMutableMatrix.synonym = "raw mutable matrix"

-- helper functions for negative indices
adjustIndex = (i, n) -> if i < 0 then n + i else i
adjustIndices = (I, n) -> apply(I, i -> adjustIndex(i, n))

rawExtract = method()

rawExtract(RawMatrix,ZZ,ZZ) :=
rawExtract(RawMutableMatrix,ZZ,ZZ) := (m,r,c) -> rawMatrixEntry(m,r,c)
rawExtract(RawMutableMatrix,ZZ,ZZ) := (m,r,c) -> (
r = adjustIndex(r, rawNumberOfRows m);
c = adjustIndex(c, rawNumberOfColumns m);
rawMatrixEntry(m, r, c))

rawExtract(RawMatrix,Sequence,Sequence) :=
rawExtract(RawMutableMatrix,Sequence,Sequence) := (m,r,c) -> rawSubmatrix(m,spliceInside r,spliceInside c)
rawExtract(RawMutableMatrix,Sequence,Sequence) := (m,r,c) -> (
r = adjustIndices(spliceInside r, rawNumberOfRows m);
c = adjustIndices(spliceInside c, rawNumberOfColumns m);
rawSubmatrix(m,spliceInside r,spliceInside c))

RawMatrix _ Sequence :=
RawMutableMatrix _ Sequence := (m,rc) -> ((r,c) -> rawExtract(m,r,c)) rc
Expand Down Expand Up @@ -364,7 +374,13 @@ new RawMatrix from RawRingElement := (RawMatrix,f) -> rawMatrix1(rawFreeModule(r
new RawMatrix from RawMutableMatrix := rawMatrix
new RawMutableMatrix from RawMatrix := rawMutableMatrix

RawMutableMatrix _ Sequence = (M,ij,val) -> ((i,j) -> (rawSetMatrixEntry(M,i,j,val); val)) ij
RawMutableMatrix _ Sequence = (M,ij,val) -> ((i,j) -> (
rawSetMatrixEntry(
M,
adjustIndex(i, rawNumberOfRows M),
adjustIndex(j, rawNumberOfColumns M),
val);
val)) ij

degree RawMatrix := rawMultiDegree
degrees RawMatrix :=f -> {rawMultiDegree rawTarget f,rawMultiDegree rawSource f}
Expand Down
1 change: 1 addition & 0 deletions M2/Macaulay2/m2/enginering.m2
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ Ring _ ZZ := RingElement => (R,i) -> (generators R)#i
protect numallvars

EngineRing _ ZZ := (R,i) -> (
if R.?numallvars and i < 0 then i += R.numallvars;
if i < 0 or R.?numallvars and i >= R.numallvars then error("index ", toString i, " out of bounds 0 .. ", toString (R.numallvars-1));
new R from R.RawRing_i
)
Expand Down
23 changes: 13 additions & 10 deletions M2/Macaulay2/m2/matrix.m2
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ toSameRing = (m,n) -> (
else (m,n))

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

Number == Matrix :=
RingElement == Matrix :=
Expand Down Expand Up @@ -431,8 +431,8 @@ Matrix || Number := (f,g) -> concatRows(f,g*id_(source f))
-----------------------------------------------------------------------------
-- submatrix, submatrixByDegrees
-----------------------------------------------------------------------------
Matrix _ List := Matrix => (f,v) -> submatrix(f,listZ splice v) -- get some columns
Matrix ^ List := Matrix => (f,v) -> submatrix(f,listZ splice v,) -- get some rows
Matrix _ List := Matrix => (f,v) -> submatrix(f, v) -- get some columns
Matrix ^ List := Matrix => (f,v) -> submatrix(f, v,) -- get some rows

Matrix _ ZZ := Vector => (m,i) -> (
R := ring m;
Expand All @@ -442,10 +442,13 @@ Matrix _ ZZ := Vector => (m,i) -> (
new target h from {h})

-- given a map of free modules, find a submatrix of it
submatrixFree = (m, rows, cols) -> map(ring m, if rows === null
then rawSubmatrix(raw cover m, listZZ cols)
else rawSubmatrix(raw cover m, listZZ rows,
if cols =!= null then listZZ cols else 0 .. numgens source m - 1))
submatrixFree = (m, rows, cols) -> (
if rows =!= null then rows = adjustIndices(listZZ rows, numRows m);
if cols =!= null then cols = adjustIndices(listZZ cols, numColumns m);
map(ring m, if rows === null
then rawSubmatrix(raw cover m, cols)
else rawSubmatrix(raw cover m, rows,
if cols =!= null then cols else 0 .. numgens source m - 1)))
-- given a module, find a part of the ambient module
-- along with corresponding generators and relations
sliceModule = (M, rows) -> (
Expand Down
6 changes: 1 addition & 5 deletions M2/Macaulay2/m2/matrix2.m2
Original file line number Diff line number Diff line change
Expand Up @@ -525,13 +525,9 @@ support Ideal := I -> support generators I
--------------------
homogenize = method()

listZ := v -> (
if not all(v,i -> class i === ZZ) then error "expected list of integers";
)

homogCheck := (R, f, v, wts) -> (
if R =!= ring v then error "homogenization requires variable in the same ring";
listZ wts;
wts = listZZ wts;
if degreeLength R =!= 1 then error "homogenization requires degrees of length 1";
-- if # wts != numgens ring f then error "homogenization weight vector has incorrect length";
i := index v;
Expand Down
14 changes: 8 additions & 6 deletions M2/Macaulay2/m2/mutablemat.m2
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ promote(MutableMatrix,Number) := Matrix => (f,S) -> (
--------------------------------
-- submatrices -----------------
--------------------------------
MutableMatrix _ List := Matrix => (f,v) -> submatrix(f,listZ splice v) -- get some columns
MutableMatrix ^ List := Matrix => (f,v) -> submatrix(f,listZ splice v,) -- get some rows
submatrix(MutableMatrix, VisibleList, VisibleList) := (m, rows, cols) -> map(ring m,rawSubmatrix(raw m, listZ toList splice rows, listZ toList splice cols))
submatrix(MutableMatrix, VisibleList ) := (m, cols) -> map(ring m,rawSubmatrix(raw m, listZ toList splice cols))
submatrix(MutableMatrix, Nothing, VisibleList) := (m, rows, cols) -> submatrix(m,cols)
submatrix(MutableMatrix, VisibleList, Nothing) := (m, rows, cols) -> map(ring m, rawSubmatrix(raw m, listZZ rows, 0 .. numColumns m - 1))
MutableMatrix _ List := Matrix => (f,v) -> submatrix(f, v) -- get some columns
MutableMatrix ^ List := Matrix => (f,v) -> submatrix(f, v,) -- get some rows
submatrix(MutableMatrix, VisibleList, VisibleList) := (m, rows, cols) -> submatrixFree(m, rows, cols)
submatrix(MutableMatrix, VisibleList) := (m, cols) -> submatrixFree(m, null, cols)
submatrix(MutableMatrix, Nothing, VisibleList) := (m, rows, cols) -> submatrix(m, cols)
submatrix(MutableMatrix, VisibleList, Nothing) := (m, rows, cols) -> submatrixFree(m, rows, null)
submatrix(MutableMatrix, Nothing, Nothing) := (m, rows, cols) -> m

--------------------------------
Expand Down Expand Up @@ -306,6 +306,8 @@ QRDecomposition Matrix := A -> (
(Q,R) := QRDecomposition A;
(matrix Q,matrix R))

cover MutableMatrix := MutableMatrix => identity

rank MutableMatrix := (M) -> (
if isField ring M then
rawLinAlgRank raw M
Expand Down
2 changes: 1 addition & 1 deletion M2/Macaulay2/packages/Macaulay2Doc/functions/cover-doc.m2
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ document {
SeeAlso => {"ambient", "super"}}

document {
Key => {(cover,Matrix)},
Key => {(cover,Matrix), (cover,MutableMatrix)},
Headline => "get the map between the covering free modules",
Usage => "cover f",
Inputs => {"f"},
Expand Down
8 changes: 7 additions & 1 deletion M2/Macaulay2/tests/normal/mutmat.m2
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ R = QQ
M = mutableMatrix(QQ,3,5)
M_(0,0) = 1_QQ
M_(2,4) = 3/4
M
M_(-1,-2) = 7
assert(M == mutableMatrix {{1,0,0,0,0}, {0,0,0,0,0}, {0,0,0,7,3/4}})
debug Core
rawSubmatrix(raw M,(0,1),(0,1))

Expand All @@ -18,6 +19,7 @@ m
assert(m_(0,2) == z)
m_(1,2) = x+y
assert(m_(1,2) == x+y)
assert(m_(-1,-1) == z)
assert(not (m == mutableMatrix f))
m
rowSwap(m,0,1)
Expand Down Expand Up @@ -72,3 +74,7 @@ assert(ring mutableMatrix(ZZ/101, {{1,2,3}}) === ZZ/101)
M = mutableMatrix {{1, 2, 3}, {4, 5, 6}}
assert Equation(target M, ZZ^2)
assert Equation(source M, ZZ^3)

-- submatrices
assert Equation(M_{0, -1}, mutableMatrix {{1, 3}, {4, 6}})
assert Equation(M^{-1}, mutableMatrix {{4, 5, 6}})
1 change: 1 addition & 0 deletions M2/Macaulay2/tests/normal/rings.m2
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ assert( f == g )
assert( class f === R )
assert( class g === R )
assert( class h === S )
assert( S_-1 === c )

--Date: Fri, 16 Apr 2004 11:43:07 +0000 (UTC)
--From: Ben Richert <[email protected]>
Expand Down
3 changes: 3 additions & 0 deletions M2/Macaulay2/tests/normal/submatrix.m2
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ N = coker matrix{{x}}
m = map(N++R^1, N++R^1, sub(M, R))
assert(m^{0} == map(N, N++R^1, {{a, b}}))
assert(m^{1} == map(R^1, N++R^1, {{c, d}}))
assert(m^{-1} == map(R^1, N++R^1, {{c, d}}))
assert(m_{0} == map(N++R^1, N, {{a}, {c}}))
assert(m_{1} == map(N++R^1, R^1, {{b}, {d}}))
assert(m_{-1} == map(N++R^1, R^1, {{b}, {d}}))
assert(m_(-1,-2) == c)

R = QQ[a..d];
M = image vars R ++ coker vars R
Expand Down
Loading