PageRenderTime 44ms CodeModel.GetById 26ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/cln-1.3.2/src/float/transcendental/cl_LF_ratseries_p.cc

#
C++ | 91 lines | 66 code | 10 blank | 15 comment | 7 complexity | 34d1e48c8dfc0898cd88edc3f5a1d3e7 MD5 | raw file
Possible License(s): GPL-2.0
 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
17namespace 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
25static void eval_p_series_aux (uintC N1, uintC N2,
26                               const cl_p_series& args,
27                               cl_I* P, 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		*T = args.pv[N1];
35		break;
36	case 2: {
37		var cl_I p01 = args.pv[N1] * args.pv[N1+1];
38		if (P) { *P = p01; }
39		*T = args.pv[N1]
40		   + p01;
41		break;
42		}
43	case 3: {
44		var cl_I p01 = args.pv[N1] * args.pv[N1+1];
45		var cl_I p012 = p01 * args.pv[N1+2];
46		if (P) { *P = p012; }
47		*T = args.pv[N1]
48		   + p01
49		   + p012;
50		break;
51		}
52	case 4: {
53		var cl_I p01 = args.pv[N1] * args.pv[N1+1];
54		var cl_I p012 = p01 * args.pv[N1+2];
55		var cl_I p0123 = p012 * args.pv[N1+3];
56		if (P) { *P = p0123; }
57		*T = args.pv[N1]
58		   + p01
59		   + p012
60		   + p0123;
61		break;
62		}
63	default: {
64		var uintC Nm = (N1+N2)/2; // midpoint
65		// Compute left part.
66		var cl_I LP, LT;
67		eval_p_series_aux(N1,Nm,args,&LP,&LT);
68		// Compute right part.
69		var cl_I RP, RT;
70		eval_p_series_aux(Nm,N2,args,(P?&RP:(cl_I*)0),&RT);
71		// Put together partial results.
72		if (P) { *P = LP*RP; }
73		// S = LS + LP * RS, so T = LT + LP*RT.
74		*T = LT + LP*RT;
75		break;
76		}
77	}
78}
79
80const cl_LF eval_rational_series (uintC N, const cl_p_series& args, uintC len)
81{
82	if (N==0)
83		return cl_I_to_LF(0,len);
84	var cl_I T;
85	eval_p_series_aux(0,N,args,NULL,&T);
86	return cl_I_to_LF(T,len);
87}
88// Bit complexity (if p(n), q(n), a(n), b(n) have length O(log(n))):
89// O(log(N)^2*M(N)).
90
91}  // namespace cln