From aa97596e3783bb827c903b445151e99bbd7e3854 Mon Sep 17 00:00:00 2001 From: anj1 Date: Mon, 19 Oct 2015 09:14:30 +1300 Subject: [PATCH 1/5] Change FloatingPoint to AbstractFloat --- REQUIRE | 1 + src/Geometry2D.jl | 5 +++-- src/line.jl | 12 ++++++------ src/point.jl | 10 +++++----- src/random.jl | 28 ++++++++++++++-------------- 5 files changed, 29 insertions(+), 27 deletions(-) diff --git a/REQUIRE b/REQUIRE index 8e79a50..475b6ed 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,2 +1,3 @@ PyPlot Elliptic +Compat \ No newline at end of file diff --git a/src/Geometry2D.jl b/src/Geometry2D.jl index 203d4d4..20473e9 100644 --- a/src/Geometry2D.jl +++ b/src/Geometry2D.jl @@ -1,5 +1,6 @@ module Geometry2D +using Compat using PyPlot import PyPlot: plot, fill, polar @@ -19,11 +20,11 @@ abstract G2dSimpleObject <: G2dObject # objects for which area,perimeter,isin, m abstract G2dCompoundObject <: G2dObject # automated promotion rules for arrays of numbers -promote_rule{T<:Integer,S<:FloatingPoint}(::Type{Array{T,1}}, ::Type{Array{S,1}} ) = Array{S,1} +promote_rule{T<:Integer,S<:AbstractFloat}(::Type{Array{T,1}}, ::Type{Array{S,1}} ) = Array{S,1} promote_rule{T<:Integer}(::Type{Array{Rational{T},1}}, ::Type{Array{T,1}}) = Array{Rational{T},1} promote_rule{T<:Integer,S<:Integer}(::Type{Array{Rational{T},1}}, ::Type{Array{S,1}}) = Array{Rational{promote_type(T,S)},1} promote_rule{T<:Integer,S<:Integer}(::Type{Array{Rational{T},1}}, ::Type{Array{Rational{S},1}}) = Array{Rational{promote_type(T,S)},1} -promote_rule{T<:Integer,S<:FloatingPoint}(::Type{Array{Rational{T},1}}, ::Type{Array{S,1}}) = Array{promote_type(T,S), 1} +promote_rule{T<:Integer,S<:AbstractFloat}(::Type{Array{Rational{T},1}}, ::Type{Array{S,1}}) = Array{promote_type(T,S), 1} # should probably put something in here for complex numbers too tolerance = eps() # global variable for tolerance of many operations in floating point diff --git a/src/line.jl b/src/line.jl index 4a87a97..4c92d19 100644 --- a/src/line.jl +++ b/src/line.jl @@ -94,23 +94,23 @@ copy(r::Ray) = Ray(r.startpoint, r.direction) copy(s::Segment) = Segment(s.startpoint, s.endpoint) -# promote_rule{T<:Integer,S<:FloatingPoint}(::Type{Line{T}}, ::Type{Line{S}} ) = Line{S} +# promote_rule{T<:Integer,S<:AbstractFloat}(::Type{Line{T}}, ::Type{Line{S}} ) = Line{S} # promote_rule{T<:Integer}(::Type{Line{Rational{T}}}, ::Type{Line{T}}) = Line{Rational{T}} # promote_rule{T<:Integer,S<:Integer}(::Type{Line{Rational{T}}}, ::Type{Line{S}}) = Line{Rational{promote_type(T,S)}} # promote_rule{T<:Integer,S<:Integer}(::Type{Line{Rational{T}}}, ::Type{Line{Rational{S}}}) = Line{Rational{promote_type(T,S)}} -# promote_rule{T<:Integer,S<:FloatingPoint}(::Type{Line{Rational{T}}}, ::Type{Line{S}}) = Line{promote_type(T,S)} +# promote_rule{T<:Integer,S<:AbstractFloat}(::Type{Line{Rational{T}}}, ::Type{Line{S}}) = Line{promote_type(T,S)} -# promote_rule{T<:Integer,S<:FloatingPoint}(::Type{Ray{T}}, ::Type{Ray{S}} ) = Ray{S} +# promote_rule{T<:Integer,S<:AbstractFloat}(::Type{Ray{T}}, ::Type{Ray{S}} ) = Ray{S} # promote_rule{T<:Integer}(::Type{Ray{Rational{T}}}, ::Type{Ray{T}}) = Ray{Rational{T}} # promote_rule{T<:Integer,S<:Integer}(::Type{Ray{Rational{T}}}, ::Type{Ray{S}}) = Ray{Rational{promote_type(T,S)}} # promote_rule{T<:Integer,S<:Integer}(::Type{Ray{Rational{T}}}, ::Type{Ray{Rational{S}}}) = Ray{Rational{promote_type(T,S)}} -# promote_rule{T<:Integer,S<:FloatingPoint}(::Type{Ray{Rational{T}}}, ::Type{Ray{S}}) = Ray{promote_type(T,S)} +# promote_rule{T<:Integer,S<:AbstractFloat}(::Type{Ray{Rational{T}}}, ::Type{Ray{S}}) = Ray{promote_type(T,S)} -# promote_rule{T<:Integer,S<:FloatingPoint}(::Type{Segment{T}}, ::Type{Segment{S}} ) = Segment{S} +# promote_rule{T<:Integer,S<:AbstractFloat}(::Type{Segment{T}}, ::Type{Segment{S}} ) = Segment{S} # promote_rule{T<:Integer}(::Type{Segment{Rational{T}}}, ::Type{Segment{T}}) = Segment{Rational{T}} # promote_rule{T<:Integer,S<:Integer}(::Type{Segment{Rational{T}}}, ::Type{Segment{S}}) = Segment{Rational{promote_type(T,S)}} # promote_rule{T<:Integer,S<:Integer}(::Type{Segment{Rational{T}}}, ::Type{Segment{Rational{S}}}) = Segment{Rational{promote_type(T,S)}} -# promote_rule{T<:Integer,S<:FloatingPoint}(::Type{Segment{Rational{T}}}, ::Type{Segment{S}}) = Segment{promote_type(T,S)} +# promote_rule{T<:Integer,S<:AbstractFloat}(::Type{Segment{Rational{T}}}, ::Type{Segment{S}}) = Segment{promote_type(T,S)} # conversion of one type to another: note though that these loose information convert{T<:Number}(::Type{Ray}, s::Segment{T}) = Ray(s.startpoint, s.endpoint-s.startpoint) diff --git a/src/point.jl b/src/point.jl index 9d2cfa2..17a462d 100644 --- a/src/point.jl +++ b/src/point.jl @@ -24,16 +24,16 @@ origin{T<:Number}(::Point{T}) = Point(convert(T,0), convert(T,0)) typealias Vect Point # automated promotion rules for Points -promote_rule{T<:Integer,S<:FloatingPoint}(::Type{Point{T}}, ::Type{Point{S}} ) = Point{S} +promote_rule{T<:Integer,S<:AbstractFloat}(::Type{Point{T}}, ::Type{Point{S}} ) = Point{S} promote_rule{T<:Integer}(::Type{Point{Rational{T}}}, ::Type{Point{T}}) = Point{Rational{T}} promote_rule{T<:Integer,S<:Integer}(::Type{Point{Rational{T}}},::Type{Point{S}}) = Point{Rational{promote_type(T,S)}} promote_rule{T<:Integer,S<:Integer}(::Type{Point{Rational{T}}},::Type{Point{Rational{S}}}) = Point{Rational{promote_type(T,S)}} -promote_rule{T<:Integer,S<:FloatingPoint}(::Type{Point{Rational{T}}}, ::Type{Point{S}}) = Point{promote_type(T,S)} -promote_rule{T<:FloatingPoint,S<:FloatingPoint}(::Type{Point{T}}, ::Type{Point{S}}) = Point{promote_type(T,S)} +promote_rule{T<:Integer,S<:AbstractFloat}(::Type{Point{Rational{T}}}, ::Type{Point{S}}) = Point{promote_type(T,S)} +promote_rule{T<:AbstractFloat,S<:AbstractFloat}(::Type{Point{T}}, ::Type{Point{S}}) = Point{promote_type(T,S)} # should probably put something in here for complex numbers too # don't seem to need these? -# convert{T<:FloatingPoint}(::Type{Point{T}}, p::Point) = Point(convert(T,p.x), convert(T,p.y)) +# convert{T<:AbstractFloat}(::Type{Point{T}}, p::Point) = Point(convert(T,p.x), convert(T,p.y)) # convert{T<:Integer}(::Type{Point{Rational{T}}}, p::Point) = Point(convert(Rational{T},p.x), convert(Rational{T},p.y)) # convert{T<:Integer}(::Type{Point{T}}, p::Point) = Point(convert(T,p.x), convert(T,p.y)) @@ -60,7 +60,7 @@ isless(p1::Point, p2::Point) = ( p1.x (p1::Point, p2::Point) = p2 Date: Mon, 19 Oct 2015 09:17:02 +1300 Subject: [PATCH 2/5] Change Union(...) to @compat Union{...} --- src/intersection.jl | 2 +- src/line.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/intersection.jl b/src/intersection.jl index 306a873..2337c13 100644 --- a/src/intersection.jl +++ b/src/intersection.jl @@ -184,7 +184,7 @@ end intersection( r::Ray, s::Segment; tolerance=1.0e-12 ) = intersection( s, r; tolerance=tolerance ) # intersection of lines with segments or Rays -RayOrSegment = Union(Ray, Segment) +RayOrSegment = @compat Union{Ray, Segment} function intersection( s::RayOrSegment, l::Line; tolerance=1.0e-12 ) #OUTPUTS: # intersect = 0 means no intersection diff --git a/src/line.jl b/src/line.jl index 4c92d19..a394560 100644 --- a/src/line.jl +++ b/src/line.jl @@ -74,7 +74,7 @@ Segment{T<:Number}(startpoint::Point{T}, endpoint::Point{T}) = Segment{T}(promot Segment{T<:Number, S<:Number}(startpoint::Point{T}, endpoint::Point{S}) = Segment(promote(startpoint, endpoint)...) # could have done this with an abstract type above the three, but wanted to try out Unions -LINETYPE = Union(Line, Ray, Segment) +LINETYPE = @compat Union{Line, Ray, Segment} # lots of alternate representations we could build constructors for # ray constructed using two points (with one as start), or point and a slope (and + or -) From 5d88af07dc7428411e233de00543e336d5b53078 Mon Sep 17 00:00:00 2001 From: anj1 Date: Mon, 19 Oct 2015 09:18:34 +1300 Subject: [PATCH 3/5] Update syntax for <( and friends --- src/point.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/point.jl b/src/point.jl index 17a462d..1dbdce4 100644 --- a/src/point.jl +++ b/src/point.jl @@ -58,13 +58,13 @@ inner(p1::Point, p2::Point) = p1.x * p2.x + p1.y * p2.y isequal(p1::Point, p2::Point) = ( p1.x==p2.x && p1.y==p2.y ) isless(p1::Point, p2::Point) = ( p1.x (p1::Point, p2::Point) = p2(p1::Point, p2::Point) = p2 (p1::Point, p2::Point) = p2(p1::Point, p2::Point) = p2=(p1::Point, p2::Point) = p1<=p2 .<=(p1::Point, p2::Point) = p2<=p1 cmp(p1::Point, p2::Point) = p1 Date: Mon, 19 Oct 2015 09:30:55 +1300 Subject: [PATCH 4/5] More compatibility changes: [a,b] -> vcat(a,b); Uint -> UInt --- src/line.jl | 8 ++++---- test/test_point.jl | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/line.jl b/src/line.jl index a394560..293fb9d 100644 --- a/src/line.jl +++ b/src/line.jl @@ -277,16 +277,16 @@ function displayPath(line::Line; bounds=default_bounds) # choose the two points that are in the bounding rectangle P = [] if i1==1 && isin(p1, bounds)[1] - P = [P, p1] + P = vcat(P, p1) end if i2==1 && isin(p2, bounds)[1] - P = [P, p2] + P = vcat(P, p2) end if i3==1 && isin(p3, bounds)[1] - P = [P, p3] + P = vcat(P, p3) end if i4==1 && isin(p4, bounds)[1] - P = [P, p4] + P = vcat(P, p4) end return unique(P) # doesn't eliminate all possible redundant points because of roundoff errors end diff --git a/test/test_point.jl b/test/test_point.jl index e5a9d57..f2e0f4d 100644 --- a/test/test_point.jl +++ b/test/test_point.jl @@ -14,8 +14,8 @@ plot(p3; marker="o", color="blue") p4 = Point(1, 1.0) p5 = Point(2, 3//4) p6 = Point(3.0, 2//3) -p7 = Point(convert(Uint32,2), convert(Uint16,3)) -p8 = Point(convert(Uint32,2)//convert(Uint32,4), convert(Uint16,3)) +p7 = Point(convert(UInt32,2), convert(UInt16,3)) +p8 = Point(convert(UInt32,2)//convert(UInt32,4), convert(UInt16,3)) # some tests of promotion of Points dump(promote(p1,p2)) From c2d43ebcc14fbcb12b8e14046d1241210b0ee686 Mon Sep 17 00:00:00 2001 From: anj1 Date: Mon, 19 Oct 2015 12:41:06 +1300 Subject: [PATCH 5/5] More compatibility changes: [1:n] -> collect(1:n) --- src/circle.jl | 2 +- test/test_ccw.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/circle.jl b/src/circle.jl index 3667fd4..a085153 100644 --- a/src/circle.jl +++ b/src/circle.jl @@ -132,7 +132,7 @@ closed(::Circle) = true # approximate as a regular polygon function approxpoly(c::Circle, n::Integer) dtheta = 2.0*pi/n - theta = dtheta*[0:n] + theta = dtheta*collect(0:n) x = c.center.x + c.radius * cos(theta) y = c.center.y + c.radius * sin(theta) return PointArray(x,y) diff --git a/test/test_ccw.jl b/test/test_ccw.jl index 56938f4..ac761e2 100644 --- a/test/test_ccw.jl +++ b/test/test_ccw.jl @@ -4,7 +4,7 @@ n = 5 srand(1) X = rand(3,n) Y = rand(3,n) -X[:,1] = [1:3] # make sure there is at least one colinear case +X[:,1] = collect(1:3) # make sure there is at least one colinear case Y[:,1] = [0,0,0] CCW = Array(Float64,6,n)