Created
April 6, 2017 13:04
-
-
Save konfou/b98139134e09d23e3a16b17fb146ab6c 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
#!/usr/bin/env python | |
# shitty way of doing a Lagrange interpolation | |
def lagint(xarr,yarr): | |
p = [] | |
k = len(xarr) # rank+1 | |
for i in range(k): | |
Li = [] | |
for j in range(k): | |
if (j != i): | |
Lij = { | |
"xj" : str(xarr[j]), | |
"div" : xarr[i] - xarr[j] | |
} | |
Li.append(Lij) | |
pi = { | |
'yi' : yarr[i], | |
'Li' : Li | |
} | |
p.append(pi) | |
return p | |
def pprintli(lagint): | |
# pretty print of given p(x) | |
# found via previous lagint func | |
p = '' | |
for pi in lagint: | |
yid = pi['yi'] | |
for Lij in pi['Li']: | |
yid /= Lij['div'] | |
if yid>0: | |
p += '\n + ' | |
else: | |
yid = abs(yid) | |
p += '\n - ' | |
p += '%.4f'%(yid)+'*' | |
p += ''.join('(x-'+Lij['xj']+')' for Lij in pi['Li']) | |
print(' '+p[3:]) | |
# xarr = [x1,x2,...,xn] | |
# yarr = [y1,y1,...,yn] | |
# pprint(lagint(xarr,yarr)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment