<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to calculate Vector Cross Product in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/How-to-calculate-Vector-Cross-Product/m-p/610312#M81115</link>
    <description>&lt;P&gt;Sorry for reviving this zombie thread, but there is a sign error in the second element here.&amp;nbsp; I believe it should be&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Matrix(
		{v1[2] * v2[3] - v1[3] * v2[2], v1[3] * v2[1] - v1[1] * v2[3], v1[1] * v2[2] - v1[2] * v2[1]}&lt;BR /&gt;)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Just want to leave this here for anyone else who comes along for a quick copy-paste.&lt;/P&gt;</description>
    <pubDate>Thu, 09 Mar 2023 19:24:34 GMT</pubDate>
    <dc:creator>klk</dc:creator>
    <dc:date>2023-03-09T19:24:34Z</dc:date>
    <item>
      <title>How to calculate Vector Cross Product</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-calculate-Vector-Cross-Product/m-p/236281#M46630</link>
      <description>&lt;P&gt;From the documentation it looks like the * operator will perform a cross product and ;* will perform a dot product. I want to use a cross product to determine the vector normal to a pair of known vectors like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;p1 = [1, 2, 4];
p2 = [2, 1, 4];
p3 = [2, 2, 4];

v1 = p3 - p1;
v2 = p2 - p1;

xProd = v1*v2;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;3 points define a plane, two vectors taken from those points do the same, the cross-product should give the normal vector of that plane. The problem seems to be that the dimensions of the two matrices being multiplied (v1 and v2) do not agree -- nRows(v1) should equal nCols(v2). I can implement this manually with a simplified formula for vector cross products but does anyone know how to format this differently to work with matrix multiplication?&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2019 15:23:00 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-calculate-Vector-Cross-Product/m-p/236281#M46630</guid>
      <dc:creator>CaseyL</dc:creator>
      <dc:date>2019-11-25T15:23:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate Vector Cross Product</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-calculate-Vector-Cross-Product/m-p/236294#M46633</link>
      <description>&lt;P&gt;If you're only ever dealing 3x1 (or 1x3) vectors, then I would think computational efficiency or elegance is not too important.&amp;nbsp; Would this be sufficient?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;cross_prod = function({v1, v2},
	matrix({v1[2]*v2[3] - v1[3]*v2[2], v1[1]*v2[3] - v1[3]*v2[1], v1[1]*v2[2] - v1[2]*v2[1]});
);

cross_prod(v1, v2);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Nov 2019 16:58:35 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-calculate-Vector-Cross-Product/m-p/236294#M46633</guid>
      <dc:creator>cwillden</dc:creator>
      <dc:date>2019-11-25T16:58:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate Vector Cross Product</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-calculate-Vector-Cross-Product/m-p/236298#M46636</link>
      <description>&lt;P&gt;I hope someone comes up with a clever way to do this. The det() function is closely related, but I don't understand how to use det(3x3 matrix) to get back more than a scalar value. I think you could use det(2x2 sub-matrix) three times, but I think the straight-forward &lt;A href="https://en.wikipedia.org/wiki/Cross_product#Mnemonic" target="_blank" rel="noopener"&gt;xyzzy&lt;/A&gt; approach is simpler and just as fast.&lt;/P&gt;&lt;P&gt;I use it for the surface normals in 3D scenes, there is some JSL in &lt;A href="https://community.jmp.com/t5/Uncharted/Custom-Visualization/ba-p/191559" target="_blank" rel="noopener"&gt;https://community.jmp.com/t5/Uncharted/Custom-Visualization/ba-p/191559&lt;/A&gt;&amp;nbsp;.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2019 16:55:16 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-calculate-Vector-Cross-Product/m-p/236298#M46636</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2019-11-25T16:55:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate Vector Cross Product</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-calculate-Vector-Cross-Product/m-p/236315#M46641</link>
      <description>&lt;P&gt;Thanks cwillden, this is essentially what&amp;nbsp; I have implemented. It does work for me since I am only concerned with 3D vectors, but I am supprised this isn't a built in function. I'm going to leave this open for a bit to see if anyone else knows of such a way.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2019 18:29:47 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-calculate-Vector-Cross-Product/m-p/236315#M46641</guid>
      <dc:creator>CaseyL</dc:creator>
      <dc:date>2019-11-25T18:29:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate Vector Cross Product</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-calculate-Vector-Cross-Product/m-p/610312#M81115</link>
      <description>&lt;P&gt;Sorry for reviving this zombie thread, but there is a sign error in the second element here.&amp;nbsp; I believe it should be&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Matrix(
		{v1[2] * v2[3] - v1[3] * v2[2], v1[3] * v2[1] - v1[1] * v2[3], v1[1] * v2[2] - v1[2] * v2[1]}&lt;BR /&gt;)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Just want to leave this here for anyone else who comes along for a quick copy-paste.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Mar 2023 19:24:34 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-calculate-Vector-Cross-Product/m-p/610312#M81115</guid>
      <dc:creator>klk</dc:creator>
      <dc:date>2023-03-09T19:24:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate Vector Cross Product</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-calculate-Vector-Cross-Product/m-p/611656#M81192</link>
      <description>&lt;P&gt;Well, the revived zombie caught me. Here is a way to do it via determinants as&amp;nbsp;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/982"&gt;@Craige_Hales&lt;/a&gt; mentioned. It is short, but I don't know... it isn't very simple.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;crossP = Function( {x, y, i = 1},
	Return( J( 3, 1, Det( (x || y)[Remove( [1, 2, 3], i++ ), 0] ) ) )
);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i.e., using the originally-supplied data:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="brady_brady_0-1678770863902.png" style="width: 999px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/50994iC68731CC95838C56/image-size/large?v=v2&amp;amp;px=999" role="button" title="brady_brady_0-1678770863902.png" alt="brady_brady_0-1678770863902.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Mar 2023 05:19:15 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-calculate-Vector-Cross-Product/m-p/611656#M81192</guid>
      <dc:creator>brady_brady</dc:creator>
      <dc:date>2023-03-14T05:19:15Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate Vector Cross Product</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-calculate-Vector-Cross-Product/m-p/611720#M81201</link>
      <description>&lt;P&gt;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/3552"&gt;@brady_brady&lt;/a&gt;&amp;nbsp; Cool! It needs a tweak if you are going to use it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;	i = 1;
	j = -1;
	J( 3, 1, (j *= -1) * Det( (v1 || v2)[Remove( [1, 2, 3], i++ ), 0] ) );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;CODE class=" language-jsl"&gt;&lt;/CODE&gt;&lt;A href="https://mathinsight.org/cross_product_formula" target="_blank"&gt;https://mathinsight.org/cross_product_formula&lt;/A&gt; shows how the middle element needs to be negative.&amp;nbsp; Above, j will alternate sign (1, -1, 1), and i increases (1, 2, 3). The J(...) function has an implicit loop that evaluates the 3rd argument for each element.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The straight forward corrected formula from&amp;nbsp;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/11962"&gt;@klk&lt;/a&gt;&amp;nbsp; is about 3 times faster and about N times easier to understand.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Test code. &lt;/P&gt;
&lt;LI-SPOILER&gt;&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;v1 = [0, 0, 1];
v2 = [0, 1, 0];

start = HP Time();
For( t = 1, t &amp;lt; 1e6, t += 1,
	i = 1;
	j = -1;
	J( 3, 1, (j *= -1) * Det( (v1 || v2)[Remove( [1, 2, 3], i++ ), 0] ) );
);
stop = HP Time();
Show( (stop - start) / 1e6 );//9.3s


start = HP Time();
For( t = 1, t &amp;lt; 1e6, t += 1,
	Matrix( {v1[2] * v2[3] - v1[3] * v2[2], v1[3] * v2[1] - v1[1] * v2[3], v1[1] * v2[2] - v1[2] * v2[1]} )
);
stop = HP Time();
Show( (stop - start) / 1e6 );//3.2s


For( q = 1, q &amp;lt; 10000, q += 1,
	v1 = J( 3, 1, Random Uniform( -1, 1 ) );
	v2 = J( 3, 1, Random Uniform( -1, 1 ) );
	If(
		All(
			Round( Matrix( {v1[2] * v2[3] - v1[3] * v2[2], v1[3] * v2[1] - v1[1] * v2[3], v1[1] * v2[2] - v1[2] * v2[1]} ), 10 ) //
			== //
			(i = 1; j = -1 ; Round( J( 3, 1, (j *= -1) * Det( (v1 || v2)[Remove( [1, 2, 3], i++ ), 0] ) ), 10 ) ; ) //
		) == 0//
	, //
		Throw( Char( q ) )
	);
);&lt;/CODE&gt;&lt;/PRE&gt;
Curiously, rounding to 11 places occasionally fails to match results. Probably nearly collinear vectors.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI-SPOILER&gt;</description>
      <pubDate>Tue, 14 Mar 2023 11:43:48 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-calculate-Vector-Cross-Product/m-p/611720#M81201</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2023-03-14T11:43:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate Vector Cross Product</title>
      <link>https://community.jmp.com/t5/Discussions/How-to-calculate-Vector-Cross-Product/m-p/611821#M81212</link>
      <description>&lt;P&gt;The inferior performance doesn't surprise... thanks for inserting the sign adjustment. I omitted the "sign checkerboard" inadvertently.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;IMO this is one of those instances where "just because you can, doesn't mean you should" applies... {cool, fun, short} doesn't outweigh {understandable, maintainable, speedy}.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Mar 2023 15:02:52 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/How-to-calculate-Vector-Cross-Product/m-p/611821#M81212</guid>
      <dc:creator>brady_brady</dc:creator>
      <dc:date>2023-03-14T15:02:52Z</dc:date>
    </item>
  </channel>
</rss>

