[R] symbolic matrix elements...

Evan Cooch evan.cooch at cornell.edu
Mon Sep 18 18:30:57 CEST 2006


Normally, I do symbolics in Maple, or Mathematica, but I'm trying to 
write a simple script for students to handle some *very* simple 
calculations (for other purposes) with matrix or vector elements, where 
the elements are coded symbolically. What I've tried with *partial" 
success is use of the tilde (~) operator. So, for example, consider a 
simple vector:

test=matrix(c(~ x^3-5*x+4, ~log(x^2-4*x)))

Now, when I look at test, I see

 > test
     [,1]     
[1,] Expression
[2,] Expression

Fine. When I try to extract one of the vector elements, I see (for example)

 > test[1]
[[1]]
~x^3 - 5 * x + 4


Fine - but now I'm trying to figure out how to use the extracted matrix 
element for anything else. For example, using D for simple symbolic 
derivatives

f <- test[1];
D(f,"x")

should *in theory* work, but I get the following:

 > D(f,"x");
[1] NA

But, if I try

f <- expression(x^3-5*x+4);
D(f,"x");

works fine.

So, even though it looks as if each element of test is coded as an 
expression, it seems as though it is somehow a different type of 
expression than if I code it explicitly as an expression. I'm *guessing* 
it has to do with the tilde operator not assigning the formula to 
anything, but I'm not sure.

Suggestions? Pointers to the obvious?

Thanks!

-- 
----------------------------------------------------------------------
 Evan Cooch                          e.mail: evan.cooch at cornell.edu
 Department of Natural Resources     voice: 607-255-1368
 Fernow Hall - Cornell University    FAX: 607-255-0349
 Ithaca, NY    14853                 http://canuck.dnr.cornell.edu
----------------------------------------------------------------------
Nihilism is best done by professionals. - Iggy Pop



More information about the R-help mailing list