Skip to content

Commit 85720ee

Browse files
committed
Improve the accuracy of sine_taylor_dekker
1 parent 72af3df commit 85720ee

File tree

3 files changed

+22
-26
lines changed

3 files changed

+22
-26
lines changed

functional_algorithms/expr.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,8 +622,11 @@ def __str__(self):
622622
return Printer().tostring(self)
623623

624624
def __repr__(self):
625-
operands = tuple(f"{o.kind}:{o.intkey}" for o in self.operands)
626-
return f"{type(self).__name__}({self.kind}, {operands}, {self.props})"
625+
if self.kind in {"symbol"}:
626+
operands = tuple(repr(o) for o in self.operands)
627+
else:
628+
operands = tuple(f"{o.kind}:{o.intkey}" for o in self.operands)
629+
return f"{type(self).__name__}({self.kind!r}, {operands}, {self.props})"
627630

628631
def __abs__(self):
629632
return self.context.absolute(self)

functional_algorithms/floating_point_algorithms.py

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -318,18 +318,6 @@ def mul_dekker(ctx, x, y, C=None):
318318
return mul_dw(ctx, x, y, xh, xl, yh, yl)
319319

320320

321-
def _mul_series_scalar(ctx, x, y):
322-
assert type(y) is not tuple
323-
terms = tuple(x_ * y for x_ in x[1:])
324-
return ctx._series(terms, dict(unit_index=x[0][0], scaling_exp=x[0][1]))
325-
326-
327-
def _mul_scalar_series(ctx, x, y):
328-
assert type(x) is not tuple
329-
terms = tuple(x * y_ for y_ in y[1:])
330-
return ctx._series(terms, dict(unit_index=y[0][0], scaling_exp=y[0][1]))
331-
332-
333321
def _div_series_scalar(ctx, x, y):
334322
assert type(y) is not tuple
335323
terms = tuple(x_ / y for x_ in x[1:])
@@ -343,16 +331,13 @@ def _mul_series_series(ctx, x, y):
343331

344332
terms = []
345333
for n in range(len(terms1) + len(terms2) - 1):
346-
xy = None
347334
for i, x in enumerate(terms1):
348335
for j, y in enumerate(terms2):
349336
if i + j == n:
350-
if xy is None:
351-
xy = x * y
337+
if ctx.parameters.get("series_uses_dekker"):
338+
_terms_add(ctx, terms, n, *mul_dekker(ctx, x, y))
352339
else:
353-
xy += x * y
354-
assert xy is not None
355-
terms.append(xy)
340+
_terms_add(ctx, terms, n, x * y)
356341

357342
return ctx._series(tuple(terms), dict(unit_index=index1 + index2, scaling_exp=sexp1))
358343

@@ -364,10 +349,10 @@ def mul_series(ctx, x, y):
364349
if type(x) is tuple:
365350
if type(y) is tuple:
366351
return _mul_series_series(ctx, x, y)
367-
return _mul_series_scalar(ctx, x, y)
352+
return _mul_series_series(ctx, x, ((0, 0), y))
368353
elif type(y) is tuple:
369-
return _mul_scalar_series(ctx, x, y)
370-
return x * y
354+
return _mul_series_series(ctx, ((0, 0), x), y)
355+
return _mul_series_series(ctx, ((0, 0), x), ((0, 0), y))
371356

372357

373358
def div_series(ctx, x, y):
@@ -1415,8 +1400,14 @@ def sine_taylor_dekker(ctx, x, order=7):
14151400
C, f = [x], 1
14161401
for i in range(3, order + 1, 2):
14171402
f *= -i * (i - 1)
1418-
f1 = ctx.constant(1 / f, x)
1419-
C.append(mul_series(ctx, x, f1))
1403+
# See sine_taylor:
1404+
if i >= 5:
1405+
f1 = ctx.constant(1 / f, x)
1406+
C.append(mul_series(ctx, x, f1))
1407+
else:
1408+
fh, fl = split_veltkamp(ctx, ctx.constant(f, x))
1409+
a = fl / fh
1410+
C.append(ctx.series(x / fh, -a * x / fh))
14201411
xx = mul_series(ctx, x, x)
14211412
# Horner's scheme is most accurate
14221413
return fast_polynomial_dekker(

functional_algorithms/tests/test_floating_point_algorithms.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,7 @@ def test_sine_taylor(dtype, func, fma):
825825
paths=[fpa],
826826
dtype=dtype,
827827
debug=(1.5 if size <= 10 else 0),
828+
parameters=dict(series_uses_dekker=True, series_uses_2sum=True),
828829
)
829830
def sin_dekker_func(ctx, x):
830831
return fpa.sine_taylor_dekker(ctx, x, order=order)
@@ -835,7 +836,8 @@ def sin_dekker_func(ctx, x):
835836
paths=[fpa],
836837
dtype=dtype,
837838
debug=(1.5 if size <= 10 else 0),
838-
rewrite_parameters=dict(optimize_cast=False, fma_backend=fma, series_uses_2sum=True),
839+
parameters=dict(series_uses_2sum=True),
840+
rewrite_parameters=dict(optimize_cast=False, fma_backend=fma),
839841
)
840842
def sin_func(ctx, x):
841843
return fpa.sine_taylor(ctx, x, order=order, split=False)

0 commit comments

Comments
 (0)