The cross product is a peculiarly 3-dimensional phenomenon. There are multiple ways of generalizing it, but each of them have trade offs.
If you require that a cross product be a product of two vectors \$\mathbf{v}_1\times\mathbf{v}_2\$ such that it is:
- linear, \$a\mathbf{v}_1\times\mathbf{v}_2 = a\left(\mathbf{v}_1\times\mathbf{v}_2\right)\$ and \$\left(\mathbf v_1+\mathbf v_2\right)\times\mathbf v_3 = \left(\mathbf v_1\times\mathbf v_3\right)+\left(\mathbf v_2\times\mathbf v_3\right)\$
- anticommutative, \$\left(\mathbf{v}_1\times\mathbf{v}_2\right) + \left(\mathbf{v}_2\times\mathbf{v}_1\right) = \mathbf{0}\$
- orthogonal, \$\left(\mathbf{v}_1\times\mathbf{v}_2\right)\cdot\mathbf v_1 = \mathbf 0\$
- not everywhere zero
Then there are two dimensions that have cross products:
- There is the ordinary cross product in 3D.
- There is also a cross product in 7D.
Today our challenge is to implement this 7-dimensional cross product.
Definition
Although the above is a technically unambiguous challenge specification, I will give a complete definition of a 7D cross product here which you can readily implement as an algorithm.
In order to determine the cross product of two vectors you only need to know the behavior on the basis vectors. You can write each vector as a combination of basis vectors, e.g.
\$ \left[a,b,c,d,e,f,g\right]^\top = a\mathbf e_1 + b\mathbf e_2 + c\mathbf e_3 + d\mathbf e_4 + e\mathbf e_5 + f\mathbf e_6 + g\mathbf e_7 \$
Since the product is linear you can distribute until any cross product is written as the sum of products of basis vectors. For example here's a 3D case:
\begin{align} \left[a,b,c\right]^\top\times\left[x,y,z\right]^\top &= \left(a\mathbf e_1 + b\mathbf e_2 + c\mathbf e_3\right)\times\left(x\mathbf e_1 + y\mathbf e_2 + z\mathbf e_3\right) \\ &= \begin{matrix}& ax\left(e_1\times e_1\right) &+& bx\left(e_2\times e_1\right) &+& cx\left(e_3\times e_1\right) \\+& ay\left(e_1\times e_2\right) &+& by\left(e_2\times e_2\right) &+& cy\left(e_3\times e_2\right) \\+& az\left(e_1\times e_3\right) &+& bz\left(e_2\times e_3\right) &+& cy\left(e_3\times e_3\right)\end{matrix} \end{align}
Then for the product of basis vectors you can use the following product table:
× | \$\mathbf e_1\$ | \$\mathbf e_2\$ | \$\mathbf e_3\$ | \$\mathbf e_4\$ | \$\mathbf e_5\$ | \$\mathbf e_6\$ | \$\mathbf e_7\$ |
---|---|---|---|---|---|---|---|
\$\mathbf e_1\$ | 0 | \$\mathbf e_3\$ | \$-\mathbf e_2\$ | \$\mathbf e_5\$ | \$-\mathbf e_4\$ | \$-\mathbf e_7\$ | \$\mathbf e_6\$ |
\$\mathbf e_2\$ | \$-\mathbf e_3\$ | 0 | \$\mathbf e_1\$ | \$\mathbf e_6\$ | \$\mathbf e_7\$ | \$-\mathbf e_4\$ | \$-\mathbf e_5\$ |
\$\mathbf e_3\$ | \$\mathbf e_2\$ | \$-\mathbf e_1\$ | 0 | \$\mathbf e_7\$ | \$-\mathbf e_6\$ | \$\mathbf e_5\$ | \$-\mathbf e_4\$ |
\$\mathbf e_4\$ | \$-\mathbf e_5\$ | \$-\mathbf e_6\$ | \$-\mathbf e_7\$ | 0 | \$\mathbf e_1\$ | \$\mathbf e_2\$ | \$\mathbf e_3\$ |
\$\mathbf e_5\$ | \$\mathbf e_4\$ | \$-\mathbf e_7\$ | \$\mathbf e_6\$ | \$-\mathbf e_1\$ | 0 | \$-\mathbf e_3\$ | \$\mathbf e_2\$ |
\$\mathbf e_6\$ | \$\mathbf e_7\$ | \$\mathbf e_4\$ | \$-\mathbf e_5\$ | \$-\mathbf e_2\$ | \$\mathbf e_3\$ | 0 | \$-\mathbf e_1\$ |
\$\mathbf e_7\$ | \$-\mathbf e_6\$ | \$\mathbf e_5\$ | \$\mathbf e_4\$ | \$-\mathbf e_3\$ | \$-\mathbf e_2\$ | \$\mathbf e_1\$ | 0 |
Rules
This is code-golf so the goal is to minimize the size of your source code as measured in bytes.
You should take input as two vectors. You may approximate real numbers in any reasonable manner.
You may choose to take input in a way that permutes the basis vectors or multiply the result by -1, so long as it satisfies the definition of the cross product above.
Test cases
The table above provides 49 basic test cases. In addition I suggest you test the two linearity properties with random vectors. If your product is bilinear and satisfies the multiplication table above then it must be correct.
That is:
- Take two random vectors and two random real numbers and test that \$a\mathbf{v}_1\times b\mathbf{v}_2 = ab\left(\mathbf{v}_1\times\mathbf{v}_2\right)\$
- Take 3 random vectors and test that \$\left(\mathbf v_1+\mathbf v_2\right)\times\mathbf v_3 = \left(\mathbf v_1\times\mathbf v_3\right)+\left(\mathbf v_2\times\mathbf v_3\right)\$