|
1 | 1 | using SparseArrays, DelimitedFiles, Random
|
2 | 2 | using Test, LinearAlgebra
|
3 | 3 | using IterativeSolvers, LinearSolve, AlgebraicMultigrid
|
4 |
| -import AlgebraicMultigrid: Pinv, Classical |
| 4 | +import AlgebraicMultigrid: Pinv, BackslashSolver, Classical |
5 | 5 | using JLD2
|
6 | 6 | using FileIO
|
7 | 7 |
|
|
71 | 71 | @testset "Coarse Solver" begin
|
72 | 72 | A = float.(poisson(10))
|
73 | 73 | b = A * ones(10)
|
| 74 | + |
| 75 | +# Test BackslashSolver (new default, much more efficient) |
| 76 | +x = similar(b) |
| 77 | +BackslashSolver(A)(x, b) |
| 78 | +@test sum(abs2, x - ones(10)) < 1e-12 # Should be more accurate than Pinv |
| 79 | + |
| 80 | +# Test Pinv (legacy solver, less efficient) |
74 | 81 | @test sum(abs2, Pinv(A)(similar(b), b) - ones(10)) < 1e-6
|
| 82 | + |
| 83 | +# Test that BackslashSolver handles multiple RHS |
| 84 | +B = hcat(b, 2*b) |
| 85 | +X = similar(B) |
| 86 | +BackslashSolver(A)(X, B) |
| 87 | +@test sum(abs2, X[:, 1] - ones(10)) < 1e-12 |
| 88 | +@test sum(abs2, X[:, 2] - 2*ones(10)) < 1e-12 |
| 89 | + |
| 90 | +# Test with sparse matrices of different types |
| 91 | +for T in [Float32, Float64] |
| 92 | + A_typed = T.(A) |
| 93 | + b_typed = T.(b) |
| 94 | + x_typed = similar(b_typed) |
| 95 | + BackslashSolver(A_typed)(x_typed, b_typed) |
| 96 | + @test sum(abs2, x_typed - ones(T, 10)) < 1e-6 |
| 97 | +end |
75 | 98 | end
|
76 | 99 |
|
77 | 100 | @testset "Multilevel" begin
|
|
0 commit comments