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

Remove a few redundant diagonal ldiv! methods #38484

Merged
merged 3 commits into from
Apr 6, 2021
Merged
Changes from 2 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
34 changes: 2 additions & 32 deletions stdlib/LinearAlgebra/src/diagonal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -419,20 +419,7 @@ 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}
function ldiv!(D::Diagonal, V::AbstractVecOrMat)
mcognetta marked this conversation as resolved.
Show resolved Hide resolved
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"))
Expand All @@ -448,6 +435,7 @@ function ldiv!(D::Diagonal{T}, V::AbstractMatrix{T}) where {T}
end
V
end

mcognetta marked this conversation as resolved.
Show resolved Hide resolved
ldiv!(x::AbstractArray, A::Diagonal, b::AbstractArray) = (x .= A.diag .\ b)

ldiv!(adjD::Adjoint{<:Any,<:Diagonal{T}}, B::AbstractVecOrMat{T}) where {T} =
Expand Down Expand Up @@ -610,24 +598,6 @@ for f in (:exp, :log, :sqrt,
@eval $f(D::Diagonal) = Diagonal($f.(D.diag))
end

#Linear solver
function ldiv!(D::Diagonal, B::StridedVecOrMat)
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"))
end
(m == 0 || n == 0) && return B
for j = 1:n
for i = 1:m
di = D.diag[i]
if di == 0
throw(SingularException(i))
end
B[i,j] = di \ B[i,j]
end
end
return B
end
mcognetta marked this conversation as resolved.
Show resolved Hide resolved
(\)(D::Diagonal, A::AbstractMatrix) =
ldiv!(D, (typeof(oneunit(eltype(D))/oneunit(eltype(A)))).(A))
Copy link
Member

Choose a reason for hiding this comment

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

Should we change this to something like copy_oftype or copyto!(similar(A, typeof(...), size(A)), A) or so, in order to preserve the structure whenever possible?


Expand Down