Rainer Sachs
2013-Jun-17 20:20 UTC
[R] package expm: matrix powers within a product of matrices: operation precedence
package expm: matrix powers within a product of matrices: operation
precedence
I want to double check that for M and N kxk mtrices and v a k-vector
M%^%2%*%N%*%v is not automatically read as (M%^%2)%*%N%*%v or
M%*%N%^%2%*%v is not automatically read as M%*%(N%^%2)%*%v or
both
I had been assuming the usual precedence (powers first, then
multiplication) applies for matrix powers and multiplication in expm. I was
getting unexpected results. In desperation I tried putting in the
parentheses and seem to be getting much more reasonable answers. But I am so
surprised that the matrix power operation is, apparently, not automatically
given precedence over the matrix multiplication operation that I do not
trust that adding parentheses has cured my bug. Maybe I am still making some
other mistake instead. Can anyone confirm that in fact the parentheses are
(sometimes?) essential? I could not find any documentation, one way or the
other, just definitions of the power operator and of matrix exponentials.
TIA Rainer K. Sachs, Prof emertitus of math
--
View this message in context:
http://r.789695.n4.nabble.com/package-expm-matrix-powers-within-a-product-of-matrices-operation-precedence-tp4669733.html
Sent from the R help mailing list archive at Nabble.com.
William Dunlap
2013-Jun-17 21:58 UTC
[R] package expm: matrix powers within a product of matrices: operation precedence
All operators of the form %<something>% have the same precedence, that of %*%. R does not look at the <something> between the percent signs to determine the precedence. Hence you must use parentheses to get the order of operations that you want. (I think that %<something>% operators are overused - providing the same functionality in a standard functional form is often more convenient. It certainly would eliminate your problem.) Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of Rainer Sachs > Sent: Monday, June 17, 2013 1:20 PM > To: r-help at r-project.org > Subject: [R] package expm: matrix powers within a product of matrices: operation > precedence > > > package expm: matrix powers within a product of matrices: operation > precedence > > I want to double check that for M and N kxk mtrices and v a k-vector > M%^%2%*%N%*%v is not automatically read as (M%^%2)%*%N%*%v or > M%*%N%^%2%*%v is not automatically read as M%*%(N%^%2)%*%v or > both > > I had been assuming the usual precedence (powers first, then > multiplication) applies for matrix powers and multiplication in expm. I was > getting unexpected results. In desperation I tried putting in the > parentheses and seem to be getting much more reasonable answers. But I am so > surprised that the matrix power operation is, apparently, not automatically > given precedence over the matrix multiplication operation that I do not > trust that adding parentheses has cured my bug. Maybe I am still making some > other mistake instead. Can anyone confirm that in fact the parentheses are > (sometimes?) essential? I could not find any documentation, one way or the > other, just definitions of the power operator and of matrix exponentials. > > TIA Rainer K. Sachs, Prof emertitus of math > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/package-expm-matrix- > powers-within-a-product-of-matrices-operation-precedence-tp4669733.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.