(n, x, X='x', Y='y')
return Add(*[ coeff*y for coeff, y in zip(coeffs, Y) ])
Construct Lagrange interpolating polynomial for ``n`` data points.
Construct Lagrange interpolating polynomial for ``n`` data points.
Construct Lagrange interpolating polynomial for n data points .
def interpolating_poly(n, x, X='x', Y='y'):
    """Construct Lagrange interpolating polynomial for ``n`` data points. """
    if isinstance(X, str):
        X = symbols("%s:%s" % (X, n))

    if isinstance(Y, str):
        Y = symbols("%s:%s" % (Y, n))

    coeffs = []

    for i in xrange(0, n):
        numer = []
        denom = []

        for j in xrange(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) ])
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 xrange ( 0 , n ) : numer = [ ] denom = [ ] for j in xrange ( 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 ) ] )