[R] symbolic matrix elements...

From: Evan Cooch <evan.cooch_at_cornell.edu>
Date: Mon 18 Sep 2006 - 16:30:57 GMT


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@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

______________________________________________
R-help@stat.math.ethz.ch 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.
Received on Tue Sep 19 02:39:30 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Mon 18 Sep 2006 - 17:30:06 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.