From 18c35304edaefabc7495835b5ca84ce6f4d1b9c0 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Tue, 5 Dec 2023 12:08:36 +0530 Subject: [PATCH 1/5] Remove unnecessary permutedims method --- src/fillalgebra.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/fillalgebra.jl b/src/fillalgebra.jl index f98ae605..ed4e2ca0 100644 --- a/src/fillalgebra.jl +++ b/src/fillalgebra.jl @@ -18,7 +18,6 @@ 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) From 33186155b3f503ff1779880610bd58eb0a0e0fcc Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Tue, 5 Dec 2023 12:43:49 +0530 Subject: [PATCH 2/5] Swap axes in permutedims --- src/fillalgebra.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/fillalgebra.jl b/src/fillalgebra.jl index ed4e2ca0..ff609adf 100644 --- a/src/fillalgebra.jl +++ b/src/fillalgebra.jl @@ -20,11 +20,11 @@ end permutedims(a::AbstractFillMatrix) = fillsimilar(a, reverse(axes(a))) -function permutedims(B::AbstractFill, perm) - dimsB = size(B) - ndimsB = length(dimsB) +Base.@constprop :aggressive 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 From 384967a7116e37146ad56fa9c353464693f42b93 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Tue, 5 Dec 2023 12:53:53 +0530 Subject: [PATCH 3/5] Constant propagation only on recent Julia versions --- src/fillalgebra.jl | 8 +++++++- test/runtests.jl | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/fillalgebra.jl b/src/fillalgebra.jl index ff609adf..6b3d41d1 100644 --- a/src/fillalgebra.jl +++ b/src/fillalgebra.jl @@ -20,7 +20,13 @@ end permutedims(a::AbstractFillMatrix) = fillsimilar(a, reverse(axes(a))) -Base.@constprop :aggressive function permutedims(B::AbstractFill, perm) +@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")) diff --git a/test/runtests.jl b/test/runtests.jl index 32cd2ed7..91c8535b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1512,6 +1512,21 @@ end @test F' == G' @test transpose(F) == transpose(G) end + + # test for inference only if aggressive constant propagation is available + 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))) + 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 From 0110df43ba34306b394061e4dc2bf6421489aade Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Tue, 5 Dec 2023 13:03:21 +0530 Subject: [PATCH 4/5] test for inference --- test/runtests.jl | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 91c8535b..f39d12ee 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) From 606f33a0aad42ccfae7c1c6d2ea8a28bf32268f7 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Tue, 5 Dec 2023 14:02:06 +0530 Subject: [PATCH 5/5] Update inference tests --- Project.toml | 5 ++++- test/runtests.jl | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index d0d3e609..317ff023 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/test/runtests.jl b/test/runtests.jl index f39d12ee..a4b6871a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1513,13 +1513,14 @@ end @test transpose(F) == transpose(G) end - # test for inference only if aggressive constant propagation is available 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))