From dab9cdfe7df2dccd140c4a16cc29a48dcf4c9aa6 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Mon, 5 Jun 2023 15:44:29 +0530 Subject: [PATCH] `AbstractArray` constructor for `OneElement` (#261) * AbstractArray constructor for OneElement * reduce duplication in tests * bump version to v1.2.0 --- Project.toml | 2 +- src/oneelement.jl | 2 ++ test/runtests.jl | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index b3a5b25b..7b99cabe 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "FillArrays" uuid = "1a297f60-69ca-5386-bcde-b61e274b549b" -version = "1.1.1" +version = "1.2.0" [deps] LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" diff --git a/src/oneelement.jl b/src/oneelement.jl index d0bacdd9..3efa1d72 100644 --- a/src/oneelement.jl +++ b/src/oneelement.jl @@ -45,6 +45,8 @@ function Base.getindex(A::OneElement{T,N}, kj::Vararg{Int,N}) where {T,N} ifelse(kj == A.ind, A.val, zero(T)) end +Base.AbstractArray{T,N}(A::OneElement{<:Any,N}) where {T,N} = OneElement(T(A.val), A.ind, A.axes) + Base.replace_in_print_matrix(o::OneElementVector, k::Integer, j::Integer, s::AbstractString) = o.ind == (k,) ? s : Base.replace_with_centered_mark(s) diff --git a/test/runtests.jl b/test/runtests.jl index 42e7dc7e..2b20a2e8 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1682,6 +1682,13 @@ end @test e₁ == [0,1,0,0,0] @test_throws BoundsError e₁[6] + f₁ = AbstractArray{Float64}(e₁) + @test f₁ isa OneElement{Float64,1} + @test f₁ == e₁ + fv₁ = AbstractVector{Float64}(e₁) + @test fv₁ isa OneElement{Float64,1} + @test fv₁ == e₁ + @test stringmime("text/plain", e₁) == "5-element OneElement{$Int, 1, Tuple{$Int}, Tuple{Base.OneTo{$Int}}}:\n ⋅\n 1\n ⋅\n ⋅\n ⋅" e₁ = OneElement{Float64}(2, 5) @@ -1693,6 +1700,13 @@ end V = OneElement(2, (2,3), (3,4)) @test V == [0 0 0 0; 0 0 2 0; 0 0 0 0] + Vf = AbstractArray{Float64}(V) + @test Vf isa OneElement{Float64,2} + @test Vf == V + VMf = AbstractMatrix{Float64}(V) + @test VMf isa OneElement{Float64,2} + @test VMf == V + @test stringmime("text/plain", V) == "3×4 OneElement{$Int, 2, Tuple{$Int, $Int}, Tuple{Base.OneTo{$Int}, Base.OneTo{$Int}}}:\n ⋅ ⋅ ⋅ ⋅\n ⋅ ⋅ 2 ⋅\n ⋅ ⋅ ⋅ ⋅" @test Base.setindex(Zeros(5), 2, 2) ≡ OneElement(2.0, 2, 5)