From 0f845bbed3c955d8d78cb4b25444ea5351a8e70e Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Tue, 24 May 2022 21:36:52 +0400 Subject: [PATCH 1/6] more IOR broadcasting --- Project.toml | 2 +- src/axes.jl | 31 +++++++++++++++++++++++++------ test/runtests.jl | 15 ++++++++++++++- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/Project.toml b/Project.toml index 89386a84..cc3ebcbe 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "OffsetArrays" uuid = "6fe1bfb0-de20-5000-8ca7-80f57d26f881" -version = "1.12.0" +version = "1.12.1" [deps] Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e" diff --git a/src/axes.jl b/src/axes.jl index 0422141f..1b530d88 100644 --- a/src/axes.jl +++ b/src/axes.jl @@ -246,12 +246,31 @@ for R in [:IIUR, :IdOffsetRange] end # offset-preserve broadcasting -Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(-), r::IdOffsetRange{T}, x::Integer) where T = - IdOffsetRange{T}(r.parent .- x, r.offset) -Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(+), r::IdOffsetRange{T}, x::Integer) where T = - IdOffsetRange{T}(r.parent .+ x, r.offset) -Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(+), x::Integer, r::IdOffsetRange{T}) where T = - IdOffsetRange{T}(x .+ r.parent, r.offset) +Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(-), r::IdOffsetRange) = + OffsetArray(.-UnitRange(r), r.offset) +Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(-), r::IdOffsetRange, x::Integer) = + IdOffsetRange(r.parent .- x, r.offset) +Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(-), x::Integer, r::IdOffsetRange) = + OffsetArray(x .- UnitRange(r), r.offset) +Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(+), r::IdOffsetRange, x::Integer) = + IdOffsetRange(r.parent .+ x, r.offset) +Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(+), x::Integer, r::IdOffsetRange) = + IdOffsetRange(x .+ r.parent, r.offset) +Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(*), x::Number, r::IdOffsetRange) = + OffsetArray(x .* UnitRange(r), r.offset) +Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(*), r::IdOffsetRange, x::Number) = + OffsetArray(UnitRange(r) .* x, r.offset) +# specialize for AbstractFloat to resolve ambiguity with OrdinalRange +Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(*), x::AbstractFloat, r::IdOffsetRange) = + OffsetArray(x .* UnitRange(r), r.offset) +Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(*), r::IdOffsetRange, x::AbstractFloat) = + OffsetArray(UnitRange(r) .* x, r.offset) +Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(\), x::Number, r::IdOffsetRange) = + OffsetArray(x .\ UnitRange(r), r.offset) +Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(/), r::IdOffsetRange, x::Number) = + OffsetArray(UnitRange(r) ./ x, r.offset) +Broadcast.broadcasted(::Base.Broadcast.DefaultArrayStyle{1}, ::typeof(big), r::IdOffsetRange) = + IdOffsetRange(big.(r.parent), r.offset) Base.show(io::IO, r::IdOffsetRange) = print(io, IdOffsetRange, "(values=",first(r), ':', last(r),", indices=",first(eachindex(r)),':',last(eachindex(r)), ")") diff --git a/test/runtests.jl b/test/runtests.jl index 078a95d3..2346b40f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -158,8 +158,21 @@ end # broadcasting behavior with scalars (issue #104) r3 = (1 .+ OffsetArrays.IdOffsetRange(3:5, -1) .+ 1) .- 1 + @test r3 isa OffsetArrays.IdOffsetRange @test same_value(r3, 3:5) - check_indexed_by(r3, 0:2) + check_indexed_by(r3, axes(r3,1)) + + r = OffsetArrays.IdOffsetRange(3:5, -1) + rc = copyto!(similar(r), r) + @test @inferred(broadcast(+, r, big(2))) == @inferred(broadcast(+, big(2), r)) == rc .+ big(2) + n = big(typemax(Int)) + 1 + @test @inferred(broadcast(+, r, n)) == @inferred(broadcast(+, n, r)) == rc .+ n + @test @inferred(broadcast(-, r)) == .-rc + @test @inferred(broadcast(-, r, big(2))) == rc .- big(2) + @test @inferred(broadcast(-, big(2), r)) == big(2) .- rc + @test @inferred(broadcast(*, r, 2)) == @inferred(broadcast(*, 2, r)) == rc .* 2 + @test @inferred(broadcast(/, r, 2)) == @inferred(broadcast(\, 2, r)) == rc ./ 2 + @test @inferred((r -> big.(r))(r)) == big.(rc) @testset "Idempotent indexing" begin @testset "Indexing into an IdOffsetRange" begin From 36c841cd2f794818f7718a8966e6ffe910df9977 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Tue, 24 May 2022 21:50:43 +0400 Subject: [PATCH 2/6] Add test for Float64 mul --- test/runtests.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/runtests.jl b/test/runtests.jl index 2346b40f..4b33ad04 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -171,6 +171,7 @@ end @test @inferred(broadcast(-, r, big(2))) == rc .- big(2) @test @inferred(broadcast(-, big(2), r)) == big(2) .- rc @test @inferred(broadcast(*, r, 2)) == @inferred(broadcast(*, 2, r)) == rc .* 2 + @test @inferred(broadcast(*, r, 2.5)) == @inferred(broadcast(*, 2.5, r)) == rc .* 2.5 @test @inferred(broadcast(/, r, 2)) == @inferred(broadcast(\, 2, r)) == rc ./ 2 @test @inferred((r -> big.(r))(r)) == big.(rc) From 45f71d5a3889c21d793d1031bb7e3e8301218d06 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Tue, 24 May 2022 23:46:25 +0400 Subject: [PATCH 3/6] tests with BigInt/BigFloat --- test/runtests.jl | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 4b33ad04..171f6ccd 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -164,16 +164,20 @@ end r = OffsetArrays.IdOffsetRange(3:5, -1) rc = copyto!(similar(r), r) - @test @inferred(broadcast(+, r, big(2))) == @inferred(broadcast(+, big(2), r)) == rc .+ big(2) - n = big(typemax(Int)) + 1 + n = big(typemax(Int)) @test @inferred(broadcast(+, r, n)) == @inferred(broadcast(+, n, r)) == rc .+ n @test @inferred(broadcast(-, r)) == .-rc - @test @inferred(broadcast(-, r, big(2))) == rc .- big(2) - @test @inferred(broadcast(-, big(2), r)) == big(2) .- rc - @test @inferred(broadcast(*, r, 2)) == @inferred(broadcast(*, 2, r)) == rc .* 2 - @test @inferred(broadcast(*, r, 2.5)) == @inferred(broadcast(*, 2.5, r)) == rc .* 2.5 - @test @inferred(broadcast(/, r, 2)) == @inferred(broadcast(\, 2, r)) == rc ./ 2 - @test @inferred((r -> big.(r))(r)) == big.(rc) + @test @inferred(broadcast(big, r)) == big.(rc) + for n in Any[2, big(2)] + @test @inferred(broadcast(+, r, n)) == @inferred(broadcast(+, n, r)) == rc .+ n + @test @inferred(broadcast(-, r, n)) == rc .- n + @test @inferred(broadcast(-, n, r)) == n .- rc + @test @inferred(broadcast(*, r, n)) == @inferred(broadcast(*, n, r)) == rc .* n + @test @inferred(broadcast(/, r, n)) == @inferred(broadcast(\, n, r)) == rc ./ n + end + for n in Any[2.5, big(5)/2] + @test @inferred(broadcast(*, r, n)) == @inferred(broadcast(*, n, r)) == rc .* n + end @testset "Idempotent indexing" begin @testset "Indexing into an IdOffsetRange" begin From 8a8f20bcc0605c3d861f502a8b740572ee536ed8 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Wed, 25 May 2022 00:01:12 +0400 Subject: [PATCH 4/6] test with typemax --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 171f6ccd..7ccb960c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -168,7 +168,7 @@ end @test @inferred(broadcast(+, r, n)) == @inferred(broadcast(+, n, r)) == rc .+ n @test @inferred(broadcast(-, r)) == .-rc @test @inferred(broadcast(big, r)) == big.(rc) - for n in Any[2, big(2)] + for n in Any[typemax(Int), big(typemax(Int))] @test @inferred(broadcast(+, r, n)) == @inferred(broadcast(+, n, r)) == rc .+ n @test @inferred(broadcast(-, r, n)) == rc .- n @test @inferred(broadcast(-, n, r)) == n .- rc From 13373dfadfc4296c84d4e88b9f7ce0efd0b1d613 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Wed, 25 May 2022 00:29:57 +0400 Subject: [PATCH 5/6] version limit division tests --- .github/workflows/UnitTest.yml | 2 +- test/runtests.jl | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.github/workflows/UnitTest.yml b/.github/workflows/UnitTest.yml index 30275ad3..94429c37 100644 --- a/.github/workflows/UnitTest.yml +++ b/.github/workflows/UnitTest.yml @@ -26,7 +26,7 @@ jobs: # https://github.com/actions/toolkit/issues/399 fail-fast: false matrix: - julia-version: ['1.0', '1', 'nightly'] + julia-version: ['1.0', '1.6', '1', 'nightly'] os: [ubuntu-latest, windows-latest, macOS-latest] julia-arch: [x64] # only test one 32-bit job diff --git a/test/runtests.jl b/test/runtests.jl index 7ccb960c..c8d0a134 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -168,14 +168,17 @@ end @test @inferred(broadcast(+, r, n)) == @inferred(broadcast(+, n, r)) == rc .+ n @test @inferred(broadcast(-, r)) == .-rc @test @inferred(broadcast(big, r)) == big.(rc) - for n in Any[typemax(Int), big(typemax(Int))] + for n in Any[2, big(typemax(Int))] @test @inferred(broadcast(+, r, n)) == @inferred(broadcast(+, n, r)) == rc .+ n @test @inferred(broadcast(-, r, n)) == rc .- n @test @inferred(broadcast(-, n, r)) == n .- rc @test @inferred(broadcast(*, r, n)) == @inferred(broadcast(*, n, r)) == rc .* n - @test @inferred(broadcast(/, r, n)) == @inferred(broadcast(\, n, r)) == rc ./ n + if VERSION >= v"1.6.0" + # this test fails on v1.0 due to a bug in evaluating (3:5) / big(2) + @test @inferred(broadcast(/, r, n)) == @inferred(broadcast(\, n, r)) == rc ./ n + end end - for n in Any[2.5, big(5)/2] + for n in Any[2.0, big(2.0)] @test @inferred(broadcast(*, r, n)) == @inferred(broadcast(*, n, r)) == rc .* n end From 1fc87209e5e0bfbc0800f7b994e919138095eef5 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Wed, 25 May 2022 00:35:50 +0400 Subject: [PATCH 6/6] avoid bigfloat test on v1.0 --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index c8d0a134..217a96bf 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -178,7 +178,7 @@ end @test @inferred(broadcast(/, r, n)) == @inferred(broadcast(\, n, r)) == rc ./ n end end - for n in Any[2.0, big(2.0)] + for n in (VERSION >= v"1.6.0" ? Any[2.0, big(2.0)] : Any[2.0]) @test @inferred(broadcast(*, r, n)) == @inferred(broadcast(*, n, r)) == rc .* n end