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 @@ -342,14 +342,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 @@ -370,6 +376,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 @@ -420,6 +427,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
2 changes: 1 addition & 1 deletion src/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1841,7 +1841,7 @@ function generic_trimatdiv!(C::AbstractVecOrMat, uploc, isunitc, ::Function, xA:
for i in axes(B,1)[2:end]
C[i,k] = oA \ B[i,k] - _ustrip(conj(A[i,1])) * C1
end
for j in axes(A,2)
for j in axes(A,2)[2:end]
Cj = C[j,k]
for i in j+1:lastindex(A,1)
C[i,k] -= _ustrip(conj(A[i,j])) * Cj
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 @@ -1578,4 +1579,17 @@ end
@test D == D2
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
13 changes: 13 additions & 0 deletions test/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1118,4 +1118,17 @@ end
end
end

@testset "issue #1749: ldiv! with lazy adjoint/transpose parent" begin
M = ComplexF64[1 2+3im 1im; 4-1im 5 2-im; 3+im 2im 7]
b = ComplexF64[1, 2, 3]
B = ComplexF64[1 4; 2 5; 3 6]
for P in (transpose(M'), adjoint(transpose(M)), transpose(M), M')
for W in (UpperTriangular, UnitUpperTriangular, LowerTriangular, UnitLowerTriangular)
A = W(P)
@test A \ b ≈ Matrix(A) \ b ≈ lu(Matrix(A)) \ b
@test A \ B ≈ Matrix(A) \ B ≈ lu(Matrix(A)) \ B
end
end
end

end # module TestTriangular
Loading