Skip to content

Sum inside an integral. #6

@carlosRmelo

Description

@carlosRmelo

Hello,

It is possible passing and array as argument to a function? Something like that:

import numpy as np
import pyquad


def test_integrand_func(x, alpha, beta, i, j, k, l):
    a = np.sum(i)
    return x * alpha * beta + a * j * k



grid = np.random.random((10, 2))

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

res, err = pyquad.quad_grid(test_integrand_func, 0, 1, grid, (i,1,1,1))

This is just a toy example. My problem actually is that i have a sum inside an integral, and i have to evaluate this integral over a grid of values (x,y). For now I'm using scipy.quad, but for a large grid the code is going very slow. Bellow I have pasted a more realistic example of I want to do.

Screenshot from 2020-11-19 14-44-24

import numpy as np
from scipy.integrate import quad


def fun(tau, x, y, alpha, beta):
    """
    x: int
    y: int
    alpha: array
    beta: array
    """
    x_til = x/alpha
    y_til = y/beta
    aux = (x_til**2 + y_til**2)
    exp = np.exp(- tau**2*aux)
    
    return (tau**2)*exp.sum()



grid = np.random.random((10, 2))

alpha = np.array([1,2,3])
beta = np.array([4,5,6])

res = np.empty_like(grid)

for i in range(len(grid)):
    res[i] = quad(fun, 0, 1, args=(grid[i][0],grid[i][1], alpha, beta))

Any help is welcome, and thanks in advance.

Cheers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions