如何在Ruby中执行向量添加?

如何在Ruby中执行向量添加

[100, 100] + [2, 3] 

产量

 [102, 103] 

(而不是加入两个数组)?

或者它也可以是另一个运营商,例如

 [100, 100] @ [2, 3] 

要么

 [100, 100] & [2, 3] 

查看Vector类:

 require "matrix" x = Vector[100, 100] y = Vector[2, 3] print x + y E:\Home> ruby t.rb Vector[102, 103] 

有关向量的其他操作,请参阅向量:

……以下操作按预期工作

  v1 = Vector[1,1,1,0,0,0] v2 = Vector[1,1,1,1,1,1] v1[0..3] # -> Vector[1,1,1] v1 += v2 # -> v1 == Vector[2,2,2,1,1,1] v1[0..3] += v2[0..3] # -> v1 == Vector[2,2,2,0,0,0] v1 + 2 # -> Vector[3,3,3,1,1,1] 

另见vectorops 。

arrays#邮编:

 $ irb irb(main):001:0> [100,100].zip([2,3]).map { |e| e.first + e.last } => [102, 103] 

短:

 irb(main):002:0> [100,100].zip([2,3]).map { |x,y| x + y } => [102, 103] 

使用#inject推广到> 2维:

 irb(main):003:0> [100,100,100].zip([2,3,4]).map { |z| z.inject(&:+) } => [102, 103, 104] 

或者,如果您想要该种类的任意维度行为(如数学向量加法)

  class Vector < Array def +(other) case other when Array raise "Incorrect Dimensions" unless self.size == other.size other = other.dup self.class.new(map{|i| i + other.shift}) else super end end end class Array def to_vector Vector.new(self) end end [100,100].to_vector + [2,3] #=> [102,103] 

缺乏lisp风格的地图是非常令人讨厌的。

在罗马… monkeypatch。

 module Enumerable def sum inject &:+ end def vector_add(*others) zip(*others).collect &:sum end end 

然后你可以做a.vector_add(b)并且它可以工作。 我相信这需要Ruby 1.8.7,或者添加Symbol.to_proc的扩展。 您也可以通过这种方式添加任意数量的矢量。

就像旁注一样,如果你(像我一样)对Ruby中默认Vector类提供的操作感到不满意,可以考虑给我的gem https://github.com/psorowka/vectorops一个看,这增加了一些function我期望从适当的Vector实现。

 module PixelAddition def +(other) zip(other).map {|num| num[0]+num[1]} end end 

然后,您可以创建一个混合在模块中的Array子类,或者将行为添加到特定数组,如:

 class <