@public
def interpolating_poly(n, x, X='x', Y='y'):
 if isinstance(X, str):
  X = symbols(('%s:%s' % (X, n)))
 if isinstance(Y, str):
  Y = symbols(('%s:%s' % (Y, n)))
 coeffs = []
 for i in range(0, n):
  numer = []
  denom = []
  for j in range(0, n):
   if (i == j):
    continue
   numer.append((x - X[j]))
   denom.append((X[i] - X[j]))
  numer = Mul(*numer)
  denom = Mul(*denom)
  coeffs.append((numer / denom))
 return Add(*[(coeff * y) for (coeff, y) in zip(coeffs, Y)])

@ public def interpolating_poly ( n , x , X = 'x' , Y = 'y' ) : if isinstance ( X , str ) : X = symbols ( ( '%s:%s' % ( X , n ) ) ) if isinstance ( Y , str ) : Y = symbols ( ( '%s:%s' % ( Y , n ) ) ) coeffs = [ ] for i in range ( 0 , n ) : numer = [ ] denom = [ ] for j in range ( 0 , n ) : if ( i == j ) : continue numer . append ( ( x - X [ j ] ) ) denom . append ( ( X [ i ] - X [ j ] ) ) numer = Mul ( * numer ) denom = Mul ( * denom ) coeffs . append ( ( numer / denom ) ) return Add ( * [ ( coeff * y ) for ( coeff , y ) in zip ( coeffs , Y ) ] )
construct lagrange interpolating polynomial for n data points .