Skip to content

Commit

Permalink
Switch to broadcast notation for segment (#50)
Browse files Browse the repository at this point in the history
* Switch to broadcast notation for segment

* map_domain overload
  • Loading branch information
dlfivefifty authored Sep 8, 2020
1 parent a04cbd7 commit 6637707
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
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

2 comments on commit 6637707

@dlfivefifty
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/21072

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.8 -m "<description of version>" 663770723cea962efe6834d250a7f2ce90680289
git push origin v0.3.8

Please sign in to comment.