1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
Copyright (C) 2012 Fredrik Johansson
This file is part of FLINT.
FLINT is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version. See <https://www.gnu.org/licenses/>.
*/
#include "arf.h"
static int _arf_are_close(const arf_t x, const arf_t y, slong prec)
{
fmpz_t xb, yb;
fmpz_t delta;
int result;
fmpz_init(xb);
fmpz_init(yb);
fmpz_init(delta);
arf_bot(xb, x);
arf_bot(yb, y);
if (fmpz_cmp(ARF_EXPREF(x), ARF_EXPREF(y)) >= 0)
fmpz_sub(delta, xb, ARF_EXPREF(y));
else
fmpz_sub(delta, yb, ARF_EXPREF(x));
fmpz_sub_ui(delta, delta, 64);
result = (fmpz_cmp_ui(delta, prec) < 0);
fmpz_clear(xb);
fmpz_clear(yb);
fmpz_clear(delta);
return result;
}
static int
_arf_add_eps(arf_t s, const arf_t x, int sgn, slong prec, arf_rnd_t rnd)
{
arf_t t;
slong bits;
bits = arf_bits(x);
if (bits == 0)
{
flint_throw(FLINT_ERROR, "_arf_add_eps\n");
}
bits = FLINT_MAX(bits, prec) + 10;
arf_init(t);
arf_set_si(t, sgn);
arf_mul_2exp_fmpz(t, t, ARF_EXPREF(x));
arf_mul_2exp_si(t, t, -bits);
arf_add(s, x, t, prec, rnd);
arf_clear(t);
return 1;
}
int
arf_sum(arf_t s, arf_srcptr terms, slong len, slong prec, arf_rnd_t rnd)
{
arf_ptr blocks;
slong i, j, used;
int have_merged, res;
/* first check if the result is inf or nan */
{
int have_pos_inf = 0;
int have_neg_inf = 0;
for (i = 0; i < len; i++)
{
if (arf_is_pos_inf(terms + i))
{
if (have_neg_inf)
{
arf_nan(s);
return 0;
}
have_pos_inf = 1;
}
else if (arf_is_neg_inf(terms + i))
{
if (have_pos_inf)
{
arf_nan(s);
return 0;
}
have_neg_inf = 1;
}
else if (arf_is_nan(terms + i))
{
arf_nan(s);
return 0;
}
}
if (have_pos_inf)
{
arf_pos_inf(s);
return 0;
}
if (have_neg_inf)
{
arf_neg_inf(s);
return 0;
}
}
blocks = flint_malloc(sizeof(arf_struct) * len);
for (i = 0; i < len; i++)
arf_init(blocks + i);
/* put all terms into blocks */
used = 0;
for (i = 0; i < len; i++)
{
if (!arf_is_zero(terms + i))
{
arf_set(blocks + used, terms + i);
used++;
}
}
/* merge blocks until all are well separated */
have_merged = 1;
while (used >= 2 && have_merged)
{
have_merged = 0;
for (i = 0; i < used && !have_merged; i++)
{
for (j = i + 1; j < used && !have_merged; j++)
{
if (_arf_are_close(blocks + i, blocks + j, prec))
{
arf_add(blocks + i, blocks + i, blocks + j,
ARF_PREC_EXACT, ARF_RND_DOWN);
/* remove the merged block */
arf_swap(blocks + j, blocks + used - 1);
used--;
/* remove the updated block if the sum is zero */
if (arf_is_zero(blocks + i))
{
arf_swap(blocks + i, blocks + used - 1);
used--;
}
have_merged = 1;
}
}
}
}
if (used == 0)
{
arf_zero(s);
res = 0;
}
else if (used == 1)
{
res = arf_set_round(s, blocks + 0, prec, rnd);
}
else
{
/* find the two largest blocks */
for (i = 1; i < used; i++)
if (arf_cmpabs(blocks + 0, blocks + i) < 0)
arf_swap(blocks + 0, blocks + i);
for (i = 2; i < used; i++)
if (arf_cmpabs(blocks + 1, blocks + i) < 0)
arf_swap(blocks + 1, blocks + i);
res = _arf_add_eps(s, blocks + 0, arf_sgn(blocks + 1), prec, rnd);
}
for (i = 0; i < len; i++)
arf_clear(blocks + i);
flint_free(blocks);
return res;
}