Skip to content

Commit a51f04a

Browse files
author
KristofferC
committed
Revert "Use copyto! in converting Diagonal/Bidiagonal/Tridiagonal to Matrix (#53912)"
This reverts commit 19919b7.
1 parent 85b5073 commit a51f04a

File tree

7 files changed

+32
-67
lines changed

7 files changed

+32
-67
lines changed

stdlib/LinearAlgebra/src/bidiag.jl

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,19 @@ end
195195

196196
#Converting from Bidiagonal to dense Matrix
197197
function Matrix{T}(A::Bidiagonal) where T
198-
B = Matrix{T}(undef, size(A))
199-
if haszero(T) # optimized path for types with zero(T) defined
200-
size(B,1) > 1 && fill!(B, zero(T))
201-
copyto!(view(B, diagind(B)), A.dv)
202-
copyto!(view(B, diagind(B, A.uplo == 'U' ? 1 : -1)), A.ev)
203-
else
204-
copyto!(B, A)
198+
n = size(A, 1)
199+
B = Matrix{T}(undef, n, n)
200+
n == 0 && return B
201+
n > 1 && fill!(B, zero(T))
202+
@inbounds for i = 1:n - 1
203+
B[i,i] = A.dv[i]
204+
if A.uplo == 'U'
205+
B[i,i+1] = A.ev[i]
206+
else
207+
B[i+1,i] = A.ev[i]
208+
end
205209
end
210+
B[n,n] = A.dv[n]
206211
return B
207212
end
208213
Matrix(A::Bidiagonal{T}) where {T} = Matrix{promote_type(T, typeof(zero(T)))}(A)

stdlib/LinearAlgebra/src/diagonal.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,11 @@ AbstractMatrix{T}(D::Diagonal{T}) where {T} = copy(D)
116116
Matrix(D::Diagonal{T}) where {T} = Matrix{promote_type(T, typeof(zero(T)))}(D)
117117
Array(D::Diagonal{T}) where {T} = Matrix(D)
118118
function Matrix{T}(D::Diagonal) where {T}
119-
B = Matrix{T}(undef, size(D))
120-
if haszero(T) # optimized path for types with zero(T) defined
121-
size(B,1) > 1 && fill!(B, zero(T))
122-
copyto!(view(B, diagind(B)), D.diag)
123-
else
124-
copyto!(B, D)
119+
n = size(D, 1)
120+
B = Matrix{T}(undef, n, n)
121+
n > 1 && fill!(B, zero(T))
122+
@inbounds for i in 1:n
123+
B[i,i] = D.diag[i]
125124
end
126125
return B
127126
end

stdlib/LinearAlgebra/src/tridiag.jl

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,13 @@ function Matrix{T}(M::SymTridiagonal) where T
135135
n = size(M, 1)
136136
Mf = Matrix{T}(undef, n, n)
137137
n == 0 && return Mf
138-
if haszero(T) # optimized path for types with zero(T) defined
139-
n > 2 && fill!(Mf, zero(T))
140-
@inbounds for i = 1:n-1
141-
Mf[i,i] = symmetric(M.dv[i], :U)
142-
Mf[i+1,i] = transpose(M.ev[i])
143-
Mf[i,i+1] = M.ev[i]
144-
end
145-
Mf[n,n] = symmetric(M.dv[n], :U)
146-
else
147-
copyto!(Mf, M)
138+
n > 2 && fill!(Mf, zero(T))
139+
@inbounds for i = 1:n-1
140+
Mf[i,i] = symmetric(M.dv[i], :U)
141+
Mf[i+1,i] = transpose(M.ev[i])
142+
Mf[i,i+1] = M.ev[i]
148143
end
144+
Mf[n,n] = symmetric(M.dv[n], :U)
149145
return Mf
150146
end
151147
Matrix(M::SymTridiagonal{T}) where {T} = Matrix{promote_type(T, typeof(zero(T)))}(M)
@@ -590,14 +586,15 @@ axes(M::Tridiagonal) = (ax = axes(M.d,1); (ax, ax))
590586

591587
function Matrix{T}(M::Tridiagonal) where {T}
592588
A = Matrix{T}(undef, size(M))
593-
if haszero(T) # optimized path for types with zero(T) defined
594-
size(A,1) > 2 && fill!(A, zero(T))
595-
copyto!(view(A, diagind(A)), M.d)
596-
copyto!(view(A, diagind(A,1)), M.du)
597-
copyto!(view(A, diagind(A,-1)), M.dl)
598-
else
599-
copyto!(A, M)
600-
end
589+
n = length(M.d)
590+
n == 0 && return A
591+
n > 2 && fill!(A, zero(T))
592+
for i in 1:n-1
593+
A[i,i] = M.d[i]
594+
A[i+1,i] = M.dl[i]
595+
A[i,i+1] = M.du[i]
596+
end
597+
A[n,n] = M.d[n]
601598
A
602599
end
603600
Matrix(M::Tridiagonal{T}) where {T} = Matrix{promote_type(T, typeof(zero(T)))}(M)

stdlib/LinearAlgebra/test/bidiag.jl

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -904,17 +904,4 @@ end
904904
@test mul!(C1, B, sv, 1, 2) == mul!(C2, B, v, 1 ,2)
905905
end
906906

907-
@testset "Matrix conversion for non-numeric and undef" begin
908-
B = Bidiagonal(Vector{BigInt}(undef, 4), fill(big(3), 3), :U)
909-
M = Matrix(B)
910-
B[diagind(B)] .= 4
911-
M[diagind(M)] .= 4
912-
@test diag(B) == diag(M)
913-
914-
B = Bidiagonal(fill(Diagonal([1,3]), 3), fill(Diagonal([1,3]), 2), :U)
915-
M = Matrix{eltype(B)}(B)
916-
@test M isa Matrix{eltype(B)}
917-
@test M == B
918-
end
919-
920907
end # module TestBidiagonal

stdlib/LinearAlgebra/test/diagonal.jl

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,17 +1298,4 @@ end
12981298
@test yadj == x'
12991299
end
13001300

1301-
@testset "Matrix conversion for non-numeric and undef" begin
1302-
D = Diagonal(Vector{BigInt}(undef, 4))
1303-
M = Matrix(D)
1304-
D[diagind(D)] .= 4
1305-
M[diagind(M)] .= 4
1306-
@test diag(D) == diag(M)
1307-
1308-
D = Diagonal(fill(Diagonal([1,3]), 2))
1309-
M = Matrix{eltype(D)}(D)
1310-
@test M isa Matrix{eltype(D)}
1311-
@test M == D
1312-
end
1313-
13141301
end # module TestDiagonal

stdlib/LinearAlgebra/test/special.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@ Random.seed!(1)
114114
Base.zero(x::Union{TypeWithoutZero, TypeWithZero}) = zero(typeof(x))
115115
Base.zero(::Type{<:Union{TypeWithoutZero, TypeWithZero}}) = TypeWithZero()
116116
LinearAlgebra.symmetric(::TypeWithoutZero, ::Symbol) = TypeWithoutZero()
117-
LinearAlgebra.symmetric_type(::Type{TypeWithoutZero}) = TypeWithoutZero
118-
Base.copy(A::TypeWithoutZero) = A
119117
Base.transpose(::TypeWithoutZero) = TypeWithoutZero()
120118
d = fill(TypeWithoutZero(), 3)
121119
du = fill(TypeWithoutZero(), 2)

stdlib/LinearAlgebra/test/tridiag.jl

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -859,12 +859,4 @@ end
859859
@test axes(B) === (ax, ax)
860860
end
861861

862-
@testset "Matrix conversion for non-numeric and undef" begin
863-
T = Tridiagonal(fill(big(3), 3), Vector{BigInt}(undef, 4), fill(big(3), 3))
864-
M = Matrix(T)
865-
T[diagind(T)] .= 4
866-
M[diagind(M)] .= 4
867-
@test diag(T) == diag(M)
868-
end
869-
870862
end # module TestTridiagonal

0 commit comments

Comments
 (0)