Skip to content

Commit

Permalink
Improve default interpolation performance
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdcs committed May 9, 2023
1 parent b75f3e1 commit 009700b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 33 deletions.
40 changes: 10 additions & 30 deletions colour/algebra/interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1222,38 +1222,18 @@ def _evaluate(self, x: NDArrayFloat) -> NDArrayFloat:
X = sdiv(x - self._xp[i], self._xp[i + 1] - self._xp[i])

r = self._yp
r_stacked = np.transpose(
as_float_array(
[r[i - 2], r[i - 1], r[i], r[i + 1], r[i + 2], r[i + 3]]
)
)

a0p = r[i]
a1p = (
2 * r[i - 2] - 16 * r[i - 1] + 16 * r[i + 1] - 2 * r[i + 2]
) / 24
a2p = (
-r[i - 2] + 16 * r[i - 1] - 30 * r[i] + 16 * r[i + 1] - r[i + 2]
) / 24
a3p = (
-9 * r[i - 2]
+ 39 * r[i - 1]
- 70 * r[i]
+ 66 * r[i + 1]
- 33 * r[i + 2]
+ 7 * r[i + 3]
) / 24
a4p = (
13 * r[i - 2]
- 64 * r[i - 1]
+ 126 * r[i]
- 124 * r[i + 1]
+ 61 * r[i + 2]
- 12 * r[i + 3]
) / 24
a5p = (
-5 * r[i - 2]
+ 25 * r[i - 1]
- 50 * r[i]
+ 50 * r[i + 1]
- 25 * r[i + 2]
+ 5 * r[i + 3]
) / 24
a1p = np.sum(r_stacked * [2, -16, 0, 16, -2, 0] / 24, axis=1)
a2p = np.sum(r_stacked * [-1, 16, -30, 16, -1, 0] / 24, axis=1)
a3p = np.sum(r_stacked * [-9, 39, -70, 66, -33, 7] / 24, axis=1)
a4p = np.sum(r_stacked * [13, -64, 126, -124, +61, -12] / 24, axis=1)
a5p = np.sum(r_stacked * [-5, 25, -50, 50, -25, 5] / 24, axis=1)

y = (
a0p
Expand Down
7 changes: 4 additions & 3 deletions colour/colorimetry/generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"""

from __future__ import annotations
from colour.algebra.interpolation import LinearInterpolator

import numpy as np

Expand Down Expand Up @@ -123,7 +124,7 @@ def sd_constant(
100.0
"""

settings = {"name": f"{k} Constant"}
settings = {"name": f"{k} Constant", "interpolator": LinearInterpolator}
settings.update(kwargs)

values = full(len(shape.wavelengths), k)
Expand Down Expand Up @@ -256,7 +257,7 @@ def msds_constant(
['a', 'b', 'c']
"""

settings = {"name": f"{k} Constant"}
settings = {"name": f"{k} Constant", "interpolator": LinearInterpolator}
settings.update(kwargs)

wavelengths = shape.wavelengths
Expand Down Expand Up @@ -755,7 +756,7 @@ def sd_multi_leds_Ohno2005(

sd = sd_zeros(shape)

for (peak_wavelength, fwhm_s, peak_power_ratio) in zip(
for peak_wavelength, fwhm_s, peak_power_ratio in zip(
peak_wavelengths, fwhm, peak_power_ratios
):
sd += (
Expand Down

0 comments on commit 009700b

Please sign in to comment.