Skip to content

Commit 5d05175

Browse files
authored
Fix opnorm for banded matrices (#1428)
1 parent ed53855 commit 5d05175

File tree

3 files changed

+24
-46
lines changed

3 files changed

+24
-46
lines changed

src/tridiag.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,7 +1166,7 @@ function _opnorm1Inf(A::Tridiagonal, p)
11661166
case = p == Inf
11671167
lowerrange, upperrange = case ? (1:length(A.dl)-1, 2:length(A.dl)) : (2:length(A.dl), 1:length(A.dl)-1)
11681168
normfirst, normend = case ? (norm(first(A.d))+norm(first(A.du)), norm(last(A.dl))+norm(last(A.d))) : (norm(first(A.d))+norm(first(A.dl)), norm(last(A.du))+norm(last(A.d)))
1169-
1169+
size(A, 1) == 2 && return max(normfirst, normend)
11701170
return max(
11711171
mapreduce(t -> sum(norm, t),
11721172
max,
@@ -1181,7 +1181,7 @@ function _opnorm1Inf(A::SymTridiagonal, p::Real)
11811181
size(A, 1) == 1 && return norm(first(A.dv))
11821182
lowerrange, upperrange = 1:length(A.ev)-1, 2:length(A.ev)
11831183
normfirst, normend = norm(first(A.dv))+norm(first(A.ev)), norm(last(A.ev))+norm(last(A.dv))
1184-
1184+
size(A, 1) == 2 && return max(normfirst, normend)
11851185
return max(
11861186
mapreduce(t -> sum(norm, t),
11871187
max,

test/bidiag.jl

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,29 +1144,17 @@ end
11441144
end
11451145

11461146
@testset "opnorms" begin
1147-
B = Bidiagonal([1,-2,3,-4], [1,2,3], 'U')
1148-
1149-
@test opnorm(B, 1) == opnorm(Matrix(B), 1)
1150-
@test opnorm(B, 2) opnorm(Matrix(B), 2)
1151-
@test opnorm(B, Inf) == opnorm(Matrix(B), Inf)
1152-
1153-
B = Bidiagonal([1,-2,3,-4], [1,2,3], 'L')
1154-
1155-
@test opnorm(B, 1) == opnorm(Matrix(B), 1)
1156-
@test opnorm(B, 2) opnorm(Matrix(B), 2)
1157-
@test opnorm(B, Inf) == opnorm(Matrix(B), Inf)
1158-
1159-
B = Bidiagonal([2], Int[], 'L')
1160-
1161-
@test opnorm(B, 1) == opnorm(Matrix(B), 1)
1162-
@test opnorm(B, 2) opnorm(Matrix(B), 2)
1163-
@test opnorm(B, Inf) == opnorm(Matrix(B), Inf)
1164-
1165-
B = Bidiagonal([2], Int[], 'U')
1166-
1167-
@test opnorm(B, 1) == opnorm(Matrix(B), 1)
1168-
@test opnorm(B, 2) opnorm(Matrix(B), 2)
1169-
@test opnorm(B, Inf) == opnorm(Matrix(B), Inf)
1147+
for B in (Bidiagonal([1,-2,3,-4], [1,2,3], 'U'),
1148+
Bidiagonal([1,-2,3,-4], [1,2,3], 'L'),
1149+
Bidiagonal([2], Int[], 'L'),
1150+
Bidiagonal([2], Int[], 'U'),
1151+
Bidiagonal([1,-2], [-4], 'U'),
1152+
Bidiagonal([1,-2], [-4], 'L')
1153+
)
1154+
@test opnorm(B, 1) == opnorm(Matrix(B), 1)
1155+
@test opnorm(B, 2) opnorm(Matrix(B), 2)
1156+
@test opnorm(B, Inf) == opnorm(Matrix(B), Inf)
1157+
end
11701158
end
11711159

11721160
@testset "convert to Bidiagonal" begin

test/tridiag.jl

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,27 +1067,17 @@ end
10671067
end
10681068

10691069
@testset "opnorms" begin
1070-
T = Tridiagonal([1,2,3], [1,-2,3,-4], [1,2,3])
1071-
1072-
@test opnorm(T, 1) == opnorm(Matrix(T), 1)
1073-
@test_skip opnorm(T, 2) opnorm(Matrix(T), 2) # currently missing
1074-
@test opnorm(T, Inf) == opnorm(Matrix(T), Inf)
1075-
1076-
S = SymTridiagonal([1,-2,3,-4], [1,2,3])
1077-
1078-
@test opnorm(S, 1) == opnorm(Matrix(S), 1)
1079-
@test_skip opnorm(S, 2) opnorm(Matrix(S), 2) # currently missing
1080-
@test opnorm(S, Inf) == opnorm(Matrix(S), Inf)
1081-
1082-
T = Tridiagonal(Int[], [-5], Int[])
1083-
@test opnorm(T, 1) == opnorm(Matrix(T), 1)
1084-
@test_skip opnorm(T, 2) opnorm(Matrix(T), 2) # currently missing
1085-
@test opnorm(T, Inf) == opnorm(Matrix(T), Inf)
1086-
1087-
S = SymTridiagonal(T)
1088-
@test opnorm(S, 1) == opnorm(Matrix(S), 1)
1089-
@test_skip opnorm(S, 2) opnorm(Matrix(S), 2) # currently missing
1090-
@test opnorm(S, Inf) == opnorm(Matrix(S), Inf)
1070+
for T in (Tridiagonal([1,2,3], [1,-2,3,-4], [1,2,3]),
1071+
SymTridiagonal([1,-2,3,-4], [1,2,3]),
1072+
Tridiagonal(Int[], [-5], Int[]),
1073+
SymTridiagonal([-5], Int[]),
1074+
Tridiagonal([1], [1,-2], [3]),
1075+
SymTridiagonal([1,-2], [3])
1076+
)
1077+
@test opnorm(T, 1) == opnorm(Matrix(T), 1)
1078+
@test_skip opnorm(T, 2) opnorm(Matrix(T), 2) # currently missing
1079+
@test opnorm(T, Inf) == opnorm(Matrix(T), Inf)
1080+
end
10911081
end
10921082

10931083
@testset "block-bidiagonal matrix indexing" begin

0 commit comments

Comments
 (0)