Skip to content

Commit 6637707

Browse files
authored
Switch to broadcast notation for segment (#50)
* Switch to broadcast notation for segment * map_domain overload
1 parent a04cbd7 commit 6637707

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ApproxFunBase"
22
uuid = "fbd15aa5-315a-5a7d-a8a4-24992e37be05"
3-
version = "0.3.7"
3+
version = "0.3.8"
44

55
[deps]
66
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"
@@ -33,7 +33,7 @@ BlockArrays = "0.12.11"
3333
BlockBandedMatrices = "0.7, 0.8, 0.9"
3434
Calculus = "0.5"
3535
DSP = "0.6"
36-
DomainSets = "0.3, 0.4"
36+
DomainSets = "0.4"
3737
DualNumbers = "0.6.2"
3838
FFTW = "0.3, 1"
3939
FastGaussQuadrature = "0.4"

src/Domains/Segment.jl

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,22 +113,40 @@ end
113113

114114
for op in (:*,:+,:-)
115115
@eval begin
116-
$op(c::Number,d::Segment) = Segment($op(c,leftendpoint(d)),$op(c,rightendpoint(d)))
117-
$op(d::Segment,c::Number) = Segment($op(leftendpoint(d),c),$op(rightendpoint(d),c))
116+
$op(c::Number,d::Segment) = broadcast($op,c,d)
117+
$op(d::Segment,c::Number) = broadcast($op,d,c)
118+
broadcasted(::typeof($op), c::Number, d::Segment) = Segment($op(c,leftendpoint(d)),$op(c,rightendpoint(d)))
119+
broadcasted(::typeof($op), d::Segment, c::Number) = Segment($op(leftendpoint(d),c),$op(rightendpoint(d),c))
118120
end
119121
end
120122

121-
broadcast(::typeof(^),c::Number,d::Segment) = Segment(c^leftendpoint(d),c^rightendpoint(d))
122-
broadcast(::typeof(^),d::Segment,c::Number) = Segment(leftendpoint(d)^c,rightendpoint(d)^c)
123+
broadcasted(::typeof(^),c::Number,d::Segment) = Segment(c^leftendpoint(d),c^rightendpoint(d))
124+
function broadcasted(::typeof(^),d::Segment,c::Number)
125+
a,b = endpoints(d)
126+
if a < 0 < b
127+
Segment(0, b^c)
128+
elseif b < 0 < a
129+
Segment(a^c, 0)
130+
else
131+
Segment(a^c,b^c)
132+
end
133+
end
123134

124-
/(d::Segment,c::Number) = Segment(leftendpoint(d)/c,rightendpoint(d)/c)
135+
broadcasted(::typeof(Base.literal_pow), ::typeof(^), d::Segment, ::Val{K}) where K =
136+
broadcasted(^, d, K)
125137

138+
/(d::Segment,c::Number) = broadcast(/,d,c)
139+
broadcasted(::typeof(/), d::Segment,c::Number) = Segment(leftendpoint(d)/c,rightendpoint(d)/c)
126140

127-
sqrt(d::Segment)=Segment(sqrt(leftendpoint(d)),sqrt(rightendpoint(d)))
141+
sqrt(d::Segment) = broadcast(sqrt, d)
142+
broadcasted(::typeof(sqrt), d::Segment)=Segment(sqrt(leftendpoint(d)),sqrt(rightendpoint(d)))
128143

129144
+(d1::Segment,d2::Segment)=Segment(d1.a+d2.a,d1.b+d2.b)
145+
broadcasted(::typeof(+),d1::Segment,d2::Segment) = Segment(d1.a+d2.a,d1.b+d2.b)
130146

131147

148+
DomainSets.map_domain(map::DomainSets.AbstractAffineMap, domain::AbstractSegment) =
149+
Segment(map(leftendpoint(domain)),map(rightendpoint(domain)))
132150

133151
## intersect/union
134152

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ end
4141
@test isambiguous(ApproxFunBase.Point(ApproxFunBase.AnyDomain()))
4242

4343
@test_skip ApproxFunBase.Point(NaN) == ApproxFunBase.Point(NaN)
44+
45+
@test Segment(-1,1) .+ 1 Segment(0,2)
46+
@test 2 .* Segment(-1,1) .+ 1 Segment(-1,3)
47+
@test Segment(-1,1) .^ 2 Segment(0,1)
48+
@test Segment(1,-1) .^ 2 Segment(1,0)
49+
@test Segment(1,2) .^ 2 Segment(1,4)
50+
@test sqrt.(Segment(1,2)) Segment(1,sqrt(2))
4451
end
4552

4653
@time include("MatrixTest.jl")

0 commit comments

Comments
 (0)