Skip to content

Commit 8318e0b

Browse files
committed
Clean up some of the Sprague logic and add more comments
1 parent 3c69d95 commit 8318e0b

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

coloraide/algebra.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,21 +1077,23 @@ def preprocess(cls, points: list[Vector]) -> None:
10771077
if len(points) < 6:
10781078
raise ValueError('Sprague interpolation requires at least 6 evenly spaced points.')
10791079
l = len(points[0])
1080-
index = [0, 1, -2, -1]
1081-
p1, p2, p3, p4 = points[0:6], points[0:6], points[-6:], points[-6:]
1080+
# Create 2 points at the start and end of the data that will guide the interpolation
1081+
# through the start and end points.
1082+
p1, p2 = points[0:6], points[-6:]
10821083
points.insert(0, [])
10831084
points.insert(1, [])
10841085
points.append([])
10851086
points.append([])
1087+
index = [0, 1, -2, -1]
10861088
for i in range(l):
1087-
n = [
1088-
[j[i] for j in p1],
1089-
[j[i] for j in p2],
1090-
[j[i] for j in p3],
1091-
[j[i] for j in p4]
1092-
]
1093-
for e, row in enumerate(multiply(cls.SPRAGUE_COEFFICIENTS, n)):
1094-
points[index[e]].append(divide(sum(row), 209))
1089+
# Each row of coefficients relates to one of the new points.
1090+
# The top rows relate to the first two points we add to the start,
1091+
# and we use the first 6 starting points as context. The last two
1092+
# relate to the end points and use the last t points as context.
1093+
for e, row in enumerate(cls.SPRAGUE_COEFFICIENTS):
1094+
points[index[e]].append(
1095+
sum(multiply(row, [j[i] for j in (p1 if e < 2 else p2)], dims=D1)) / 209
1096+
)
10951097

10961098
def interpolate(self, p0: float, p1: float, p2: float, p3: float, p4: float, p5: float, t: float) -> float:
10971099
"""Interpolate with Sprague."""

0 commit comments

Comments
 (0)