这是Maple
中扩展欧几里得算法的代码,它应返回l
的整数pi,ri,si,ti
,多项式0<=i<=l+1
。 qi
的多项式为1<=i<=l
,si(f)+ti(g) = ri
和sl(f)+tl(g)=rl=GCD(f,g)
。
问题是,我一直被零除。此外,它每次都会将pi = lcoeff(ri-1 - qiri)
评估为零。即使我删除了它,它仍然表示存在零除法,它必须来自qi:=quo(ri-1,ri, x);
但是我不知道为什么考虑循环的要求是r[i]
不等于零。我真的可以用一双新眼睛来看看我做错了什么。
ExtEuclidean := proc (f, g) local p, r, s, t, i, q, l;
p[0] := lcoeff(f);
p[1] := lcoeff(g);
r[0] := f/p[0];
r[1] := g/p[1];
s[0] := 1;
s[1] := 0;
t[0] := 0;
t[1] := 1;
i := 1;
while r[i] <> 0 do
l := i;
q[i] := quo(r[i-1], r[i], x, 'remainder');
simplify(q[i]);
p[i+1] := lcoeff(r[i-1]-q[i]*r[i]);
r[i+1] := (r[i-1]-q[i]*r[i])/p[i+1];
s[i+1] := (s[i-1]-q[i]*s[i])/p[i+1];
t[i+1] := (t[i-1]-q[i]*t[i])/p[i+1];
i := i+1;
simplify(r[i])
end do;
return l, p[i], r[i], s[i], t[i], q[i]
end proc;
&#13;
答案 0 :(得分:1)
您的索引看起来有问题。我没想太多,但这似乎适用于基本的例子。我在循环中将p [i + 1]更改为p [i]并更改了输出
ExtEuclidean := proc (f, g) local p, r, s, t, i, q, l;
p[0] := lcoeff(f);
p[1] := lcoeff(g);
r[0] := f/p[0];
r[1] := g/p[1];
s[0] := 1;
s[1] := 0;
t[0] := 0;
t[1] := 1;
i := 1;
while r[i] <> 0 do
l := i;
q[i] := quo(r[i-1], r[i], x, 'remainder');
simplify(q[i]);
p[i+1] := lcoeff(r[i-1]-q[i]*r[i]);
r[i+1] := (r[i-1]-q[i]*r[i])/p[i];
s[i+1] := (s[i-1]-q[i]*s[i])/p[i];
t[i+1] := (t[i-1]-q[i]*t[i])/p[i];
i := i+1;
simplify(r[i])
end do;
return l, p[i-1], r[i-1], s[i-1], t[i-1], q[i-1]
end proc;