Skip to content

Commit

Permalink
Use axes function instead of field access (#320)
Browse files Browse the repository at this point in the history
* Use axes function instead of field access

* Don't change for FillMatrix

* Add tests for custom structs

* Typo
  • Loading branch information
jishnub authored Feb 27, 2024
1 parent 9482273 commit 2784902
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/fillalgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ for OP in (:transpose, :adjoint)
end

permutedims(a::AbstractFillVector) = fillsimilar(a, (1, length(a)))
permutedims(a::AbstractFillMatrix) = fillsimilar(a, reverse(a.axes))
permutedims(a::AbstractFillMatrix) = fillsimilar(a, reverse(axes(a)))

function permutedims(B::AbstractFill, perm)
dimsB = size(B)
Expand Down
8 changes: 4 additions & 4 deletions src/fillbroadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ broadcasted(::DefaultArrayStyle, ::typeof(+), r::AbstractOnes) = r

broadcasted(::DefaultArrayStyle{N}, ::typeof(conj), r::AbstractZeros{T,N}) where {T,N} = r
broadcasted(::DefaultArrayStyle{N}, ::typeof(conj), r::AbstractOnes{T,N}) where {T,N} = r
broadcasted(::DefaultArrayStyle{N}, ::typeof(real), r::AbstractZeros{T,N}) where {T,N} = Zeros{real(T)}(r.axes)
broadcasted(::DefaultArrayStyle{N}, ::typeof(real), r::AbstractOnes{T,N}) where {T,N} = Ones{real(T)}(r.axes)
broadcasted(::DefaultArrayStyle{N}, ::typeof(imag), r::AbstractZeros{T,N}) where {T,N} = Zeros{real(T)}(r.axes)
broadcasted(::DefaultArrayStyle{N}, ::typeof(imag), r::AbstractOnes{T,N}) where {T,N} = Zeros{real(T)}(r.axes)
broadcasted(::DefaultArrayStyle{N}, ::typeof(real), r::AbstractZeros{T,N}) where {T,N} = Zeros{real(T)}(axes(r))
broadcasted(::DefaultArrayStyle{N}, ::typeof(real), r::AbstractOnes{T,N}) where {T,N} = Ones{real(T)}(axes(r))
broadcasted(::DefaultArrayStyle{N}, ::typeof(imag), r::AbstractZeros{T,N}) where {T,N} = Zeros{real(T)}(axes(r))
broadcasted(::DefaultArrayStyle{N}, ::typeof(imag), r::AbstractOnes{T,N}) where {T,N} = Zeros{real(T)}(axes(r))

### Binary broadcasting

Expand Down
27 changes: 27 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2012,6 +2012,33 @@ end
@test FillArrays.getindex_value(transpose(a)) == FillArrays.unique_value(transpose(a)) == 2.0 + im
@test convert(Fill, transpose(a)) Fill(2.0+im,1,5)
end

@testset "custom AbstractFill types" begin
# implicit axes
struct StaticZerosVec{L,T} <: FillArrays.AbstractZeros{T,1,Tuple{SOneTo{L}}} end
Base.size(::StaticZerosVec{L}) where {L} = (L,)
Base.axes(::StaticZerosVec{L}) where {L} = (SOneTo(L),)
S = StaticZerosVec{3,Int}()
@test real.(S) == S
@test imag.(S) == S

struct StaticOnesVec{L,T} <: FillArrays.AbstractOnes{T,1,Tuple{SOneTo{L}}} end
Base.size(::StaticOnesVec{L}) where {L} = (L,)
Base.axes(::StaticOnesVec{L}) where {L} = (SOneTo(L),)
S = StaticOnesVec{3,Int}()
@test real.(S) == S
@test imag.(S) == zero(S)

struct StaticFill{S1,S2,T} <: FillArrays.AbstractFill{T,2,Tuple{SOneTo{S1},SOneTo{S2}}}
x :: T
end
StaticFill{S1,S2}(x::T) where {S1,S2,T} = StaticFill{S1,S2,T}(x)
Base.size(::StaticFill{S1,S2}) where {S1,S2} = (S1,S2)
Base.axes(::StaticFill{S1,S2}) where {S1,S2} = (SOneTo(S1), SOneTo(S2))
FillArrays.getindex_value(S::StaticFill) = S.x
S = StaticFill{2,3}(2)
@test permutedims(S) == Fill(2, reverse(size(S)))
end
end

@testset "Statistics" begin
Expand Down

0 comments on commit 2784902

Please sign in to comment.