From a02ba9a1c54d887e6d0e83659a32ec9bdef703f5 Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 21 Mar 2023 09:37:40 +1300 Subject: [PATCH 1/4] Add similar_array_type for Linearalgebra.Diagonal --- src/implementations/LinearAlgebra.jl | 7 +++++++ test/interface.jl | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/implementations/LinearAlgebra.jl b/src/implementations/LinearAlgebra.jl index 3553c66..d684168 100644 --- a/src/implementations/LinearAlgebra.jl +++ b/src/implementations/LinearAlgebra.jl @@ -155,6 +155,13 @@ 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,Vector{S}} +end + 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} diff --git a/test/interface.jl b/test/interface.jl index 9e3e654..33ea409 100644 --- a/test/interface.jl +++ b/test/interface.jl @@ -6,6 +6,7 @@ using Test import MutableArithmetics as MA +import LinearAlgebra struct DummyMutable end @@ -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 From 4fc116998c571b17ddbc090e222ee483db4eb7c6 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Tue, 28 Nov 2023 10:51:15 +1300 Subject: [PATCH 2/4] Update LinearAlgebra.jl --- src/implementations/LinearAlgebra.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/implementations/LinearAlgebra.jl b/src/implementations/LinearAlgebra.jl index d684168..1678340 100644 --- a/src/implementations/LinearAlgebra.jl +++ b/src/implementations/LinearAlgebra.jl @@ -159,9 +159,11 @@ function similar_array_type( ::Type{LinearAlgebra.Diagonal{T,VT}}, ::Type{S}, ) where {S,T,VT<:AbstractVector{T}} - return LinearAlgebra.Diagonal{S,Vector{S}} + return LinearAlgebra.Diagonal{S,similar_array_type(VT, S)} end +similar_array_type(::Type{<:AbstractVector}, ::Type{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} From 3aa6ecfc343bdd3b4f5cb7db2686fa2535f62027 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Tue, 28 Nov 2023 12:59:28 +1300 Subject: [PATCH 3/4] Update src/implementations/LinearAlgebra.jl --- src/implementations/LinearAlgebra.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/implementations/LinearAlgebra.jl b/src/implementations/LinearAlgebra.jl index 1678340..92f761c 100644 --- a/src/implementations/LinearAlgebra.jl +++ b/src/implementations/LinearAlgebra.jl @@ -162,7 +162,7 @@ function similar_array_type( return LinearAlgebra.Diagonal{S,similar_array_type(VT, S)} end -similar_array_type(::Type{<:AbstractVector}, ::Type{T}} = Vector{T} +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} From c868bc543770ba7f19efd2b6e575b90867a7eae2 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Tue, 28 Nov 2023 13:02:17 +1300 Subject: [PATCH 4/4] Update src/implementations/LinearAlgebra.jl --- src/implementations/LinearAlgebra.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/implementations/LinearAlgebra.jl b/src/implementations/LinearAlgebra.jl index 92f761c..5415d22 100644 --- a/src/implementations/LinearAlgebra.jl +++ b/src/implementations/LinearAlgebra.jl @@ -163,7 +163,6 @@ function similar_array_type( 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}