SciPy - matmul_toeplitz() Function



The scipy.linalg.matmul_toeplitz() function figures out the result of multiplying a Toeplitz matrix A with a vector or matrix x. It does this without creating the whole matrix. Instead, it takes advantage of how Toeplitz matrices are set up to use less memory and work faster.

This approach is helpful in areas where you often see Toeplitz matrices such as processing signals looking at data over time, and doing convolution tasks. It can handle inputs with one dimension or many dimensions, which means it's handy for all sorts of number-crunching jobs.

We can pair matmul_toeplitz() with toeplitz() to get a complete picture of the matrix and verify your results. For large-scale projects, you can boost speed by using the workers argument for parallel processing. It also works well with solve_toeplitz(), which finds answers to Toeplitz systems.

This method eliminates the need to construct the entire matrix by making use of the Toeplitz structure to make calculations faster. The workers parameter enables multi-threading making it suitable for big matrix jobs.

Syntax

Following is the syntax of the SciPy matmul_toeplitz() method −

.matmul_toeplitz(c_or_cr, x, check_finite=False, workers=None)

Parameters

Following are the parameters of matmul_toeplitz() method

  • c_or_cr − The Toeplitz matrix A is represented by its first column c and optionally its first row r. If a tuple (c,r) is provided, the first element of c and r must be the same to ensure consistency.

  • x − The vector or matrix to be multiplied by A.

  • check_finite − If True, checks if inputs contain only finite values. Disabling this can improve performance but risks errors if inputs are invalid. Default is False.

  • workers − Specifies the number of threads to use for parallel computation. Default is None, meaning a single thread.

Return Value

result (ndarray) − The result of the multiplication Ax.

Example 1

In the below example we have created a Toeplitz matrix using its first column.Instead of forming the full matrix, the matmul_toeplitz function directly computes the product of implicit matric and the vector.

import numpy as np
import scipy.linalg
from scipy.linalg import matmul_toeplitz

# First column of the Toeplitz matrix
c = [1, 2, 3]

# Input vector
x = [4, 5, 6]

result = scipy.linalg.matmul_toeplitz(c, x)
print(result)

When we run above program, it produces following result

[32. 25. 28.]

Example 2

This method bypasses the whole process of a matrix formation and effectively multiplies a Toeplitz matrix A with a vector x.

The following example shows how a multiplication of a vector with a Toeplitz matrix can be performed when the matrix is defined with its first column and row.

import numpy as np
from scipy.linalg import matmul_toeplitz

# Define the first column and row of the Toeplitz matrix
c = np.array([2, -1, 0])  
r = np.array([2, 3, 4])   

# Define the vector
x = np.array([1, 2, 3])

# Perform the multiplication
result = matmul_toeplitz((c, r), x, check_finite=True)

print("Result of A @ x:", result)

Following is an output of the above code

Result of A @ x: [20. 12.  4.]

Example 3

The function matmul_toeplitz() is highly efficient for large-scale matrix-matrix multiplications, particularly when the workers parameter allows for parallel computing.

To enhance computation speed, this example utilizes multiple threads to multiply a Toeplitz matrix A with a matrix X.

import numpy as np
from scipy.linalg import matmul_toeplitz

# Define the first column and row of the Toeplitz matrix
c = np.array([3, 1, 2])  # First column
r = np.array([3, 4, 5])  # First row

# Define the matrix
X = np.array([[1, 2], [3, 4], [5, 6]])

# Perform the multiplication with multi-threading
result = matmul_toeplitz((c, r), X, workers=4)

print("Result of A @ X:\n", result)

Output of the above code is as follows

Result of A @ X:
 [[40. 52.]
 [30. 38.]
 [20. 26.]]
scipy_linalg.htm
Advertisements