Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to best represent a list of points

Tags:

julia

I have a set of points in 2D space that I'm representing as a rank 2 array:

points = [0 0; 0 1; 1 0]

It is a useful representation because it allows easy access to the x-, and y-components of the points. E.g.

plot(x=points[:,1], y=points[:,2])

However, sometimes it would be better to view points as a list/set of points rather than as a matrix. For example, I need to check if a certain point (e.g. [0 1]) is an element of points. The straight-forward version does not work:

[0 1] in points  # is false

Instead I have to manually expand points to a list of points:

[0 1] in [points[i,:] for i in 1:size(points)[1]]  # is true

What is the proper julian way to define such a set of points, access components, and check for membership?


Update: As @Jubobs suggested, I went ahead and defined my own type. As it turned out, I actually needed a vector, so I went ahead and called it Vec2 instead of Point.

immutable Vec2{T<:Real}
    x :: T
    y :: T
end
Vec2{T<:Real}(x::T, y::T) = Vec2{T}(x, y)
Vec2(x::Real, y::Real) = Vec2(promote(x,y)...)

convert{T<:Real}(::Type{Vec2{T}}, p::Vec2) =
    Vec2(convert(T,p.x), convert(T,p.y))
convert{Tp<:Real, T<:Real, S<:Real}(::Type{Vec2{Tp}}, t::(T,S)) =
    Vec2(convert(Tp, t[1]), convert(Tp, t[2]))

promote_rule{T<:Real, S<:Real}(::Type{Vec2{T}}, ::Type{Vec2{S}}) =
    Vec2{promote_type(T,S)}

+(l::Vec2, r::Vec2) = Vec2(l.x+r.x, l.y+r.y)
-(l::Vec2, r::Vec2) = Vec2(l.x-r.x, l.y-r.y)
*(a::Real, p::Vec2) = Vec2(a*p.x, a*p.y)
*(p::Vec2, a::Real) = Vec2(a*p.x, a*p.y)
/(p::Vec2, a::Real) = Vec2(a/p.x, a/p.y)
dot(a::Vec2, b::Vec2) = a.x*b.x + a.y*b.y
zero{T<:Real}(p::Vec2{T}) = Vec2{T}(zero(T),zero(T))
zero{T<:Real}(::Type{Vec2{T}}) = Vec2{T}(zero(T),zero(T))
like image 557
Lemming Avatar asked Nov 26 '14 17:11

Lemming


1 Answers

I have a set of points in 2D space that I'm representing as a rank-2 array [...]

This calls for a set of pairs (tuples with two elements).

julia> myset = Set( [(0,0), (0,1), (1,0)] ) # define a set of tuples
Set{(Int64,Int64)}({(0,0),(1,0),(0,1)})

julia> in((0,0),myset)             # testing for membership is easy
true

julia> x = map (p -> p[1], myset)  # access to x values is easy with 'map'
3-element Array{Any,1}:
 0
 1
 0

julia> y = map (p -> p[2], myset)  # same thing with y values
3-element Array{Any,1}:
 0
 0
 1

julia> push!(myset,(3,2))          # adding an element to the set
Set{(Int64,Int64)}({(0,0),(1,0),(3,2),(0,1)})

julia> pop!(myset,(3,2))           # removing an element from the set
(3,2)

julia> myset
Set{(Int64,Int64)}({(0,0),(1,0),(0,1)})
like image 139
jub0bs Avatar answered Oct 02 '22 19:10

jub0bs