Skip to content

Commit 3ebc113

Browse files
committed
Bespoke wrapper to avoid Diagonal methods
1 parent ff9af69 commit 3ebc113

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

test/diagonal.jl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ using .Main.SizedArrays
2424
const n=12 # Size of matrix problem to test
2525
Random.seed!(1)
2626

27+
# wrapper to avoid dispatching to diagonal methods
28+
struct NotDiagonal{T,A<:AbstractMatrix{T}} <: AbstractMatrix{T}
29+
a :: A
30+
end
31+
Base.size(N::NotDiagonal) = size(N.a)
32+
Base.getindex(N::NotDiagonal, i::Int, j::Int) = N.a[i, j]
33+
LinearAlgebra.isdiag(N::NotDiagonal) = false # this contradicts `getindex`
34+
LinearAlgebra.ishermitian(N::NotDiagonal) = ishermitian(N.a)
35+
LinearAlgebra.istriu(N::NotDiagonal) = istriu(N.a)
36+
LinearAlgebra.istril(N::NotDiagonal) = istril(N.a)
37+
2738
@testset for relty in (Float32, Float64, BigFloat), elty in (relty, Complex{relty})
2839
dd=convert(Vector{elty}, randn(n))
2940
vv=convert(Vector{elty}, randn(n))
@@ -37,8 +48,9 @@ Random.seed!(1)
3748
M = Matrix(D)
3849
# we can't directly compare with a Matrix, since the dense methods often dispatch
3950
# to Diagonal ones. We therefore compare with other structured matrix types
40-
# which have their own implementations
41-
DM = elty <: Real ? Hermitian(M) : UpperTriangular(M)
51+
# which have their own implementations.
52+
# We wrap the complex matrices in NotDiagonal to avoid falling back to Diagonal methods
53+
DM = elty <: Real ? Hermitian(M) : NotDiagonal(UpperTriangular(M))
4254

4355
@testset "constructor" begin
4456
for x in (dd, GenericArray(dd))

0 commit comments

Comments
 (0)