Integration of functions returning an array in Python

I have a lot of data to integrate and would like to find a way to do all this with just matrices and would be willing to compromise on accuracy to improve performance. What I mean is something like this:

import numpy
import scipy

a = np.array([1,2,3])

def func(x):
    return x**2 + x

def func2(x):
    global a
    return a*x

def integrand(x):
    return func(x)*func2(x)

integrated = quad(integrand, 0, 1)

So, I am trying to integrate each element into an array that comes out integrand.

I know that it is possible to use numpy.vectorize()as follows:

integrated = numpy.vectorize(scipy.integrate.quad)(integrand, 0, 1)

but i can't get this to work. Is there any way to do this in python?

Decision

, , , , - . - , , . , , , -

import numpy as np
import scipy.integrate.quad

a = np.array([1, 2, 3]) # arbitrary array, can be any size

def func(x):
    return x**2 + x

def func2(x, a):
    return a*x

def integrand(x, a):
    return func(x)*func2(x, a)

def integrated(a):
    integrated, tmp = scipy.integrate.quad(integrand, 0, 1, args = (a))
    return integrated

def vectorizeInt():
    global a
    integrateArray = []
    for i in range(len(a)):
        integrate = integrated(a[i])
        integrateArray.append(integrate)
    return integrateArray

, , , . scipy.integrate.quad. , , self (.. x def integrand(self, x, a):). args = (a) quad a integrand. integrand , def integrand(x, a, b, c, d):, args. , args = (a, b, c, d).

+6
2

vectorize , quad. quad, , integrate.

, numpy.trapz scipy.integrate.simps.

( , ) numpy, , , x [0, 1], :

In [270]: x = np.linspace(0.0, 1.0, 9).reshape(-1,1)

In [271]: x
Out[271]: 
array([[ 0.   ],
       [ 0.125],
       [ 0.25 ],
       [ 0.375],
       [ 0.5  ],
       [ 0.625],
       [ 0.75 ],
       [ 0.875],
       [ 1.   ]])

In [272]: integrand(x)
Out[272]: 
array([[ 0.        ,  0.        ,  0.        ],
       [ 0.01757812,  0.03515625,  0.05273438],
       [ 0.078125  ,  0.15625   ,  0.234375  ],
       [ 0.19335938,  0.38671875,  0.58007812],
       [ 0.375     ,  0.75      ,  1.125     ],
       [ 0.63476562,  1.26953125,  1.90429688],
       [ 0.984375  ,  1.96875   ,  2.953125  ],
       [ 1.43554688,  2.87109375,  4.30664062],
       [ 2.        ,  4.        ,  6.        ]])

, x (n, 1), , integrand(x), (n, 3). a .

numpy.trapz() scipy.integrate.simps(), axis=0, . , :

In [292]: x = np.linspace(0.0, 1.0, 101).reshape(-1,1)

In [293]: np.trapz(integrand(x), x, axis=0)
Out[293]: array([ 0.583375,  1.16675 ,  1.750125])

In [294]: simps(integrand(x), x, axis=0)
Out[294]: array([ 0.58333333,  1.16666667,  1.75      ])

quad:

In [296]: np.array([quad(lambda t: integrand(t)[k], 0, 1)[0] for k in range(len(a))])
Out[296]: array([ 0.58333333,  1.16666667,  1.75      ])

integrate ( ) , . , , simps .

+4

quadpy ( ) .

pip install quadpy

do

import numpy
import quadpy


def integrand(x):
    return [numpy.sin(x), numpy.exp(x)]  # ,...


res = quadpy.line_segment.integrate(
        integrand,
        [0, 1],
        quadpy.line_segment.GaussLegendre(5)
        )
print(res)

:

[ 0.45969769  1.71828183]
0

Source: https://habr.com/ru/post/1620648/


All Articles