python - scipy.integrate.quad() without using lambda -
i making program involves rectification. module can complex integration scipy scipy.integrate.quad()
command. however, in code, need have variable represents function itself, since there more different functions used in rectification process derived using preceding equation. every source have seen command either involves lambda or creating definition outputs equation.you input equation manually. so, question is, there way integrate without doing that? here's code:
import scipy import sympy scipy.integrate import quad def rectify(smallestterm, nextsmallestterm, poly, coef, exp): test = rectification(coef,exp) = quad(test, smallestterm, nextsmallestterm) print(a) = a[0] dist = a/2 return dist def test(x): return rectification(coef, exp) def rectification(coef, exp): u = poly(lint(coef,exp)) #generated equation b = u.all_coeffs() poly = b c = len(poly) - 1 exponents = [] while c + 1 > 0: exponents.append(c) c = c - 1 poly = function(poly,exponents) #generated equation in form can used , identified function. return sqrt(1+(pow(diff(poly),2)))
where coef
list of leading coefficients of polynomial , exp
list of leading exponents of polynomial. essentially, both indirectly combined in definition, function(coef, exp)
(code not shown) outputs polynomial (the variable "poly
").
i.e.
function([2,4,5],[1,6,0])
outputs
4*x**6 + 2*x + 5
this code (above function code) not work doesn't allow me use variable "a" represent entire function, recognizes "a" function in itself. so, lambda not work in case. can't like:
import scipy import sympy scipy.integrate import quad poly = 2*x**5 + 5*x**4 - 4*x**2 + 10 #some complicated polynomial d = diff(poly) #differential of polynomial = sqrt(1+(pow(d,2))) e = quad(a, 1, 5) e = e[1]/2 return e
if need see full code understand other functions in code, please ask , happily provide it!
to understanding, code produces symbolic expressions using sympy. these not functions in sense of python, cannot called. can't used directly first argument of quad
. sympy provides lambdify
method wraps expression function:
quad(lambdify(x, expr), 0, 1)
here expr
can symbolic expression variable x
in it, example expr = 4*x**6 + 2*x + 5
.
Comments
Post a Comment