SciPy – Using integrate.quad_vec() function (3 examples)

Updated: March 7, 2024 By: Guest Contributor Post a comment

Introduction

SciPy is a fundamental library for scientific computing in Python, offering a wide range of tools for mathematics, science, and engineering. The integrate.quad_vec() function is a powerful vectorized integrator, enabling efficient integration of functions that return arrays.

Understanding integrate.quad_vec()

The quad_vec function, part of SciPy’s integration sub-package, provides an interface for integrating vector-valued functions over a single variable. Unlike its sibling quad, quad_vec can handle functions that return vectors, making it particularly useful for simultaneous integration of multi-component systems. It employs adaptive quadrature to accurately evaluate integrals, even with functions that exhibit difficult behavior such as rapid oscillations or singularities.

Basic Example

Let’s start with a simple example, integrating a single-variable function that returns a vector value.

import numpy as np
from scipy.integrate import quad_vec

def example_function(x):
    return np.array([x**2, x**3])

result, error = quad_vec(example_function, 0, 1)
print(f'Result: {result}, Error: {error}')

Output:

Result: [0.33333333 0.25      ], Error: 1.3922208752510748e-14

This code integrates the function example_function from 0 to 1, returning the integral of x^2 and x^3 in the first and second elements of the result array, respectively.

Integrating Across Multiple Dimensions

Next, we explore integrating a function over multiple dimensions simultaneously. This is a more complex scenario that quad_vec can handle efficiently through its vectorized nature.

import numpy as np
from scipy.integrate import quad_vec

def multi_dim_function(x):
    return np.array([np.sin(x), np.cos(x)])

result, error = quad_vec(multi_dim_function, 0, np.pi)
print(f'Result: {result}, Error: {error}')

Output:

Result: [2.00000000e+00 2.22044605e-16], Error: 9.41333496923768e-14

This example demonstrates the integral of sin(x) and cos(x) over the interval from 0 to π. The function returns the integral values for both the sine and cosine, illustrating quad_vec‘s ability to process multiple outcomes simultaneously.

Handling Parameterized Functions

The ability to handle parameterized functions adds a layer of flexibility to quad_vec, making it ideal for a wide range of scientific computing tasks. Here’s how to integrate a parameterized vector-valued function.

import numpy as np
from scipy.integrate import quad_vec

def param_function(x, a, b):
    return np.array([a * np.exp(-x * a), b * np.sin(x * b)])

a, b = 2, 3
result, error = quad_vec(param_function, 0, 2, args=(a, b))
print(f'Result: {result}, Error: {error}')

Output:

Result: [0.98168436 0.03982971], Error: 1.3742241605887892e-13

In this scenario, our param_function is parameterized by a and b, allowing for the dynamic modification of its behavior. The quad_vec seamlessly integrates this parameterized function over the interval from 0 to 2.

Advanced Usage

For those looking to delve deeper, quad_vec supports advanced features such as specifying limits for singularity points, controlling the error tolerance, and handling complex-valued functions. These capabilities allow for more sophisticated integration scenarios tailored to the needs of complex scientific computing projects.

Conclusion

The integrate.quad_vec() function is a versatile and powerful tool within SciPy’s repertoire, catering to a broad spectrum of integration needs from basic to complex. Its ability to handle vector-valued functions, parameterized inputs, and complex integration scenarios makes it an essential tool for scientists, engineers, and anyone involved in mathematical computing. Harnessing the power of quad_vec can streamline your scientific computing workflows and enable more accurate and efficient problem-solving.