diff --git a/Project.toml b/Project.toml index d0d3e60..317ff02 100644 --- a/Project.toml +++ b/Project.toml @@ -20,6 +20,7 @@ FillArraysStatisticsExt = "Statistics" [compat] Aqua = "0.8" +Base64 = "1" Documenter = "1" Infinities = "0.1" LinearAlgebra = "1.6" @@ -35,6 +36,7 @@ julia = "1.6" [extras] Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" +Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" Infinities = "e1ba4f0e-776d-440f-acd9-e1d2e9742647" PDMats = "90014a1f-27ba-587c-ab20-58faa44d9150" @@ -47,4 +49,5 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["Aqua", "Test", "Infinities", "PDMats", "ReverseDiff", "SparseArrays", "StaticArrays", "Statistics", "Quaternions", "Documenter", "Random"] +test = ["Aqua", "Base64", "Test", "Infinities", "PDMats", "ReverseDiff", "SparseArrays", "StaticArrays", "Statistics", "Quaternions", "Documenter", "Random"] + diff --git a/src/fillalgebra.jl b/src/fillalgebra.jl index f98ae60..6b3d41d 100644 --- a/src/fillalgebra.jl +++ b/src/fillalgebra.jl @@ -18,14 +18,19 @@ for OP in (:transpose, :adjoint) end end -permutedims(a::AbstractFillVector) = fillsimilar(a, (1, length(a))) permutedims(a::AbstractFillMatrix) = fillsimilar(a, reverse(axes(a))) -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) +else + permutedims(B::AbstractFill, perm) = _permutedims(B, perm) +end + +@inline function _permutedims(B::AbstractFill, perm) + dimsB = axes(B) + ndimsB = ndims(B) (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) fillsimilar(B, dimsP) end diff --git a/test/runtests.jl b/test/runtests.jl index 32cd2ed..a4b6871 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1488,17 +1488,17 @@ end @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) + @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) @testset "recursive" begin S = SMatrix{2,3}(1:6) @@ -1512,6 +1512,22 @@ end @test F' == G' @test transpose(F) == transpose(G) end + + H = if VERSION >= v"1.8" + @inferred(permutedims(Fill(2, (SOneTo(2), SOneTo(3))))) + else + permutedims(Fill(2, (SOneTo(2), SOneTo(3)))) + end + @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