7 #include "cl_rational.h"
13 #include "cl_integer.h"
16 const cl_RA operator+ (const cl_RA& r, const cl_RA& s)
18 // Methode (vgl. [Buchberger, Collins, Loos: Computer Algebra, S.200-201])
19 // r,s beide Integers -> klar.
20 // r=a/b, s=c -> Ergebnis (a+b*c)/b
21 // (mit b>1 und ggT(a+b*c,b) = ggT(a,b) = 1)
22 // Bei c=0 direkt r als Ergebnis.
23 // r=a, s=c/d -> Ergebnis (a*d+c)/d
24 // (mit d>1 und ggT(a*d+c,d) = ggT(c,d) = 1)
25 // Bei a=0 direkt s als Ergebnis.
29 // Ergebnis (a*d+b*c)/(b*d),
30 // (mit b*d>1 wegen b>1, d>1, und
31 // ggT(a*d+b*c,b*d) = 1
32 // wegen ggT(a*d+b*c,b) = ggT(a*d,b) = 1 (wegen ggT(a,b)=1 und ggT(d,b)=1)
33 // und ggT(a*d+b*c,d) = ggT(b*c,d) = 1 (wegen ggT(b,d)=1 und ggT(c,d)=1)
35 // Sonst b' := b/g, d' := d/g. e := a*d'+b'*c, f:= b'*d = b*d'.
36 // Es ist g = ggT(g*b',g*d') = g*ggT(b',d'), also ggT(b',d')=1.
37 // Es ist r+s = (a*d+b*c)/(b*d) = (nach Kürzen mit g) e/f.
39 // ggT(a,b') teilt ggT(a,b)=1, also ggT(a,b')=1. Mit ggT(d',b')=1 folgt
40 // 1 = ggT(a*d',b') = ggT(a*d'+b'*c,b') = ggT(e,b').
41 // ggT(c,d') teilt ggT(c,d)=1, also ggT(c,d')=1. Mit ggT(b',d')=1 folgt
42 // 1 = ggT(b'*c,d') = ggT(a*d'+b'*c,d') = ggT(e,d').
43 // Daher ist ggT(e,f) = ggT(e,b'*d'*g) = ggT(e,g).
44 // Errechne daher h=ggT(e,g).
45 // Bei h=1 ist e/f das Ergebnis (mit f>1, da d>1, und ggT(e,f)=1),
46 // sonst ist (e/h)/(f/h) das Ergebnis.
50 if (eq(s,0)) { return r; } // s=0 -> r als Ergebnis
57 var const cl_I& a = numerator(r);
58 var const cl_I& b = denominator(r);
59 var const cl_I& c = s;
61 return I_I_to_RT(a+b*c,b);
69 if (eq(r,0)) { return s; } // r=0 -> s als Ergebnis
70 var const cl_I& a = r;
71 var const cl_I& c = numerator(s);
72 var const cl_I& d = denominator(s);
74 return I_I_to_RT(a*d+c,d);
78 var const cl_I& a = numerator(r);
79 var const cl_I& b = denominator(r);
80 var const cl_I& c = numerator(s);
81 var const cl_I& d = denominator(s);
82 var cl_I g = gcd(b,d); // g = ggT(b,d) >0 bilden
84 // g=1 -> Ergebnis (a*d+b*c)/(b*d)
85 return I_I_to_RT(a*d+b*c,b*d);
87 var cl_I bp = exquopos(b,g); // b' := b/g (b,g>0)
88 var cl_I dp = exquopos(d,g); // d' := d/g (d,g>0)
89 var cl_I e = a*dp+bp*c; // e := a*d'+b'*c
90 var cl_I f = bp*d; // f := b'*d
91 var cl_I h = gcd(e,g); // h := ggT(e,g)
94 return I_I_to_RT(e,f);
96 return I_I_to_RA(exquo(e,h),exquopos(f,h)); // (e/h)/(f/h) als Ergebnis