Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define gcd & lcm of single element #3595

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions M2/Macaulay2/m2/factor.m2
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ gcd(RingElement,RingElement) := RingElement => (r,s) -> (
if s%a != 0 then error "can't find gcd in this ring";
s // a))
else notImplemented()))
gcd RingElement := identity

gcdCoefficients(RingElement,RingElement) := (f,g) -> ( -- ??
R := ring f;
Expand All @@ -52,6 +53,7 @@ lcm(RingElement,RingElement) := (f,g) -> (
d := gcd(f, g);
if d == 0 then d
else f * (g // d))
lcm RingElement := identity

-----------------------------------------------------------------------------

Expand Down
2 changes: 2 additions & 0 deletions M2/Macaulay2/m2/integers.m2
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ gcd(QQ,ZZ) := QQ => (y,x) -> gcd(x * denominator y, numerator y) / denominator y
gcd(QQ,QQ) := QQ => (x,y) -> (
d := denominator x * (denominator y // gcd(denominator x, denominator y));
gcd(numerator (x * d), numerator (y * d)) / d)
gcd ZZ := gcd QQ := identity

abs = method()
abs ZZ := abs RR := abs RRi := abs CC := abs QQ := abs0
Expand All @@ -68,6 +69,7 @@ lcm(QQ,QQ) := (f,g) -> (
d := gcd(f, g);
if d == 0 then 0_QQ
else abs f * (abs g / d))
lcm ZZ := lcm QQ := identity
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Number, perhaps?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that. I stuck with ZZ and QQ since those are the ones we have binary methods for. It seems like it might be a bit strange if, say, gcd(3.0) worked but gcd(3.0, 6.0) didn't.


odd = x -> 1 === x%2
even = x -> 0 === x%2
Expand Down
9 changes: 8 additions & 1 deletion M2/Macaulay2/packages/Macaulay2Doc/operators.m2
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ document {
(gcd, QQ, ZZ),
(gcd, ZZ, ZZ),
(gcd,RingElement,ZZ),
(gcd,ZZ,RingElement)},
(gcd,ZZ,RingElement),
(gcd, ZZ),
(gcd, QQ),
(gcd, RingElement)
},
Headline => "greatest common divisor",
Usage => "gcd(x,y,...)",
Inputs => { "x" => ZZ, ", or ", ofClass QQ, ", or ",ofClass RingElement },
Expand All @@ -175,6 +179,9 @@ doc ///
(lcm, ZZ, ZZ)
(lcm,RingElement,ZZ)
(lcm,ZZ,RingElement)
(lcm, ZZ)
(lcm, QQ)
(lcm, RingElement)
Headline
least common multiple
Usage
Expand Down
4 changes: 4 additions & 0 deletions M2/Macaulay2/tests/normal/gcd.m2
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ assert( gcd(2*3*5,2*3*7,2*5*7,3*5*7) == 1 )
assert( gcd(1000:2) == 2 )
assert( gcd splice(1000:2,3) == 1 )
assert( gcd {} == 0 )
assert( gcd 2 == 2 )
assert( lcm(2,3,5,7) == 210 )
assert( lcm(1000:2) == 2 )
assert( lcm(0, 0) == 0 )
assert( lcm(0/1, 0/1) == 0 )
assert( lcm {} == 1 )
assert( lcm 2 == 2 )

R = ZZ/32003[x,y]
f = (x+y)^3*(x-y^2)
g = (x+y)^2*(x^3-x*y+y^3)^4
assert ( gcd(f,g) == (x+y)^2 )
assert ( lcm(0_R, 0_R) == 0 )
assert ( gcd f == f )
assert ( lcm f == f )

GF 729[x, y, z]
assert( gcd((x^5+y^3+a+1)*(y-1),(x^5+y^3+a+1)*(z+1)) == x^5+y^3+a+1 )
Expand Down
Loading