1 /* Implementation of the Maxima user functions gcdex and igcdex
3 gcdex(f,g) yields a list [a,b,u]
4 where u is the GCD of f and g, and u = f*a+g*b.
6 F and G should be univariate polynomials, or a main variable
7 should be supplied so that we are working in the polynomial ring over
8 the rational functions in the other variables, and thus in a
9 principal ideal domain, and the ideal generated by (f,g) is indeed
10 generated by the gcd of f and g.
12 igcdex(n,k) yields a list [a,b,u]
13 where u is the GCD of the integers n and k, and u = n*a+k*b.
15 igcdex is called from the function gcdex for integer arguments.
17 This file is part of Maxima -- GPL CAS based on DOE-MACSYMA
21 block([q0:[], q1:[], ok:true, lis1:[], lis2:[], lis3:[], q:[]],
22 if integerp(f) and integerp(g) then return(igcdex(f, g)),
23 if var = [] then var:listofvars([f,g]),
24 if not (length(var) = 1) then
26 if (f=0 and g=0) then return([0,0,0]),
27 error("Specify one main variable or give univariate polynomials")
32 /* Do not divide by zero, but return the result */
33 if g = 0 then return([1, 0, f]),
34 /* divide(f,g) => [q:quotient(f,g), remainder:f-g*q] */
36 /* if f/g is 0 then we reverse them */
38 ( /* the deg f < deg g */
39 lis2:gcdex(g, f, var),
40 return([second(lis2), first(lis2), third(lis2)])
42 if second(q0) = 0 then
46 q1:divide(g, second(q0), var),
47 lis1:[1, -first(q0), second(q0)],
48 if second(q1) = 0 then
51 ( /* lisi are always perpendicular to [f,g,-1] */
52 lis2:[-first(q1), 1+first(q0)*first(q1), second(q1)],
55 q:divide(third(lis1), third(lis2), var),
56 lis3:lis1-lis2*first(q),
57 if third(lis3) = 0 then
62 lis2:lis3/first(content(third(lis3), var))
67 if freeof(var, third(lis2)) then
74 block([x:0, lx:1, y:1, ly:0, q, r],
75 if not (integerp(a) and integerp(b)) then
76 error("igcdex: The arguments must be integers.")