/
pyamg_with_lobpcg.py
43 lines (32 loc) · 1.03 KB
/
pyamg_with_lobpcg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
Compute eigenvectors and eigenvalues using a preconditioned eigensolver
========================================================================
In this example Smoothed Aggregation (SA) is used to precondition
the LOBPCG eigensolver on a two-dimensional Poisson problem with
Dirichlet boundary conditions.
"""
import scipy
from scipy.sparse.linalg import lobpcg
import matplotlib.pyplot as plt
from pyamg import smoothed_aggregation_solver
from pyamg.gallery import poisson
N = 100
K = 9
A = poisson((N,N), format='csr')
# create the AMG hierarchy
ml = smoothed_aggregation_solver(A)
# initial approximation to the K eigenvectors
X = scipy.rand(A.shape[0], K)
# preconditioner based on ml
M = ml.aspreconditioner()
# compute eigenvalues and eigenvectors with LOBPCG
W,V = lobpcg(A, X, M=M, tol=1e-8, largest=False)
#plot the eigenvectors
plt.figure(figsize=(9,9))
for i in range(K):
plt.subplot(3, 3, i+1)
plt.title('Eigenvector %d' % i)
plt.pcolor(V[:,i].reshape(N,N))
plt.axis('equal')
plt.axis('off')
plt.show()