Why Ruby cross_product returns a different value from a cross product on paper

u = [1, -2, 1] # u = i -2j + k
v = [3, 1, -2] # v = 3i + j -2k

On paper, when I do cross-product u x v, I get 3i + 5j + 7k, which should be [3, 5, 7]

But when I do this on IRB, I get - (uhv)

irb(main):081:0> u
=> Vector[1, -2, 1]
irb(main):082:0> v
=> Vector[3, 1, -2]
irb(main):083:0> u.cross_product v
=> Vector[-3, -5, -7]

This response to the cross product of v and u, v x u.

Does u.cross_product vRuby Mean v x u?

I'm confused.

+4
source share
2 answers

Here is the source of the function cross_product( here ):

# File matrix.rb, line 1764
def cross_product(v)
  Vector.Raise ErrDimensionMismatch unless size == v.size && v.size == 3
  Vector[ v[1]*@elements[2] - v[2]*@elements[1],
          v[2]*@elements[0] - v[0]*@elements[2],
          v[0]*@elements[1] - v[1]*@elements[0] ]
end

, , v , , , . , , , . , , , :

v.cross_product u
+2

Ruby, Ruby-2.1.2 Ruby-2.1.2.

, , -. "A x B" , "A.cross_product B" .

ruby ​​subversion , DMKE , , ruby-2.1.3.

Debian ruby.

+2

Source: https://habr.com/ru/post/1541781/


All Articles