From aa87ea991570acf8d0c55ce116999ac170c915c2 Mon Sep 17 00:00:00 2001 From: marcandre Date: Wed, 29 Oct 2014 02:43:52 +0000 Subject: * lib/matrix.rb: Generalize Vector#cross_product to arbitrary dimensions based on a patch by gogo tanaka [#10074] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48183 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/matrix.rb | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) (limited to 'lib/matrix.rb') diff --git a/lib/matrix.rb b/lib/matrix.rb index f7acd9f1fc..f7d75b328f 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -1962,14 +1962,36 @@ class Vector alias_method :dot, :inner_product # - # Returns the cross product of this vector with the other. + # Returns the cross product of this vector with the others. # Vector[1, 0, 0].cross_product Vector[0, 1, 0] => Vector[0, 0, 1] # - def cross_product(v) - Vector.Raise ErrDimensionMismatch unless size == v.size && v.size == 3 - Vector[ v[2]*@elements[1] - v[1]*@elements[2], - v[0]*@elements[2] - v[2]*@elements[0], - v[1]*@elements[0] - v[0]*@elements[1] ] + # It is generalized to other dimensions to return a vector perpendicular + # to the arguments. + # Vector[1, 2].cross_product # => Vector[-2, 1] + # Vector[1, 0, 0, 0].cross_product( + # Vector[0, 1, 0, 0], + # Vector[0, 0, 1, 0] + # ) #=> Vector[0, 0, 0, 1] + # + def cross_product(*vs) + raise ErrOperationNotDefined, "cross product is not defined on vectors of dimension #{size}" unless size >= 2 + raise ArgumentError, "wrong number of arguments (#{vs.size} for #{size - 2})" unless vs.size == size - 2 + vs.each do |v| + raise TypeError, "expected Vector, got #{v.class}" unless v.is_a? Vector + Vector.Raise ErrDimensionMismatch unless v.size == size + end + case size + when 2 + Vector[-@elements[1], @elements[0]] + when 3 + v = vs[0] + Vector[ v[2]*@elements[1] - v[1]*@elements[2], + v[0]*@elements[2] - v[2]*@elements[0], + v[1]*@elements[0] - v[0]*@elements[1] ] + else + rows = self, *vs, Array.new(size) {|i| Vector.basis(size: size, index: i) } + Matrix.rows(rows).laplace_expansion(row: size - 1) + end end alias_method :cross, :cross_product -- cgit v1.2.3