[R] A slightly unorthodox matrix product.

2018-08-04 Thread Thomas Jagger
> Date: Sat, 4 Aug 2018 17:52:37 +1200 > > From: Rolf Turner > To: r-help > Subject: [R] A slightly unorthodox matrix product. > Message-ID: > Content-Type: text/plain; charset="utf-8"; Format="flowed" > > > Can anyone think of a sexy way of forming following "product"? > > Given matrices A and

Re: [R] A slightly unorthodox matrix product.

2018-08-04 Thread Rolf Turner
On 05/08/18 16:41, Thomas Jagger wrote: > Date: Sat, 4 Aug 2018 17:52:37 +1200 > > From: Rolf Turner > > To: r-help mailto:r-help@r-project.org>> > Subject: [R] A slightly unorthodox matrix product. > Message-ID:

Re: [R] [FORGED] Re: A slightly unorthodox matrix product.

2018-08-04 Thread Rolf Turner
Thanks to Eric Berger, Jeff Newmiller and Chuck Berry for their help with this. Thanks especially to Eric for catching the bugs in my proposed solution. I can *never* keep the indexing straight in multidimensional arrays! It's tough being a -wit! I wouldn't have figured out the "ans0b"

Re: [R] A slightly unorthodox matrix product.

2018-08-04 Thread Berry, Charles
> On Aug 4, 2018, at 12:59 PM, Jeff Newmiller wrote: > ... > Slick work on the vectorizing, but for the future reference it was slightly > buggy: > Thanks for catching that! Chuck __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more,

Re: [R] A slightly unorthodox matrix product.

2018-08-04 Thread Jeff Newmiller
Sorry I missed your intent on the sapply. Slick work on the vectorizing, but for the future reference it was slightly buggy: ### A <- matrix( 1:12, nrow = 3 ) B <- matrix( 1:15, nrow = 3 ) # for loop ans2 <- function( a, b ) { zzz <- array( rep( NA, nrow( a ) * ncol( a ) * ncol( b ) )

Re: [R] A slightly unorthodox matrix product.

2018-08-04 Thread Berry, Charles
> On Aug 4, 2018, at 11:43 AM, Jeff Newmiller wrote: > > Sometimes a good old for loop performs best, even if it doesn't look sexy: > > Fair enough, but a vectorized solution beats them all (see below). Also, [SNIP] > # Charles > ans1b <- function( a, b ) > { The lapply you put here wa

Re: [R] A slightly unorthodox matrix product.

2018-08-04 Thread Jeff Newmiller
Sometimes a good old for loop performs best, even if it doesn't look sexy: A <- matrix( 1:12, nrow = 3 ) B <- matrix( 1:15, nrow = 3 ) library(abind) # Eric ans1 <- function( a, b ) { xxx <- lapply( seq.int( nrow( A ) ) , function( i ) { A[ i, ] %o%

Re: [R] A slightly unorthodox matrix product.

2018-08-04 Thread Berry, Charles
> On Aug 4, 2018, at 10:01 AM, Eric Berger wrote: > > Hi Rolf, > A few edits because (i) nrow(a) should be nrow(A) and (ii) you have > calculated C[j,k,i] = A[i,j]*B[i,k], (iii) minor style change on lapply. > > library(abind) > xxx <- lapply(1:nrow(A),function(i){A[i,]%o%B[i,]}) > yyy <- do.

Re: [R] A slightly unorthodox matrix product.

2018-08-04 Thread Eric Berger
Hi Rolf, A few edits because (i) nrow(a) should be nrow(A) and (ii) you have calculated C[j,k,i] = A[i,j]*B[i,k], (iii) minor style change on lapply. library(abind) xxx <- lapply(1:nrow(A),function(i){A[i,]%o%B[i,]}) yyy <- do.call(abind,c(xxx,list(along=3))) zzz <- aperm(yyy,c(3,1,2)) HTH, Eric