Skip to content

Commit b4ad651

Browse files
committed
Simplify helper function; fix docstring; update package version.
1 parent d18f4d2 commit b4ad651

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "lagrange"
3-
version = "3.0.0"
3+
version = "3.0.1"
44
description = """\
55
Pure-Python implementation of Lagrange \
66
interpolation over finite fields.\

src/lagrange/lagrange.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,22 @@
99
import functools
1010
import itertools
1111

12-
def _inv(a: int, prime: int) -> int:
13-
"""
14-
Compute multiplicative inverse modulo a prime.
15-
"""
16-
return pow(a, prime - 2, prime)
17-
1812
def interpolate(
1913
points: Union[dict, Sequence[int], Iterable[Sequence[int]]],
2014
modulus: int,
2115
degree: Optional[int] = None
2216
) -> int:
2317
"""
2418
Determine the value at the origin of the domain (*e.g.*, where *x* = 0)
25-
given a collection of points. The point information can be represented as
26-
a collection of two-component coordinates, as a dictionary, or as a sequence
27-
of values.
19+
given a collection of points.
2820
2921
:param points: Collection of points to interpolate.
3022
:param modulus: Modulus representing the finite field within which to interpolate.
3123
:param degree: Degree of the target polynomial.
3224
25+
The point information can be represented as a collection of two-component
26+
coordinates, as a dictionary, or as a sequence of values.
27+
3328
>>> interpolate([(1, 15), (2, 9), (3, 3)], 17)
3429
4
3530
>>> interpolate({1: 15, 2: 9, 3: 3}, 17)
@@ -158,6 +153,7 @@ def interpolate(
158153
ValueError: expecting a nonnegative integer degree
159154
"""
160155
# pylint: disable=too-many-branches # Allow large number of branches for type checking.
156+
# pylint: disable=unnecessary-lambda-assignment # Allow small local functions.
161157
values = None # Initially, assume that the supplied point data is not valid.
162158

163159
if isinstance(points, dict):
@@ -224,8 +220,9 @@ def interpolate(
224220
xs = list(values.keys())[:degree + 1]
225221

226222
# Field arithmetic helper functions.
227-
mul = lambda a, b: (a % modulus) * b % modulus # pylint: disable=unnecessary-lambda-assignment
228-
div = lambda a, b: mul(a, _inv(b, modulus)) # pylint: disable=unnecessary-lambda-assignment
223+
inv = lambda a, p: pow(a, p - 2, p)
224+
mul = lambda a, b: ((a % modulus) * b) % modulus
225+
div = lambda a, b: mul(a, inv(b, modulus))
229226

230227
# Compute the value of each unique Lagrange basis polynomial at ``0``,
231228
# then sum them all up to get the resulting value at ``0``.

0 commit comments

Comments
 (0)