# 找到叉积

20

• ﻿$$c⃗ C→\vec c$$正交于$$a⃗ 一种→\vec a$$$$b⃗ b→\vec b$$

• 的大小$$c⃗ C→\vec c$$等于平行四边形的通过形成在区域$$a⃗ 一种→\vec a$$$$b⃗ b→\vec b$$

• 的方向$$a⃗ 一种→\vec a$$$$b⃗ b→\vec b$$，和$$c⃗ C→\vec c$$，按照这个顺序，遵循右手定则



$\stackrel{\to }{a}×\stackrel{\to }{b}=det\left[\begin{array}{ccc}\stackrel{\to }{i}& \stackrel{\to }{j}& \stackrel{\to }{k}\\ {a}_{1}& {a}_{2}& {a}_{3}\\ {b}_{1}& {b}_{2}& {b}_{3}\end{array}\right]$

### 测试用例

[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]


lirtosiast '16

14

# 果冻，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]].
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字节）

ṙ-×
ç_ç@ṙ-


### 怎么运行的

ṙ-×     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).


ETHproductions 2016年

10

## LISP，128个 122字节

(defmacro D(x y)(list(*(cadr,x)(caddr,y))(*(caddr,x)(car,y))(*(car,x)(cadr,y))))(defun c(a b)(mapcar #'- (D a b)(D b a)))


(defmacro D(x y)(list(*(cadr,x)(caddr,y))(*(caddr,x)(car,y))(*(car,x)(cadr,y))))(defun c(a b)(mapcar #'- (D a b)(D b a)))

(format T "Inputs: (3 1 4), (1 5 9)~%")
(format T "Result ~S~%~%" (c '(3 1 4) '(1 5 9)))

(format T "Inputs: (5 0 -3), (-3 -2 -8)~%")
(format T "Result ~S~%~%" (c '(5 0 -3) '(-3 -2 -8)))

(format T "Inputs: (0.95972 0.25833 0.22140), (0.93507 -0.80917 -0.99177)~%")
(format T "Result ~S~%" (c '(0.95972 0.25833 0.22140) '(0.93507 -0.80917 -0.99177)))

(format T "Inputs: (1024.28 -2316.39 2567.14), (-2290.77 1941.87 712.09)~%")
(format T "Result ~S~%" (c '(1024.28 -2316.39 2567.14) '(-2290.77 1941.87 712.09)))


wizzwizz4 2016年

9

# Dyalog APL，12个字节

2⌽p⍨-p←⊣×2⌽⊢


TryAPL上在线尝试。

### 怎么运行的

2⌽p⍨-p←⊣×2⌽⊢  Dyadic function.
Left argument: a = [a1, a2, a3]. Right argument: b = [b1, b2, b3].

2⌽⊢  Rotate b 2 units to the left. Yields [b3, b1, b2].
⊣×     Multiply the result by a. Yields [a1b3, a2b1, a3b2].
p←       Save the tacit function to the right (NOT the result) in p.
p⍨          Apply p to b and a (reversed). Yields [b1a3, b2a1, b3a2].
-         Subtract the right result (p) from the left one (p⍨).
This yields [a3b1 - a1b3, a1b2 - a2b1, a2b3 - a3b2].
2⌽            Rotate the result 2 units to the left.
This yields [a2b3 - a3b2, a3b1 - a1b3, a1b2 - a2b1].


9

# J，27 14字节

2|.v~-v=.*2&|.


         *2&|.     NB. Dyadic verb: Left input * twice-rotated right input
v=.          NB. Locally assign to v
v~-             NB. Commute arguments, negate left
2|.                NB. Left rotate twice


    f =: 2|.v~-v=.*2&|.
3 1 4 f 1 5 9
_11 _23 14


@randomra太好了，谢谢！我不是J专家，所以我仍在弄清楚它的工作原理，但是我有一个总体思路。
Alex A.

randomra

@randomra好的，这很有道理。再次感谢！
Alex A.

6

# C，156个154 150 148 144字节

#include <stdio.h>
main(){float v[6];int i=7,j,k;for(;--i;)scanf("%f",v+6-i);for(i=1;i<4;)j=i%3,k=++i%3,printf("%f ",v[j]*v[k+3]-v[k]*v[j+3]);}


• 输入是用换行符或空格分隔的列表（即a1 a2 a3 b1 b2 b3），输出是用空格分隔的列表（即c1 c2 c3）。
• 循环排列两个输入向量的索引以计算乘积-比写出行列式占用更少的字符！

#include <cstdio>
int main()
{
float v[6];
int i = 7, j, k;
for (; --i; ) scanf("%f", v + 6 - 1);
for (i = 1; i < 4; )
j = i % 3,
k = ++i % 3,
printf("%f ", v[j] * v[k + 3] - v[k] * v[j + 3]);
}


1

wizzwizz4 2016年

2

calvinsykes '16

1

Alchymist '02

calvinsykes '16

4

x(a,b,c)(d,e,f)=(b*f-c*e,c*d-a*f,a*e-b*d)

4

# Bash + coreutils，51岁

eval set {$1}*{$2}
bc<<<"scale=4;$6-$8;$7-$3;$2-$4"

• 第1行构造了一个括号扩展，该扩展给出了两个向量的笛卡尔积，并将它们设置为位置参数。
• 第2行减去相应的项；bc对所需的精度进行算术评估。

$./crossprod.sh 0.95972,0.25833,0.22140 0.93507,-0.80917,-0.99177 -.07705 1.15884 -1.01812$


4

# MATL，17个字节

!*[6,7,2;8,3,4])d


### 说明

!              % input b as a row array and transpose into a column array
*              % input a as a row array. Compute 3x3 matrix of pairwise products
[6,7,2;8,3,4]  % 2x3 matrix that picks elements from the former in column-major order
)              % apply index
d              % difference within each column


4

# Pyth，16个字节

-VF*VM.<VLQ_BMS2


### 说明：

-VF*VM.<VLQ_BMS2   Q = input, pair of vectors [u, v]
S2   creates the list [1, 2]
_BM     transforms it to [[1, -1], [2, -2]]
.<VLQ        rotate of the input vectors accordingly to the left:
[[u by 1, v by -1], [u by 2, v by -2]]
*VM             vectorized multiplication for each of the vector-pairs
-VF                vectorized subtraction of the resulting two vectors


3

# K5，44个40 37 32字节

{{x[y]-x[|y]}[*/x@']'3 3\'5 6 1}


 cross: {{x[y]-x[|y]}[*/x@']'3 3\'5 6 1};

cross (3 1 4;1 5 9)
-11 -23 14
cross (0.95972 0.25833 0.22140;0.93507 -0.80917 -0.99177)
-7.705371e-2 1.158846 -1.018133


## 编辑1：

old: {m:{*/x@'y}(x;y);{m[x]-m[|x]}'(1 2;2 0;0 1)}
new: {m:{*/x@'y}x    ;{m[x]-m[|x]}'(1 2;2 0;0 1)}


## 编辑2：

old: {m:{*/x@'y}x;{m[x]-m[|x]}'(1 2;2 0;0 1)}
new: {m:{*/x@'y}x;{m[x]-m[|x]}'3 3\'5 6 1}


## 编辑3：

old: {m:{*/x@'y}x;{m[x]-m[|x]}'3 3\'5 6 1}
new: {{x[y]-x[|y]}[*/x@']     '3 3\'5 6 1}


3

# 红宝石，49字节

->u,v{(0..2).map{|a|u[a-2]*v[a-1]-u[a-1]*v[a-2]}}

2年后返回，我使用Ruby处理负数组索引的方式减少了12个字节。-1是数组的最后一个元素，倒数-2第二个，依此类推。

# Ruby，57岁

->u,v{(0..2).map{|a|u[b=(a+1)%3]*v[c=(a+2)%3]-u[c]*v[b]}}

f=->u,v{(0..2).map{|a|u[b=(a+1)%3]*v[c=(a+2)%3]-u[c]*v[b]}}

p f[[3, 1, 4], [1, 5, 9]]

p f[[5, 0, -3], [-3, -2, -8]]

p f[[0.95972, 0.25833, 0.22140],[0.93507, -0.80917, -0.99177]]

p f[[1024.28, -2316.39, 2567.14], [-2290.77, 1941.87, 712.09]]

2

# Python，73 48字节

lambda (a,b,c),(d,e,f):[b*f-c*e,c*d-a*f,a*e-b*d]


lambda (a,b,c),(d,e,f):...应该节省很多。
FryAmTheEggman '16

@FryAmTheEggman你是对的。我忘记了lambda可以指定参数的方式。
TanMath

2

# 果冻，5个字节

$$[ [ x1个，X2] ，[ y1个，ÿ2] ，[ ž1个，ž2] ][[X1个，X2]，[ÿ1个，ÿ2]，[ž1个，ž2]][[x_1,x_2],[y_1,y_2],[z_1,z_2]]$$Z

ṁ4ÆḊƝ


### 分析形式的叉积

$$（x1个，ÿ1个，ž1个）（X1个，ÿ1个，ž1个）(x_1,y_1,z_1)$$$$v1个→v1个→\vec{v_1}$$$$（x2，ÿ2，ž2）（X2，ÿ2，ž2）(x_2,y_2,z_2)$$$$v2→v2→\vec{v_2}$$



$\stackrel{\mathbf{\to }}{{\mathbit{v}}_{\mathbf{1个}}}={X}_{1个}\cdot \stackrel{\mathbf{\to }}{\mathbit{一世}}+{ÿ}_{1个}\cdot \stackrel{\mathbf{\to }}{\mathbit{Ĵ}}+{ž}_{1个}\cdot \stackrel{\mathbf{\to }}{\mathbit{ķ}}$
$$v2→= x2⋅ 我⃗ + y2⋅ Ĵ⃗ + z2⋅ ķ⃗ v2→=X2⋅一世→+ÿ2⋅Ĵ→+ž2⋅ķ→\boldsymbol{\vec{v_2}}=x_2\cdot \boldsymbol{\vec{i}}+y_2\cdot \boldsymbol{\vec{j}}+z_2\cdot\boldsymbol{\vec{k}}$$

$$Ø X ÿžØXÿžOxyz$$



$\stackrel{\mathbf{\to }}{{\mathbit{v}}_{\mathbf{1个}}}×\stackrel{\mathbf{\to }}{{\mathbit{v}}_{\mathbf{2}}}=（{X}_{1个}\cdot \stackrel{\mathbf{\to }}{\mathbit{一世}}+{ÿ}_{1个}\cdot \stackrel{\mathbf{\to }}{\mathbit{Ĵ}}+{ž}_{1个}\cdot \stackrel{\mathbf{\to }}{\mathbit{ķ}}）×（{X}_{2}\cdot \stackrel{\mathbf{\to }}{\mathbit{一世}}+{ÿ}_{2}\cdot \stackrel{\mathbf{\to }}{\mathbit{Ĵ}}+{ž}_{2}\cdot \stackrel{\mathbf{\to }}{\mathbit{ķ}}）$



$\stackrel{\mathbf{\to }}{\mathbit{一世}}×\stackrel{\mathbf{\to }}{\mathbit{Ĵ}}=\stackrel{\mathbf{\to }}{\mathbit{ķ}}，\phantom{\rule{mediummathspace}{0ex}}\phantom{\rule{mediummathspace}{0ex}}\stackrel{\mathbf{\to }}{\mathbit{一世}}×\stackrel{\mathbf{\to }}{\mathbit{ķ}}=-\stackrel{\mathbf{\to }}{\mathbit{Ĵ}}，\phantom{\rule{mediummathspace}{0ex}}\phantom{\rule{mediummathspace}{0ex}}\stackrel{\mathbf{\to }}{\mathbit{Ĵ}}×\stackrel{\mathbf{\to }}{\mathbit{一世}}=-\stackrel{\mathbf{\to }}{\mathbit{ķ}}，\phantom{\rule{mediummathspace}{0ex}}\phantom{\rule{mediummathspace}{0ex}}\stackrel{\mathbf{\to }}{\mathbit{Ĵ}}×\stackrel{\mathbf{\to }}{\mathbit{ķ}}=\stackrel{\mathbf{\to }}{\mathbit{一世}}，\phantom{\rule{mediummathspace}{0ex}}\phantom{\rule{mediummathspace}{0ex}}\stackrel{\mathbf{\to }}{\mathbit{ķ}}×\stackrel{\mathbf{\to }}{\mathbit{一世}}=\stackrel{\mathbf{\to }}{\mathbit{Ĵ}}，\phantom{\rule{mediummathspace}{0ex}}\phantom{\rule{mediummathspace}{0ex}}\stackrel{\mathbf{\to }}{\mathbit{ķ}}×\stackrel{\mathbf{\to }}{\mathbit{Ĵ}}=-\stackrel{\mathbf{\to }}{\mathbit{一世}}$



$\stackrel{\mathbf{\to }}{{\mathbit{v}}_{\mathbf{1个}}}×\stackrel{\mathbf{\to }}{{\mathbit{v}}_{\mathbf{2}}}=（{ÿ}_{1个}{ž}_{2}-{ž}_{1个}{ÿ}_{2}）\cdot \stackrel{\mathbf{\to }}{\mathbit{一世}}+（{ž}_{1个}{X}_{2}-{X}_{1个}{ž}_{2}）\cdot \stackrel{\mathbf{\to }}{\mathbit{Ĵ}}+（{X}_{1个}{ÿ}_{2}-{ÿ}_{1个}{X}_{2}）\cdot \stackrel{\mathbf{\to }}{\mathbit{ķ}}$

### 与矩阵行列式的密切关系



$$ž1个X2− x1个ž2= ∣∣∣ž1个 ž2X1个X2∣∣∣ž1个X2-X1个ž2=|ž1个X1个 ž2X2|z_1x_2-x_1z_2=\left|\begin{matrix}z_1 & x_1 \\\ z_2 & x_2\end{matrix}\right|$$ $$ÿ1个ž2- ž1个ÿ2= ∣∣∣ÿ1个 ÿ2ž1个ž2∣∣∣ÿ1个ž2-ž1个ÿ2=|ÿ1个ž1个 ÿ2ž2|y_1z_2-z_1y_2=\left|\begin{matrix}y_1 & z_1 \\\ y_2 & z_2\end{matrix}\right|$$

$$| ⋅ ||⋅|\left|\cdot\right|$$

### 果冻代码说明



ṁ4ÆḊƝ – Monadic Link. Takes input as [[x1,x2],[y1,y2],[z1,z2]].
ṁ4    – Mold 4. Cycle the list up to length 4, reusing the elements if necessary.
Generates [[x1,x2],[y1,y2],[z1,z2],[x1,x2]].
Ɲ – For each pair of neighbours: [[x1,x2],[y1,y2]], [[y1,y2],[z1,z2]], [[z1,z2],[x1,x2]].
ÆḊ  – Compute the determinant of those 2 paired together into a single matrix.


2

# Wolfram语言（Mathematica），38 33字节

Det@{a={i,j,k},##}~Coefficient~a&


1

## ES6，40个字节

(a,b,c,d,e,f)=>[b*f-c*e,c*d-a*f,a*e-b*d]


([a,b,c],[d,e,f])=>[b*f-c*e,c*d-a*f,a*e-b*d]


52字节是更有趣的版本：

(a,b)=>a.map((_,i)=>a[x=++i%3]*b[y=++i%3]-a[y]*b[x])


1

# 朱莉娅0.7，45个 39字节

f(a,b)=1:3 .|>i->det([eye(3)[i,:] a b])

39个字节的两个技巧：f(a,b)=1:3 .|>i->det([eye(3)[i,:] a b])
H.PWiz

0

# APL（NARS），23个字符，46个字节

{((1⌽⍺)×5⌽⍵)-(5⌽⍺)×1⌽⍵}


  f←{((1⌽⍺)×5⌽⍵)-(5⌽⍺)×1⌽⍵}
(3 1 4) f (1 5 9)
¯11 ¯23 14
(5 0 ¯3) f (¯3 ¯2 ¯8)
¯6 49 ¯10
(0.95972 0.25833 0.22140) f (0.93507 ¯0.80917 ¯0.99177)
¯0.0770537061 1.158846002 ¯1.018133265
(1024.28 ¯2316.39 2567.14) f (¯2290.77 1941.87 712.09)
¯6634530.307 ¯6610106.843 ¯3317298.117