With permission I offer this exchange. Rolf and I have different notions of what u %*% v should mean, but the arbiter is obviously the original poster:
Begin forwarded message: > From: David Winsemius <dwinsem...@comcast.net> > Subject: Re: [R] functions and matrices > Date: July 1, 2013 6:21:09 PM PDT > To: Rolf Turner <rolf.tur...@xtra.co.nz> > > > On Jul 1, 2013, at 5:09 PM, Rolf Turner wrote: > >> On 02/07/13 11:37, David Winsemius wrote: >>> On Jul 1, 2013, at 3:32 PM, Rolf Turner wrote: >>> >>>> Basically R does things *numerically* and what you want to do really >>>> amounts to symbolic manipulation. Of course R could be cajoled into >>>> doing it --- see fortune("Yoda") --- but probably only with a great deal of >>>> effort and code-writing. >>>> >>>> OTOH you could quite easily write a function that would calculate >>>> det(u%*%v)(x) for any given numerical value of x: >>>> >>>> foo <- function(a,b,x){ >>>> a1 <- apply(a,c(1,2),function(m,x){m[[1]](x)},x=x) >>>> b1 <- apply(b,c(1,2),function(m,x){m[[1]](x)},x=x) >>>> det(a1%*%b1) >>>> } >>>> >>>> Then doing >>>> >>>> foo(u,v,2) >>> I would have thought that (u %*% v) would be: >>> >>> u[1,1]( v[1,1](x) ) + u[1,2]( v[2,1](x) ) u[1,1]( v[1,2](x) ) + >>> u[1,2]( v[2,2](x) ) >>> u[2,1]( v[1,1](x) ) + u[2,2]( v[2,1](x) ) u[2,1]( v[2,1](x) ) + >>> u[2,2]( v[2,2](x) ) >>> >>> (Crossing my fingers that I got the row and column conventions correct for >>> matrix multiplication.) >>> >> <SNIP> >> >> Not quite sure what you're getting at here. It looks to me that you are >> calculating the *composition* of the functions rather than their *product*. > > Exactly. That is how I understood successive application of functions > embedded in matrices . The symbol used in my differential topology course lo > those 40 years ago was an open circle, but I assumed the OP wanted something > along those lines to perform a composite mapping: > > compose <- function(u, v, x) matrix( c( > u[1,1][[1]]( v[1,1][[1]](x) ) + u[1,2][[1]]( v[2,1][[1]](x) ) , > u[1,1][[1]]( v[1,2][[1]](x) ) + u[1,2][[1]]( v[2,2][[1]](x) ), > u[2,1][[1]]( v[1,1][[1]](x) ) + u[2,2][[1]]( v[2,1][[1]](x) ), > u[2,1][[1]]( v[2,1][[1]](x) ) + u[2,2][[1]]( v[2,2][[1]](x) ) ), > 2,2,byrow=TRUE) > > compose(u,v,2) > [,1] [,2] > [1,] 75 1332 > [2,] 5427 1680128 > > (Noting that I may have reversed the roles of u and v.) > >> >> I.e. you are taking the (i,j)th entry of "u%*%v" (evaluated at x) to be the >> sum over k of >> >> u[i,k](v[k,j](x)) >> >> This is not what I understood the OP to want. I assumed he wanted the >> product of the function values rather than the composition of the functions, >> i.e. that he wanted the (i,j)th entry to be the sum over k of >> >> u[i,k](x) * v[k,j](x) >> >> which is what my function provides. This seems to me to be the most >> "reasonable" interpretation, but I could be wrong. >> >> BTW --- you cannot actually do u[i,k](x). E.g. >> >> u[1,2](2) >> >> gives "Error: attempt to apply non-function". One needs to do u[1,2][[1]](2) >> (which gives 4, as it should). > > Yes. I was playing fast and loose with notation. I didn't think the code > would really run as offered.I was a bit surprise that this worked, but I > suppose you bear credit (and blame?) for pushing my program closer to > completion. > >> v[1,1][[1]]( u[1,1][[1]]( 2 )) > [1] 11 > > Any problem with me copying this to the list? > > >> >> cheers, >> >> Rolf > > Best; > > > David Winsemius > Alameda, CA, USA > David Winsemius Alameda, CA, USA ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.