Sum of absolute values of polynomials with python numpy -
here's wrote: it's classical exercise on interpolation, finished , sent. wondering if there (longer) way...
q list of floats (the points of interpolation)
i index of lagrange polynomial
x point evaluated:
def l(q,i,x): poly=1.0 j,p in enumerate(q): if j==i: go on poly *=(x-p)/(q[i]-p) homecoming poly then there function on i'm working:
def lambda(q,x): value=0.0 j in range(0,len(q)): value+=abs(l(q,j,x)) homecoming value now can utilize routines of python find it's maxium value in interval [0,1] , did. in python there polynomial module, can re-define l:
import numpy.polynomial.polynomial p def l_poly(q,i): poly = [] j,p in enumerate(q): if j==i: go on poly.append(p/(q[i]-p)) homecoming p.polyfromroots(poly) i'd same lambda can find maximum using built in function of derivative (find zeros , on , forth). problem is sum of abs(polynomials). there way this? or mix polynomial derivative , derivative of abs(...)?
take absolute value of each polynomial , add together them together.
from numpy.polynomial import polynomial p def absolute_poly_addition(polynomial_list): """ returns polynomial object sum of abslute value of polynomials in argument list """ final_poly = p([0]) poly in polynomial_list: abs_poly = p(abs(poly.coef)) final_poly += abs_poly homecoming final_poly = p([1,2,3]) # 1 + 2x + 3x**2 c = p([-3]) # -3 b = p([-1,0,0,4]) # -1 + 4x**3 d = p([2,-1,-1]) # 2 - x - x**2 e = + b + c + d # -1 + x + 2x**2 + 4x**3 f = absolute_poly_summation([a,b,c,d]) # 7 + 3x + 4x**2 + 4x**3 print(e) print(f) outputs:
poly([-1. 1. 2. 4.]) poly([ 7. 3. 4. 4.]) python numpy polynomials
No comments:
Post a Comment