]> www.ginac.de Git - cln.git/blob - src/float/transcendental/cl_LF_ratseries_b.cc
Update to recently found large Mersenne prime.
[cln.git] / src / float / transcendental / cl_LF_ratseries_b.cc
1 // eval_rational_series().
2
3 // General includes.
4 #include "base/cl_sysdep.h"
5
6 // Specification.
7 #include "float/transcendental/cl_LF_tran.h"
8
9
10 // Implementation.
11
12 #include "cln/lfloat.h"
13 #include "cln/integer.h"
14 #include "cln/exception.h"
15 #include "float/lfloat/cl_LF.h"
16
17 namespace cln {
18
19 // Subroutine.
20 // Evaluates S = sum(N1 <= n < N2, a(n)/b(n) * (p(N1)...p(n))/(q(N1)...q(n)))
21 // and returns P = p(N1)...p(N2-1), Q = q(N1)...q(N2-1), B = B(N1)...B(N2-1)
22 // and T = B*Q*S (all integers). On entry N1 < N2.
23 // P will not be computed if a NULL pointer is passed.
24
25 static void eval_b_series_aux (uintC N1, uintC N2,
26                                const cl_b_series& args,
27                                cl_I* B, cl_I* T)
28 {
29         switch (N2 - N1) {
30         case 0:
31                 throw runtime_exception(); break;
32         case 1:
33                 *B = args.bv[N1];
34                 *T = 1;
35                 break;
36         case 2: {
37                 *B = args.bv[N1] * args.bv[N1+1];
38                 *T = args.bv[N1+1]
39                    + args.bv[N1];
40                 break;
41                 }
42         case 3: {
43                 var cl_I b12 = args.bv[N1+1] * args.bv[N1+2];
44                 *B = args.bv[N1] * b12;
45                 *T = b12
46                    + args.bv[N1] * (args.bv[N1+2]
47                                     + args.bv[N1+1]);
48                 break;
49                 }
50         case 4: {
51                 var cl_I b01 = args.bv[N1] * args.bv[N1+1];
52                 var cl_I b23 = args.bv[N1+2] * args.bv[N1+3];
53                 *B = b01 * b23;
54                 *T = b23 * (args.bv[N1+1]
55                             + args.bv[N1])
56                    + b01 * (args.bv[N1+3]
57                             + args.bv[N1+2]);
58                 break;
59                 }
60         default: {
61                 var uintC Nm = (N1+N2)/2; // midpoint
62                 // Compute left part.
63                 var cl_I LB, LT;
64                 eval_b_series_aux(N1,Nm,args,&LB,&LT);
65                 // Compute right part.
66                 var cl_I RB, RT;
67                 eval_b_series_aux(Nm,N2,args,&RB,&RT);
68                 // Put together partial results.
69                 *B = LB*RB;
70                 // S = LS + RS, so T = RB*LT + LB*RT.
71                 *T = RB*LT + LB*RT;
72                 break;
73                 }
74         }
75 }
76
77 const cl_LF eval_rational_series (uintC N, const cl_b_series& args, uintC len)
78 {
79         if (N==0)
80                 return cl_I_to_LF(0,len);
81         var cl_I B, T;
82         eval_b_series_aux(0,N,args,&B,&T);
83         return cl_I_to_LF(T,len) / cl_I_to_LF(B,len);
84 }
85 // Bit complexity (if p(n), q(n), a(n), b(n) have length O(log(n))):
86 // O(log(N)^2*M(N)).
87
88 }  // namespace cln