Skip to content

Commit f0bbd16

Browse files
Merge pull request #401 from avik-pal/ap/banded_qr
Banded QR for non square matrices
2 parents 6baa523 + 718557e commit f0bbd16

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

.github/workflows/CI.yml

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ jobs:
2828
group: 'LinearSolveHYPRE'
2929
- version: '1'
3030
group: 'LinearSolvePardiso'
31+
- version: '1'
32+
group: 'LinearSolveBandedMatrices'
3133
steps:
3234
- uses: actions/checkout@v4
3335
- uses: julia-actions/setup-julia@v1

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,4 @@ SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
107107
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
108108

109109
[targets]
110-
test = ["Test", "IterativeSolvers", "InteractiveUtils", "JET", "KrylovKit", "Pkg", "Random", "SafeTestsets", "MultiFloats", "ForwardDiff", "HYPRE", "MPI", "BlockDiagonals", "Enzyme", "FiniteDiff"]
110+
test = ["Test", "IterativeSolvers", "InteractiveUtils", "JET", "KrylovKit", "Pkg", "Random", "SafeTestsets", "MultiFloats", "ForwardDiff", "HYPRE", "MPI", "BlockDiagonals", "Enzyme", "FiniteDiff", "BandedMatrices"]

ext/LinearSolveBandedMatricesExt.jl

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ import LinearSolve: defaultalg,
55
do_factorization, init_cacheval, DefaultLinearSolver, DefaultAlgorithmChoice
66

77
# Defaults for BandedMatrices
8-
function defaultalg(A::BandedMatrix, b, ::OperatorAssumptions)
9-
return DefaultLinearSolver(DefaultAlgorithmChoice.DirectLdiv!)
8+
function defaultalg(A::BandedMatrix, b, oa::OperatorAssumptions)
9+
if oa.issq
10+
return DefaultLinearSolver(DefaultAlgorithmChoice.DirectLdiv!)
11+
elseif LinearSolve.is_underdetermined(A)
12+
error("No solver for underdetermined `A::BandedMatrix` is currently implemented!")
13+
else
14+
return DefaultLinearSolver(DefaultAlgorithmChoice.QRFactorization)
15+
end
1016
end
1117

1218
function defaultalg(A::Symmetric{<:Number, <:BandedMatrix}, b, ::OperatorAssumptions)

test/banded.jl

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using BandedMatrices, LinearAlgebra, LinearSolve, Test
2+
3+
# Square Case
4+
n = 8
5+
A = BandedMatrix(Matrix(I, n, n), (2, 2))
6+
b = ones(n)
7+
A1 = A / 1;
8+
b1 = rand(n);
9+
x1 = zero(b);
10+
A2 = A / 2;
11+
b2 = rand(n);
12+
x2 = zero(b);
13+
14+
sol1 = solve(LinearProblem(A1, b1; u0 = x1))
15+
@test sol1.u A1 \ b1
16+
sol2 = solve(LinearProblem(A2, b2; u0 = x2))
17+
@test sol2.u A2 \ b2
18+
19+
# Square Symmetric
20+
A1s = Symmetric(A1)
21+
A2s = Symmetric(A2)
22+
23+
sol1s = solve(LinearProblem(A1s, b1; u0 = x1))
24+
@test sol1s.u A1s \ b1
25+
sol2s = solve(LinearProblem(A2s, b2; u0 = x2))
26+
@test sol2s.u A2s \ b2
27+
28+
# Underdetermined
29+
A = BandedMatrix(rand(8, 10), (2, 2))
30+
b = rand(8)
31+
32+
@test_throws ErrorException solve(LinearProblem(A, b)).u
33+
34+
# Overdetermined
35+
A = BandedMatrix(ones(10, 8), (2, 0))
36+
b = rand(10)
37+
38+
@test_nowarn solve(LinearProblem(A, b))

test/runtests.jl

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ if GROUP == "All" || GROUP == "Core"
1515
@time @safetestset "Default Alg Tests" include("default_algs.jl")
1616
VERSION >= v"1.9" && @time @safetestset "Enzyme Derivative Rules" include("enzyme.jl")
1717
@time @safetestset "Traits" include("traits.jl")
18+
VERSION >= v"1.9" && @time @safetestset "BandedMatrices" include("banded.jl")
1819
end
1920

2021
if GROUP == "LinearSolveCUDA"

0 commit comments

Comments
 (0)