1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
marcandre 2014-10-29 02:43:52 +00:00
parent 764524f65b
commit aa87ea9915
4 changed files with 47 additions and 8 deletions

View file

@ -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

1
NEWS
View file

@ -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

View file

@ -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

View file

@ -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