# Re: [R] %*% in Matrix objects

Date: Sat 27 Jan 2007 - 22:42:34 GMT

Hi Martin,

x <- Matrix(1:12, 3,4, sparse = TRUE)

>I hope that you are aware of the fact that it's not efficient at
>all to store a dense matrix (it has *no* 0 entry) as a sparse one..
>
>and your posting is indeed an incentive for the Matrix developers
>to improve that part ... ;-)
>

Yes, the toy example is not sparse but the actual data is, and very large; I'm aware that coercing a dense matrix into the Sparse format is not leading to any saving (on the contrary). I'm talking about a real application with large sparse matrices; from now on, I'll post small examples using sparse matrices as well to avoid confusion.

Jose> so I tried

```    Jose> x = matrix(1:12,3,4)
Jose> x = as(x, "CsparseMatrix")
Jose> xnorms  = sqrt(colSums(x^2))
Jose> xnorms = as(xnorms, "CsparseMatrix")
Jose> (xnormed = t(x) * (1/xnorms))

```

Jose> But now, instead of a warning I get     Jose> "Error: Matrices must have same dimensions in t(x) * (1/xnorms)"

>yes. And the same happens with traditional matrices -- and well so:
>For arithmetic with matrices (traditional or "Matrices"),
>
> A o B (o in {"+", "*", "^", ....})
> -----
>
>does require that matrices A and B are ``conformable'', i.e.,
>have exact same dimensions.
>
>Only when one of A or B is *not* a matrix,
>then the usual S-language recycling rules are applied,
>and that's what you were using in your first example
>(<Matrix> * <numeric>) above.
>

Right. So this means that the * operator is not overloaded in Matrix (that is, if I use it, I'll get my Matrix coherced to matrix. Is that correct? Does this mean that there is no easy way to do element-by-element multiplication
without leaving the sparse Matrix format?

Jose> I suspect I'm facing the drop=T as before...
> why??

Because when I got a row out of a Matrix object, the resulting vector is not of class Matrix but numeric, and then (<Matrix> * <numeric>) is applied.

Last, I shouldn't consider myself the most standard user of the matrix package, since my lineal algebra is really basic. But in any case, you should know that your package is being enormously useful for me. Keep up the good work. And if I can help by posting my very basic questions, I'm glad to help.

```--
Cheers,
-Jose

--
Research fellow, Psychology Dept.
Sussex University, Brighton, UK