Pryme8 Posted February 23, 2018 Share Posted February 23, 2018 How does one get the cross product of two finite vectors of indeterminate dimensions. I know how to cross 2,3,4 nth dimension vectors... but how would I write up a dynamic one for any cases? JackFalcon 1 Quote Link to comment Share on other sites More sharing options...
adam Posted February 23, 2018 Share Posted February 23, 2018 I'd try something like this: for(var i = 0; i < numDim; i++){ r[i] = a[(i + numDim - 1) % numDim] * b[(i + numDim) % numDim]; } I haven't tested this. Edit: for(var i = 0; i < numDim; i++){ r[i] = a[(i + numDim - 2) % numDim] * b[(i + numDim - 1) % numDim] - a[(i + numDim - 1) % numDim] * b[(i + numDim - 2) % numDim]; } Pryme8 1 Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted February 23, 2018 Author Share Posted February 23, 2018 so like: Array.prototype.cross = function(a){ var x,y,z = []; if(this.length>=a.length){x=this;y=a;}else{x=a;y=this;} var xl = x.length; for(var i=0; i<xl; i++){ z.push(x[(i + xl - 1) % xl] * y[(i + xl) % xl]); } return z; } I tried to do it with: var arr = [0, 1, 0]; arr = arr.cross([-1,0,0]); but that returns [0,0,0] Quote Link to comment Share on other sites More sharing options...
adam Posted February 23, 2018 Share Posted February 23, 2018 In my example, a and b are the vectors that you are crossing and r is the result. The vectors are arrays of dimensions (x is at index 0, y is at index 1, z is at index 2 and so on). Maybe you knew this already. JackFalcon 1 Quote Link to comment Share on other sites More sharing options...
JohnK Posted February 23, 2018 Share Posted February 23, 2018 This paper http://www.unizar.es/matematicas/algebra/elduque/Talks/crossproducts.pdf proves the following theorem Theorem 1. Let × be a vector cross product on the vector space V . Then dimV = 1, 3 or 7. That is vector cross products only exist in 1, 3 and 7 dimensional space. This paper https://arxiv.org/pdf/1310.5197.pdf generalises the result to define a cross product that works in some special vector spaces with odd dimensions. However none of this stops you taking two n dimensional arrays and combining them in any way you wish. JackFalcon, focomoso and Pryme8 3 Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted February 23, 2018 Author Share Posted February 23, 2018 https://arxiv.org/pdf/1310.5197.pdf Is just what I needed! thanks @JohnK. Now to actually understand it... Quote Link to comment Share on other sites More sharing options...
adam Posted February 23, 2018 Share Posted February 23, 2018 http://playground.babylonjs.com/#3HG8C3#1 I haven't looked at the pdf. This might not be the correct approach. Pryme8 1 Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted February 23, 2018 Author Share Posted February 23, 2018 You are a boss! JackFalcon 1 Quote Link to comment Share on other sites More sharing options...
adam Posted February 23, 2018 Share Posted February 23, 2018 LOL thanks for the compliment, but I'm not convinced it's correct. You might need a nested loop. Not feeling good about the numDim -2, numDim -1 part. Hopefully, my attempt and the paper that John supplied will be enough for you to solve the problem. Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted February 23, 2018 Author Share Posted February 23, 2018 looks like its working: var arr = [0, 0.5, 0, 0.5] arr = arr.cross([0, -0.8, 0, 0.2]); returns 0,-0.2,0.8,0 and just looking that seems right. thanks http://pryme8.github.io/plibs I am adding a bunch of "vector" methods to the array global object as a polyfill lib. I have not uploaded the math stuff yet and there is a ton of methods I have not put up yet, but yeah... thanks! This one was bugging me, dot scalars, and other algebraic stuff was easy, can I mention you provided this solution in my code? JackFalcon 1 Quote Link to comment Share on other sites More sharing options...
adam Posted February 23, 2018 Share Posted February 23, 2018 20 minutes ago, Pryme8 said: can I mention you provided this solution in my code I'd feel better about that if we could prove that it works with more dimensions. Quote Link to comment Share on other sites More sharing options...
adam Posted February 23, 2018 Share Posted February 23, 2018 Check this out: https://math.stackexchange.com/questions/2371022/cross-product-in-higher-dimensions/2371039 I think you are going to need another loop if you really need this to work with unlimited dimensions. Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted February 23, 2018 Author Share Posted February 23, 2018 1 minute ago, adam said: Check this out: https://math.stackexchange.com/questions/2371022/cross-product-in-higher-dimensions/2371039 I think you are going to need another loop if you really need this to work with unlimited dimensions. That's what I thought, maybe I can test it on wolfram or something. Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted February 23, 2018 Author Share Posted February 23, 2018 n = size(V,1); k = size(V,2); U = zeros(n,k); U(:,1) = V(:,1)/sqrt(V(:,1)'*V(:,1)); for i = 2:k U(:,i) = V(:,i); for j = 1:i-1 U(:,i) = U(:,i) - ( U(:,i)'*U(:,j) )/( U(:,j)'*U(:,j) )*U(:,j); end U(:,i) = U(:,i)/sqrt(U(:,i)'*U(:,i)); end this is a matlab solution for it. https://en.wikipedia.org/wiki/Gram–Schmidt_process adam 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.