tags:

views:

97

answers:

3

Hi, I am using D to get derivatives of a function. However, R does not simplify the expression when returning the derivative. I need to figure out if a function has a derivative that can be expressed generically. Is there some way in R to simplify the expression?

> D(expression(sqrt(1 - x^2)), 'x')
-(0.5 * (2 * x * (1 - x^2)^-0.5))
> D(D(expression(sqrt(1 - x^2)), 'x'), 'x')
-(0.5 * (2 * (1 - x^2)^-0.5 - 2 * x * (-0.5 * (2 * x * (1 - x^2)^-1.5))))

Secondly, is there a way in R to do numerical integration?

+2  A: 

As far as I know, R will not simplify the result of D(). It sounds as though you want a proper computer algebra system, and R is definitely not a full CAS. Mathematica and Maple are the most well-known, but there are also a number of open-source alternatives (as discussed on this SO post).

R can do numerical integration - for this kind of question it is worth searching in the R help pages first (i.e. help.search('integrate')). You can use integrate() in the stats package. There is also area() in the MASS package, but that is much simpler (i.e. for demonstration purposes).

nullglob
R symbolic capabilities can be extended with Ryacas or rSymPy.
mbq
A: 

You might want to check Octave… It's free and afaik Math people like it.

EDIT: @mbq, I am not so sure ... that's what I thought too. Basically it´s free, might be able to do what he wants – why not given it a try. There is some evidence that my guess wasn't that bad. Of course it's also possible that I did not understand a thing :)

ran2
Octave has symbolics? I though it is only a "GNU MATLAB".
mbq
thought also it was GNU matlab, just wanted to say that it's probably easier to handle in a math software, even if it needs some extensions.
ran2
You can also look at Maxima. http://maxima.sourceforge.net/
Ken Williams
+1  A: 
library(Ryacas)
x <- Sym("x")
Simplify(deriv(sqrt(1 - x^2),x,2))  # return the result simplified

gives

expression((x^2 - 1 - x^2)/root(1 - x^2, 2)^3)

You can also try

PrettyForm(Simplify(deriv(sqrt(1 - x^2),x,2)))

which gives

   2        2  
  x  - 1 - x   
---------------
              3
    /      2 \ 
Sqrt\ 1 - x  / 

As for numerical integration try giving this to see what is available

library(sos)
findFn('{numerical+integration}')
gd047
this is really helpful. it makes searching functions so much easier!!