Skip to content

Instantly share code, notes, and snippets.

@tuelwer
Last active November 13, 2023 11:23
Show Gist options
  • Save tuelwer/ee45332100a361074df37f1f963b1242 to your computer and use it in GitHub Desktop.
Save tuelwer/ee45332100a361074df37f1f963b1242 to your computer and use it in GitHub Desktop.
Solving a linear system with circulant matrix using FFT
import numpy as np
from scipy.linalg import circulant
def solve_circulant(A,b):
return np.fft.ifft(np.fft.fft(b)/np.fft.fft(A[:,0]))
n = 10000
a = np.random.randn(1,n)
A = circulant(a)
b = np.random.rand(n)
import time
t = time.time()
result_numpy = np.linalg.solve(A, b)
print('np.linalg.solve:', time.time()-t, 'seconds')
print('Residual:', np.linalg.norm(A@result_numpy-b))
print('--------------------')
t = time.time()
result_solve_circulant = solve_circulant(A, b)
print('solve_circulant2:', time.time()-t, 'seconds')
print('Residual:', np.linalg.norm(A@result_solve_circulant-b))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment