Skip to content
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
8 changes: 8 additions & 0 deletions src/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -346,14 +346,20 @@ function (*)(D::Diagonal, V::AbstractVector)
return D.diag .* V
end

_wrapperop_reverses_products(::Adjoint, ::Diagonal) = true
_wrapperop_reverses_products(::Transpose{<:Union{Real,Complex}}, ::Diagonal{<:Union{Real,Complex}}) = true
_wrapperop_reverses_products(A, D) = false

function mul(A::AdjOrTransAbsMat, D::Diagonal)
_wrapperop_reverses_products(A, D) || return @invoke mul(A::AbstractMatrix, D::AbstractMatrix)
adj = _wrapperop(A)
copy(adj(adj(D) * adj(A)))
end
function mul(A::AdjOrTransAbsMat{<:Number, <:StridedMatrix}, D::Diagonal{<:Number})
@invoke mul(A::AbstractMatrix, D::AbstractMatrix)
end
function mul(D::Diagonal, A::AdjOrTransAbsMat)
_wrapperop_reverses_products(A, D) || return @invoke mul(D::AbstractMatrix, A::AbstractMatrix)
adj = _wrapperop(A)
copy(adj(adj(A) * adj(D)))
end
Expand All @@ -374,6 +380,7 @@ end
# A' = A' * D => A = D' * A
# This uses the fact that D' is a Diagonal
function rmul!(A::AdjOrTransAbsMat, D::Diagonal)
_wrapperop_reverses_products(A, D) || return @invoke rmul!(A::AbstractMatrix, D::Diagonal)
f = _wrapperop(A)
lmul!(f(D), f(A))
A
Expand Down Expand Up @@ -421,6 +428,7 @@ end
# A' = D * A' => A = A * D'
# This uses the fact that D' is a Diagonal
function lmul!(D::Diagonal, A::AdjOrTransAbsMat)
_wrapperop_reverses_products(A, D) || return @invoke lmul!(D::Diagonal, A::AbstractVecOrMat)
f = _wrapperop(A)
rmul!(f(A), f(D))
A
Expand Down
14 changes: 14 additions & 0 deletions test/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ using Main.LinearAlgebraTestHelpers.InfiniteArrays
using Main.LinearAlgebraTestHelpers.FillArrays
using Main.LinearAlgebraTestHelpers.SizedArrays
using Main.LinearAlgebraTestHelpers.ImmutableArrays
using Main.LinearAlgebraTestHelpers.Quaternions

const n=12 # Size of matrix problem to test
Random.seed!(1)
Expand Down Expand Up @@ -1633,4 +1634,17 @@ end
end
end

@testset "products of Diagonal with transposed non-commutative matrices (issue #1750)" begin
i, j, k = Quaternion(0.0,1,0,0), Quaternion(0.0,0,1,0), Quaternion(0.0,0,0,1)
M = [i j; zero(i) k]
A = view(M, [1, 2], :)
D = Diagonal([j, k])
for op in (transpose, adjoint)
@test op(A) * D == Matrix(op(A)) * D
@test D * op(A) == D * Matrix(op(A))
@test rmul!(op(copy(M)), D) == Matrix(op(M)) * D
@test lmul!(D, op(copy(M))) == D * Matrix(op(M))
end
end

end # module TestDiagonal
Loading