[R] functions and matrices

David Winsemius dwinsemius at comcast.net
Tue Jul 2 01:37:26 CEST 2013


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.)


> 
> gives 0.  (In fact foo(u,v,anything) gives 0 for your collection of functions;
> the matrix "u(x)" is singular for any x --- the second row is x^2 times the
> first row.)
> 
> Perhaps this is good enough for your purposes?  If not, you should probably
> be looking at a symbolic manipulation package.  The R package "Ryacas" has
> some capabilities in this regard, but I have no experience with it and cannot
> advise.
> 
>    cheers,
> 
>        Rolf Turner
> 
> On 02/07/13 05:37, Naser Jamil wrote:
>> Dear R-user,
>> May I seek your help, please. I have two matrices, u and v, elements of
>> which are some functions
>> of x. I just want to multiply them and express the determinant of the
>> resulting matrix as a function of
>> x and of course, this is for some reason. Actually the original problem has
>> more matrices to multiply and I'm just wondering whether I can simplify it
>> anyway through the R codes. It may even be non-sense, but just want to hear
>> from you. The below is the code.
>> 
>> ---------------------------------------------------------------------------------------------
>> 
>> f1<-function(x) {x}
>> f2<-function(x) {x^2}
>> f3<-function(x) {x^3}
>> f4<-function(x) {x^4}
>> 
>> f5<-function(x) {x^2+7}
>> f6<-function(x) {x^3+14*x}
>> f7<-function(x) {x^2+2*x}
>> f8<-function(x) {x^4+10*x}
>> 
>> u<-matrix(c(f1,f2,f3,f4), nrow=2, ncol=2, byrow=TRUE)
>> v<-matrix(c(f5,f6,f7,f8), nrow=2, ncol=2, byrow=TRUE)
>> 
>> det(u %*% v) # Is that possible?
>> 
>> ------------------------------------------------------------------------------------------------
>> 
>> Any suggestion will be more than great!
> 
> ______________________________________________
> R-help at 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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list