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

Add similar_array_type for LinearAlgebra.Diagonal #205

Merged
merged 4 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions src/implementations/LinearAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ function similar_array_type(
return LinearAlgebra.Symmetric{S,similar_array_type(MT, S)}
end

function similar_array_type(
::Type{LinearAlgebra.Diagonal{T,VT}},
::Type{S},
) where {S,T,VT<:AbstractVector{T}}
return LinearAlgebra.Diagonal{S,similar_array_type(VT, S)}
end

similar_array_type(::Type{<:AbstractVector}, ::Type{T}) where {T} = Vector{T}
similar_array_type(::Type{Array{T,N}}, ::Type{S}) where {S,T,N} = Array{S,N}

similar_array_type(::Type{BitArray{N}}, ::Type{S}) where {S,N} = Array{S,N}
Expand Down
13 changes: 13 additions & 0 deletions test/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using Test
import MutableArithmetics as MA
import LinearAlgebra

struct DummyMutable end

Expand Down Expand Up @@ -142,3 +143,15 @@ end
@test MA.similar_array_type(BitArray{2}, Int) == Array{Int,2}
@test MA.similar_array_type(BitArray{2}, Bool) == BitArray{2}
end

@testset "similar_array_type_Diagonal" begin
z = zeros(2, 2)
y = MA.operate!!(MA.add_mul, z, big(1), LinearAlgebra.I(2))
@test y == BigFloat[1 0; 0 1]
y = MA.operate!!(MA.add_mul, z, 2.4, LinearAlgebra.I(2))
@test y === z
@test y == Float64[2.4 0; 0 2.4]
z = zeros(2, 2)
y = MA.operate!!(MA.add_mul, z, 2.4, LinearAlgebra.Diagonal(1:2))
@test y == LinearAlgebra.Diagonal(2.4 * (1:2))
end