Skip to content

Instantly share code, notes, and snippets.

@aerosadegh
Last active November 17, 2015 08:50
Show Gist options
  • Save aerosadegh/ccdae4b0a2ef7c8181da to your computer and use it in GitHub Desktop.
Save aerosadegh/ccdae4b0a2ef7c8181da to your computer and use it in GitHub Desktop.
from mpmath import *
# mpmath Option
mp.dps = 5; mp.pretty = True
class DifferForward_Newton_Func:
def __init__(self, xi , fi):
"""x_i , f_i"""
self.xi = xi
self.fi = fi
self.ls = []
def _C(s, k):
## s = mpf(s)
## if s-k < 0 and s-int(s) == 0: return 0
## return fac(s) / ( fac(k) * fac(s - k) )
return binomial(s,k)
def _f(self,x):
return self.fi[self.xi.index(x)]
def _fr(self,l):
if len(l) == 1 :
return self._f(l[0])
rev = ( self._fr(l[1:]) - self._fr(l[:-1]) )
self.ls += [mpf(rev)]
return rev
def calc(self):
'''calculate p(x) function.'''
self._fr(self.xi)
fc = [self._f(self.xi[0])] + self.ls[ -(len(self.xi) - 1) :]
n = len(fc)
h = abs(self.xi[1] - self.xi[0])
xi =[]
xi += self.xi
px = lambda x: sum( fc[i] * \
DifferForward_Newton_Func._C((x - xi[0]) / h, i) for i in range(n) )
return px
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment