]> www.ginac.de Git - cln.git/blob - src/float/transcendental/cl_LF_ratseries_pab.cc
Update to recently found large Mersenne prime.
[cln.git] / src / float / transcendental / cl_LF_ratseries_pab.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_pab_series_aux (uintC N1, uintC N2,
26                                  const cl_pab_series& args,
27                                  cl_I* P, cl_I* B, cl_I* T)
28 {
29         switch (N2 - N1) {
30         case 0:
31                 throw runtime_exception(); break;
32         case 1:
33                 if (P) { *P = args.pv[N1]; }
34                 *B = args.bv[N1];
35                 *T = args.av[N1] * args.pv[N1];
36                 break;
37         case 2: {
38                 var cl_I p01 = args.pv[N1] * args.pv[N1+1];
39                 if (P) { *P = p01; }
40                 *B = args.bv[N1] * args.bv[N1+1];
41                 *T = args.bv[N1+1] * args.av[N1] * args.pv[N1]
42                    + args.bv[N1] * args.av[N1+1] * p01;
43                 break;
44                 }
45         case 3: {
46                 var cl_I p01 = args.pv[N1] * args.pv[N1+1];
47                 var cl_I p012 = p01 * args.pv[N1+2];
48                 if (P) { *P = p012; }
49                 var cl_I b12 = args.bv[N1+1] * args.bv[N1+2];
50                 *B = args.bv[N1] * b12;
51                 *T = b12 * args.av[N1] * args.pv[N1]
52                    + args.bv[N1] * (args.bv[N1+2] * args.av[N1+1] * p01
53                                     + args.bv[N1+1] * args.av[N1+2] * p012);
54                 break;
55                 }
56         case 4: {
57                 var cl_I p01 = args.pv[N1] * args.pv[N1+1];
58                 var cl_I p012 = p01 * args.pv[N1+2];
59                 var cl_I p0123 = p012 * args.pv[N1+3];
60                 if (P) { *P = p0123; }
61                 var cl_I b01 = args.bv[N1] * args.bv[N1+1];
62                 var cl_I b23 = args.bv[N1+2] * args.bv[N1+3];
63                 *B = b01 * b23;
64                 *T = b23 * (args.bv[N1+1] * args.av[N1] * args.pv[N1]
65                             + args.bv[N1] * args.av[N1+1] * p01)
66                    + b01 * (args.bv[N1+3] * args.av[N1+2] * p012
67                             + args.bv[N1+2] * args.av[N1+3] * p0123);
68                 break;
69                 }
70         default: {
71                 var uintC Nm = (N1+N2)/2; // midpoint
72                 // Compute left part.
73                 var cl_I LP, LB, LT;
74                 eval_pab_series_aux(N1,Nm,args,&LP,&LB,&LT);
75                 // Compute right part.
76                 var cl_I RP, RB, RT;
77                 eval_pab_series_aux(Nm,N2,args,(P?&RP:(cl_I*)0),&RB,&RT);
78                 // Put together partial results.
79                 if (P) { *P = LP*RP; }
80                 *B = LB*RB;
81                 // S = LS + LP * RS, so T = RB*LT + LB*LP*RT.
82                 *T = RB*LT + LB*LP*RT;
83                 break;
84                 }
85         }
86 }
87
88 const cl_LF eval_rational_series (uintC N, const cl_pab_series& args, uintC len)
89 {
90         if (N==0)
91                 return cl_I_to_LF(0,len);
92         var cl_I B, T;
93         eval_pab_series_aux(0,N,args,NULL,&B,&T);
94         return cl_I_to_LF(T,len) / cl_I_to_LF(B,len);
95 }
96 // Bit complexity (if p(n), q(n), a(n), b(n) have length O(log(n))):
97 // O(log(N)^2*M(N)).
98
99 }  // namespace cln