Skip to content

Instantly share code, notes, and snippets.

@kayhman
Created November 4, 2020 20:40
Show Gist options
  • Save kayhman/e1bd20b92cddd4cc76fc1000cee7fb85 to your computer and use it in GitHub Desktop.
Save kayhman/e1bd20b92cddd4cc76fc1000cee7fb85 to your computer and use it in GitHub Desktop.
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