Feature #10074

[PATCH] generalize Vector#cross_product

Added by gogo tanaka about 1 year ago. Updated 9 months ago.

[ruby-core:63860]
Status:Closed
Priority:Normal
Assignee:Marc-Andre Lafortune

Description

Usually We use cross_product in only 3 dimensions.

Sometimes we want to handle cross_product in n-dimensions.

But I recognize Ruby should be not for mathematician but rubyist.

Ruby doesn't need too academic method.

So If you think it is too academic you can ignore my patches.

generalize_Vector#cross_product.patch Magnifier (1.71 KB) gogo tanaka, 07/19/2014 07:46 AM

Associated revisions

Revision 48183
Added by Marc-Andre Lafortune 9 months ago

  • lib/matrix.rb: Generalize Vector#cross_product to arbitrary dimensions based on a patch by gogo tanaka [#10074]

Revision 48183
Added by Marc-Andre Lafortune 9 months ago

  • lib/matrix.rb: Generalize Vector#cross_product to arbitrary dimensions based on a patch by gogo tanaka [#10074]

History

#1 Updated by gogo tanaka about 1 year ago

Before

 Vector[1, 0, 0].cross_product Vector[0, 1, 0]
=> Vector[0, 0, 1]

Vector[1, 2].cross_product
=> ArgumentError: wrong number of arguments (0 for 1)

 Vector[3, 5, 2, 1].cross_product(Vector[4, 3, 1, 8], Vector[2, 9, 4, 3])
=> ArgumentError: wrong number of arguments (0 for 1)

After

 Vector[1, 0, 0].cross_product Vector[0, 1, 0]
=> Vector[0, 0, 1]

Vector[1, 2].cross_product
=> Vector[2, -1]

 Vector[3, 5, 2, 1].cross_product(Vector[4, 3, 1, 8], Vector[2, 9, 4, 3])
=> Vector[-16, 65, -139, 1]

This patch needs https://bugs.ruby-lang.org/issues/10072 and https://bugs.ruby-lang.org/issues/10073

gogo.

#2 Updated by Hiroshi SHIBATA about 1 year ago

  • Category set to lib
  • Status changed from Open to Assigned
  • Assignee set to Marc-Andre Lafortune
  • Target version set to current: 2.2.0

#3 Updated by Marc-Andre Lafortune 9 months ago

I committed this change, with a couple of changes.

In particular, the sign was wrong for even dimensions. I changed it so we now have, for any n:

first, *middle, last = Matrix.identity(n).column_vectors
first.cross(*middle) == last

Thanks for this feature request!

#4 Updated by Marc-Andre Lafortune 9 months ago

  • Status changed from Assigned to Closed

#5 Updated by gogo tanaka 9 months ago

@Marc-Andre Lafortune

I'm really appreciate your help.
And your changes totally makes sense.

Thanks.

Also available in: Atom PDF