[R] symbolic matrix elements...
Eik Vettorazzi
vettorazzi at econ.uni-hamburg.de
Mon Sep 18 18:57:02 CEST 2006
test=matrix(c( expression(x^3-5*x+4), expression(log(x^2-4*x))))
works.
btw. you recieved an error because D expects an expression and you offered
a list
> class(test[1])
[1] "list"
to get the error relating to the misuse of the tilde operator you have to
prompt the "correct" extractor "[["
f<-test[[1]]
D(f,"x")
Am Mon, 18 Sep 2006 18:30:57 +0200 schrieb Evan Cooch
<evan.cooch at cornell.edu>:
> 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!
>
--
--------------------
Universität Hamburg
Institut für Statistik und Ökonometrie
Dipl.-Wi.-Math. Eik Vettorazzi
Von-Melle-Park 5
20146 Hamburg
Tel.: +49 40-42838-3540
More information about the R-help
mailing list