cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
%3CLINGO-SUB%20id%3D%22lingo-sub-236281%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EHow%20to%20calculate%20Vector%20Cross%20Product%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-236281%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EFrom%20the%20documentation%20it%20looks%20like%20the%20*%20operator%20will%20perform%20a%20cross%20product%20and%20%3B*%20will%20perform%20a%20dot%20product.%20I%20want%20to%20use%20a%20cross%20product%20to%20determine%20the%20vector%20normal%20to%20a%20pair%20of%20known%20vectors%20like%20this%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Ep1%20%3D%20%5B1%2C%202%2C%204%5D%3B%0Ap2%20%3D%20%5B2%2C%201%2C%204%5D%3B%0Ap3%20%3D%20%5B2%2C%202%2C%204%5D%3B%0A%0Av1%20%3D%20p3%20-%20p1%3B%0Av2%20%3D%20p2%20-%20p1%3B%0A%0AxProd%20%3D%20v1*v2%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B3%20points%20define%20a%20plane%2C%20two%20vectors%20taken%20from%20those%20points%20do%20the%20same%2C%20the%20cross-product%20should%20give%20the%20normal%20vector%20of%20that%20plane.%20The%20problem%20seems%20to%20be%20that%20the%20dimensions%20of%20the%20two%20matrices%20being%20multiplied%20(v1%20and%20v2)%20do%20not%20agree%20--%20nRows(v1)%20should%20equal%20nCols(v2).%20I%20can%20implement%20this%20manually%20with%20a%20simplified%20formula%20for%20vector%20cross%20products%20but%20does%20anyone%20know%20how%20to%20format%20this%20differently%20to%20work%20with%20matrix%20multiplication%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-611821%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20How%20to%20calculate%20Vector%20Cross%20Product%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-611821%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EThe%20inferior%20performance%20doesn't%20surprise...%20thanks%20for%20inserting%20the%20sign%20adjustment.%20I%20omitted%20the%20%22sign%20checkerboard%22%20inadvertently.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIMO%20this%20is%20one%20of%20those%20instances%20where%20%22just%20because%20you%20can%2C%20doesn't%20mean%20you%20should%22%20applies...%20%7Bcool%2C%20fun%2C%20short%7D%20doesn't%20outweigh%20%7Bunderstandable%2C%20maintainable%2C%20speedy%7D.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-611720%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20How%20to%20calculate%20Vector%20Cross%20Product%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-611720%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F3552%22%20target%3D%22_blank%22%3E%40brady_brady%3C%2FA%3E%26nbsp%3B%20Cool!%20It%20needs%20a%20tweak%20if%20you%20are%20going%20to%20use%20it%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3E%20i%20%3D%201%3B%0A%20j%20%3D%20-1%3B%0A%20J(%203%2C%201%2C%20(j%20*%3D%20-1)%20*%20Det(%20(v1%20%7C%7C%20v2)%5BRemove(%20%5B1%2C%202%2C%203%5D%2C%20i%2B%2B%20)%2C%200%5D%20)%20)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3CCODE%20class%3D%22%20language-jsl%22%3E%3C%2FCODE%3E%3CA%20href%3D%22https%3A%2F%2Fmathinsight.org%2Fcross_product_formula%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fmathinsight.org%2Fcross_product_formula%3C%2FA%3E%20shows%20how%20the%20middle%20element%20needs%20to%20be%20negative.%26nbsp%3B%20Above%2C%20j%20will%20alternate%20sign%20(1%2C%20-1%2C%201)%2C%20and%20i%20increases%20(1%2C%202%2C%203).%20The%20J(...)%20function%20has%20an%20implicit%20loop%20that%20evaluates%20the%203rd%20argument%20for%20each%20element.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20straight%20forward%20corrected%20formula%20from%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F11962%22%20target%3D%22_blank%22%3E%40klk%3C%2FA%3E%26nbsp%3B%20is%20about%203%20times%20faster%20and%20about%20N%20times%20easier%20to%20understand.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETest%20code.%3C%2FP%3E%0A%3CDIV%20class%3D%22lia-spoiler-container%22%3E%3CA%20class%3D%22lia-spoiler-link%22%20href%3D%22%23%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%20target%3D%22_blank%22%3EView%20more...%3C%2FA%3E%3CNOSCRIPT%3E%3CDIV%20class%3D%22lia-spoiler-content%22%3E%3CBR%20%2F%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Ev1%20%3D%20%5B0%2C%200%2C%201%5D%3B%0Av2%20%3D%20%5B0%2C%201%2C%200%5D%3B%0A%0Astart%20%3D%20HP%20Time()%3B%0AFor(%20t%20%3D%201%2C%20t%20%26lt%3B%201e6%2C%20t%20%2B%3D%201%2C%0A%20i%20%3D%201%3B%0A%20j%20%3D%20-1%3B%0A%20J(%203%2C%201%2C%20(j%20*%3D%20-1)%20*%20Det(%20(v1%20%7C%7C%20v2)%5BRemove(%20%5B1%2C%202%2C%203%5D%2C%20i%2B%2B%20)%2C%200%5D%20)%20)%3B%0A)%3B%0Astop%20%3D%20HP%20Time()%3B%0AShow(%20(stop%20-%20start)%20%2F%201e6%20)%3B%2F%2F9.3s%0A%0A%0Astart%20%3D%20HP%20Time()%3B%0AFor(%20t%20%3D%201%2C%20t%20%26lt%3B%201e6%2C%20t%20%2B%3D%201%2C%0A%20Matrix(%20%7Bv1%5B2%5D%20*%20v2%5B3%5D%20-%20v1%5B3%5D%20*%20v2%5B2%5D%2C%20v1%5B3%5D%20*%20v2%5B1%5D%20-%20v1%5B1%5D%20*%20v2%5B3%5D%2C%20v1%5B1%5D%20*%20v2%5B2%5D%20-%20v1%5B2%5D%20*%20v2%5B1%5D%7D%20)%0A)%3B%0Astop%20%3D%20HP%20Time()%3B%0AShow(%20(stop%20-%20start)%20%2F%201e6%20)%3B%2F%2F3.2s%0A%0A%0AFor(%20q%20%3D%201%2C%20q%20%26lt%3B%2010000%2C%20q%20%2B%3D%201%2C%0A%20v1%20%3D%20J(%203%2C%201%2C%20Random%20Uniform(%20-1%2C%201%20)%20)%3B%0A%20v2%20%3D%20J(%203%2C%201%2C%20Random%20Uniform(%20-1%2C%201%20)%20)%3B%0A%20If(%0A%20%20All(%0A%20%20%20Round(%20Matrix(%20%7Bv1%5B2%5D%20*%20v2%5B3%5D%20-%20v1%5B3%5D%20*%20v2%5B2%5D%2C%20v1%5B3%5D%20*%20v2%5B1%5D%20-%20v1%5B1%5D%20*%20v2%5B3%5D%2C%20v1%5B1%5D%20*%20v2%5B2%5D%20-%20v1%5B2%5D%20*%20v2%5B1%5D%7D%20)%2C%2010%20)%20%2F%2F%0A%20%20%20%3D%3D%20%2F%2F%0A%20%20%20(i%20%3D%201%3B%20j%20%3D%20-1%20%3B%20Round(%20J(%203%2C%201%2C%20(j%20*%3D%20-1)%20*%20Det(%20(v1%20%7C%7C%20v2)%5BRemove(%20%5B1%2C%202%2C%203%5D%2C%20i%2B%2B%20)%2C%200%5D%20)%20)%2C%2010%20)%20%3B%20)%20%2F%2F%0A%20%20)%20%3D%3D%200%2F%2F%0A%20%2C%20%2F%2F%0A%20%20Throw(%20Char(%20q%20)%20)%0A%20)%3B%0A)%3B%3C%2FCODE%3E%3C%2FPRE%3ECuriously%2C%20rounding%20to%2011%20places%20occasionally%20fails%20to%20match%20results.%20Probably%20nearly%20collinear%20vectors.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3CNOSCRIPT%3E%3CDIV%20class%3D%22lia-spoiler-noscript-content%22%3Ev1%20%3D%20%5B0%2C%200%2C%201%5D%3B%20v2%20%3D%20%5B0%2C%201%2C%200%5D%3B%20start%20%3D%20HP%20Time()%3B%20For(%20t%20%3D%201%2C%20t%20%26lt%3B%201e6%2C%20t%20%2B%3D%201%2C%20i%20%3D%201%3B%20j%20%3D%20-1%3B%20J(%203%2C%201%2C%20(j%20*%3D%20-1)%20*%20Det(%20(v1%20%7C%7C%20v2)%5BRemove(%20%5B1%2C%202%2C%203%5D%2C%20i%2B%2B%20)%2C%200%5D%20)%20)%3B%20)%3B%20stop%20%3D%20HP%20Time()%3B%20Show(%20(stop%20-%20start)%20%2F%201e6%20)%3B%2F%2F9.3s%20start%20%3D%20HP%20Time()%3B%20For(%20t%20%3D%201%2C%20t%20%26lt%3B%201e6%2C%20t%20%2B%3D%201%2C%20Matrix(%20%7Bv1%5B2%5D%20*%20v2%5B3%5D%20-%20v1%5B3%5D%20*%20v2%5B2%5D%2C%20v1%5B3%5D%20*%20v2%5B1%5D%20-%20v1%5B1%5D%20*%20v2%5B3%5D%2C%20v1%5B1%5D%20*%20v2%5B2%5D%20-%20v1%5B2%5D%20*%20v2%5B1%5D%7D%20)%20)%3B%20stop%20%3D%20HP%20Time()%3B%20Show(%20(stop%20-%20start)%20%2F%201e6%20)%3B%2F%2F3.2s%20For(%20q%20%3D%201%2C%20q%20%26lt%3B%2010000%2C%20q%20%2B%3D%201%2C%20v1%20%3D%20J(%203%2C%201%2C%20Random%20Uniform(%20-1%2C%201%20)%20)%3B%20v2%20%3D%20J(%203%2C%201%2C%20Random%20Uniform(%20-1%2C%201%20)%20)%3B%20If(%20All(%20Round(%20Matrix(%20%7Bv1%5B2%5D%20*%20v2%5B3%5D%20-%20v1%5B3%5D%20*%20v2%5B2%5D%2C%20v1%5B3%5D%20*%20v2%5B1%5D%20-%20v1%5B1%5D%20*%20v2%5B3%5D%2C%20v1%5B1%5D%20*%20v2%5B2%5D%20-%20v1%5B2%5D%20*%20v2%5B1%5D%7D%20)%2C%2010%20)%20%2F%2F%20%3D%3D%20%2F%2F%20(i%20%3D%201%3B%20j%20%3D%20-1%20%3B%20Round(%20J(%203%2C%201%2C%20(j%20*%3D%20-1)%20*%20Det(%20(v1%20%7C%7C%20v2)%5BRemove(%20%5B1%2C%202%2C%203%5D%2C%20i%2B%2B%20)%2C%200%5D%20)%20)%2C%2010%20)%20%3B%20)%20%2F%2F%20)%20%3D%3D%200%2F%2F%20%2C%20%2F%2F%20Throw(%20Char(%20q%20)%20)%20)%3B%20)%3B%20Curiously%2C%20rounding%20to%2011%20places%20occasionally%20fails%20to%20match%20results.%20Probably%20nearly%20collinear%20vectors.%3C%2FDIV%3E%3C%2FNOSCRIPT%3E%3C%2FNOSCRIPT%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-611656%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20How%20to%20calculate%20Vector%20Cross%20Product%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-611656%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EWell%2C%20the%20revived%20zombie%20caught%20me.%20Here%20is%20a%20way%20to%20do%20it%20via%20determinants%20as%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F982%22%20target%3D%22_blank%22%3E%40Craige_Hales%3C%2FA%3E%20mentioned.%20It%20is%20short%2C%20but%20I%20don't%20know...%20it%20isn't%20very%20simple.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3EcrossP%20%3D%20Function(%20%7Bx%2C%20y%2C%20i%20%3D%201%7D%2C%0A%20Return(%20J(%203%2C%201%2C%20Det(%20(x%20%7C%7C%20y)%5BRemove(%20%5B1%2C%202%2C%203%5D%2C%20i%2B%2B%20)%2C%200%5D%20)%20)%20)%0A)%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Ei.e.%2C%20using%20the%20originally-supplied%20data%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22brady_brady_0-1678770863902.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22brady_brady_0-1678770863902.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22brady_brady_0-1678770863902.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F50994iC68731CC95838C56%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22brady_brady_0-1678770863902.png%22%20alt%3D%22brady_brady_0-1678770863902.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-610312%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20How%20to%20calculate%20Vector%20Cross%20Product%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-610312%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3ESorry%20for%20reviving%20this%20zombie%20thread%2C%20but%20there%20is%20a%20sign%20error%20in%20the%20second%20element%20here.%26nbsp%3B%20I%20believe%20it%20should%20be%3C%2FP%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3EMatrix(%0A%20%20%7Bv1%5B2%5D%20*%20v2%5B3%5D%20-%20v1%5B3%5D%20*%20v2%5B2%5D%2C%20v1%5B3%5D%20*%20v2%5B1%5D%20-%20v1%5B1%5D%20*%20v2%5B3%5D%2C%20v1%5B1%5D%20*%20v2%5B2%5D%20-%20v1%5B2%5D%20*%20v2%5B1%5D%7D%3CBR%20%2F%3E)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EJust%20want%20to%20leave%20this%20here%20for%20anyone%20else%20who%20comes%20along%20for%20a%20quick%20copy-paste.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-236315%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20How%20to%20calculate%20Vector%20Cross%20Product%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-236315%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EThanks%20cwillden%2C%20this%20is%20essentially%20what%26nbsp%3B%20I%20have%20implemented.%20It%20does%20work%20for%20me%20since%20I%20am%20only%20concerned%20with%203D%20vectors%2C%20but%20I%20am%20supprised%20this%20isn't%20a%20built%20in%20function.%20I'm%20going%20to%20leave%20this%20open%20for%20a%20bit%20to%20see%20if%20anyone%20else%20knows%20of%20such%20a%20way.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-236298%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20How%20to%20calculate%20Vector%20Cross%20Product%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-236298%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EI%20hope%20someone%20comes%20up%20with%20a%20clever%20way%20to%20do%20this.%20The%20det()%20function%20is%20closely%20related%2C%20but%20I%20don't%20understand%20how%20to%20use%20det(3x3%20matrix)%20to%20get%20back%20more%20than%20a%20scalar%20value.%20I%20think%20you%20could%20use%20det(2x2%20sub-matrix)%20three%20times%2C%20but%20I%20think%20the%20straight-forward%20%3CA%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FCross_product%23Mnemonic%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Exyzzy%3C%2FA%3E%20approach%20is%20simpler%20and%20just%20as%20fast.%3C%2FP%3E%3CP%3EI%20use%20it%20for%20the%20surface%20normals%20in%203D%20scenes%2C%20there%20is%20some%20JSL%20in%20%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2FUncharted%2FCustom-Visualization%2Fba-p%2F191559%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Fcommunity.jmp.com%2Ft5%2FUncharted%2FCustom-Visualization%2Fba-p%2F191559%3C%2FA%3E%26nbsp%3B.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-236294%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20How%20to%20calculate%20Vector%20Cross%20Product%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-236294%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EIf%20you're%20only%20ever%20dealing%203x1%20(or%201x3)%20vectors%2C%20then%20I%20would%20think%20computational%20efficiency%20or%20elegance%20is%20not%20too%20important.%26nbsp%3B%20Would%20this%20be%20sufficient%3F%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Ecross_prod%20%3D%20function(%7Bv1%2C%20v2%7D%2C%0A%20matrix(%7Bv1%5B2%5D*v2%5B3%5D%20-%20v1%5B3%5D*v2%5B2%5D%2C%20v1%5B1%5D*v2%5B3%5D%20-%20v1%5B3%5D*v2%5B1%5D%2C%20v1%5B1%5D*v2%5B2%5D%20-%20v1%5B2%5D*v2%5B1%5D%7D)%3B%0A)%3B%0A%0Across_prod(v1%2C%20v2)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
Choose Language Hide Translation Bar
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?

7 REPLIES 7
cwillden
Super User (Alumni)

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
Craige_Hales
Super User

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
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. 

klk
klk
Level III

Re: How to calculate Vector Cross Product

Sorry for reviving this zombie thread, but there is a sign error in the second element here.  I believe it should be

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]}
)

Just want to leave this here for anyone else who comes along for a quick copy-paste.

Re: How to calculate Vector Cross Product

Well, the revived zombie caught me. Here is a way to do it via determinants as @Craige_Hales mentioned. It is short, but I don't know... it isn't very simple.

 

crossP = Function( {x, y, i = 1},
	Return( J( 3, 1, Det( (x || y)[Remove( [1, 2, 3], i++ ), 0] ) ) )
);

 

i.e., using the originally-supplied data:

brady_brady_0-1678770863902.png

 

Craige_Hales
Super User

Re: How to calculate Vector Cross Product

@brady_brady  Cool! It needs a tweak if you are going to use it:

	i = 1;
	j = -1;
	J( 3, 1, (j *= -1) * Det( (v1 || v2)[Remove( [1, 2, 3], i++ ), 0] ) );

 https://mathinsight.org/cross_product_formula shows how the middle element needs to be negative.  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.

 

The straight forward corrected formula from @klk  is about 3 times faster and about N times easier to understand.

 

Test code.

View more...

v1 = [0, 0, 1];
v2 = [0, 1, 0];

start = HP Time();
For( t = 1, t < 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 < 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 < 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 ) )
	);
);
Curiously, rounding to 11 places occasionally fails to match results. Probably nearly collinear vectors.

Craige

Re: How to calculate Vector Cross Product

The inferior performance doesn't surprise... thanks for inserting the sign adjustment. I omitted the "sign checkerboard" inadvertently.

 

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}.