作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在二维空间中有一组点,我将它们表示为 2 级数组:
points = [0 0; 0 1; 1 0]
plot(x=points[:,1], y=points[:,2])
points
会更好作为一个列表/一组点而不是一个矩阵。例如,我需要检查某个点(例如
[0 1]
)是否是
points
的元素.直接版本不起作用:
[0 1] in points # is false
points
到点列表:
[0 1] in [points[i,:] for i in 1:size(points)[1]] # is true
Vec2
而不是
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))
最佳答案
I have a set of points in 2D space that I'm representing as a rank-2 array [...]
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)})
关于julia - 如何最好地表示点列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27155176/
我是一名优秀的程序员,十分优秀!