Skip to content
Prev 173886 / 398503 Next

matrix multiplication, tensor product, block-diagonal and fast computation

Dear Chuck,

	thanks a lot for your reply.
Nevertheless, and though not so elegant, the for-loop solution seems faster (see below).

Best,
Carlo Giovanni Camarda

m <- 50
n <- 40
nl <- 1000
A <- array(1:(m*m*n), dim=c(m,m,n))
M <- matrix(1:(m*n), nrow=m, ncol=n)

# for-loop solution
system.time(
for(j in 1:nl){
    M1 <- matrix(nrow=m, ncol=n)
    for(i in 1:n){
        M1[,i] <- A[,,i] %*% M[,i]
    }
})
#   user  system elapsed 
#   5.65    0.03    5.72

# rowSums solution
system.time(
for(j in 1:nl){
    M5 <- rowSums( aperm(A * as.vector( M[ rep(1:ncol(A),each=nrow(A)),]),c(1,3,2)), dims = 2 )
})
#   user  system elapsed 
#  12.00    1.08   13.13



-----------------------------------------------------
Dr. Carlo Giovanni Camarda
Research Scientist
Max Planck Institute for Demographic Research
Laboratory of Statistical Demography
Konrad-Zuse-Stra?e 1
18057 Rostock - Germany
Phone: +49 (0)381 2081 172
Fax: +49 (0)381 2081 472
camarda at demogr.mpg.de
http://www.demogr.mpg.de/en/staff/camarda/default.htm
-----------------------------------------------------





-----Original Message-----
From: Charles C. Berry [mailto:cberry at tajo.ucsd.edu] 
Sent: Thursday, March 12, 2009 4:25 PM
To: Camarda, Carlo Giovanni
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] matrix multiplication, tensor product, block-diagonal and fast computation
On Wed, 11 Mar 2009, Camarda, Carlo Giovanni wrote:

            
This is fairly quick:

rowSums( aperm(A * as.vector( M[ rep(1:ncol(A),each=nrow(A)),]),c(1,3,2)), dims = 2 )

But my advice is to code this in C along the lines of your first solution 
(using the BLAS routines to carry it out in the inner products). Your code
will be easier to read and debug and will probably be faster and easier on
memory, too.

Years ago I wrote a lot of stuff like this in native S. I 'optimized' the 
heck out of it using tricks like the one above as I was debugging the 
code. I had to rewrite the bulk of it in C anyway. The S code was so hard 
to read that I could not migrate it to C bit by bit.  I had to start over 
and the effort spent debugging it in S was lost.

As an alternative you might try the 'jit' package on your first solution.

HTH,

Chuck
Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



----------
This mail has been sent through the MPI for Demographic Research.  Should you receive a mail that is apparently from a MPI user without this text displayed, then the address has most likely been faked. If you are uncertain about the validity of this message, please check the mail header or ask your system administrator for assistance.