Skip to content

Switch to broadcast notation for segment #50

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

Merged
merged 2 commits into from
Sep 8, 2020
Merged
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
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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"
Expand Down
30 changes: 24 additions & 6 deletions src/Domains/Segment.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down