1

My question multiplying numbers and symbols in R was answered and here I would like to give an example of using this for quaternion multiplication. Actually, I am using this on a much larger set (a group of 256 elements) but the principle is the same. I'm very new to working with data.tables so any additional tips are appreciated.

groupMult = data.table(
               e = c("i","j","k", "e"),
               i = c("-e","-k","j", "i"), 
               j = c("k","-e","-i", "j"), 
               k = c("-j","i","-e", "k")
               );
row.names(groupMult) = c("i", "j", "k", "e");
setkey(groupMult);

# Find X*Y with X = 2i - 3j, Y = k - 4e
X = data.table(i = 2, j = -3);
Y = data.table(k = 1, e = -4); 

# reduce groupMult to the vectors we need for multiplication
multMa = groupMult[names(X), names(Y), with = F];

# repeat values of Y ncol(X) times
multY = Y[rep(seq_len(nrow(Y)),   each=ncol(X)),];
# repeat values of X ncol(Y) times
multX = t(X[rep(seq_len(nrow(X)), each=ncol(Y)),]);

# coefficient matrix
multMaNum = multY*multX;
row.names(multMaNum) = names(X);

# elementwise multiplicaton of multMaNum with multMa
res = mapply(paste, multMaNum, multMa, MoreArgs=list(sep='*') )
res[] <- sapply(res , function(x) sub("(.*)([-])(.*)", "\\2\\1\\3", x));

# collapse all elements of the data.table to get final result 
res = paste(lapply(res, paste, collapse = " "), collapse = " + ");
> res
[1] "-2*j + -3*i + -8*i + 12*j"
Community
  • 1
  • 1
Dement
  • 85
  • 6
  • Why didn't you upvote/accept any of the answers to your previous question? Didn't they help you in any way? – nicola Nov 07 '15 at 11:34
  • I did uprate, but the answer came back that this will not be shown until I have a reputation of at least 15. – Dement Nov 07 '15 at 11:36
  • You can also accept without upvoting. Click on the tick mark next of the answer you want to accept. You also gain rep in doing so. – nicola Nov 07 '15 at 11:39
  • 1
    Actually, I made clear that both answers are fine and I will have to do a performance test to check which one is better. I wanted to wait for the results on that to choose. But maybe the important thing is to signify the question is answered so I'll just mark one of the two now as the answer. – Dement Nov 07 '15 at 11:43
  • There are already many packages in R that implement quaternion algebra. Is there a reason to reinvent this wheel? And is this just just a request for further tips when you already have a working solution? – IRTFM Nov 07 '15 at 18:52
  • 1
    I'm working on extending "floretions" from 2nd to 4th order in R http://mathoverflow.net/questions/210514/what-is-the-motivation-and-purpose-of-the-floretion-group Quaternions can be seen as "1st order floretions". I'll know if this is a "working solution" when I implement the code and confirm that existing algorithms using multiplication defined similar to above process sufficiently fast (which I'm doing right now). I only posted the above to show why I asked the initial question and to know if I'm missing anything obvious. – Dement Nov 07 '15 at 19:47

0 Answers0