]> www.ginac.de Git - cln.git/blob - src/complex/misc/cl_C_expt_I.cc
* All Files have been modified for inclusion of namespace cln;
[cln.git] / src / complex / misc / cl_C_expt_I.cc
1 // expt().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cln/complex.h"
8
9
10 // Implementation.
11
12 #include "cl_C.h"
13 #include "cln/real.h"
14 #include "cl_I.h"
15
16 namespace cln {
17
18 // Methode:
19 // Für y>0:
20 //   a:=x, b:=y.
21 //   Solange b gerade, setze a:=a*a, b:=b/2. [a^b bleibt invariant, = x^y.]
22 //   c:=a.
23 //   Solange b:=floor(b/2) >0 ist,
24 //     setze a:=a*a, und falls b ungerade, setze c:=a*c.
25 //   Ergebnis c.
26 // Für y=0: Ergebnis 1.
27 // Für y<0: (/ (expt x (- y))).
28
29 // Assume y>0.
30 inline const cl_N expt_pos (const cl_N& x, const cl_I& y)
31 {
32         var cl_N a = x;
33         var cl_I b = y;
34         while (!oddp(b)) { a = square(a); b = b >> 1; }
35         var cl_N c = a;
36         until (eq(b,1))
37           { b = b >> 1;
38             a = square(a);
39             if (oddp(b)) { c = a * c; }
40           }
41         return c;
42 }
43
44 const cl_N expt (const cl_N& x, const cl_I& y)
45 {
46         if (realp(x)) {
47                 DeclareType(cl_R,x);
48                 // x reell -> schnellere Routine
49                 return expt(x,y);
50         }
51         if (eq(y,0)) { return 1; } // y=0 -> Ergebnis 1
52         var cl_boolean y_negative = minusp(y);
53         var cl_I abs_y = (y_negative ? -y : y); // Betrag von y nehmen
54         var cl_N z = expt_pos(x,abs_y); // (expt x (abs y))
55         return (y_negative ? recip(z) : z); // evtl. noch Kehrwert nehmen
56 }
57
58 }  // namespace cln