Skip to content

Commit

Permalink
Non-numeric eltypes in OneElement (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub authored Dec 3, 2023
1 parent ba796e8 commit 8c7ace3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
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.8.0"
version = "1.9.0"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
6 changes: 2 additions & 4 deletions src/FillArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,10 @@ const FillVecOrMat{T} = Union{FillVector{T},FillMatrix{T}}
Fill{T,N,Axes}(x, sz::Axes) where Axes<:Tuple{Vararg{AbstractUnitRange,N}} where {T, N} =
Fill{T,N,Axes}(convert(T, x)::T, sz)

Fill{T,0}(x::T, ::Tuple{}) where T = Fill{T,0,Tuple{}}(x, ()) # ambiguity fix
Fill{T,0}(x, ::Tuple{}) where T = Fill{T,0,Tuple{}}(convert(T, x)::T, ()) # ambiguity fix

@inline Fill{T, N}(x::T, sz::Axes) where Axes<:Tuple{Vararg{AbstractUnitRange,N}} where {T, N} =
Fill{T,N,Axes}(x, sz)
@inline Fill{T, N}(x, sz::Axes) where Axes<:Tuple{Vararg{AbstractUnitRange,N}} where {T, N} =
Fill{T,N}(convert(T, x)::T, sz)
Fill{T,N,Axes}(convert(T, x)::T, sz)

@inline Fill{T, N}(x, sz::SZ) where SZ<:Tuple{Vararg{Integer,N}} where {T, N} =
Fill{T,N}(x, oneto.(sz))
Expand Down
3 changes: 2 additions & 1 deletion src/oneelement.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ struct OneElement{T,N,I,A} <: AbstractArray{T,N}
val::T
ind::I
axes::A
OneElement(val::T, ind::I, axes::A) where {T<:Number, I<:NTuple{N,Int}, A<:NTuple{N,AbstractUnitRange}} where {N} = new{T,N,I,A}(val, ind, axes)
OneElement(val::T, ind::I, axes::A) where {T, I<:NTuple{N,Int}, A<:NTuple{N,AbstractUnitRange}} where {N} = new{T,N,I,A}(val, ind, axes)
OneElement(val::T, ind::Tuple{}, axes::Tuple{}) where {T} = new{T,0,Tuple{},Tuple{}}(val, ind, axes)
end

const OneElementVector{T,I,A} = OneElement{T,1,I,A}
Expand Down
19 changes: 17 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,18 @@ oneton(sz...) = oneton(Float64, sz...)


for T in (Int, Float64)
F = Fill{T}(one(T), 5)
F = Fill{T, 0}(2)
@test size(F) == ()
@test F[] === T(2)

F = Fill{T}(1, 5)

@test eltype(F) == T
@test Array(F) == fill(one(T),5)
@test Array{T}(F) == fill(one(T),5)
@test Array{T,1}(F) == fill(one(T),5)

F = Fill{T}(one(T), 5, 5)
F = Fill{T}(1, 5, 5)
@test eltype(F) == T
@test Array(F) == fill(one(T),5,5)
@test Array{T}(F) == fill(one(T),5,5)
Expand Down Expand Up @@ -1901,6 +1905,10 @@ end
end

@testset "OneElement" begin
A = OneElement(2, (), ())
@test A == Fill(2, ())
@test A[] === 2

e₁ = OneElement(2, 5)
@test e₁ == [0,1,0,0,0]
@test_throws BoundsError e₁[6]
Expand Down Expand Up @@ -1936,6 +1944,13 @@ end
@test Base.setindex(Zeros(5,3), 2, 2, 3) OneElement(2.0, (2,3), (5,3))
@test_throws BoundsError Base.setindex(Zeros(5), 2, 6)

@testset "non-numeric" begin
S = SMatrix{2,2}(1:4)
A = OneElement(S, (2,2), (2,2))
@test A[2,2] === S
@test A[1,1] === A[1,2] === A[2,1] === zero(S)
end

@testset "adjoint/transpose" begin
A = OneElement(3im, (2,4), (4,6))
@test A' === OneElement(-3im, (4,2), (6,4))
Expand Down

2 comments on commit 8c7ace3

@jishnub
Copy link
Member Author

@jishnub jishnub commented on 8c7ace3 Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/96421

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.9.0 -m "<description of version>" 8c7ace3bf2834fd62f8719e082c57a0a1159487f
git push origin v1.9.0

Please sign in to comment.