使用lambda 計算 Euclidean distance

若p(1,1), q(3,3)

則 d(p,q)

 ->(xs,ys,xt,yt){Math.sqrt((xs-xt)**2+(ys-yt)**2)}.(1,1,3,3)

 

lambda fuction

 f = { 1 => ->(x) { puts "#{x}  from 1" },

         2 => ->(x) { puts "#{x} bye from 2" }

      }

f.call[1]("mary")

 

HyperSphere 中,求任兩點的的距離(EuclideanDistance)

def EuclideanDistance(x,y)
  sum = 0
  for i in (0..x.size-1)
    printf("x[%d]=%d,y[%d]=%d\n",i,x[i],i,y[i])
    ->(px,py){ sum += (px-py)**2}.(x[i],y[i])
  end
puts Math.sqrt(sum)
end

x=[1,1]
y=[3,3]
p=[1,1,1]
q=[3,3,3]

a=[1,1,1,1]
b=[3,3,3,3]

EuclideanDistance(x,y)
EuclideanDistance(p,q)

EuclideanDistance(a,b)

 

igogo 發表在 痞客邦 留言(0) 人氣()