Created
November 4, 2020 20:40
-
-
Save kayhman/e1bd20b92cddd4cc76fc1000cee7fb85 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import reduce | |
import numpy as np | |
import jax.numpy as jnp | |
class LagrangianPolynome: | |
def __init__(self, Ts, Xs): | |
self.Ts = Ts | |
self.Xs = Xs | |
def numerator(self, idx, t): | |
coef = reduce(lambda a, b: a * b, | |
[(t - t_i) for t_idx, t_i in enumerate(self.Ts) if t_idx != idx], 1.0) | |
return coef * self.Xs[idx] | |
def denominator(self, idx): | |
return np.multiply.reduce([(self.Ts[idx] - t_i) | |
for t_idx, t_i | |
in enumerate(self.Ts) if t_idx != idx]) | |
def eval(self, t): | |
val = 0.0 | |
for idx in range(len(self.Ts)): | |
val += self.numerator(idx, t) / self.denominator(idx) | |
return val |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment