|
1 | | -# import numpy as np |
2 | | -# from scipy.sparse import csc_matrix |
3 | | -# import nanoeigenpy |
4 | | - |
5 | | -# dim = 5 |
6 | | -# rng = np.random.default_rng(42) |
7 | | - |
8 | | -# A = rng.random((dim, dim)) |
9 | | -# A += np.diag(10.0 + rng.random(dim)) |
10 | | -# A = csc_matrix(A) |
11 | | - |
12 | | -# ilut = nanoeigenpy.IncompleteLUT(A) |
13 | | -# assert ilut.info() == nanoeigenpy.ComputationInfo.Success |
14 | | -# assert ilut.rows() == dim |
15 | | -# assert ilut.cols() == dim |
16 | | - |
17 | | -# X = rng.random((dim, 5)) |
18 | | -# B = A.dot(X) |
19 | | -# X_est = ilut.solve(B) |
20 | | -# assert isinstance(X_est, np.ndarray) |
21 | | -# print("X") |
22 | | -# print(X) |
23 | | -# print("X_est") |
24 | | -# print(X_est) |
25 | | -# assert nanoeigenpy.is_approx(X, X_est) |
26 | | - |
27 | | -# x = rng.random(dim) |
28 | | -# b = A.dot(x) |
29 | | -# x_est = ilut.solve(b) |
30 | | -# assert isinstance(x_est, np.ndarray) |
31 | | -# assert nanoeigenpy.is_approx(x, x_est) |
32 | | - |
33 | | -# X_sparse = csc_matrix(rng.random((dim, 10))) |
34 | | -# B_sparse = A.dot(X_sparse).tocsc() |
35 | | -# if not B_sparse.has_sorted_indices: |
36 | | -# B_sparse.sort_indices() |
37 | | -# X_est_sparse = ilut.solve(B_sparse) |
38 | | -# assert isinstance(X_est_sparse, csc_matrix) |
39 | | - |
40 | | -# ilut.analyzePattern(A) |
41 | | -# ilut.factorize(A) |
42 | | -# assert ilut.info() == nanoeigenpy.ComputationInfo.Success |
43 | | - |
44 | | -# ilut_params = nanoeigenpy.IncompleteLUT(A, 1e-4, 15) |
45 | | -# assert ilut_params.info() == nanoeigenpy.ComputationInfo.Success |
46 | | - |
47 | | -# ilut_set = nanoeigenpy.IncompleteLUT() |
48 | | -# ilut_set.setDroptol(1e-3) |
49 | | -# ilut_set.setFillfactor(20) |
50 | | -# ilut_set.compute(A) |
51 | | -# assert ilut_set.info() == nanoeigenpy.ComputationInfo.Success |
| 1 | +import numpy as np |
| 2 | +from scipy.sparse import csc_matrix |
| 3 | +import nanoeigenpy |
| 4 | + |
| 5 | +dim = 100 |
| 6 | +rng = np.random.default_rng() |
| 7 | + |
| 8 | +A = rng.random((dim, dim)) |
| 9 | +A = (A + A.T) * 0.5 + np.diag(5.0 + rng.random(dim)) |
| 10 | +A = csc_matrix(A) |
| 11 | + |
| 12 | +ilut = nanoeigenpy.IncompleteLUT(A) |
| 13 | +assert ilut.info() == nanoeigenpy.ComputationInfo.Success |
| 14 | +assert ilut.rows() == dim |
| 15 | +assert ilut.cols() == dim |
| 16 | + |
| 17 | +X = rng.random((dim, 100)) |
| 18 | +B = A.dot(X) |
| 19 | +X_est = ilut.solve(B) |
| 20 | +assert isinstance(X_est, np.ndarray) |
| 21 | +residual = np.linalg.norm(B - A.dot(X_est)) / np.linalg.norm(B) |
| 22 | +assert residual < 0.1 |
| 23 | + |
| 24 | +x = rng.random(dim) |
| 25 | +b = A.dot(x) |
| 26 | +x_est = ilut.solve(b) |
| 27 | +assert isinstance(x_est, np.ndarray) |
| 28 | +residual = np.linalg.norm(b - A.dot(x_est)) / np.linalg.norm(b) |
| 29 | +assert residual < 0.1 |
| 30 | + |
| 31 | +X_sparse = csc_matrix(rng.random((dim, 10))) |
| 32 | +B_sparse = A.dot(X_sparse).tocsc() |
| 33 | +if not B_sparse.has_sorted_indices: |
| 34 | + B_sparse.sort_indices() |
| 35 | +X_est_sparse = ilut.solve(B_sparse) |
| 36 | +assert isinstance(X_est_sparse, csc_matrix) |
| 37 | + |
| 38 | +ilut.analyzePattern(A) |
| 39 | +ilut.factorize(A) |
| 40 | +assert ilut.info() == nanoeigenpy.ComputationInfo.Success |
| 41 | + |
| 42 | +ilut_params = nanoeigenpy.IncompleteLUT(A, 1e-4, 15) |
| 43 | +assert ilut_params.info() == nanoeigenpy.ComputationInfo.Success |
| 44 | + |
| 45 | +ilut_set = nanoeigenpy.IncompleteLUT() |
| 46 | +ilut_set.setDroptol(1e-3) |
| 47 | +ilut_set.setFillfactor(20) |
| 48 | +ilut_set.compute(A) |
| 49 | +assert ilut_set.info() == nanoeigenpy.ComputationInfo.Success |
0 commit comments