Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix matrix multiplication interaction with HermOrSym and Diagonal #22474

Merged
merged 1 commit into from
Jun 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions base/linalg/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ end
(*)(D::Diagonal, B::AbstractTriangular) = A_mul_B!(D, copy(B))

(*)(A::AbstractMatrix, D::Diagonal) =
scale!(similar(A, promote_op(*, eltype(A), eltype(D.diag))), A, D.diag)
scale!(similar(A, promote_op(*, eltype(A), eltype(D.diag)), size(A)), A, D.diag)
Copy link
Member Author

@fredrikekre fredrikekre Jun 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fix is a bit dirty; currently similar(A::Symmetric) return a Matrix iff you supply both a new element type and size, can we rely on this behavior?
Ref: #19016 #15198 and #13731

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seem like this is used elsewhere also, so I think this is OK.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some ongoing open API issues between similar vs "copystructure" but for now I think it's okay

(*)(D::Diagonal, A::AbstractMatrix) =
scale!(similar(A, promote_op(*, eltype(A), eltype(D.diag))), D.diag, A)
scale!(similar(A, promote_op(*, eltype(A), eltype(D.diag)), size(A)), D.diag, A)

A_mul_B!(A::Union{LowerTriangular,UpperTriangular}, D::Diagonal) =
typeof(A)(A_mul_B!(A.data, D))
Expand Down
15 changes: 15 additions & 0 deletions test/linalg/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,18 @@ end
@test logm(D) == Diagonal([logm([1 2; 3 4]), logm([1 2; 3 4])])
@test sqrtm(D) == Diagonal([sqrtm([1 2; 3 4]), sqrtm([1 2; 3 4])])
end

@testset "multiplication with Symmetric/Hermitian" begin
for T in (Float64, Complex128)
D = Diagonal(randn(T, n))
A = randn(T, n, n); A = A'A
S = Symmetric(A)
H = Hermitian(A)
for f in (*, Ac_mul_B, A_mul_Bc, Ac_mul_Bc, At_mul_B, A_mul_Bt, At_mul_Bt)
@test f(D, S) f(Matrix(D), Matrix(S))
@test f(D, H) f(Matrix(D), Matrix(H))
@test f(S, D) f(Matrix(S), Matrix(D))
@test f(S, H) f(Matrix(S), Matrix(H))
end
end
end