SciPy - solve_circulant()Function



The scipy.linalg.solve_circulant() function can solve linear systems of the Ax=b form, where A is a circulant matrix. This function can even work on singular and non-singular circulant matrices and it makes use of the Fast Fourier Transform.

This approach is particularly useful when dealing with cyclic or periodic systems, which appear fairly often in image processing and time-series analysis, as well as signal processing. It uses pseudo-inverses to ensure robust solutions should they be needed and works for singular matrices.

Syntax

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

.solve_circulant(c, b, singular='raise', tol=None, caxis=0, baxis=0, outaxis=0)

Parameters

Following is the parameters of solve_circulant() method

  • c (array_like) − 1D array representing the first row of the circulant matrix

  • b array_like, shape (n,) or (n, k) − Input equation of Right-hand side.

  • singular (str, optional) − Specifies behavior for singular matrices

  • tol (float, optional)− Tolerance for determining singular matrices.

  • caxis (int, optional) − Axis along which the first column of the circulant matrix is stored in c. Default is 0.

  • baxis (int, optional) − Axis along which the right-hand side is stored in b. Default is 0.

  • outaxis (int, optional) − Axis along which the output is returned. Default is 0.

Return Value

x (ndarray) − The solution to the circulant system.

Example 1: Solving a Basic Circulant System

In the below example, we have created a small circulant matrix defined by its first column c. The function solve_circulant() solves the system C x = b.

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

# Define the first column of the circulant matrix
c = np.array([4, 1, 2])

# Define the right-hand side vector
b = np.array([7, 8, 9])

# Solve the circulant system
x = scipy.linalg.solve_circulant(c, b)

print("Solution x:", x)

When we run the above program, it produces the following result

Solution x: [0.85714286 1.         1.57142857]

Example 2: Solving a Singular Circulant Matrix

The function solve_circulant() calculates an internal pseudo-inverse when it is solving problems that have singular circulant matrices so as to ensure a least-squares solution, even if the matrix cannot be inverted directly.

The method handles a singular matrix by internally calculating the pseudo-inverse when set with singular=True. This example solves a singular circulant system where direct inversion is not possible.

import numpy as np
from scipy.linalg import solve_circulant

# Define the first column of a singular circulant matrix
c = np.array([2, 1, 1])  
b = np.array([5, 5, 5])

# Solve the system with singular=True
x = solve_circulant(c, b, singular=True)

print("Solution x:", x)

Following is an output of the above code

Solution x: [1.25 1.25 1.25]

Example 3: Combining solve_circulant with FFT

The solve_circulant() method leverages FFT internally, making it ideal for applications involving frequency-domain operations.

This example demonstrates how to solve a circulant system x, and then fft is applied to x to transform it into the frequency domain.

import numpy as np
from scipy.linalg import solve_circulant
from scipy.fft import fft

c = np.array([3, 2, 1])
b = np.array([6, 7, 8])
x = solve_circulant(c, b)

# Compute the FFT of the solution to analyze frequency components
frequency_spectrum = fft(x)

print("Solution x:", x)
print("Frequency Spectrum of Solution:", frequency_spectrum)

Following is the output of the above code

Solution x: [0.5 1.5 1.5]
Frequency Spectrum of Solution: [ 3.5-0.j -1. +0.j -1. -0.j]
scipy_linalg.htm
Advertisements