Computes the Taylor series of functions or characters.

taylor(
  f,
  var,
  params = list(),
  order = 1,
  accuracy = 4,
  stepsize = NULL,
  zero = 1e-07
)

Arguments

f

character, or function returning a numeric scalar value.

var

vector giving the variable names with respect to which the derivatives are to be computed and/or the point where the derivatives are to be evaluated (the center of the Taylor series). See derivative.

params

list of additional parameters passed to f.

order

the order of the Taylor approximation.

accuracy

degree of accuracy for numerical derivatives.

stepsize

finite differences stepsize for numerical derivatives. It is based on the precision of the machine by default.

zero

tolerance used for deciding which derivatives are zero. Absolute values less than this number are set to zero.

Value

list with components:

f

the Taylor series.

order

the approximation order.

terms

data.frame containing the variables, coefficients and degrees of each term in the Taylor series.

References

Guidotti E (2022). "calculus: High-Dimensional Numerical and Symbolic Calculus in R." Journal of Statistical Software, 104(5), 1-37. doi:10.18637/jss.v104.i05

See also

Other polynomials: hermite()

Other derivatives: derivative()

Examples

### univariate taylor series (in x=0)
taylor("exp(x)", var = "x", order = 2)
#> $f
#> [1] "(1) * 1 + (1) * x^1 + (0.5) * x^2"
#> 
#> $order
#> [1] 2
#> 
#> $terms
#>   var coef degree
#> 0   1  1.0      0
#> 1 x^1  1.0      1
#> 2 x^2  0.5      2
#> 

### univariate taylor series of user-defined functions (in x=0)
f <- function(x) exp(x)
taylor(f = f, var = c(x=0), order = 2)
#> $f
#> [1] "(1) * 1 + (0.999999999996052) * x^1 + (0.499999999979692) * x^2"
#> 
#> $order
#> [1] 2
#> 
#> $terms
#>   var coef degree
#> 0   1  1.0      0
#> 1 x^1  1.0      1
#> 2 x^2  0.5      2
#> 

### multivariate taylor series (in x=0 and y=1)
taylor("x*(y-1)", var = c(x=0, y=1), order = 4)
#> $f
#> [1] "(1) * x^1*(y-1)^1"
#> 
#> $order
#> [1] 4
#> 
#> $terms
#>             var coef degree
#> 0,0           1    0      0
#> 0,1     (y-1)^1    0      1
#> 1,0         x^1    0      1
#> 0,2     (y-1)^2    0      2
#> 2,0         x^2    0      2
#> 1,1 x^1*(y-1)^1    1      2
#> 0,3     (y-1)^3    0      3
#> 3,0         x^3    0      3
#> 1,2 x^1*(y-1)^2    0      3
#> 2,1 x^2*(y-1)^1    0      3
#> 0,4     (y-1)^4    0      4
#> 4,0         x^4    0      4
#> 1,3 x^1*(y-1)^3    0      4
#> 3,1 x^3*(y-1)^1    0      4
#> 2,2 x^2*(y-1)^2    0      4
#> 

### multivariate taylor series of user-defined functions (in x=0 and y=1)
f <- function(x,y) x*(y-1)
taylor(f, var = c(x=0, y=1), order = 4)
#> $f
#> [1] "(0.999999999999994) * x^1*(y-1)^1"
#> 
#> $order
#> [1] 4
#> 
#> $terms
#>             var coef degree
#> 0,0           1    0      0
#> 0,1     (y-1)^1    0      1
#> 1,0         x^1    0      1
#> 0,2     (y-1)^2    0      2
#> 2,0         x^2    0      2
#> 1,1 x^1*(y-1)^1    1      2
#> 0,3     (y-1)^3    0      3
#> 3,0         x^3    0      3
#> 1,2 x^1*(y-1)^2    0      3
#> 2,1 x^2*(y-1)^1    0      3
#> 0,4     (y-1)^4    0      4
#> 4,0         x^4    0      4
#> 1,3 x^1*(y-1)^3    0      4
#> 3,1 x^3*(y-1)^1    0      4
#> 2,2 x^2*(y-1)^2    0      4
#> 

### vectorized interface
f <- function(x) prod(x)
taylor(f, var = c(0,0,0), order = 3)
#> $f
#> [1] "(1) * x1^1*x2^1*x3^1"
#> 
#> $order
#> [1] 3
#> 
#> $terms
#>                  var coef degree
#> 0,0,0              1    0      0
#> 0,0,1           x3^1    0      1
#> 0,1,0           x2^1    0      1
#> 1,0,0           x1^1    0      1
#> 0,0,2           x3^2    0      2
#> 0,2,0           x2^2    0      2
#> 2,0,0           x1^2    0      2
#> 0,1,1      x2^1*x3^1    0      2
#> 1,0,1      x1^1*x3^1    0      2
#> 1,1,0      x1^1*x2^1    0      2
#> 0,0,3           x3^3    0      3
#> 0,3,0           x2^3    0      3
#> 3,0,0           x1^3    0      3
#> 0,1,2      x2^1*x3^2    0      3
#> 0,2,1      x2^2*x3^1    0      3
#> 1,0,2      x1^1*x3^2    0      3
#> 1,2,0      x1^1*x2^2    0      3
#> 2,0,1      x1^2*x3^1    0      3
#> 2,1,0      x1^2*x2^1    0      3
#> 1,1,1 x1^1*x2^1*x3^1    1      3
#>