The formula referenced there
cp=:(1 _1 1 * 1 (-/ . *)\. ])@,.
compared to mine
cross=: ((1: |.[)*(_1: |. ]))-((_1: |.[)*(1:|.]))
agree for single vectors of course
]u=. rand 3
0.622471 0.324707 0.907825
]v=. rand 3
0.0631566 0.38662 0.338598
u cp v
_0.241038 _0.153432 0.220153
u cross v
_0.241038 _0.153432 0.220153
but for arays of vectors
]u=. 4 3$rand 12
0.095767 0.601479 0.285658
0.926716 0.299674 0.417604
0.687686 0.837773 0.792088
0.465073 0.605581 0.190086
]v=. 4 3$rand 12
0.732158 0.199016 0.654682
0.925557 0.409382 0.619391
0.891663 0.888594 0.716629
0.9962 0.477721 0.946355
u cross v
0.492744 _0.104753 0.277464
0.418103 _0.36774 0.313855
_0.492744 0.104753 _0.277464
_0.418103 0.36774 _0.313855
u cp v
|length error: cp
| u cp v
How do you generalize this for such arrays (or |:u arrays)?
I often take cross products of millions of vectors as part
of Monte Carlo scattering computations, so it must be fast.
Patrick
On Sat, 8 Feb 2020, R.E. Boss wrote:
https://code.jsoftware.com/wiki/Phrases/Matrices#Vector_cross_product
R.E. Boss
-----Oorspronkelijk bericht-----
Van: Programming <[email protected]>
Namens J. Patrick Harrington
Verzonden: zaterdag 8 februari 2020 15:33
Aan: [email protected]
Onderwerp: Re: [Jprogramming] cross product of two vectors
Something I've used on big arrays is this:
cross=: ((1: |.[)*(_1: |. ]))-((_1: |.[)*(1:|.]))
E.g.
v=. 2 1 7
u=. _3 4 _1
u cross v
29 19 _11
rand=: ?@# 0:
u=. 1e6 3$rand 3e6
v=. 1e6 3$rand 3e6
ts'z=. u cross v' NB. time & space
0.054138 1.00665e8
$z
1000000 3
Give it a try :-)
Patrick
On Sat, 8 Feb 2020, [email protected] wrote:
Dear list,
is there a concise way to calculate a cross product of 2 vectors (in
3D, of course).
Thanks
Ruda
----------------------------------------------------------------------
For information about J forums see
http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm