# Calculus: Derivative and Anti-derivative operators In mosaicCalc: Function-Based Numerical and Symbolic Differentiation and Antidifferentiation

## Description

Operators for computing derivatives and anti-derivatives as functions.

## Usage

 ```1 2 3 4 5 6 7``` ```D(formula, ..., .hstep = NULL, add.h.control = FALSE) antiD(formula, ..., lower.bound = 0, force.numeric = FALSE) makeAntiDfun(.function, .wrt, from, .tol = .Machine\$double.eps^0.25) numerical_integration(f, wrt, av, args, vi.from, ciName = "C", .tol) ```

## Arguments

 `formula` A formula. The right side of a formula specifies the variable(s) with which to carry out the integration or differentiation. On the left side should be an expression or a function that returns a numerical vector of the same length as its argument. The expression can contain unbound variables. Functions will be differentiated as if the formula f(x) ~ x were specified but with `x` replaced by the first argument of `f`. `...` Default values to be given to unbound variables in the expression `expr`. See examples.#' Note that in creating anti-derivative functions, default values of "from" and "to" can be assigned. They are to be written with the name of the variable as a prefix, e.g. `y.from`. `.hstep` horizontal distance between points used for secant slope calculation in numerical derivatives. `add.h.control` logical indicating whether the returned derivative function should have an additional parameter for setting .hstep. Meaningful only for numerical derivatives. `lower.bound` for numerical integration only, the lower bound used `force.numeric` If `TRUE`, a numerical integral is performed even when a symbolic integral is available. `.function` function to be integrated `.wrt` character string naming the variable of integration `from` default value for the lower bound of the integral region `.tol` Numerical tolerance. See `integrate()`. `f` A function. `wrt` Character string naming a variable: the var. of integration. `av` A list of the arguments passed to the function calling this. `args` Default values (if any) for parameters. `vi.from` The the lower bound of the interval of integration. `ciName` Character string giving the name of the symbol for the constant of integration.

## Details

`D` attempts to find a symbolic derivative for simple expressions, but will provide a function that is a numerical derivative if the attempt at symbolic differentiation is unsuccessful. The symbolic derivative can be of any order (although the expression may become unmanageably complex). The numerical derivative is limited to first or second-order partial derivatives (including mixed partials). `antiD` will attempt simple symbolic integration but if it fails it will return a numerically-based anti-derivative.

`antiD` returns a function with the same arguments as the expression passed to it. The returned function is the anti-derivative of the expression, e.g., antiD(f(x)~x) -> F(x). To calculate the integral of f(x), use F(to) - F(from).

## Value

For derivatives, the return value is a function of the variable(s) of differentiation, as well as any other symbols used in the expression. Thus, `D(A*x^2 + B*y ~ x + y)` will compute the mixed partial with respect to x then y (that is, d2f/dydx). The returned value will be a function of x and y, as well as A and B. In evaluating the returned function, it's best to use the named form of arguments, to ensure the order is correct.

a function of the same arguments as the original expression with a constant of integration set to zero by default, named "C", "D", ... depending on the first such letter not otherwise in the argument list.

## Note

`numerical_integration` is not intended for direct use. It packages up the numerical anti-differentiation process so that the contents of functions produced by `antiD` look nicer to human readers.

## Examples

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21``` ```D(sin(t) ~ t) D(A*sin(t) ~ t ) D(A*sin(2*pi*t/P) ~ t, A=2, P=10) # default values for parameters. f <- D(A*x^3 ~ x + x, A=1) # 2nd order partial -- note, it's a function of x f(x=2) f(x=2,A=10) # override default value of parameter A g <- D(f(x=t, A=1)^2 ~ t) # note: it's a function of t g(t=1) gg <- D(f(x=t, A=B)^2 ~ t, B=10) # note: it's a function of t and B gg(t=1) gg(t=1, B=100) f <- makeFun(x^2~x) D(f(cos(z))~z) #will look in user functions also antiD( a*x^2 ~ x, a = 3) antiD( A/x~x ) # This gives a warning about no default value for A F <- antiD( A*exp(-k*t^2 ) ~ t, A=1, k=0.1) F(t=Inf) one = makeFun(1 ~ x + y) by.x = antiD(one(x=x, y=y) ~ x, y=1) by.xy = antiD(by.x(x = sqrt(1-y^2), y = y) ~ y) 4 * by.xy(y = 1) # area of quarter circle ```

### Example output

```Loading required package: mosaicCore

Attaching package: 'mosaicCalc'

The following object is masked from 'package:stats':

D

function (t)
cos(t)
function (t, A)
A * cos(t)
Warning message:
In makeFun.formula(formula, ...) :
Implicit variables without default values (dangerous!):  A
function (t, A = 2, P = 10)
A * (cos(2 * pi * t/P) * (2 * pi/P))
[1] 12
[1] 120
[1] 72
[1] 7200
[1] 720000
function (z)
numerical.first.partial(.function, .wrt, .hstep, match.call())
<environment: 0x6d8b728>
function (x, a = 3, C = 0)
a * 1/3 * x^3 + C
function (x, C = 0, A)
A * log((x)) + C
Warning message:
In makeFun.formula(A * log((x)) + C ~ x, C = 0) :
Implicit variables without default values (dangerous!):  A
[1] 2.802496
[1] 3.141593
```

mosaicCalc documentation built on July 2, 2020, 3:17 a.m.