Choose Language Hide Translation Bar
Highlighted
CaseyL
Level II

How to calculate Vector Cross Product

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:

 

p1 = [1, 2, 4];
p2 = [2, 1, 4];
p3 = [2, 2, 4];

v1 = p3 - p1;
v2 = p2 - p1;

xProd = v1*v2;

 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?

3 REPLIES 3
Highlighted
cwillden
Super User

Re: How to calculate Vector Cross Product

If you're only ever dealing 3x1 (or 1x3) vectors, then I would think computational efficiency or elegance is not too important.  Would this be sufficient?

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);
-- Cameron Willden
Highlighted
Craige_Hales
Staff (Retired)

Re: How to calculate Vector Cross Product

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 xyzzy approach is simpler and just as fast.

I use it for the surface normals in 3D scenes, there is some JSL in https://community.jmp.com/t5/Uncharted/Custom-Visualization/ba-p/191559 .

Craige
Highlighted
CaseyL
Level II

Re: How to calculate Vector Cross Product

Thanks cwillden, this is essentially what  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. 

Article Labels

    There are no labels assigned to this post.