Search code examples
rintegralderivative

Retrieve integral expression of a function


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.


Solution

  • 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.