果冻,14 13 12字节
;"s€2U×¥/ḅ-U
在线尝试!
怎么运行的
;"s€2U×¥/ḅ-U Main link. Input: [a1, a2, a3], [b1, b2, b3]
;" Concatenate each [x1, x2, x3] with itself.
Yields [a1, a2, a3, a1, a2, a3], [b1, b2, b3, b1, b2, b3].
s€2 Split each array into pairs.
Yields [[a1, a2], [a3, a1], [a2, a3]], [[b1, b2], [b3, b1], [b2, b3]].
¥ Define a dyadic chain:
U Reverse the order of all arrays in the left argument.
× Multiply both arguments, element by element.
/ Reduce the 2D array of pairs by this chain.
Reversing yields [a2, a1], [a1, a3], [a3, a2].
Reducing yields [a2b1, a1b2], [a1b3, a3b1], [a3b2, a2b3].
ḅ- Convert each pair from base -1 to integer.
This yields [a1b2 - a2b1, a3b1 - a1b3, a2b3 - a3b2]
U Reverse the array.
This yields [a2b3 - a3b2, a3b1 - a1b3, a1b2 - a2b1] (cross product).
非竞争版本(10字节)
好的,这很尴尬,但是数组操纵语言Jelly直到现在还没有内置的数组旋转功能。有了这个新的内置函数,我们可以节省两个额外的字节。
ṙ-×
ç_ç@ṙ-
这使用@AlexA。的J answer中的方法。在线尝试!
怎么运行的
ṙ-× Helper link. Left input: x = [x1, x2, x3]. Right input: y = [y1, y2, y3].
ṙ- Rotate x 1 unit to the right (actually, -1 units to the left).
This yields [x3, x1, x2].
× Multiply the result with y.
This yields [x3y1, x1y2, x2y3].
ç_ç@ṙ- Main link. Left input: a = [a1, a2, a3]. Right input: b = [b1, b2, b3].
ç Call the helper link with arguments a and b.
This yields [a3b1, a1b2, a2b3].
ç@ Call the helper link with arguments b and a.
This yields [b3a1, b1a2, b2a3].
_ Subtract the result to the right from the result to the left.
This yields [a3b1 - a1b3, a1b2 - a2b1, a2b3 - a3b2].
ṙ- Rotate the result 1 unit to the right.
This yields [a2b3 - a3b2, a3b1 - a1b3, a1b2 - a2b1] (cross product).