def lagrange(x, w):
 M = len(x)
 p = poly1d(0.0)
 for j in xrange(M):
  pt = poly1d(w[j])
  for k in xrange(M):
   if (k == j):
    continue
   fac = (x[j] - x[k])
   pt *= (poly1d([1.0, (- x[k])]) / fac)
  p += pt
 return p

def lagrange ( x , w ) : M = len ( x ) p = poly1d ( 0.0 ) for j in xrange ( M ) : pt = poly1d ( w [ j ] ) for k in xrange ( M ) : if ( k == j ) : continue fac = ( x [ j ] - x [ k ] ) pt *= ( poly1d ( [ 1.0 , ( - x [ k ] ) ] ) / fac ) p += pt return p
return a lagrange interpolating polynomial .