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

Preserve axes in permutedims #319

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 12 additions & 6 deletions src/fillalgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@
end
end

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

Check warning on line 21 in src/fillalgebra.jl

View check run for this annotation

Codecov / codecov/patch

src/fillalgebra.jl#L21

Added line #L21 was not covered by tests

function permutedims(B::AbstractFill, perm)
dimsB = size(B)
ndimsB = length(dimsB)

@static if VERSION >= v"1.9"
Base.@constprop :aggressive permutedims(B::AbstractFill, perm) = _permutedims(B, perm)

Check warning on line 25 in src/fillalgebra.jl

View check run for this annotation

Codecov / codecov/patch

src/fillalgebra.jl#L25

Added line #L25 was not covered by tests
else
permutedims(B::AbstractFill, perm) = _permutedims(B, perm)

Check warning on line 27 in src/fillalgebra.jl

View check run for this annotation

Codecov / codecov/patch

src/fillalgebra.jl#L27

Added line #L27 was not covered by tests
end

@inline function _permutedims(B::AbstractFill, perm)
dimsB = axes(B)
ndimsB = ndims(B)

Check warning on line 32 in src/fillalgebra.jl

View check run for this annotation

Codecov / codecov/patch

src/fillalgebra.jl#L30-L32

Added lines #L30 - L32 were not covered by tests
(ndimsB == length(perm) && isperm(perm)) || throw(ArgumentError("no valid permutation of dimensions"))
dimsP = ntuple(i->dimsB[perm[i]], ndimsB)::typeof(dimsB)
dimsP = ntuple(i->dimsB[perm[i]], ndimsB)

Check warning on line 34 in src/fillalgebra.jl

View check run for this annotation

Codecov / codecov/patch

src/fillalgebra.jl#L34

Added line #L34 was not covered by tests
fillsimilar(B, dimsP)
end

Expand Down
38 changes: 25 additions & 13 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1458,19 +1458,7 @@ end
@test copy(transpose(Ones(5))) ≡ transpose(Ones(5))
@test Fill([1+im 2; 3 4; 5 6], 2,3)' == Fill([1+im 2; 3 4; 5 6]', 3,2)
@test transpose(Fill([1+im 2; 3 4; 5 6], 2,3)) == Fill(transpose([1+im 2; 3 4; 5 6]), 3,2)

@test permutedims(Ones(10)) ≡ Ones(1,10)
@test permutedims(Zeros(10)) ≡ Zeros(1,10)
@test permutedims(Fill(2.0,10)) ≡ Fill(2.0,1,10)
@test permutedims(Ones(10,3)) ≡ Ones(3,10)
@test permutedims(Zeros(10,3)) ≡ Zeros(3,10)
@test permutedims(Fill(2.0,10,3)) ≡ Fill(2.0,3,10)

@test permutedims(Ones(2,4,5), [3,2,1]) == permutedims(Array(Ones(2,4,5)), [3,2,1])
@test permutedims(Ones(2,4,5), [3,2,1]) ≡ Ones(5,4,2)
@test permutedims(Zeros(2,4,5), [3,2,1]) ≡ Zeros(5,4,2)
@test permutedims(Fill(2.0,2,4,5), [3,2,1]) ≡ Fill(2.0,5,4,2)


@testset "recursive" begin
S = SMatrix{2,3}(1:6)
Z = Zeros(typeof(S), 2, 3)
Expand All @@ -1483,6 +1471,30 @@ end
@test F' == G'
@test transpose(F) == transpose(G)
end

@test @inferred(permutedims(Ones(10))) ≡ Ones(1,10)
@test @inferred(permutedims(Zeros(10))) ≡ Zeros(1,10)
@test @inferred(permutedims(Fill(2.0,10))) ≡ Fill(2.0,1,10)
@test @inferred(permutedims(Ones(10,3))) ≡ Ones(3,10)
@test @inferred(permutedims(Zeros(10,3))) ≡ Zeros(3,10)
@test @inferred(permutedims(Fill(2.0,10,3))) ≡ Fill(2.0,3,10)

@test @inferred(permutedims(Ones(2,4,5), [3,2,1])) == permutedims(Array(Ones(2,4,5)), [3,2,1])
@test @inferred(permutedims(Ones(2,4,5), [3,2,1])) ≡ Ones(5,4,2)
@test @inferred(permutedims(Zeros(2,4,5), [3,2,1])) ≡ Zeros(5,4,2)
@test @inferred(permutedims(Fill(2.0,2,4,5), [3,2,1])) ≡ Fill(2.0,5,4,2)

H = @inferred(permutedims(Fill(2, (SOneTo(2), SOneTo(3)))))
@test H === Fill(2, (SOneTo(3), SOneTo(2)))

# test for inference only if aggressive constant propagation is available
F = Fill(2, (SOneTo(2), SOneTo(3), SOneTo(1)))
H = if VERSION >= v"1.8"
@inferred((F -> permutedims(F, (3,1,2)))(F))
else
(F -> permutedims(F, (3,1,2)))(F)
end
@test H === Fill(2, (SOneTo(1), SOneTo(2), SOneTo(3)))
end

@testset "reverse" begin
Expand Down
Loading