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

gcdCoefficents improvements #3597

Open
wants to merge 4 commits 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
23 changes: 23 additions & 0 deletions M2/Macaulay2/d/actors3.d
Original file line number Diff line number Diff line change
Expand Up @@ -2362,6 +2362,29 @@ gcd(x:Expr,y:Expr):Expr := (
gcdfun(e:Expr):Expr := accumulate(plus0,plus1,gcd,e);
setupfun("gcd0",gcdfun);

gcdCoefficients(e:Expr):Expr := (
when e
is a:Sequence do (
if length(a) == 2 then (
when a.0
is x:ZZcell do (
when a.1
is y:ZZcell do (
g := newZZmutable();
s := newZZmutable();
t := newZZmutable();
Ccode(void, "mpz_gcdext(", g, ", ", s, ", ", t, ", ", x.v,
", ", y.v, ")");
list(
Expr(ZZcell(moveToZZandclear(g))),
Expr(ZZcell(moveToZZandclear(s))),
Expr(ZZcell(moveToZZandclear(t)))))
else WrongArgZZ(2))
else WrongArgZZ(1))
else WrongNumArgs(2))
else WrongNumArgs(2));
setupfun("gcdCoefficients0", gcdCoefficients);

binomial(e:Expr):Expr := (
when e
is a:Sequence do (
Expand Down
2 changes: 2 additions & 0 deletions M2/Macaulay2/m2/factor.m2
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ gcd(RingElement,RingElement) := RingElement => (r,s) -> (
s // a))
else notImplemented()))

gcdCoefficients(ZZ, RingElement) := (r, s) -> gcdCoefficients(r_(ring s), s)
gcdCoefficients(RingElement, ZZ) := (r, s) -> gcdCoefficients(r, s_(ring r))
gcdCoefficients(RingElement,RingElement) := (f,g) -> ( -- ??
R := ring f;
if R =!= ring g then error "expected elements of the same ring";
Expand Down
13 changes: 1 addition & 12 deletions M2/Macaulay2/m2/quotient.m2
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,7 @@ saturate = method(Options => options quotient)
-- moved to packages/Saturation.m2 in October 2020
annihilator = method(Options => {Strategy => null}) -- Intersection or Quotient

gcdCoefficients(ZZ,ZZ) := (a,b) -> (
m := {a,1,0};
n := {b,0,1};
if a<0 then m=-m;
if b<0 then n=-n;
if a>b then (k :=m;m=n;n=k);
while m#0 > 0 do (
t := n#0 // m#0;
n = n - apply(m,y -> t * y);
(k=m;m=n;n=k);
);
n);
gcdCoefficients(ZZ,ZZ) := gcdCoefficients0

mod = (i,n) -> i * 1_(ZZ/n)

Expand Down
44 changes: 35 additions & 9 deletions M2/Macaulay2/packages/Macaulay2Doc/doc_arithmetic.m2
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,41 @@ document {
"conjugate 3"
}
}
document {
Key => {gcdCoefficients,(gcdCoefficients, RingElement, RingElement),(gcdCoefficients, ZZ, ZZ)},
Headline => "gcd with coefficients",
TT "gcdCoefficients(a,b)", " -- returns ", TT "{d,r,s}", " so that
", TT"a*r + b*s", " is the greatest common divisor ", TT "d", " of ", TT "a", "
and ", TT "b", ".",
PARA{},
"Works for integers or elements of polynomial rings in onve variable.",
SeeAlso => "gcd"}

doc ///
Key
gcdCoefficients
(gcdCoefficients, RingElement, RingElement)
(gcdCoefficients, RingElement, ZZ)
(gcdCoefficients, ZZ, RingElement)
(gcdCoefficients, ZZ, ZZ)
Headline
greatest common divisor with coefficients
Usage
gcdCoefficients(a, b)
Inputs
a:{RingElement, ZZ}
b:{RingElement, ZZ}
Description
Text
This returns a list of the form @CODE "{d, r, s}"@ so that $d = ar + bs$
and $d=\gcd(a, b)$.

It works for integers or elements of polynomial rings in one variable.
Example
gcdCoefficients(46, 240)
gcd(46, 240)
46 * 47 + 240 * (-9)
R = ZZ/2[x]
f = x^8 + x^4 + x^3 + x + 1
g = x^6 + x^4 + x + 1
gcdCoefficients(f, g)
gcd(f, g)
f * (x^5 + x^4 + x^3 + x^2 + 1) + g * (x^7 + x^6 + x^3 + x)
SeeAlso
gcd
///

document {
Key => mod,
Headline => "reduce modulo an integer",
Expand Down
Loading