Skip to content

Commit 4e93c86

Browse files
committed
remove try in promote part
1 parent 6b639b8 commit 4e93c86

File tree

3 files changed

+28
-37
lines changed

3 files changed

+28
-37
lines changed

M2/Macaulay2/m2/classes.m2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ ancestors = T -> unique join({T}, while (T = parent T) =!= Thing list T, {Thing
99
ancestors' = T -> unique join({T}, while (T = class T) =!= Type list T, {Type})
1010

1111
-- TODO: make this TT toString X later?
12-
synonym = X -> if X.?synonym then X.synonym else "object of class " | toString X
12+
synonym = X -> X.synonym ?? "object of class " | toString X
1313
-- TODO: find a more permanent solution
1414
plurals = new MutableHashTable from {
1515
"body" => "bodies",
@@ -22,7 +22,7 @@ pluralize = s -> demark_" " append(
2222
drop(ws := separate_" " s, -1),
2323
if plurals#?(last ws)
2424
then plurals#(last ws) else last ws | "s")
25-
pluralsynonym = T -> if T.?synonym then pluralize T.synonym else "objects of class " | toString T
25+
pluralsynonym = T -> pluralize T.synonym ?? "objects of class " | toString T
2626

2727
Time.synonym = "timing result"
2828
Boolean.synonym = "Boolean value"

M2/Macaulay2/m2/matrix.m2

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@
33

44
needs "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+
622
oops := 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
7995
Matrix * InfiniteNumber := (m,r) -> r*m
8096
Number * Matrix :=
8197
RingElement * 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)))
84100
Matrix * Number :=
85101
Matrix * 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)))
88104
Matrix / Number :=
89105
Matrix / 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-
99107
Matrix _ 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

129137
Matrix + Matrix := Matrix => (
130138
(f,g) -> map(target f, source f, reduce(target f, raw f + raw g))
131-
) @@ toSameRing
139+
) @@ tosamering
132140
Matrix + RingElement :=
133141
Matrix + Number := (f,r) -> if r == 0 then f else f + r*id_(target f)
134142
RingElement + Matrix :=
@@ -138,7 +146,7 @@ Number + Vector := RingElement + Vector := (r,v) -> vector(r + matrix v)
138146

139147
Matrix - Matrix := Matrix => (
140148
(f,g) -> map(target f, source f, reduce(target f, raw f - raw g))
141-
) @@ toSameRing
149+
) @@ tosamering
142150
Matrix - RingElement :=
143151
Matrix - Number := (f,r) -> if r == 0 then f else f - r*id_(target f)
144152
RingElement - 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
519521
diff(RingElement, RingElement) := RingElement => (f,g) -> (diff(matrix{{f}},matrix{{g}}))_(0,0)
520522
diff(Matrix, RingElement) := (m,f) -> diff(m,matrix{{f}})
521523
diff(RingElement, Matrix) := (f,m) -> diff(matrix{{f}},m)
@@ -525,7 +527,7 @@ diff(Vector, Vector) := (v,w) -> diff(matrix{v}, transpose matrix{w})
525527
diff(Matrix, Vector) := (m,w) -> diff(m,transpose matrix {w})
526528
diff(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
529531
contract(RingElement, RingElement) := RingElement => (f,g) -> (contract(matrix{{f}},matrix{{g}}))_(0,0)
530532
contract(Matrix, RingElement) := (m,f) -> contract(m,matrix{{f}})
531533
contract(RingElement, Matrix) := (f,m) -> contract(matrix{{f}},m)
@@ -543,8 +545,8 @@ contract(Number, Matrix) := (f,m) -> contract(matrix{{f}},m)
543545
contract(Vector, Number) := (v,f) -> (contract(matrix{v},matrix{{f}}))_0
544546
contract(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

549551
jacobian = method()
550552
jacobian Matrix := Matrix => (m) -> diff(transpose vars ring m, m)

M2/Macaulay2/m2/matrix1.m2

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,6 @@ needs "quotient.m2"
66

77
-----------------------------------------------------------------------------
88

9-
notsamering := (X,Y) -> (
10-
if X === Y then error("expected ",pluralsynonym X, " for the same ring")
11-
else error("expected ",X.synonym," and ",Y.synonym," for the same ring"))
12-
nottosamering := (X,Y) -> (
13-
if X === Y then error("expected ",pluralsynonym X, " for compatible rings")
14-
else error("expected ",X.synonym," and ",Y.synonym," for compatible rings"))
15-
samering = (M,N) -> if ring M === ring N then (M,N) else notsamering(class M,class N)
16-
tosamering := (M,N) -> if ring M === ring N then (M,N) else (
17-
z := try 0_(ring M) + 0_(ring N) else nottosamering(class M,class N);
18-
(promote(M,ring z),promote(N,ring z)))
19-
209
module Ring := Module => (cacheValue symbol module)(R -> R^1)
2110

2211
matrix(RingFamily,List) := Matrix => opts -> (R,m) -> matrix(default R, m, opts)
@@ -382,7 +371,7 @@ tensor(Matrix, Matrix) := Matrix => {} >> opts -> ((f, g) -> (
382371
map(target f ** target g,
383372
source f ** source g,
384373
map(R, f.RawMatrix ** g.RawMatrix),
385-
Degree => degree f + degree g))) @@ toSameRing
374+
Degree => degree f + degree g))) @@ tosamering
386375

387376
Matrix ** Module := Matrix => (f, M) -> tensor(f, id_M)
388377
Module ** Matrix := Matrix => (M, f) -> tensor(id_M, f)

0 commit comments

Comments
 (0)