Skip to content

Commit

Permalink
Improve perf of Diagonal' * Vector|Matrix (#21302)
Browse files Browse the repository at this point in the history
Includes tests
Only addresses diagonal part of 21286, not sparse matrices
  • Loading branch information
georgemarrows authored and andreasnoack committed Apr 18, 2017
1 parent 94214c9 commit b0c7084
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
10 changes: 10 additions & 0 deletions base/linalg/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ A_mul_B!(A::AbstractMatrix,B::Diagonal) = scale!(A,B.diag)
A_mul_Bt!(A::AbstractMatrix,B::Diagonal) = scale!(A,B.diag)
A_mul_Bc!(A::AbstractMatrix,B::Diagonal) = scale!(A,conj(B.diag))

# Get ambiguous method if try to unify AbstractVector/AbstractMatrix here using AbstractVecOrMat
A_mul_B!(out::AbstractVector, A::Diagonal, in::AbstractVector) = out .= A.diag .* in
Ac_mul_B!(out::AbstractVector, A::Diagonal, in::AbstractVector) = out .= ctranspose.(A.diag) .* in
At_mul_B!(out::AbstractVector, A::Diagonal, in::AbstractVector) = out .= transpose.(A.diag) .* in

A_mul_B!(out::AbstractMatrix, A::Diagonal, in::AbstractMatrix) = out .= A.diag .* in
Ac_mul_B!(out::AbstractMatrix, A::Diagonal, in::AbstractMatrix) = out .= ctranspose.(A.diag) .* in
At_mul_B!(out::AbstractMatrix, A::Diagonal, in::AbstractMatrix) = out .= transpose.(A.diag) .* in


/(Da::Diagonal, Db::Diagonal) = Diagonal(Da.diag ./ Db.diag)
function A_ldiv_B!{T}(D::Diagonal{T}, v::AbstractVector{T})
if length(v) != length(D.diag)
Expand Down
18 changes: 18 additions & 0 deletions test/linalg/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ srand(1)
#division of two Diagonals
@test D/D2 Diagonal(D.diag./D2.diag)
@test D\D2 Diagonal(D2.diag./D.diag)

# Performance specialisations for A*_mul_B!
vv = similar(v)
@test (r = full(D) * v ; A_mul_B!(vv, D, v) r vv)
@test (r = full(D)' * v ; Ac_mul_B!(vv, D, v) r vv)
@test (r = full(D).' * v ; At_mul_B!(vv, D, v) r vv)

UU = similar(U)
@test (r = full(D) * U ; A_mul_B!(UU, D, U) r UU)
@test (r = full(D)' * U ; Ac_mul_B!(UU, D, U) r UU)
@test (r = full(D).' * U ; At_mul_B!(UU, D, U) r UU)
end
@testset "triu/tril" begin
@test istriu(D)
Expand Down Expand Up @@ -177,6 +188,9 @@ srand(1)
@test Array(conj(D)) conj(DM)
@test ctranspose(D) == conj(D)
end
# Translates to Ac/t_mul_B, which is specialized after issue 21286
@test(D' * v == conj(D) * v)
@test(D.' * v == D * v)
end

#logdet
Expand Down Expand Up @@ -316,6 +330,10 @@ end
@test Dsym' == Diagonal([[1 1-im; 1-im 1], [1 1-im; 1-im 1]])
@test Dsym.' == Dsym

v = [[1, 2], [3, 4]]
@test Dherm' * v == Dherm * v
@test D.' * v == [[7, 10], [15, 22]]

@test issymmetric(D) == false
@test issymmetric(Dherm) == false
@test issymmetric(Dsym) == true
Expand Down

0 comments on commit b0c7084

Please sign in to comment.