Skip to content

Commit

Permalink
Refactor/simplify interpolation calculation.
Browse files Browse the repository at this point in the history
  • Loading branch information
lapets committed Jul 24, 2022
1 parent 181a3cb commit f0103bb
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/lagrange/lagrange.py
Expand Up @@ -4,8 +4,9 @@
from __future__ import annotations
from functools import reduce
from typing import Union, Optional, Sequence, Iterable
import collections.abc
import doctest
import collections.abc
import itertools

def _inv(a: int, prime: int) -> int:
"""
Expand Down Expand Up @@ -228,19 +229,16 @@ def interpolate(
# Compute the value of each unique Lagrange basis polynomial at ``0``,
# then sum them all up to get the resulting value at ``0``.
return sum(
mul(
values[x],
reduce(
mul,
(
# Extrapolate using the fact that *y* = ``1`` if
# ``x`` = ``x_known``, and *y* = ``0`` for the other
# known values in the domain.
div(0 - x_known, x - x_known)
for x_known in xs if x_known is not x
),
1
)
reduce(
mul,
itertools.chain([values[x]], (
# Extrapolate using the fact that *y* = ``1`` if
# ``x`` = ``x_known``, and *y* = ``0`` for the other
# known values in the domain.
div(0 - x_known, x - x_known)
for x_known in xs if x_known is not x
)),
1
)
for x in xs
) % modulus
Expand Down

0 comments on commit f0103bb

Please sign in to comment.