3

Base R provides a function D() that outputs the expression of the derivative of a given function.

For example:

f <- expression(x^3+2*x)
D(f, "x") # with respect to x
# 3 * x^2 + 2

Is there a similar function that would yield the expression of the integral of a function?

I know that stats::integrate() will evaluate the integral of a function for a given interval, but as far as I know, it will not output the expression of the Integral.

Fred
  • 410
  • 3
  • 12
  • 1
    Perhaps helpful, though it requires both python and java, ick: http://www.di.fc.ul.pt/~jpn/r/symbolic/ – r2evans Oct 23 '18 at 19:06
  • 1
    No accepted answer at this other question (https://stackoverflow.com/q/34856279/3358272), but @BenBolker links to https://cran.r-project.org/web/packages/Ryacas/index.html – r2evans Oct 23 '18 at 19:07
  • Thank you for the links! I am surprised there is a base function for the derivative expression but not for the integral. – Fred Oct 23 '18 at 19:11
  • It's always seemed more difficult to my brain, perhaps it's harder for the computer, too? I don't know, I've never tried to write a function/program that evaluates symbolic deriv or integ. – r2evans Oct 23 '18 at 19:13

1 Answers1

3

As mentioned in the comments, the package Ryacas can do indefinite integrals:

library(Ryacas)
x = Sym('x')
f = expression(x^3 + 2*x)
Integrate(f, x)
## expression(x^4/4 + x^2)

There is no base function for integration but for differentiation because differentiation is mechanics, integration is art.

sieste
  • 8,296
  • 3
  • 33
  • 48