Skip to content
Prev 173505 / 398506 Next

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