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

AbstractArray constructor for OneElement #261

Merged
merged 4 commits into from
Jun 5, 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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 2 additions & 0 deletions src/oneelement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
14 changes: 14 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down