summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog6
-rw-r--r--NEWS1
-rw-r--r--lib/matrix.rb34
-rw-r--r--test/matrix/test_vector.rb14
4 files changed, 47 insertions, 8 deletions
diff --git a/ChangeLog b/ChangeLog
index 1210ac03a9..6731fbb616 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Wed Oct 29 11:43:43 2014 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
+
+ * lib/matrix.rb: Generalize Vector#cross_product to arbitrary
+ dimensions
+ based on a patch by gogo tanaka [#10074]
+
Wed Oct 29 11:43:11 2014 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
* lib/matrix.rb: Add Matrix#adjucate
diff --git a/NEWS b/NEWS
index bd7e773689..8c46c971a1 100644
--- a/NEWS
+++ b/NEWS
@@ -155,6 +155,7 @@ with all sufficient information, see the ChangeLog file.
along the +num+ -th row or column.
* Vector.basis(size:, index:) returns the specified basis vector
* Unary - and + added for Vector and Matrix
+ * Vector#cross_product generalized to arbitrary dimensions
* Vector#dot and #cross are aliases for #inner_product and #cross_product
* Pathname
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
diff --git a/test/matrix/test_vector.rb b/test/matrix/test_vector.rb
index 4e2bca6ef6..a063d2ca12 100644
--- a/test/matrix/test_vector.rb
+++ b/test/matrix/test_vector.rb
@@ -169,7 +169,17 @@ class TestVector < Test::Unit::TestCase
def test_cross_product
v = Vector[1, 0, 0].cross_product Vector[0, 1, 0]
assert_equal(Vector[0, 0, 1], v)
- v = Vector[1, 0, 0].cross Vector[0, 1, 0]
- assert_equal(Vector[0, 0, 1], v)
+ v2 = Vector[1, 2].cross_product
+ assert_equal(Vector[-2, 1], v2)
+ v3 = Vector[3, 5, 2, 1].cross(Vector[4, 3, 1, 8], Vector[2, 9, 4, 3])
+ assert_equal(Vector[16, -65, 139, -1], v3)
+ assert_equal Vector[0, 0, 0, 1],
+ Vector[1, 0, 0, 0].cross(Vector[0, 1, 0, 0], Vector[0, 0, 1, 0])
+ assert_equal Vector[0, 0, 0, 0, 1],
+ Vector[1, 0, 0, 0, 0].cross(Vector[0, 1, 0, 0, 0], Vector[0, 0, 1, 0, 0], Vector[0, 0, 0, 1, 0])
+ assert_raise(Vector::ErrDimensionMismatch) { Vector[1, 2, 3].cross_product(Vector[1, 4]) }
+ assert_raise(TypeError) { Vector[1, 2, 3].cross_product(42) }
+ assert_raise(ArgumentError) { Vector[1, 2].cross_product(Vector[2, -1]) }
+ assert_raise(Vector::ErrOperationNotDefined) { Vector[1].cross_product }
end
end