Iloczyn dwóch wektorów trójwymiarowych i jest unikalny wektor takie, że:
jest ortogonalny zarówno do i
Wielkość jest równa powierzchni równoległoboku utworzonego przez i
Kierunki , → b i → c , w tej kolejności, przestrzegaj zasady prawej ręki .
Istnieje kilka równoważnych wzorów dla produktu krzyżowego, ale jeden z nich jest następujący:
where , , and are the unit vectors in the first, second, and third dimensions.
Challenge
Given two 3D vectors, write a full program or function to find their cross product. Builtins that specifically calculate the cross product are disallowed.
Input
Two arrays of three real numbers each. If your language doesn't have arrays, the numbers still must be grouped into threes. Both vectors will have magnitude . Note that the cross product is noncommutative (), so you should have a way to specify order.
Output
Their cross product, in a reasonable format, with each component accurate to four significant figures or , whichever is looser. Scientific notation is optional.
Test cases
[3, 1, 4], [1, 5, 9]
[-11, -23, 14]
[5, 0, -3], [-3, -2, -8]
[-6, 49, -10]
[0.95972, 0.25833, 0.22140],[0.93507, -0.80917, -0.99177]
[-0.077054, 1.158846, -1.018133]
[1024.28, -2316.39, 2567.14], [-2290.77, 1941.87, 712.09]
[-6.6345e+06, -6.6101e+06, -3.3173e+06]
This is code-golf, so the shortest solution in bytes wins.
Maltysen posted a similar challenge, but the response was poor and the question wasn't edited.
źródło
Odpowiedzi:
Jelly,
141312 bytesTry it online!
How it works
Non-competing version (10 bytes)
OK, this is embarrassing, but the array manipulation language Jelly did not have a built-in for array rotation until just now. With this new built-in, we can save two additional bytes.
This uses the approach from @AlexA.'s J answer. Try it online!
How it works
źródło
LISP,
128122 bytesHi! This is my code:
I know that it isn't the shortest solution, but nobody has provided one in Lisp, until now :)
Copy and paste the following code here to try it!
źródło
Dyalog APL, 12 bytes
Based on @AlexA.'s J answer and (coincidentally) equivalent to @randomra's improvement in that answer's comment section.
Try it online on TryAPL.
How it works
źródło
J,
2714 bytesThis is a dyadic verb that accepts arrays on the left and right and returns their cross product.
Explanation:
Example:
Try it here
Saved 13 bytes thanks to randomra!
źródło
*2&|.
is a fork of two verbs:*
and2&|.
. It multiplies the left input by a rotated by 2 right input. This fork is stored inv
so when we writev~
, it is equivalent to(*2&|.)~
, where the~
swaps the left and right input parameters for the parenthesized part.C,
156154150148144 bytesNot going to be winning any prizes for length, but thought I'd have a go anyway.
Demo
Ungolfed:
źródło
for
doesn't need{}
Haskell, 41 bytes
A straightforward solution.
źródło
Bash + coreutils, 51
bc
does the arithmetic evaluation to the required precision.Input is as two comma-separated lists on the command-line. Output as newline-separated lines:
źródło
MATL, 17 bytes
First input is a, second is b.
Try it online!
Explanation
źródło
Pyth, 16 bytes
Try it online: Demonstration
Explanation:
źródło
K5,
44403732 bytesWrote this one quite a while ago and dusted it off again recently.
In action:
Edit 1:
Saved 4 bytes by taking input as a list of lists instead of two separate arguments:
Edit 2:
Saved 3 bytes by computing a lookup table with base-decode:
Edit 3:
Save 5 bytes by rearranging application to permit using a tacit definition instead of a local lambda. Unfortunately, this solution no longer works in oK, and requires the official k5 interpreter. Gonna have to take my word for this one until I fix the bug in oK:
źródło
Ruby, 49 bytes
Try it online!
Returning after 2 years, I shaved off 12 bytes by using how Ruby treats negative array indices.
-1
is the last element of the array,-2
the second last etc.Ruby, 57
In test program
źródło
Python,
7348 bytesThanks @FryAmTheEggman
This is based on the component definition of the vector cross product.
Try it here
źródło
lambda (a,b,c),(d,e,f):...
should save a lot.Jelly, 5 bytes
Takes input in the form[[x1,x2],[y1,y2],[z1,z2]] . If you want them to be two lists of x-y-z coordinates, just prepend
Z
to the beginning of the program.Try it online!
Here is a PDF explanation in case SE markdown can't handle it.
The cross-product in analytic form
Let(x1,y1,z1) be the coordinates of v1→ and (x2,y2,z2) be the coordinates of v2→ . Their analytic expressions are as follows:
The only thing left to do now is to also write their cross-product in terms of its coordinates in theOxyz space.
Keeping in mind that:i⃗ ×j⃗ =k⃗ ,i⃗ ×k⃗ =−j⃗ ,j⃗ ×i⃗ =−k⃗ ,j⃗ ×k⃗ =i⃗ ,k⃗ ×i⃗ =j⃗ ,k⃗ ×j⃗ =−i⃗
After the necessary rearrangements and calculations:
The close relationship with matrix determinants
There's an interesting thing to note here:
Where we use the notation|⋅| for matrix determinant. Notice the beautiful rotational symmetry?
Jelly code explanation
Well... not much to explain here. It just generates the matrix:
And for each pair of neighbouring matrices, it computes the determinant of the matrix formed by joining the two.
źródło
Wolfram Language (Mathematica),
3833 bytesTry it online!
źródło
ES6, 40 bytes
44 bytes if the input needs to be two arrays:
52 bytes for a more interesting version:
źródło
Julia 0.7,
4539 bytesTry it online!
Uses the determinant-based formula given in the task description.
Thanks to H.PWiz for -6 bytes.
źródło
f(a,b)=1:3 .|>i->det([eye(3)[i,:] a b])
APL(NARS), 23 chars, 46 bytes
test:
źródło
Pari/GP, 41 bytes
Try it online!
źródło