[R] how to multiply a constant to a matrix?

Martin Maechler maechler at stat.math.ethz.ch
Mon May 29 16:55:15 CEST 2006


>>>>> "comtech" == comtech usa <comtech.usa at gmail.com>
>>>>>     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 <pburns at pburns.seanet.com> 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
    >> patrick at burns-stat.com
    >> +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 at 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 at 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



More information about the R-help mailing list