From 663770723cea962efe6834d250a7f2ce90680289 Mon Sep 17 00:00:00 2001 From: Sheehan Olver Date: Tue, 8 Sep 2020 20:15:41 +0100 Subject: [PATCH] Switch to broadcast notation for segment (#50) * Switch to broadcast notation for segment * map_domain overload --- Project.toml | 4 ++-- src/Domains/Segment.jl | 30 ++++++++++++++++++++++++------ test/runtests.jl | 7 +++++++ 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/Project.toml b/Project.toml index cfc6a839..7ab3387d 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "ApproxFunBase" uuid = "fbd15aa5-315a-5a7d-a8a4-24992e37be05" -version = "0.3.7" +version = "0.3.8" [deps] AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c" @@ -33,7 +33,7 @@ BlockArrays = "0.12.11" BlockBandedMatrices = "0.7, 0.8, 0.9" Calculus = "0.5" DSP = "0.6" -DomainSets = "0.3, 0.4" +DomainSets = "0.4" DualNumbers = "0.6.2" FFTW = "0.3, 1" FastGaussQuadrature = "0.4" diff --git a/src/Domains/Segment.jl b/src/Domains/Segment.jl index 4ac4eebf..0eb16b0f 100644 --- a/src/Domains/Segment.jl +++ b/src/Domains/Segment.jl @@ -113,22 +113,40 @@ end for op in (:*,:+,:-) @eval begin - $op(c::Number,d::Segment) = Segment($op(c,leftendpoint(d)),$op(c,rightendpoint(d))) - $op(d::Segment,c::Number) = Segment($op(leftendpoint(d),c),$op(rightendpoint(d),c)) + $op(c::Number,d::Segment) = broadcast($op,c,d) + $op(d::Segment,c::Number) = broadcast($op,d,c) + broadcasted(::typeof($op), c::Number, d::Segment) = Segment($op(c,leftendpoint(d)),$op(c,rightendpoint(d))) + broadcasted(::typeof($op), d::Segment, c::Number) = Segment($op(leftendpoint(d),c),$op(rightendpoint(d),c)) end end -broadcast(::typeof(^),c::Number,d::Segment) = Segment(c^leftendpoint(d),c^rightendpoint(d)) -broadcast(::typeof(^),d::Segment,c::Number) = Segment(leftendpoint(d)^c,rightendpoint(d)^c) +broadcasted(::typeof(^),c::Number,d::Segment) = Segment(c^leftendpoint(d),c^rightendpoint(d)) +function broadcasted(::typeof(^),d::Segment,c::Number) + a,b = endpoints(d) + if a < 0 < b + Segment(0, b^c) + elseif b < 0 < a + Segment(a^c, 0) + else + Segment(a^c,b^c) + end +end -/(d::Segment,c::Number) = Segment(leftendpoint(d)/c,rightendpoint(d)/c) +broadcasted(::typeof(Base.literal_pow), ::typeof(^), d::Segment, ::Val{K}) where K = + broadcasted(^, d, K) +/(d::Segment,c::Number) = broadcast(/,d,c) +broadcasted(::typeof(/), d::Segment,c::Number) = Segment(leftendpoint(d)/c,rightendpoint(d)/c) -sqrt(d::Segment)=Segment(sqrt(leftendpoint(d)),sqrt(rightendpoint(d))) +sqrt(d::Segment) = broadcast(sqrt, d) +broadcasted(::typeof(sqrt), d::Segment)=Segment(sqrt(leftendpoint(d)),sqrt(rightendpoint(d))) +(d1::Segment,d2::Segment)=Segment(d1.a+d2.a,d1.b+d2.b) +broadcasted(::typeof(+),d1::Segment,d2::Segment) = Segment(d1.a+d2.a,d1.b+d2.b) +DomainSets.map_domain(map::DomainSets.AbstractAffineMap, domain::AbstractSegment) = + Segment(map(leftendpoint(domain)),map(rightendpoint(domain))) ## intersect/union diff --git a/test/runtests.jl b/test/runtests.jl index 90a8d95f..436e35d6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -41,6 +41,13 @@ end @test isambiguous(ApproxFunBase.Point(ApproxFunBase.AnyDomain())) @test_skip ApproxFunBase.Point(NaN) == ApproxFunBase.Point(NaN) + + @test Segment(-1,1) .+ 1 ≡ Segment(0,2) + @test 2 .* Segment(-1,1) .+ 1 ≡ Segment(-1,3) + @test Segment(-1,1) .^ 2 ≡ Segment(0,1) + @test Segment(1,-1) .^ 2 ≡ Segment(1,0) + @test Segment(1,2) .^ 2 ≡ Segment(1,4) + @test sqrt.(Segment(1,2)) ≡ Segment(1,sqrt(2)) end @time include("MatrixTest.jl")