Skip to content

Cross product for vectors of size 2. Fixes #1037

Reference issue

Fixes #1037 (closed)

What does this implement/fix?

Implements cross product for vectors of size 2. The result is a scalar equal to the signed area of a parallelepiped spanned by the input vectors. Or, to put it differently, the cross product of (v1, v2) and (w1, w2) is the 3rd coordinate of the cross product of (v1, v2, v3) and (w1, w2, w3).

Additional information

This is my first time contributing to Eigen! I tried to do as best as I could, but I still have some doubts

  • I think we can consider this MR fixes #1037 (closed) because the issue specifically requests a cross product of 2D vectors. Actually, that issue seems to be a little big wider, also proposing a more general implementation for arbitrary-size vectors returning skew-symmetric matrices, possibly named wedge. This is not implemented by this MR, but since the main focus of that issue seems to be 2D vectors I think it can be closed. A comment in the thread mentions numpy, allowing arbitrary combinations of 2D and 3D vectors, where a 2D vector is interpreted as a 3D vector with vanishing z-component. This behavior is not implemented either in this MR, the cross product is only defined between vectors of the same size
  • A few words about cross_product_return_type
    • As a shift in paradigm, I took its definition out of MatrixBase, turning it from a nested struct into an independent templated struct. I thought it more elegant. This change is not backward compatible, but I took this liberty because the struct was undocumented anyways, and also it was used nowhere in the code base. If requested, I can easily revert this change.
    • I decided to document it as a type rather than hide it behind something like PlainObject as it was previously done. It makes it easier to explain the output. Also, I didn't like the idea of obfuscating the doc
    • I am not sure about naming conventions, especially now that it's a public type. Should it be CrossProductReturnType::Type instead of cross_product_return_type::type? Maybe it could even be renamed to something completely different such as CrossProductTraits::ReturnType?
  • In passing, I removed a seemingly-orphaned forward declaration of a class called Cross which looked like a relic, as it wasn't used anywhere in the code base and indeed I could not even find its full declaration. Again, if requested, I can put it back
  • A simpler implementation (mentioned in #1037 (closed)) would have consisted in giving the method for size-2 vectors a different name, like cross2. However, I decided to call it cross because I liked it better as an interface, which seems to be shared by other important resources such as numpy. Also, I actively enjoyed template metaprogramming!
Edited by Gabriele Buondonno

Merge request reports

Loading