Skip to content

Commit c838f91

Browse files
authored
Add similar_array_type for LinearAlgebra.Diagonal (#205)
1 parent 3a6c406 commit c838f91

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/implementations/LinearAlgebra.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@ function similar_array_type(
155155
return LinearAlgebra.Symmetric{S,similar_array_type(MT, S)}
156156
end
157157

158+
function similar_array_type(
159+
::Type{LinearAlgebra.Diagonal{T,VT}},
160+
::Type{S},
161+
) where {S,T,VT<:AbstractVector{T}}
162+
return LinearAlgebra.Diagonal{S,similar_array_type(VT, S)}
163+
end
164+
165+
similar_array_type(::Type{<:AbstractVector}, ::Type{T}) where {T} = Vector{T}
158166
similar_array_type(::Type{Array{T,N}}, ::Type{S}) where {S,T,N} = Array{S,N}
159167

160168
similar_array_type(::Type{BitArray{N}}, ::Type{S}) where {S,N} = Array{S,N}

test/interface.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
using Test
88
import MutableArithmetics as MA
9+
import LinearAlgebra
910

1011
struct DummyMutable end
1112

@@ -142,3 +143,15 @@ end
142143
@test MA.similar_array_type(BitArray{2}, Int) == Array{Int,2}
143144
@test MA.similar_array_type(BitArray{2}, Bool) == BitArray{2}
144145
end
146+
147+
@testset "similar_array_type_Diagonal" begin
148+
z = zeros(2, 2)
149+
y = MA.operate!!(MA.add_mul, z, big(1), LinearAlgebra.I(2))
150+
@test y == BigFloat[1 0; 0 1]
151+
y = MA.operate!!(MA.add_mul, z, 2.4, LinearAlgebra.I(2))
152+
@test y === z
153+
@test y == Float64[2.4 0; 0 2.4]
154+
z = zeros(2, 2)
155+
y = MA.operate!!(MA.add_mul, z, 2.4, LinearAlgebra.Diagonal(1:2))
156+
@test y == LinearAlgebra.Diagonal(2.4 * (1:2))
157+
end

0 commit comments

Comments
 (0)