aboutsummaryrefslogtreecommitdiffstats
path: root/lib/matrix.rb
diff options
context:
space:
mode:
authormarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-10-29 02:43:52 +0000
committermarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-10-29 02:43:52 +0000
commitaa87ea991570acf8d0c55ce116999ac170c915c2 (patch)
tree06b524401be188e3d09994db3cf7d0d78bea104a /lib/matrix.rb
parent764524f65b1b27a0670c81e3605a9ac8e3385040 (diff)
downloadruby-aa87ea991570acf8d0c55ce116999ac170c915c2.tar.gz
* 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
Diffstat (limited to 'lib/matrix.rb')
-rw-r--r--lib/matrix.rb34
1 files changed, 28 insertions, 6 deletions
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