Created
March 29, 2018 21:04
-
-
Save mzaglia/df5d61914ea384030f1f70a9f33f756d 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
# -*- coding: utf-8 -*- | |
from sympy import * | |
init_printing() | |
def lagrange(matrix): | |
size = len(matrix) | |
x = Symbol('x') | |
a = 0 | |
for k in range(0, size): | |
b = 1 | |
for i in range(0, size): | |
if i == k: | |
continue | |
b = b * (x - matrix[i][0]) / (matrix[k][0] - matrix[i][0]) | |
a = a + (matrix[k][1] * b) | |
return simplify(a) | |
pprint(lagrange([(-1, 4),(0,1),(2,-1)])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment