33
44needs " modules.m2"
55
6+ notsamering := (X,Y) -> (
7+ if X === Y then error (" expected " ,pluralsynonym X, " for the same ring" )
8+ else error (" expected " ,synonym X," and " ,synonym Y," for the same ring" ))
9+ samering = (M,N) -> if ring M === ring N then (M,N) else notsamering(class M,class N)
10+ nottosamering := (X,Y) -> (
11+ if X === Y then error (" expected " ,pluralsynonym X, " for compatible rings" )
12+ else error (" expected " ,synonym X," and " ,synonym Y," for compatible rings" ))
13+ tosamering = (M,N) -> (
14+ R := ring M; S := ring N;
15+ if R =!= S then (
16+ if isPromotable(R,S) then (promote (M,S),N)
17+ else if isPromotable(S,R) then (M,promote (N,R))
18+ else nottosamering(class M,class N)
19+ )
20+ else (M,N))
21+
622oops := R -> error (
723 if degreeLength R === 1
824 then " expected degree to be an integer or list of integers of length 1"
@@ -79,23 +95,15 @@ InfiniteNumber * Matrix := (r,m) -> (map(target m, source m, matrix(r*(entries m
7995Matrix * InfiniteNumber := (m,r) -> r* m
8096Number * Matrix :=
8197RingElement * Matrix := (r,m) -> (
82- if ring r =!= ring m then try r = promote (r,ring m) else m = promote (m, ring r );
98+ (r,m) = tosamering(r,m );
8399 map (target m, source m, reduce(target m, raw r * raw m)))
84100Matrix * Number :=
85101Matrix * RingElement := (m,r) -> (
86- if ring r =!= ring m then try r = promote (r,ring m) else m = promote (m, ring r );
102+ (r,m) = tosamering(r,m );
87103 map (target m, source m, reduce(target m, raw m * raw r)))
88104Matrix / Number :=
89105Matrix / RingElement := (m,r) -> m * (1/r)
90106
91- toSameRing = (m,n) -> (
92- if ring m =!= ring n then (
93- try (promote (m,ring n) , n) else
94- try (m , promote (n,ring m))
95- else error " expected compatible rings"
96- )
97- else (m,n))
98-
99107Matrix _ Sequence := RingElement => (m,ind) -> (
100108 if # ind === 2
101109 then promote (rawMatrixEntry(m.RawMatrix, ind#0, ind#1), ring m)
@@ -128,7 +136,7 @@ Matrix == ZZ := (m,i) -> if i === 0 then rawIsZero m.RawMatrix else m - i == 0
128136
129137Matrix + Matrix := Matrix => (
130138 (f,g) -> map (target f, source f, reduce(target f, raw f + raw g))
131- ) @@ toSameRing
139+ ) @@ tosamering
132140Matrix + RingElement :=
133141Matrix + Number := (f,r) -> if r == 0 then f else f + r* id_(target f)
134142RingElement + Matrix :=
@@ -138,7 +146,7 @@ Number + Vector := RingElement + Vector := (r,v) -> vector(r + matrix v)
138146
139147Matrix - Matrix := Matrix => (
140148 (f,g) -> map (target f, source f, reduce(target f, raw f - raw g))
141- ) @@ toSameRing
149+ ) @@ tosamering
142150Matrix - RingElement :=
143151Matrix - Number := (f,r) -> if r == 0 then f else f - r* id_(target f)
144152RingElement - Matrix :=
@@ -176,13 +184,7 @@ Matrix * Matrix := Matrix => (m,n) -> (
176184 q,
177185 Degree => degree m + if m.?RingMap then m.RingMap .cache .DegreeMap degree n else degree n))
178186 else (
179- R := ring m;
180- S := ring target n;
181- if R =!= S then ( -- use toSameRing?
182- try m = promote (m,S) else
183- try n = promote (n,R) else
184- error " maps over incompatible rings" ;
185- );
187+ (m,n) = tosamering(m,n);
186188 M = target m;
187189 P := source m;
188190 N = source n;
@@ -515,7 +517,7 @@ bothFree := (f,g) -> (
515517 or not isFreeModule source g or not isFreeModule target g then error " expected a homomorphism between free modules"
516518 else (f,g))
517519
518- diff (Matrix , Matrix ) := Matrix => ( (f,g) -> map (ring f, rawMatrixDiff(f.RawMatrix, g.RawMatrix)) ) @@ bothFree @@ toSameRing
520+ diff (Matrix , Matrix ) := Matrix => ( (f,g) -> map (ring f, rawMatrixDiff(f.RawMatrix, g.RawMatrix)) ) @@ bothFree @@ tosamering
519521diff (RingElement , RingElement ) := RingElement => (f,g) -> (diff (matrix {{f}},matrix {{g}}))_(0,0)
520522diff (Matrix , RingElement ) := (m,f) -> diff (m,matrix {{f}})
521523diff (RingElement , Matrix ) := (f,m) -> diff (matrix {{f}},m)
@@ -525,7 +527,7 @@ diff(Vector, Vector) := (v,w) -> diff(matrix{v}, transpose matrix{w})
525527diff (Matrix , Vector ) := (m,w) -> diff (m,transpose matrix {w})
526528diff (Vector , Matrix ) := (v,m) -> diff (matrix {v}, m)
527529
528- contract (Matrix , Matrix ) := Matrix => ( (f,g) -> map (ring f, rawMatrixContract(f.RawMatrix, g.RawMatrix)) ) @@ bothFree @@ toSameRing
530+ contract (Matrix , Matrix ) := Matrix => ( (f,g) -> map (ring f, rawMatrixContract(f.RawMatrix, g.RawMatrix)) ) @@ bothFree @@ tosamering
529531contract (RingElement , RingElement ) := RingElement => (f,g) -> (contract (matrix {{f}},matrix {{g}}))_(0,0)
530532contract (Matrix , RingElement ) := (m,f) -> contract (m,matrix {{f}})
531533contract (RingElement , Matrix ) := (f,m) -> contract (matrix {{f}},m)
@@ -543,8 +545,8 @@ contract(Number, Matrix) := (f,m) -> contract(matrix{{f}},m)
543545contract (Vector , Number ) := (v,f) -> (contract (matrix {v},matrix {{f}}))_0
544546contract (Number , Vector ) := (f,v) -> contract (matrix {{f}},transpose matrix {v})
545547
546- diff '(Matrix , Matrix ) := Matrix => ((m,n) -> ( flip (dual target n, target m) * diff (n,m) * flip (source m, dual source n) )) @@ bothFree @@ toSameRing
547- contract '(Matrix , Matrix ) := Matrix => ((m,n) -> ( flip (dual target n, target m) * contract (n,m) * flip (source m, dual source n) )) @@ bothFree @@ toSameRing
548+ diff '(Matrix , Matrix ) := Matrix => ((m,n) -> ( flip (dual target n, target m) * diff (n,m) * flip (source m, dual source n) )) @@ bothFree @@ tosamering
549+ contract '(Matrix , Matrix ) := Matrix => ((m,n) -> ( flip (dual target n, target m) * contract (n,m) * flip (source m, dual source n) )) @@ bothFree @@ tosamering
548550
549551jacobian = method ()
550552jacobian Matrix := Matrix => (m) -> diff (transpose vars ring m, m)
0 commit comments