Re: [R] how to multiply a constant to a matrix?
> "comtech" == comtech usa <[EMAIL PROTECTED]> > on Tue, 23 May 2006 23:14:21 -0700 writes: comtech> imagine when you have complicated matrix algebra computation using R, comtech> you cannot prevent some middle-terms become quadratic and absorbs into one comtech> scalar, right? comtech> if R cannot intelligently determine this, and you have to manually add comtech> "drop" everywhere, comtech> do you think it is reasonable? I don't think you can find any reasonable example where `` you have to manually add "drop" everywhere ''. To the contrary: In my experience, the S behavior (which won't be changed -- forget about that if you still consider it!!), is even rather useful, pointing to ``infelicities'' in your code most of the time, when matrix dimensions do not match. Further note, that c( <1x1--matrix> ) also produces a scalar and gives slightly more readable formulae --- however in the present case, c() is only advisable if you *know* you don't have a ``proper'' (n x k, with n, k > 1) matrix. Martin Maechler, ETH Zurich comtech> On 5/23/06, Patrick Burns <[EMAIL PROTECTED]> wrote: >> >> I think >> >> drop(B/D) * solve(A) >> >> would be a more transparent approach. >> >> It isn't that R can not do what you want, it is that >> it is saving you from shooting yourself in the foot >> in your attempt. What you are doing is not really >> a matrix computation. >> >> >> Patrick Burns >> [EMAIL PROTECTED] >> +44 (0)20 8525 0696 >> http://www.burns-stat.com >> (home of S Poetry and "A Guide for the Unwilling S User") >> >> Michael wrote: >> >> >This is very strange: >> > >> >I want compute the following in R: >> > >> >g = B/D * solve(A) >> > >> >where B and D are quadratics so they are just a scalar number, e.g. >> B=t(a) >> >%*% F %*% a; >> > >> >I want to multiply B/D to A^(-1), >> > >> >but R just does not allow me to do that and it keeps complaining that >> >"nonconformable array, etc." >> > >> > >> >I tried the following two tricks and they worked: >> > >> >as.numeric(B/D) * solve(A) >> > >> >diag(as.numeric(B/D), 5, 5) %*% solve (A) >> > >> > >> > >> >But if R cannot intelligently do scalar and matrix multiplication, it is >> >really problemetic. >> > >> >It basically cannot be used to do computations, since in complicated >> matrix >> >algebras, you have to distinguish where is scalar, and scalars obtained >> from >> >quadratics cannot be directly used to multiply another matrix, etc. It is >> >going to a huge mess... >> > >> >Any thoughts? >> > >> > [[alternative HTML version deleted]] >> > >> >__ >> >R-help@stat.math.ethz.ch mailing list >> >https://stat.ethz.ch/mailman/listinfo/r-help >> >PLEASE do read the posting guide! >> http://www.R-project.org/posting-guide.html >> > >> > >> > >> > >> comtech> [[alternative HTML version deleted]] comtech> __ comtech> R-help@stat.math.ethz.ch mailing list comtech> https://stat.ethz.ch/mailman/listinfo/r-help comtech> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] how to multiply a constant to a matrix?
I still can't see why this is a problem. If a 1x1 matrix should be treated as a scalar, then it can just be wrapped in drop(), and the arithmetic will be computed correctly by R. Are there any cases where this cannot be done? More specifically, are there any matrix algebra expressions where, depending on the particular dimensions of the variables used, drop() must be used in some cases, and not in other cases? A related but different behavior is the default dropping dimensions with extent equal to one by indexing operations. This can be problematic because if one is not careful, incorrect results can be obtained for particular values used in the expression. For example, consider the following, in which we are trying to compute the cross product of some columns of x with some rows of y. If x has n rows and y has n columns, then the result should always be an nxn matrix. However, if we are not careful with using drop=F in the indexing expressions, we can inadvertently end up with a 1x1 inner product matrix result for the case where we just use one column of x and one row of y. The solution to this is to always use drop=F in indexing in situations where this can occur. > x <- matrix(1:9, ncol=3) > y <- matrix(-(1:9), ncol=3) > i <- 1:2 > x[,i] %*% y[i,] [,1] [,2] [,3] [1,] -9 -24 -39 [2,] -12 -33 -54 [3,] -15 -42 -69 > i <- 1:3 > x[,i] %*% y[i,] [,1] [,2] [,3] [1,] -30 -66 -102 [2,] -36 -81 -126 [3,] -42 -96 -150 > # i has just one element -- the expression without drop=F > # no longer computes an outer product > i <- 2 > x[,i] %*% y[i,] [,1] [1,] -81 > x[,i,drop=F] %*% y[i,,drop=F] [,1] [,2] [,3] [1,] -8 -20 -32 [2,] -10 -25 -40 [3,] -12 -30 -48 > Cannot all cases in the situations you mention be handled in an analogous manner, by always wrapping appropriate quadratic expressions in drop(), or are there some cases where the result of the quadratic expression must be treated as a matrix, and other cases where the result of the quadratic expression must be treated as a scalar? -- Tony Plate Michael wrote: > imagine when you have complicated matrix algebra computation using R, > > you cannot prevent some middle-terms become quadratic and absorbs into one > scalar, right? > > if R cannot intelligently determine this, and you have to manually add > "drop" everywhere, > > do you think it is reasonable? > > On 5/23/06, Patrick Burns <[EMAIL PROTECTED]> wrote: > >>I think >> >>drop(B/D) * solve(A) >> >>would be a more transparent approach. >> >>It isn't that R can not do what you want, it is that >>it is saving you from shooting yourself in the foot >>in your attempt. What you are doing is not really >>a matrix computation. >> >> >>Patrick Burns >>[EMAIL PROTECTED] >>+44 (0)20 8525 0696 >>http://www.burns-stat.com >>(home of S Poetry and "A Guide for the Unwilling S User") >> >>Michael wrote: >> >> >>>This is very strange: >>> >>>I want compute the following in R: >>> >>>g = B/D * solve(A) >>> >>>where B and D are quadratics so they are just a scalar number, e.g. >> >>B=t(a) >> >>>%*% F %*% a; >>> >>>I want to multiply B/D to A^(-1), >>> >>>but R just does not allow me to do that and it keeps complaining that >>>"nonconformable array, etc." >>> >>> >>>I tried the following two tricks and they worked: >>> >>>as.numeric(B/D) * solve(A) >>> >>>diag(as.numeric(B/D), 5, 5) %*% solve (A) >>> >>> >>> >>>But if R cannot intelligently do scalar and matrix multiplication, it is >>>really problemetic. >>> >>>It basically cannot be used to do computations, since in complicated >> >>matrix >> >>>algebras, you have to distinguish where is scalar, and scalars obtained >> >>from >> >>>quadratics cannot be directly used to multiply another matrix, etc. It is >>>going to a huge mess... >>> >>>Any thoughts? >>> >>> [[alternative HTML version deleted]] >>> >>>__ >>>R-help@stat.math.ethz.ch mailing list >>>https://stat.ethz.ch/mailman/listinfo/r-help >>>PLEASE do read the posting guide! >> >>http://www.R-project.org/posting-guide.html >> >>> >>> >>> > > [[alternative HTML version deleted]] > > __ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] how to multiply a constant to a matrix?
imagine when you have complicated matrix algebra computation using R, you cannot prevent some middle-terms become quadratic and absorbs into one scalar, right? if R cannot intelligently determine this, and you have to manually add "drop" everywhere, do you think it is reasonable? On 5/23/06, Patrick Burns <[EMAIL PROTECTED]> wrote: > > I think > > drop(B/D) * solve(A) > > would be a more transparent approach. > > It isn't that R can not do what you want, it is that > it is saving you from shooting yourself in the foot > in your attempt. What you are doing is not really > a matrix computation. > > > Patrick Burns > [EMAIL PROTECTED] > +44 (0)20 8525 0696 > http://www.burns-stat.com > (home of S Poetry and "A Guide for the Unwilling S User") > > Michael wrote: > > >This is very strange: > > > >I want compute the following in R: > > > >g = B/D * solve(A) > > > >where B and D are quadratics so they are just a scalar number, e.g. > B=t(a) > >%*% F %*% a; > > > >I want to multiply B/D to A^(-1), > > > >but R just does not allow me to do that and it keeps complaining that > >"nonconformable array, etc." > > > > > >I tried the following two tricks and they worked: > > > >as.numeric(B/D) * solve(A) > > > >diag(as.numeric(B/D), 5, 5) %*% solve (A) > > > > > > > >But if R cannot intelligently do scalar and matrix multiplication, it is > >really problemetic. > > > >It basically cannot be used to do computations, since in complicated > matrix > >algebras, you have to distinguish where is scalar, and scalars obtained > from > >quadratics cannot be directly used to multiply another matrix, etc. It is > >going to a huge mess... > > > >Any thoughts? > > > > [[alternative HTML version deleted]] > > > >__ > >R-help@stat.math.ethz.ch mailing list > >https://stat.ethz.ch/mailman/listinfo/r-help > >PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > > > > > > > > [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html