diff --git a/stdlib/LinearAlgebra/src/diagonal.jl b/stdlib/LinearAlgebra/src/diagonal.jl index 4b7d9bd9d4af13..4be9f0ecef5b4b 100644 --- a/stdlib/LinearAlgebra/src/diagonal.jl +++ b/stdlib/LinearAlgebra/src/diagonal.jl @@ -77,8 +77,16 @@ Diagonal{T}(D::Diagonal{T}) where {T} = D Diagonal{T}(D::Diagonal) where {T} = Diagonal{T}(D.diag) AbstractMatrix{T}(D::Diagonal) where {T} = Diagonal{T}(D) -Matrix(D::Diagonal) = diagm(0 => D.diag) -Array(D::Diagonal) = Matrix(D) +Matrix(D::Diagonal{T}) where {T} = Matrix{T}(D) +Array(D::Diagonal{T}) where {T} = Matrix{T}(D) +function Matrix{T}(D::Diagonal) where {T} + n = size(D, 1) + B = zeros(T, n, n) + for i in 1:n + B[i,i] = D.diag[i] + end + return B +end """ Diagonal{T}(undef, n) diff --git a/stdlib/LinearAlgebra/src/special.jl b/stdlib/LinearAlgebra/src/special.jl index ad2c97568c536d..beac0c524f2f4f 100644 --- a/stdlib/LinearAlgebra/src/special.jl +++ b/stdlib/LinearAlgebra/src/special.jl @@ -310,7 +310,7 @@ function _qlmul(Q::AbstractQ, B) end function _qlmul(adjQ::Adjoint{<:Any,<:AbstractQ}, B) TQB = promote_type(eltype(adjQ), eltype(B)) - lmul!(adjoint(convert(AbstractMatrix{TQB}, parent(adjQ))), copy_similar(B, TQB)) + lmul!(adjoint(convert(AbstractMatrix{TQB}, parent(adjQ))), Matrix{TQB}(B)) end *(Q::AbstractQ, B::AbstractTriangular) = _qlmul(Q, B) @@ -325,7 +325,7 @@ rmul!(A::AbstractTriangular, Q::Adjoint{<:Any,<:AbstractQ}) = rmul!(full!(A), Q) function _qrmul(A, Q::AbstractQ) TAQ = promote_type(eltype(A), eltype(Q)) - return rmul!(copy_similar(A, TAQ), convert(AbstractMatrix{TAQ}, Q)) + return rmul!(Matrix{TAQ}(A), convert(AbstractMatrix{TAQ}, Q)) end function _qrmul(A, adjQ::Adjoint{<:Any,<:AbstractQ}) Q = adjQ.parent diff --git a/stdlib/LinearAlgebra/src/tridiag.jl b/stdlib/LinearAlgebra/src/tridiag.jl index 5a3c7612f67844..f99e1951af01de 100644 --- a/stdlib/LinearAlgebra/src/tridiag.jl +++ b/stdlib/LinearAlgebra/src/tridiag.jl @@ -571,15 +571,14 @@ function size(M::Tridiagonal, d::Integer) end end -function Matrix{T}(M::Tridiagonal{T}) where T +function Matrix{T}(M::Tridiagonal) where {T} A = zeros(T, size(M)) - for i = 1:length(M.d) + for i in 1:length(M.d)-1 A[i,i] = M.d[i] - end - for i = 1:length(M.d)-1 A[i+1,i] = M.dl[i] A[i,i+1] = M.du[i] end + A[n,n] = M.d[n] A end Matrix(M::Tridiagonal{T}) where {T} = Matrix{T}(M)