[R] functions and matrices

Rolf Turner rolf.turner at xtra.co.nz
Tue Jul 2 00:32:00 CEST 2013


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)

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!



More information about the R-help mailing list