Skip to content

Commit

Permalink
Remove unused lagrange3, lagrange4, & find_nearest
Browse files Browse the repository at this point in the history
  • Loading branch information
singularitti committed Dec 20, 2023
1 parent 35c40dd commit 690c106
Showing 1 changed file with 0 additions and 126 deletions.
126 changes: 0 additions & 126 deletions src/qha/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,139 +18,13 @@

# ===================== What can be exported? =====================
__all__ = [
"find_nearest",
"vectorized_find_nearest",
"lagrange3",
"lagrange4",
"is_monotonic_decreasing",
"arange",
"calibrate_energy_on_reference",
]


def lagrange4(xs: Vector, ys: Vector) -> Callable[[float], float]:
"""
A third-order Lagrange polynomial function. Given 4 points for interpolation:
:math:`(x_0, y_0), \\ldots, (x_3, y_3)`, evaluate the Lagrange polynomial on :math:`x`, referenced from
`Wolfram MathWorld <http://mathworld.wolfram.com/LagrangeInterpolatingPolynomial.html>`_.
:param xs: A vector of the x-coordinates' of the 4 points.
:param ys: A vector of the y-coordinates' of the 4 points.
:return: A function that can evaluate the value of an x-coordinate within the range of
:math:`[\\text{min}(xs), \\text{max}(xs)]`, where :math:`xs` denotes the argument *xs*.
"""
if not len(xs) == len(ys) == 4:
raise ValueError("The *xs* and *ys* must both have length 4!")

x0, x1, x2, x3 = xs
y0, y1, y2, y3 = ys

@vectorize([float64(float64)], nopython=True, cache=True)
def f(x: float) -> float:
"""
A helper function that only does the evaluation.
:param x: The variable on which the Lagrange polynomial is going to be applied.
:return: The value of the Lagrange polynomial on :math:`x`, i.e., :math:`L(x)`.
"""
return (
(x - x1) * (x - x2) * (x - x3) / (x0 - x1) / (x0 - x2) / (x0 - x3) * y0
+ (x - x0) * (x - x2) * (x - x3) / (x1 - x0) / (x1 - x2) / (x1 - x3) * y1
+ (x - x0) * (x - x1) * (x - x3) / (x2 - x0) / (x2 - x1) / (x2 - x3) * y2
+ (x - x0) * (x - x1) * (x - x2) / (x3 - x0) / (x3 - x1) / (x3 - x2) * y3
)

return f


def lagrange3(xs: Vector, ys: Vector) -> Callable[[float], float]:
"""
A second-order Lagrange polynomial function. Given 3 points for interpolation:
:math:`(x_0, y_0), \\ldots, (x_2, y_2)`, evaluate the Lagrange polynomial on :math:`x`, referenced from
`Wolfram MathWorld also <http://mathworld.wolfram.com/LagrangeInterpolatingPolynomial.html>`_.
.. doctest::
>>> xs = [0, 1, 3]
>>> ys = [2, 4, 5]
>>> f = lagrange3(xs, ys)
>>> f(2.5)
5.125
:param xs: A vector of the x-coordinates' of the 3 points.
:param ys: A vector of the y-coordinates' of the 3 points.
:return: A function that can evaluate the value of an x-coordinate within the range of
:math:`[\\text{min}(xs), \\text{max}(xs)]`, where :math:`xs` denotes the argument *xs*.
"""
if not len(xs) == len(ys) == 3:
raise ValueError("The *xs* and *ys* must both have length 3!")

if len(set(xs)) < 3: # The ``set`` will remove duplicated items.
raise ValueError("Some elements of *xs* are duplicated!")

x0, x1, x2 = xs
y0, y1, y2 = ys

@vectorize([float64(float64)], nopython=True, cache=True)
def f(x: float) -> float:
"""
A helper function that only does the evaluation.
:param x: The variable on which the Lagrange polynomial is going to be applied.
:return: The value of the Lagrange polynomial on :math:`x`, i.e., :math:`L(x)`.
"""
return (
(x - x1) * (x - x2) / (x0 - x1) / (x0 - x2) * y0
+ (x - x0) * (x - x2) / (x1 - x0) / (x1 - x2) * y1
+ (x - x0) * (x - x1) / (x2 - x0) / (x2 - x1) * y2
)

return f


@jit([float64[:](float64)], nopython=True, nogil=True, cache=True)
def find_nearest(array: Vector, value: Scalar) -> int:
"""
Given an *array* , and given a *value* , returns an index ``j`` such that *value* is between ``array[j]``
and ``array[j+1]``. The *array* must be monotonic increasing. ``j=-1`` or ``j=len(array)`` is returned
to indicate that *value* is out of range below and above respectively.
If *array* is unsorted, consider first using an :math:`O(n \\log n)` sort and then use this function.
Referenced from `Stack Overflow <https://stackoverflow.com/questions/2566412/find-nearest-value-in-numpy-array>`_.
:param array: An array of monotonic increasing real numbers.
:param value: The value which user wants to find between two of the consecutive elements in *array*.
:return: The index mentioned above.
.. doctest::
>>> find_nearest([1, 3, 4, 6, 9, 11], 3.5)
1
>>> find_nearest([1, 3, 4, 6, 9, 11], 0)
0
>>> find_nearest([1, 3, 4, 6, 9, 11], 12)
4
"""
n: int = len(array)

if value <= array[0]:
return 0
elif value >= array[-1]:
return n - 2

j_low = 0 # Initialize lower limit.
j_up = n - 1 # Initialize upper limit.

while j_up - j_low > 1: # If we are not yet done,
j_mid: int = (j_up + j_low) // 2 # compute a midpoint,
if value >= array[j_mid]:
j_low = j_mid # and replace either the lower limit
else:
j_up = j_mid # or the upper limit, as appropriate.
# Repeat until the test condition is satisfied.

return j_low


@guvectorize(
[void(float64[:], float64[:], int64[:])], "(m),(n)->(n)", nopython=True, cache=True
)
Expand Down

0 comments on commit 690c106

Please sign in to comment.