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%3ECome%20calcolare%20il%20prodotto%20incrociato%20vettoriale%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-236281%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EDalla%20documentazione%20sembra%20che%20l'operatore%20*%20eseguir%C3%A0%20un%20prodotto%20incrociato%20e%20%3B*%20eseguir%C3%A0%20un%20prodotto%20scalare.%20Voglio%20utilizzare%20un%20prodotto%20incrociato%20per%20determinare%20il%20vettore%20normale%20a%20una%20coppia%20di%20vettori%20noti%20come%20questo%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%3E3%20punti%20definiscono%20un%20piano%2C%20due%20vettori%20presi%20da%20quei%20punti%20fanno%20lo%20stesso%2C%20il%20prodotto%20incrociato%20dovrebbe%20dare%20il%20vettore%20normale%20di%20quel%20piano.%20Il%20problema%20sembra%20essere%20che%20le%20dimensioni%20delle%20due%20matrici%20moltiplicate%20(v1%20e%20v2)%20non%20concordano%3A%20nRows(v1)%20dovrebbe%20essere%20uguale%20a%20nCols(v2).%20Posso%20implementarlo%20manualmente%20con%20una%20formula%20semplificata%20per%20i%20prodotti%20incrociati%20vettoriali%2C%20ma%20qualcuno%20sa%20come%20formattarlo%20in%20modo%20diverso%20per%20lavorare%20con%20la%20moltiplicazione%20di%20matrici%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%20Come%20calcolare%20il%20prodotto%20incrociato%20vettoriale%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-611821%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3ELe%20prestazioni%20inferiori%20non%20sorprendono...%20grazie%20per%20aver%20inserito%20la%20regolazione%20del%20segno.%20Ho%20omesso%20inavvertitamente%20la%20%22scacchiera%20del%20segno%22.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIMO%20questo%20%C3%A8%20uno%20di%20quei%20casi%20in%20cui%20si%20applica%20%22solo%20perch%C3%A9%20puoi%2C%20non%20significa%20che%20dovresti%22...%20%7Bbello%2C%20divertente%2C%20breve%7D%20non%20supera%20%7Bcomprensibile%2C%20gestibile%2C%20veloce%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%20Come%20calcolare%20il%20prodotto%20incrociato%20vettoriale%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%20Freddo!%20Ha%20bisogno%20di%20una%20modifica%20se%20intendi%20usarlo%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%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%20mostra%20come%20l'elemento%20centrale%20deve%20essere%20negativo.Sopra%2C%20j%20alterner%C3%A0%20il%20segno%20(1%2C%20-1%2C%201)%20e%20i%20aumenter%C3%A0%20(1%2C%202%2C%203).%20La%20funzione%20J(...)%20ha%20un%20ciclo%20implicito%20che%20valuta%20il%20terzo%20argomento%20per%20ciascun%20elemento.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELa%20formula%20corretta%20semplice%20da%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F11962%22%20target%3D%22_blank%22%3E%20%40klk%3C%2FA%3E%20%C3%A8%20circa%203%20volte%20pi%C3%B9%20veloce%20e%20circa%20N%20volte%20pi%C3%B9%20facile%20da%20capire.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECodice%20di%20prova.%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%3EVisualizza%20altro...%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%3ECuriosamente%2C%20l'arrotondamento%20a%2011%20posizioni%20a%20volte%20non%20riesce%20a%20far%20corrispondere%20i%20risultati.%20Probabilmente%20vettori%20quasi%20collineari.%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%20inizio%20%3D%20Tempo%20HP()%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)%5BRimuovi(%20%5B%201%2C%202%2C%203%5D%2C%20i%2B%2B%20)%2C%200%5D%20)%20)%3B%20)%3B%20stop%20%3D%20Tempo%20HP()%3B%20Mostra(%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%20Matrice(%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%5B%201%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%20Tempo%20HP()%3B%20Mostra(%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%20Casuale%20Uniforme(%20-1%2C%201%20)%20)%3B%20v2%20%3D%20J(%203%2C%201%2C%20Casuale%20Uniforme(%20-1%2C%201%20)%20)%3B%20If(%20Tutto(%20Rotondo(%20Matrice(%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)%5BRimuovi(%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%20Lancia(%20Char(%20q%20)%20)%20)%3B%20)%3B%20Curiosamente%2C%20l'arrotondamento%20a%2011%20posizioni%20a%20volte%20non%20riesce%20a%20far%20corrispondere%20i%20risultati.%20Probabilmente%20vettori%20quasi%20collineari.%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%20Come%20calcolare%20il%20prodotto%20incrociato%20vettoriale%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-611656%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EBene%2C%20lo%20zombie%20rianimato%20mi%20ha%20catturato.%20Ecco%20un%20modo%20per%20farlo%20tramite%20determinanti%20come%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F982%22%20target%3D%22_blank%22%3E%20%40Craige_Hales%3C%2FA%3E%20menzionato.%20%C3%88%20breve%2C%20ma%20non%20lo%20so...%20non%20%C3%A8%20molto%20semplice.%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%3Eovvero%2C%20utilizzando%20i%20dati%20forniti%20originariamente%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%20Come%20calcolare%20il%20prodotto%20incrociato%20vettoriale%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-610312%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3ECi%20scusiamo%20per%20aver%20rilanciato%20questo%20thread%20sugli%20zombie%2C%20ma%20c'%C3%A8%20un%20errore%20di%20segno%20nel%20secondo%20elemento%20qui.Credo%20che%20dovrebbe%20essere%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%3EVoglio%20solo%20lasciare%20questo%20qui%20per%20chiunque%20altro%20venga%20per%20un%20veloce%20copia-incolla.%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%20Come%20calcolare%20il%20prodotto%20incrociato%20vettoriale%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-236315%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EGrazie%20cwillden%2C%20questo%20%C3%A8%20essenzialmente%20ci%C3%B2%20che%20ho%20implementato.%20Funziona%20per%20me%20poich%C3%A9%20mi%20occupo%20solo%20di%20vettori%203D%2C%20ma%20sono%20supposto%20che%20non%20sia%20una%20funzione%20integrata.%20Lascer%C3%B2%20la%20questione%20aperta%20per%20un%20po'%20per%20vedere%20se%20qualcun%20altro%20conosce%20un%20metodo%20del%20genere.%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%20Come%20calcolare%20il%20prodotto%20incrociato%20vettoriale%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-236298%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3ESpero%20che%20qualcuno%20trovi%20un%20modo%20intelligente%20per%20farlo.%20La%20funzione%20det()%20%C3%A8%20strettamente%20correlata%2C%20ma%20non%20capisco%20come%20utilizzare%20det(matrice%203x3)%20per%20ottenere%20pi%C3%B9%20di%20un%20valore%20scalare.%20Penso%20che%20potresti%20usare%20det(2x2%20sub-matrix)%20tre%20volte%2C%20ma%20penso%20che%20sia%20semplice%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%3E%20xyzzy%3C%2FA%3E%20l'approccio%20%C3%A8%20pi%C3%B9%20semplice%20e%20altrettanto%20veloce.%3C%2FP%3E%3CP%3ELo%20uso%20per%20le%20normali%20di%20superficie%20nelle%20scene%203D%2C%20c'%C3%A8%20del%20JSL%20dentro%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2FUncharted%2FCustom-Visualization%2Fba-p%2F191559%22%20target%3D%22_blank%22%3E%20https%3A%2F%2Fcommunity.jmp.com%2Ft5%2FUncharted%2FCustom-Visualization%2Fba-p%2F191559%3C%2FA%3E%20.%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%20Come%20calcolare%20il%20prodotto%20incrociato%20vettoriale%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-236294%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3ESe%20hai%20a%20che%20fare%20solo%20con%20vettori%203x1%20(o%201x3)%2C%20allora%20penserei%20che%20l'efficienza%20computazionale%20o%20l'eleganza%20non%20siano%20troppo%20importanti.Questo%20sarebbe%20sufficiente%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}.