diff --git a/stdlib/LinearAlgebra/src/diagonal.jl b/stdlib/LinearAlgebra/src/diagonal.jl index 52a0b782c2c77..0d48dfc9d72ae 100644 --- a/stdlib/LinearAlgebra/src/diagonal.jl +++ b/stdlib/LinearAlgebra/src/diagonal.jl @@ -419,35 +419,6 @@ mul!(C::AbstractMatrix, A::Transpose{<:Any,<:Diagonal}, B::Transpose{<:Any,<:Rea (/)(Da::Diagonal, Db::Diagonal) = Diagonal(Da.diag ./ Db.diag) -function ldiv!(D::Diagonal{T}, v::AbstractVector{T}) where {T} - if length(v) != length(D.diag) - throw(DimensionMismatch("diagonal matrix is $(length(D.diag)) by $(length(D.diag)) but right hand side has $(length(v)) rows")) - end - for i = 1:length(D.diag) - d = D.diag[i] - if iszero(d) - throw(SingularException(i)) - end - v[i] = d\v[i] - end - v -end -function ldiv!(D::Diagonal{T}, V::AbstractMatrix{T}) where {T} - require_one_based_indexing(V) - if size(V,1) != length(D.diag) - throw(DimensionMismatch("diagonal matrix is $(length(D.diag)) by $(length(D.diag)) but right hand side has $(size(V,1)) rows")) - end - for i = 1:length(D.diag) - d = D.diag[i] - if iszero(d) - throw(SingularException(i)) - end - for j = 1:size(V,2) - @inbounds V[i,j] = d\V[i,j] - end - end - V -end ldiv!(x::AbstractArray, A::Diagonal, b::AbstractArray) = (x .= A.diag .\ b) ldiv!(adjD::Adjoint{<:Any,<:Diagonal{T}}, B::AbstractVecOrMat{T}) where {T} = @@ -610,8 +581,13 @@ for f in (:exp, :log, :sqrt, @eval $f(D::Diagonal) = Diagonal($f.(D.diag)) end -#Linear solver -function ldiv!(D::Diagonal, B::StridedVecOrMat) +(\)(D::Diagonal, A::AbstractMatrix) = + ldiv!(D, (typeof(oneunit(eltype(D))/oneunit(eltype(A)))).(A)) + +(\)(D::Diagonal, b::AbstractVector) = D.diag .\ b +(\)(Da::Diagonal, Db::Diagonal) = Diagonal(Da.diag .\ Db.diag) + +function ldiv!(D::Diagonal, B::AbstractVecOrMat) m, n = size(B, 1), size(B, 2) if m != length(D.diag) throw(DimensionMismatch("diagonal matrix is $(length(D.diag)) by $(length(D.diag)) but right hand side has $m rows")) @@ -628,11 +604,6 @@ function ldiv!(D::Diagonal, B::StridedVecOrMat) end return B end -(\)(D::Diagonal, A::AbstractMatrix) = - ldiv!(D, (typeof(oneunit(eltype(D))/oneunit(eltype(A)))).(A)) - -(\)(D::Diagonal, b::AbstractVector) = D.diag .\ b -(\)(Da::Diagonal, Db::Diagonal) = Diagonal(Da.diag .\ Db.diag) function inv(D::Diagonal{T}) where T Di = similar(D.diag, typeof(inv(zero(T))))