1 // eval_rational_series().
7 #include "cl_LF_tran.h"
12 #include "cln/lfloat.h"
13 #include "cln/integer.h"
14 #include "cln/abort.h"
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.
25 static void eval_pqa_series_aux (uintL N1, uintL N2,
26 const cl_pqa_series& args,
27 cl_I* P, cl_I* Q, cl_I* T)
33 if (P) { *P = args.pv[N1]; }
35 *T = args.av[N1] * args.pv[N1];
38 var cl_I p01 = args.pv[N1] * args.pv[N1+1];
40 *Q = args.qv[N1] * args.qv[N1+1];
41 *T = args.qv[N1+1] * args.av[N1] * args.pv[N1]
42 + args.av[N1+1] * p01;
46 var cl_I p01 = args.pv[N1] * args.pv[N1+1];
47 var cl_I p012 = p01 * args.pv[N1+2];
49 var cl_I q12 = args.qv[N1+1] * args.qv[N1+2];
50 *Q = args.qv[N1] * q12;
51 *T = q12 * args.av[N1] * args.pv[N1]
52 + args.qv[N1+2] * args.av[N1+1] * p01
53 + args.av[N1+2] * p012;
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 q23 = args.qv[N1+2] * args.qv[N1+3];
62 var cl_I q123 = args.qv[N1+1] * q23;
63 *Q = args.qv[N1] * q123;
64 *T = q123 * args.av[N1] * args.pv[N1]
65 + q23 * args.av[N1+1] * p01
66 + args.qv[N1+3] * args.av[N1+2] * p012
67 + args.av[N1+3] * p0123;
71 var uintL Nm = (N1+N2)/2; // midpoint
74 eval_pqa_series_aux(N1,Nm,args,&LP,&LQ,<);
75 // Compute right part.
77 eval_pqa_series_aux(Nm,N2,args,(P?&RP:(cl_I*)0),&RQ,&RT);
78 // Put together partial results.
79 if (P) { *P = LP*RP; }
81 // S = LS + LP/LQ * RS, so T = RQ*LT + LP*RT.
88 static void eval_pqsa_series_aux (uintL N1, uintL N2,
89 const cl_pqa_series& args,
90 cl_I* P, cl_I* Q, uintL* QS, cl_I* T)
96 if (P) { *P = args.pv[N1]; }
99 *T = args.av[N1] * args.pv[N1];
102 var cl_I p01 = args.pv[N1] * args.pv[N1+1];
104 *Q = args.qv[N1] * args.qv[N1+1];
105 *QS = args.qsv[N1] + args.qsv[N1+1];
106 *T = ((args.qv[N1+1] * args.av[N1] * args.pv[N1]) << args.qsv[N1+1])
107 + args.av[N1+1] * p01;
111 var cl_I p01 = args.pv[N1] * args.pv[N1+1];
112 var cl_I p012 = p01 * args.pv[N1+2];
113 if (P) { *P = p012; }
114 var cl_I q12 = args.qv[N1+1] * args.qv[N1+2];
115 *Q = args.qv[N1] * q12;
116 *QS = args.qsv[N1] + args.qsv[N1+1] + args.qsv[N1+2];
117 *T = ((q12 * args.av[N1] * args.pv[N1]) << (args.qsv[N1+1] + args.qsv[N1+2]))
118 + ((args.qv[N1+2] * args.av[N1+1] * p01) << args.qsv[N1+2])
119 + args.av[N1+2] * p012;
123 var cl_I p01 = args.pv[N1] * args.pv[N1+1];
124 var cl_I p012 = p01 * args.pv[N1+2];
125 var cl_I p0123 = p012 * args.pv[N1+3];
126 if (P) { *P = p0123; }
127 var cl_I q23 = args.qv[N1+2] * args.qv[N1+3];
128 var cl_I q123 = args.qv[N1+1] * q23;
129 *Q = args.qv[N1] * q123;
130 *QS = args.qsv[N1] + args.qsv[N1+1] + args.qsv[N1+2] + args.qsv[N1+3];
131 *T = ((((((q123 * args.av[N1] * args.pv[N1]) << args.qsv[N1+1])
132 + q23 * args.av[N1+1] * p01) << args.qsv[N1+2])
133 + args.qv[N1+3] * args.av[N1+2] * p012) << args.qsv[N1+3])
134 + args.av[N1+3] * p0123;
138 var uintL Nm = (N1+N2)/2; // midpoint
139 // Compute left part.
142 eval_pqsa_series_aux(N1,Nm,args,&LP,&LQ,&LQS,<);
143 // Compute right part.
146 eval_pqsa_series_aux(Nm,N2,args,(P?&RP:(cl_I*)0),&RQ,&RQS,&RT);
147 // Put together partial results.
148 if (P) { *P = LP*RP; }
151 // S = LS + LP/LQ * RS, so T = RQ*LT + LP*RT.
152 *T = ((RQ*LT) << RQS) + LP*RT;
158 const cl_LF eval_rational_series (uintL N, const cl_pqa_series& args, uintC len)
161 return cl_I_to_LF(0,len);
164 eval_pqa_series_aux(0,N,args,NULL,&Q,&T);
165 return cl_I_to_LF(T,len) / cl_I_to_LF(Q,len);
167 // Precomputation of the shift counts:
168 // Split qv[n] into qv[n]*2^qsv[n].
170 var cl_I* qp = args.qv;
171 var uintL* qsp = args.qsv;
172 for (var uintL n = 0; n < N; n++, qp++, qsp++) {
173 // Pull out maximal power of 2 out of *qp = args.qv[n].
185 eval_pqsa_series_aux(0,N,args,NULL,&Q,&QS,&T);
186 return cl_I_to_LF(T,len) / scale_float(cl_I_to_LF(Q,len),QS);
189 // Bit complexity (if p(n), q(n), a(n), b(n) have length O(log(n))):