Title: | High Dimensional Numerical and Symbolic Calculus |
---|---|
Description: | Efficient C++ optimized functions for numerical and symbolic calculus as described in Guidotti (2022) <doi:10.18637/jss.v104.i05>. It includes basic arithmetic, tensor calculus, Einstein summing convention, fast computation of the Levi-Civita symbol and generalized Kronecker delta, Taylor series expansion, multivariate Hermite polynomials, high-order derivatives, ordinary differential equations, differential operators (Gradient, Jacobian, Hessian, Divergence, Curl, Laplacian) and numerical integration in arbitrary orthogonal coordinate systems: cartesian, polar, spherical, cylindrical, parabolic or user defined by custom scale factors. |
Authors: | Emanuele Guidotti [aut, cre] |
Maintainer: | Emanuele Guidotti <[email protected]> |
License: | GPL-3 |
Version: | 1.0.1 |
Built: | 2024-11-21 03:49:53 UTC |
Source: | https://github.com/eguidotti/calculus |
Elementwise difference of numeric
or character
arrays.
x %diff% y
x %diff% y
x |
|
y |
|
array
.
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
Other basic arithmetic:
%div%()
,
%dot%()
,
%inner%()
,
%kronecker%()
,
%outer%()
,
%prod%()
,
%sum%()
### vector x <- c("a+1","b+2") x %diff% x ### matrix x <- matrix(letters[1:4], ncol = 2) x %diff% x ### array x <- array(letters[1:12], dim = c(2,2,3)) y <- array(1:12, dim = c(2,2,3)) x %diff% y
### vector x <- c("a+1","b+2") x %diff% x ### matrix x <- matrix(letters[1:4], ncol = 2) x %diff% x ### array x <- array(letters[1:12], dim = c(2,2,3)) y <- array(1:12, dim = c(2,2,3)) x %diff% y
Elementwise division of numeric
or character
arrays.
x %div% y
x %div% y
x |
|
y |
|
array
.
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
Other basic arithmetic:
%diff%()
,
%dot%()
,
%inner%()
,
%kronecker%()
,
%outer%()
,
%prod%()
,
%sum%()
### vector x <- c("a+1","b+2") x %div% x ### matrix x <- matrix(letters[1:4], ncol = 2) x %div% x ### array x <- array(letters[1:12], dim = c(2,2,3)) y <- array(1:12, dim = c(2,2,3)) x %div% y
### vector x <- c("a+1","b+2") x %div% x ### matrix x <- matrix(letters[1:4], ncol = 2) x %div% x ### array x <- array(letters[1:12], dim = c(2,2,3)) y <- array(1:12, dim = c(2,2,3)) x %div% y
The dot product between arrays with different dimensions is computed by taking the inner product on the last dimensions of the two arrays.
x %dot% y
x %dot% y
x |
|
y |
|
The dot product between two arrays A
and B
is computed as:
array
.
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
Other basic arithmetic:
%diff%()
,
%div%()
,
%inner%()
,
%kronecker%()
,
%outer%()
,
%prod%()
,
%sum%()
### inner product x <- array(1:12, dim = c(3,4)) x %dot% x ### dot product x <- array(1:24, dim = c(3,2,4)) y <- array(letters[1:8], dim = c(2,4)) x %dot% y
### inner product x <- array(1:12, dim = c(3,4)) x %dot% x ### dot product x <- array(1:24, dim = c(3,2,4)) y <- array(letters[1:8], dim = c(2,4)) x %dot% y
Computes the inner product of two numeric
or character
arrays.
x %inner% y
x %inner% y
x |
|
y |
|
The inner product between two arrays A
and B
is computed as:
numeric
or character
.
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
Other basic arithmetic:
%diff%()
,
%div%()
,
%dot%()
,
%kronecker%()
,
%outer%()
,
%prod%()
,
%sum%()
### numeric inner product x <- array(1:4, dim = c(2,2)) x %inner% x ### symbolic inner product x <- array(letters[1:4], dim = c(2,2)) x %inner% x
### numeric inner product x <- array(1:4, dim = c(2,2)) x %inner% x ### symbolic inner product x <- array(letters[1:4], dim = c(2,2)) x %inner% x
Computes the generalised Kronecker product of two numeric
or character
arrays.
x %kronecker% y
x %kronecker% y
x |
|
y |
|
array
.
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
Other basic arithmetic:
%diff%()
,
%div%()
,
%dot%()
,
%inner%()
,
%outer%()
,
%prod%()
,
%sum%()
### numeric Kronecker product c(1,2) %kronecker% c(2,3) ### symbolic Kronecker product array(1:4, dim = c(2,2)) %kronecker% c("a","b")
### numeric Kronecker product c(1,2) %kronecker% c(2,3) ### symbolic Kronecker product array(1:4, dim = c(2,2)) %kronecker% c("a","b")
Computes the outer product of two numeric
or character
arrays.
x %outer% y
x %outer% y
x |
|
y |
|
The outer product between two arrays A
and B
is computed as:
array
.
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
Other basic arithmetic:
%diff%()
,
%div%()
,
%dot%()
,
%inner%()
,
%kronecker%()
,
%prod%()
,
%sum%()
### numeric outer product c(1,2) %outer% c(2,3) ### symbolic outer product c("a","b") %outer% c("c","d")
### numeric outer product c(1,2) %outer% c(2,3) ### symbolic outer product c("a","b") %outer% c("c","d")
Elementwise product of numeric
or character
arrays.
x %prod% y
x %prod% y
x |
|
y |
|
array
.
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
Other basic arithmetic:
%diff%()
,
%div%()
,
%dot%()
,
%inner%()
,
%kronecker%()
,
%outer%()
,
%sum%()
### vector x <- c("a+1","b+2") x %prod% x ### matrix x <- matrix(letters[1:4], ncol = 2) x %prod% x ### array x <- array(letters[1:12], dim = c(2,2,3)) y <- array(1:12, dim = c(2,2,3)) x %prod% y
### vector x <- c("a+1","b+2") x %prod% x ### matrix x <- matrix(letters[1:4], ncol = 2) x %prod% x ### array x <- array(letters[1:12], dim = c(2,2,3)) y <- array(1:12, dim = c(2,2,3)) x %prod% y
Elementwise sum of numeric
or character
arrays.
x %sum% y
x %sum% y
x |
|
y |
|
array
.
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
Other basic arithmetic:
%diff%()
,
%div%()
,
%dot%()
,
%inner%()
,
%kronecker%()
,
%outer%()
,
%prod%()
### vector x <- c("a+1","b+2") x %sum% x ### matrix x <- matrix(letters[1:4], ncol = 2) x %sum% x ### array x <- array(letters[1:12], dim = c(2,2,3)) y <- array(1:12, dim = c(2,2,3)) x %sum% y
### vector x <- c("a+1","b+2") x %sum% x ### matrix x <- matrix(letters[1:4], ncol = 2) x %sum% x ### array x <- array(letters[1:12], dim = c(2,2,3)) y <- array(1:12, dim = c(2,2,3)) x %sum% y
Converts characters
to expressions
.
c2e(x)
c2e(x)
x |
|
expression
.
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
Other utilities:
e2c()
,
evaluate()
,
wrap()
### convert characters c2e("a") ### convert array of characters c2e(array("a", dim = c(2,2)))
### convert characters c2e("a") ### convert array of characters c2e(array("a", dim = c(2,2)))
Sums over repeated indices in an array
.
contraction(x, i = NULL, drop = TRUE)
contraction(x, i = NULL, drop = TRUE)
x |
indexed |
i |
subset of repeated indices to sum up. If |
drop |
|
array
.
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
Other tensor algebra:
delta()
,
diagonal()
,
einstein()
,
epsilon()
,
index()
### matrix trace x <- matrix(letters[1:4], nrow = 2) contraction(x) ### tensor trace x <- array(1:27, dim = c(3,3,3)) contraction(x) #### tensor contraction over repeated indices x <- array(1:27, dim = c(3,3,3)) index(x) <- c("i","i","j") contraction(x) #### tensor contraction over specific repeated indices only x <- array(1:16, dim = c(2,2,2,2)) index(x) <- c("i","i","k","k") contraction(x, i = "k") #### tensor contraction keeping dummy dimensions x <- array(letters[1:16], dim = c(2,2,2,2)) index(x) <- c("i","i","k","k") contraction(x, drop = FALSE)
### matrix trace x <- matrix(letters[1:4], nrow = 2) contraction(x) ### tensor trace x <- array(1:27, dim = c(3,3,3)) contraction(x) #### tensor contraction over repeated indices x <- array(1:27, dim = c(3,3,3)) index(x) <- c("i","i","j") contraction(x) #### tensor contraction over specific repeated indices only x <- array(1:16, dim = c(2,2,2,2)) index(x) <- c("i","i","k","k") contraction(x, i = "k") #### tensor contraction keeping dummy dimensions x <- array(letters[1:16], dim = c(2,2,2,2)) index(x) <- c("i","i","k","k") contraction(x, drop = FALSE)
Computes the cross product of vectors of length
.
cross(...) x %cross% y
cross(...) x %cross% y
... |
|
x |
|
y |
|
-dimensional vector orthogonal to the
vectors.
x %cross% y
: binary operator for 3-dimensional cross products.
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
### canonical basis 4-d cross(c(1,0,0,0), c(0,1,0,0), c(0,0,0,1)) ### canonical basis 3-d cross(c(1,0,0), c(0,1,0)) ### symbolic cross product 3-d c(1,0,0) %cross% c(0,1,0) ### symbolic cross product 3-d c("a","b","c") %cross% c(0,0,1)
### canonical basis 4-d cross(c(1,0,0,0), c(0,1,0,0), c(0,0,0,1)) ### canonical basis 3-d cross(c(1,0,0), c(0,1,0)) ### symbolic cross product 3-d c(1,0,0) %cross% c(0,1,0) ### symbolic cross product 3-d c("a","b","c") %cross% c(0,0,1)
Computes the numerical curl of functions
or the symbolic curl of characters
in arbitrary orthogonal coordinate systems.
curl( f, var, params = list(), coordinates = "cartesian", accuracy = 4, stepsize = NULL, drop = TRUE ) f %curl% var
curl( f, var, params = list(), coordinates = "cartesian", accuracy = 4, stepsize = NULL, drop = TRUE ) f %curl% var
f |
array of |
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. See |
params |
|
coordinates |
coordinate system to use. One of: |
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. |
drop |
if |
The curl of a vector-valued function at a point is represented by a
vector whose length and direction denote the magnitude and axis of the maximum
circulation.
In 2 dimensions, the
curl
is computed in arbitrary orthogonal coordinate
systems using the scale factors and the Levi-Civita symbol
epsilon
:
In 3 dimensions:
where . In
dimensions, the
curl
is implemented in such
a way that the formula reduces correctly to the previous cases for and
:
When is an
array
of vector-valued functions the
curl
is computed for each vector:
Vector for vector-valued functions when drop=TRUE
, array
otherwise.
f %curl% var
: binary operator with default parameters.
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
Other differential operators:
derivative()
,
divergence()
,
gradient()
,
hessian()
,
jacobian()
,
laplacian()
### symbolic curl of a 2-d vector field f <- c("x^3*y^2","x") curl(f, var = c("x","y")) ### numerical curl of a 2-d vector field in (x=1, y=1) f <- function(x,y) c(x^3*y^2, x) curl(f, var = c(x=1, y=1)) ### numerical curl of a 3-d vector field in (x=1, y=1, z=1) f <- function(x,y,z) c(x^3*y^2, x, z) curl(f, var = c(x=1, y=1, z=1)) ### vectorized interface f <- function(x) c(x[1]^3*x[2]^2, x[1], x[3]) curl(f, var = c(1,1,1)) ### symbolic array of vector-valued 3-d functions f <- array(c("x*y","x","y*z","y","x*z","z"), dim = c(2,3)) curl(f, var = c("x","y","z")) ### numeric array of vector-valued 3-d functions in (x=1, y=1, z=1) f <- function(x,y,z) array(c(x*y,x,y*z,y,x*z,z), dim = c(2,3)) curl(f, var = c(x=1, y=1, z=1)) ### binary operator c("x*y","y*z","x*z") %curl% c("x","y","z")
### symbolic curl of a 2-d vector field f <- c("x^3*y^2","x") curl(f, var = c("x","y")) ### numerical curl of a 2-d vector field in (x=1, y=1) f <- function(x,y) c(x^3*y^2, x) curl(f, var = c(x=1, y=1)) ### numerical curl of a 3-d vector field in (x=1, y=1, z=1) f <- function(x,y,z) c(x^3*y^2, x, z) curl(f, var = c(x=1, y=1, z=1)) ### vectorized interface f <- function(x) c(x[1]^3*x[2]^2, x[1], x[3]) curl(f, var = c(1,1,1)) ### symbolic array of vector-valued 3-d functions f <- array(c("x*y","x","y*z","y","x*z","z"), dim = c(2,3)) curl(f, var = c("x","y","z")) ### numeric array of vector-valued 3-d functions in (x=1, y=1, z=1) f <- function(x,y,z) array(c(x*y,x,y*z,y,x*z,z), dim = c(2,3)) curl(f, var = c(x=1, y=1, z=1)) ### binary operator c("x*y","y*z","x*z") %curl% c("x","y","z")
Computes the Generalized Kronecker Delta.
delta(n, p = 1)
delta(n, p = 1)
n |
number of elements for each dimension. |
p |
order of the generalized Kronecker delta, |
array
representing the generalized Kronecker delta tensor.
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
Other tensor algebra:
contraction()
,
diagonal()
,
einstein()
,
epsilon()
,
index()
### Kronecker delta 3x3 delta(3) ### generalized Kronecker delta 3x3 of order 2 delta(3, p = 2)
### Kronecker delta 3x3 delta(3) ### generalized Kronecker delta 3x3 of order 2 delta(3, p = 2)
Computes symbolic derivatives based on the D
function, or numerical derivatives based on finite differences.
derivative( f, var, params = list(), order = 1, accuracy = 4, stepsize = NULL, drop = TRUE, deparse = TRUE )
derivative( f, var, params = list(), order = 1, accuracy = 4, stepsize = NULL, drop = TRUE, deparse = TRUE )
f |
array of |
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. See details. |
params |
|
order |
integer vector, giving the differentiation order for each variable. See details. |
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. |
drop |
if |
deparse |
if |
The function behaves differently depending on the arguents order
,
the order of differentiation, and var
, the variable names with respect to
which the derivatives are computed.
When multiple variables are provided and order
is a single integer ,
then the
-th order derivative is computed for each element of
f
with respect to each variable:
that is:
where is the array of functions and
denotes the
-th order partial derivative with respect to the
-th variable.
When order
matches the length of var
, it is assumed that the
differentiation order is provided for each variable. In this case, each element
is derived times with respect to the
-th variable, for each
of the
variables.
The same applies when order
is a named vector giving the differentiation
order for each variable. For example, order = c(x=1, y=2)
differentiates
once with respect to and twice with respect to
. A call with
order = c(x=1, y=0)
is equivalent to order = c(x=1)
.
To compute numerical derivatives or to evaluate symbolic derivatives at a point,
the function accepts a named vector for the argument var
; e.g.
var = c(x=1, y=2)
evaluates the derivatives in and
.
For
functions
where the first argument is used as a parameter vector,
var
should be a numeric
vector indicating the point at which the
derivatives are to be calculated.
array
.
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
Other derivatives:
taylor()
Other differential operators:
curl()
,
divergence()
,
gradient()
,
hessian()
,
jacobian()
,
laplacian()
### symbolic derivatives derivative(f = "sin(x)", var = "x") ### numerical derivatives f <- function(x) sin(x) derivative(f = f, var = c(x=0)) ### higher order derivatives f <- function(x) sin(x) derivative(f = f, var = c(x=0), order = 3) ### multivariate functions ## - derive once with respect to x ## - derive twice with respect to y ## - evaluate in x=0 and y=0 f <- function(x, y) y^2*sin(x) derivative(f = f, var = c(x=0, y=0), order = c(1,2)) ### vector-valued functions ## - derive each element twice with respect to each variable ## - evaluate in x=0 and y=0 f <- function(x, y) c(x^2, y^2) derivative(f, var = c(x=0, y=0), order = 2) ### vectorized interface f <- function(x) c(sum(x), prod(x)) derivative(f, var = c(0,0,0), order = 1)
### symbolic derivatives derivative(f = "sin(x)", var = "x") ### numerical derivatives f <- function(x) sin(x) derivative(f = f, var = c(x=0)) ### higher order derivatives f <- function(x) sin(x) derivative(f = f, var = c(x=0), order = 3) ### multivariate functions ## - derive once with respect to x ## - derive twice with respect to y ## - evaluate in x=0 and y=0 f <- function(x, y) y^2*sin(x) derivative(f = f, var = c(x=0, y=0), order = c(1,2)) ### vector-valued functions ## - derive each element twice with respect to each variable ## - evaluate in x=0 and y=0 f <- function(x, y) c(x^2, y^2) derivative(f, var = c(x=0, y=0), order = 2) ### vectorized interface f <- function(x) c(sum(x), prod(x)) derivative(f, var = c(0,0,0), order = 1)
Functions to extract or replace the diagonals of an array
, or construct a diagonal array
.
diagonal(x = 1, dim = rep(2, 2)) diagonal(x) <- value
diagonal(x = 1, dim = rep(2, 2)) diagonal(x) <- value
x |
an |
dim |
the dimensions of the (square) |
value |
vector giving the values of the diagonal entries. |
Vector of the diagonal entries of x
if x
is an array
.
If x
is a vector, returns the diagonal array
with the
entries given by x
.
diagonal(x) <- value
: set diagonals.
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
Other tensor algebra:
contraction()
,
delta()
,
einstein()
,
epsilon()
,
index()
### 3x3 matrix diagonal(x = 1, dim = c(3,3)) ### 2x2x2 array diagonal(x = 1:2, dim = c(2,2,2)) ### extract diagonals x <- diagonal(1:5, dim = c(5,5,5)) diagonal(x) ### set diagonals x <- array(0, dim = c(2,2,2)) diagonal(x) <- 1:2 x
### 3x3 matrix diagonal(x = 1, dim = c(3,3)) ### 2x2x2 array diagonal(x = 1:2, dim = c(2,2,2)) ### extract diagonals x <- diagonal(1:5, dim = c(5,5,5)) diagonal(x) ### set diagonals x <- array(0, dim = c(2,2,2)) diagonal(x) <- 1:2 x
Computes the numerical divergence of functions
or the symbolic divergence of characters
in arbitrary orthogonal coordinate systems.
divergence( f, var, params = list(), coordinates = "cartesian", accuracy = 4, stepsize = NULL, drop = TRUE ) f %divergence% var
divergence( f, var, params = list(), coordinates = "cartesian", accuracy = 4, stepsize = NULL, drop = TRUE ) f %divergence% var
f |
array of |
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. See |
params |
|
coordinates |
coordinate system to use. One of: |
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. |
drop |
if |
The divergence of a vector-valued function produces a scalar value
representing the volume density of the outward flux of the
vector field from an infinitesimal volume around a given point.
The
divergence
is computed in arbitrary orthogonal coordinate systems using the
scale factors :
where . When
is an
array
of vector-valued functions
, the
divergence
is computed for each vector:
Scalar for vector-valued functions when drop=TRUE
, array
otherwise.
f %divergence% var
: binary operator with default parameters.
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
Other differential operators:
curl()
,
derivative()
,
gradient()
,
hessian()
,
jacobian()
,
laplacian()
### symbolic divergence of a vector field f <- c("x^2","y^3","z^4") divergence(f, var = c("x","y","z")) ### numerical divergence of a vector field in (x=1, y=1, z=1) f <- function(x,y,z) c(x^2, y^3, z^4) divergence(f, var = c(x=1, y=1, z=1)) ### vectorized interface f <- function(x) c(x[1]^2, x[2]^3, x[3]^4) divergence(f, var = c(1,1,1)) ### symbolic array of vector-valued 3-d functions f <- array(c("x^2","x","y^2","y","z^2","z"), dim = c(2,3)) divergence(f, var = c("x","y","z")) ### numeric array of vector-valued 3-d functions in (x=0, y=0, z=0) f <- function(x,y,z) array(c(x^2,x,y^2,y,z^2,z), dim = c(2,3)) divergence(f, var = c(x=0, y=0, z=0)) ### binary operator c("x^2","y^3","z^4") %divergence% c("x","y","z")
### symbolic divergence of a vector field f <- c("x^2","y^3","z^4") divergence(f, var = c("x","y","z")) ### numerical divergence of a vector field in (x=1, y=1, z=1) f <- function(x,y,z) c(x^2, y^3, z^4) divergence(f, var = c(x=1, y=1, z=1)) ### vectorized interface f <- function(x) c(x[1]^2, x[2]^3, x[3]^4) divergence(f, var = c(1,1,1)) ### symbolic array of vector-valued 3-d functions f <- array(c("x^2","x","y^2","y","z^2","z"), dim = c(2,3)) divergence(f, var = c("x","y","z")) ### numeric array of vector-valued 3-d functions in (x=0, y=0, z=0) f <- function(x,y,z) array(c(x^2,x,y^2,y,z^2,z), dim = c(2,3)) divergence(f, var = c(x=0, y=0, z=0)) ### binary operator c("x^2","y^3","z^4") %divergence% c("x","y","z")
Converts expressions
to characters
.
e2c(x)
e2c(x)
x |
|
character
.
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
Other utilities:
c2e()
,
evaluate()
,
wrap()
### convert expressions expr <- parse(text = "a") e2c(expr) ### convert array of expressions expr <- array(parse(text = "a"), dim = c(2,2)) e2c(expr)
### convert expressions expr <- parse(text = "a") e2c(expr) ### convert array of expressions expr <- array(parse(text = "a"), dim = c(2,2)) e2c(expr)
Implements the Einstein notation for summation over repeated indices.
einstein(..., drop = TRUE)
einstein(..., drop = TRUE)
... |
arbitrary number of indexed |
drop |
|
array
.
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
Other tensor algebra:
contraction()
,
delta()
,
diagonal()
,
epsilon()
,
index()
### A{i,j} B{j,k} a <- array(letters[1:6], dim = c(i=2, j=3)) b <- array(letters[1:3], dim = c(j=3, k=1)) einstein(a,b) ### A{i,j} B{j,k,k} C{k,l} D{j,k} a <- array(1:10, dim = c(i=2, j=5)) b <- array(1:45, dim = c(j=5, k=3, k=3)) c <- array(1:12, dim = c(k=3, l=4)) d <- array(1:15, dim = c(j=5, k=3)) einstein(a,b,c,d)
### A{i,j} B{j,k} a <- array(letters[1:6], dim = c(i=2, j=3)) b <- array(letters[1:3], dim = c(j=3, k=1)) einstein(a,b) ### A{i,j} B{j,k,k} C{k,l} D{j,k} a <- array(1:10, dim = c(i=2, j=5)) b <- array(1:45, dim = c(j=5, k=3, k=3)) c <- array(1:12, dim = c(k=3, l=4)) d <- array(1:15, dim = c(j=5, k=3)) einstein(a,b,c,d)
Computes the Levi-Civita totally antisymmetric tensor.
epsilon(n)
epsilon(n)
n |
number of dimensions. |
array
representing the Levi-Civita symbol.
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
Other tensor algebra:
contraction()
,
delta()
,
diagonal()
,
einstein()
,
index()
### Levi-Civita symbol in 2 dimensions epsilon(2) ### Levi-Civita symbol in 3 dimensions epsilon(3)
### Levi-Civita symbol in 2 dimensions epsilon(2) ### Levi-Civita symbol in 3 dimensions epsilon(3)
Evaluates an array of characters
or expressions
.
evaluate(f, var, params = list(), vectorize = TRUE)
evaluate(f, var, params = list(), vectorize = TRUE)
f |
array of |
var |
named vector or |
params |
|
vectorize |
|
Evaluated object. When var
is a named vector, the return is an array
with the same dimensions of f
. When var
is a data.frame
, the
return is a matrix
with columns corresponding to the entries of f
and
rows corresponding to the rows of var
.
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
Other utilities:
c2e()
,
e2c()
,
wrap()
### single evaluation f <- array(letters[1:4], dim = c(2,2)) var <- c(a = 1, b = 2, c = 3, d = 4) evaluate(f, var) ### multiple evaluation f <- array(letters[1:4], dim = c(2,2)) var <- data.frame(a = 1:3, b = 2:4, c = 3:5, d = 4:6) evaluate(f, var) ### multiple evaluation with additional parameters f <- "a*sum(x)" var <- data.frame(a = 1:3) params <- list(x = 1:3) evaluate(f, var, params) ### multiple evaluation of non-vectorized expressions f <- "a*myf(x)" myf <- function(x) if(x>0) 1 else -1 var <- data.frame(a = 1:3, x = -1:1) evaluate(f, var, params = list(myf = myf), vectorize = FALSE)
### single evaluation f <- array(letters[1:4], dim = c(2,2)) var <- c(a = 1, b = 2, c = 3, d = 4) evaluate(f, var) ### multiple evaluation f <- array(letters[1:4], dim = c(2,2)) var <- data.frame(a = 1:3, b = 2:4, c = 3:5, d = 4:6) evaluate(f, var) ### multiple evaluation with additional parameters f <- "a*sum(x)" var <- data.frame(a = 1:3) params <- list(x = 1:3) evaluate(f, var, params) ### multiple evaluation of non-vectorized expressions f <- "a*myf(x)" myf <- function(x) if(x>0) 1 else -1 var <- data.frame(a = 1:3, x = -1:1) evaluate(f, var, params = list(myf = myf), vectorize = FALSE)
Computes the numerical gradient of functions
or the symbolic gradient of characters
in arbitrary orthogonal coordinate systems.
gradient( f, var, params = list(), coordinates = "cartesian", accuracy = 4, stepsize = NULL, drop = TRUE ) f %gradient% var
gradient( f, var, params = list(), coordinates = "cartesian", accuracy = 4, stepsize = NULL, drop = TRUE ) f %gradient% var
f |
array of |
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. See |
params |
|
coordinates |
coordinate system to use. One of: |
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. |
drop |
if |
The gradient of a scalar-valued function is the vector
whose components are the partial derivatives of
with respect to each variable
.
The
gradient
is computed in arbitrary orthogonal coordinate systems using the
scale factors :
When the function is a tensor-valued function
,
the
gradient
is computed for each scalar component. In particular, it becomes
the Jacobian matrix for vector-valued function.
Gradient vector for scalar-valued functions when drop=TRUE
, array
otherwise.
f %gradient% var
: binary operator with default parameters.
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
Other differential operators:
curl()
,
derivative()
,
divergence()
,
hessian()
,
jacobian()
,
laplacian()
### symbolic gradient gradient("x*y*z", var = c("x", "y", "z")) ### numerical gradient in (x=1, y=2, z=3) f <- function(x, y, z) x*y*z gradient(f = f, var = c(x=1, y=2, z=3)) ### vectorized interface f <- function(x) x[1]*x[2]*x[3] gradient(f = f, var = c(1, 2, 3)) ### symbolic vector-valued functions f <- c("y*sin(x)", "x*cos(y)") gradient(f = f, var = c("x","y")) ### numerical vector-valued functions f <- function(x) c(sum(x), prod(x)) gradient(f = f, var = c(0,0,0)) ### binary operator "x*y^2" %gradient% c(x=1, y=3)
### symbolic gradient gradient("x*y*z", var = c("x", "y", "z")) ### numerical gradient in (x=1, y=2, z=3) f <- function(x, y, z) x*y*z gradient(f = f, var = c(x=1, y=2, z=3)) ### vectorized interface f <- function(x) x[1]*x[2]*x[3] gradient(f = f, var = c(1, 2, 3)) ### symbolic vector-valued functions f <- c("y*sin(x)", "x*cos(y)") gradient(f = f, var = c("x","y")) ### numerical vector-valued functions f <- function(x) c(sum(x), prod(x)) gradient(f = f, var = c(0,0,0)) ### binary operator "x*y^2" %gradient% c(x=1, y=3)
Computes univariate and multivariate Hermite polynomials.
hermite(order, sigma = 1, var = "x", transform = NULL)
hermite(order, sigma = 1, var = "x", transform = NULL)
order |
the order of the Hermite polynomial. |
sigma |
the covariance |
var |
|
transform |
|
Hermite polynomials are obtained by differentiation of the Gaussian kernel:
where is a
-dimensional square matrix and
is the vector representing the order of
differentiation for each variable
.
In the case where
and
the formula reduces to the
standard univariate Hermite polynomials:
If transform
is not NULL
, the variables var
are replaced with
transform
to compute the polynomials
list
of Hermite polynomials with components:
the Hermite polynomial.
the order of the Hermite polynomial.
data.frame
containing the variables, coefficients and degrees of each term in the Hermite polynomial.
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
Other polynomials:
taylor()
### univariate Hermite polynomials up to order 3 hermite(3) ### multivariate Hermite polynomials up to order 2 hermite(order = 2, sigma = matrix(c(1,0,0,1), nrow = 2), var = c('z1', 'z2')) ### multivariate Hermite polynomials with transformation of variables hermite(order = 2, sigma = matrix(c(1,0,0,1), nrow = 2), var = c('z1', 'z2'), transform = c('z1+z2','z1-z2'))
### univariate Hermite polynomials up to order 3 hermite(3) ### multivariate Hermite polynomials up to order 2 hermite(order = 2, sigma = matrix(c(1,0,0,1), nrow = 2), var = c('z1', 'z2')) ### multivariate Hermite polynomials with transformation of variables hermite(order = 2, sigma = matrix(c(1,0,0,1), nrow = 2), var = c('z1', 'z2'), transform = c('z1+z2','z1-z2'))
Computes the numerical Hessian of functions
or the symbolic Hessian of characters
.
hessian(f, var, params = list(), accuracy = 4, stepsize = NULL, drop = TRUE) f %hessian% var
hessian(f, var, params = list(), accuracy = 4, stepsize = NULL, drop = TRUE) f %hessian% var
f |
array of |
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. See |
params |
|
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. |
drop |
if |
In Cartesian coordinates, the Hessian of a scalar-valued function is the
square matrix of second-order partial derivatives:
When the function is a tensor-valued function
,
the
hessian
is computed for each scalar component.
It might be tempting to apply the definition of the Hessian as the Jacobian of the gradient to write it in arbitrary orthogonal coordinate systems. However, this results in a Hessian matrix that is not symmetric and ignores the distinction between vector and covectors in tensor analysis. The generalization to arbitrary coordinate system is not currently supported.
Hessian matrix for scalar-valued functions when drop=TRUE
, array
otherwise.
f %hessian% var
: binary operator with default parameters.
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
Other differential operators:
curl()
,
derivative()
,
divergence()
,
gradient()
,
jacobian()
,
laplacian()
### symbolic Hessian hessian("x*y*z", var = c("x", "y", "z")) ### numerical Hessian in (x=1, y=2, z=3) f <- function(x, y, z) x*y*z hessian(f = f, var = c(x=1, y=2, z=3)) ### vectorized interface f <- function(x) x[1]*x[2]*x[3] hessian(f = f, var = c(1, 2, 3)) ### symbolic vector-valued functions f <- c("y*sin(x)", "x*cos(y)") hessian(f = f, var = c("x","y")) ### numerical vector-valued functions f <- function(x) c(sum(x), prod(x)) hessian(f = f, var = c(0,0,0)) ### binary operator "x*y^2" %hessian% c(x=1, y=3)
### symbolic Hessian hessian("x*y*z", var = c("x", "y", "z")) ### numerical Hessian in (x=1, y=2, z=3) f <- function(x, y, z) x*y*z hessian(f = f, var = c(x=1, y=2, z=3)) ### vectorized interface f <- function(x) x[1]*x[2]*x[3] hessian(f = f, var = c(1, 2, 3)) ### symbolic vector-valued functions f <- c("y*sin(x)", "x*cos(y)") hessian(f = f, var = c("x","y")) ### numerical vector-valued functions f <- function(x) c(sum(x), prod(x)) hessian(f = f, var = c(0,0,0)) ### binary operator "x*y^2" %hessian% c(x=1, y=3)
Functions to get or set the names of the dimensions of an array
.
index(x) index(x) <- value
index(x) index(x) <- value
x |
|
value |
vector of indices. |
Vector of indices.
index(x) <- value
: set indices.
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
Other tensor algebra:
contraction()
,
delta()
,
diagonal()
,
einstein()
,
epsilon()
### array with no indices x <- array(1, dim = c(1, 3, 2)) index(x) ### indices on initialization x <- array(1, dim = c(i=1, j=3, k=2)) index(x) ### set indices on the fly x <- array(1, dim = c(1, 3, 2)) index(x) <- c("i", "j", "k") index(x)
### array with no indices x <- array(1, dim = c(1, 3, 2)) index(x) ### indices on initialization x <- array(1, dim = c(i=1, j=3, k=2)) index(x) ### set indices on the fly x <- array(1, dim = c(1, 3, 2)) index(x) <- c("i", "j", "k") index(x)
Computes the integrals of functions
or characters
in arbitrary
orthogonal coordinate systems.
integral( f, bounds, params = list(), coordinates = "cartesian", relTol = 0.001, absTol = 1e-12, method = NULL, vectorize = NULL, drop = TRUE, verbose = FALSE, ... )
integral( f, bounds, params = list(), coordinates = "cartesian", relTol = 0.001, absTol = 1e-12, method = NULL, vectorize = NULL, drop = TRUE, verbose = FALSE, ... )
f |
array of |
bounds |
|
params |
|
coordinates |
coordinate system to use. One of: |
relTol |
the maximum relative tolerance. |
absTol |
the absolute tolerance. |
method |
the method to use. One of |
vectorize |
|
drop |
if |
verbose |
|
... |
additional arguments passed to |
The function integrates seamlessly with cubature for efficient
numerical integration in C. If the package cubature is not
installed, the function implements a naive Monte Carlo integration by default.
For arbitrary orthogonal coordinates the integral is computed as:
where is the Jacobian determinant of the transformation
and is equal to the product of the scale factors
.
list with components
the final estimate of the integral.
estimate of the modulus of the absolute error.
cubature output when method "hcubature"
, "pcubature"
, "cuhre"
, "divonne"
, "suave"
or "vegas"
is used.
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
Other integrals:
ode()
### unidimensional integral i <- integral("sin(x)", bounds = list(x = c(0,pi))) i$value ### multidimensional integral f <- function(x,y) x*y i <- integral(f, bounds = list(x = c(0,1), y = c(0,1))) i$value ### vector-valued integrals f <- function(x,y) c(x, y, x*y) i <- integral(f, bounds = list(x = c(0,1), y = c(0,1))) i$value ### tensor-valued integrals f <- function(x,y) array(c(x^2, x*y, x*y, y^2), dim = c(2,2)) i <- integral(f, bounds = list(x = c(0,1), y = c(0,1))) i$value ### area of a circle i <- integral(1, bounds = list(r = c(0,1), theta = c(0,2*pi)), coordinates = "polar") i$value ### surface of a sphere i <- integral(1, bounds = list(r = 1, theta = c(0,pi), phi = c(0,2*pi)), coordinates = "spherical") i$value ### volume of a sphere i <- integral(1, bounds = list(r = c(0,1), theta = c(0,pi), phi = c(0,2*pi)), coordinates = "spherical") i$value
### unidimensional integral i <- integral("sin(x)", bounds = list(x = c(0,pi))) i$value ### multidimensional integral f <- function(x,y) x*y i <- integral(f, bounds = list(x = c(0,1), y = c(0,1))) i$value ### vector-valued integrals f <- function(x,y) c(x, y, x*y) i <- integral(f, bounds = list(x = c(0,1), y = c(0,1))) i$value ### tensor-valued integrals f <- function(x,y) array(c(x^2, x*y, x*y, y^2), dim = c(2,2)) i <- integral(f, bounds = list(x = c(0,1), y = c(0,1))) i$value ### area of a circle i <- integral(1, bounds = list(r = c(0,1), theta = c(0,2*pi)), coordinates = "polar") i$value ### surface of a sphere i <- integral(1, bounds = list(r = 1, theta = c(0,pi), phi = c(0,2*pi)), coordinates = "spherical") i$value ### volume of a sphere i <- integral(1, bounds = list(r = c(0,1), theta = c(0,pi), phi = c(0,2*pi)), coordinates = "spherical") i$value
Computes the numerical Jacobian of functions
or the symbolic Jacobian of characters
in arbitrary orthogonal coordinate systems.
jacobian( f, var, params = list(), coordinates = "cartesian", accuracy = 4, stepsize = NULL ) f %jacobian% var
jacobian( f, var, params = list(), coordinates = "cartesian", accuracy = 4, stepsize = NULL ) f %jacobian% var
f |
array of |
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. See |
params |
|
coordinates |
coordinate system to use. One of: |
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. |
The function is basically a wrapper for gradient
with drop=FALSE
.
array
.
f %jacobian% var
: binary operator with default parameters.
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
Other differential operators:
curl()
,
derivative()
,
divergence()
,
gradient()
,
hessian()
,
laplacian()
### symbolic Jacobian jacobian("x*y*z", var = c("x", "y", "z")) ### numerical Jacobian in (x=1, y=2, z=3) f <- function(x, y, z) x*y*z jacobian(f = f, var = c(x=1, y=2, z=3)) ### vectorized interface f <- function(x) x[1]*x[2]*x[3] jacobian(f = f, var = c(1, 2, 3)) ### symbolic vector-valued functions f <- c("y*sin(x)", "x*cos(y)") jacobian(f = f, var = c("x","y")) ### numerical vector-valued functions f <- function(x) c(sum(x), prod(x)) jacobian(f = f, var = c(0,0,0)) ### binary operator "x*y^2" %jacobian% c(x=1, y=3)
### symbolic Jacobian jacobian("x*y*z", var = c("x", "y", "z")) ### numerical Jacobian in (x=1, y=2, z=3) f <- function(x, y, z) x*y*z jacobian(f = f, var = c(x=1, y=2, z=3)) ### vectorized interface f <- function(x) x[1]*x[2]*x[3] jacobian(f = f, var = c(1, 2, 3)) ### symbolic vector-valued functions f <- c("y*sin(x)", "x*cos(y)") jacobian(f = f, var = c("x","y")) ### numerical vector-valued functions f <- function(x) c(sum(x), prod(x)) jacobian(f = f, var = c(0,0,0)) ### binary operator "x*y^2" %jacobian% c(x=1, y=3)
Computes the numerical Laplacian of functions
or the symbolic Laplacian of characters
in arbitrary orthogonal coordinate systems.
laplacian( f, var, params = list(), coordinates = "cartesian", accuracy = 4, stepsize = NULL, drop = TRUE ) f %laplacian% var
laplacian( f, var, params = list(), coordinates = "cartesian", accuracy = 4, stepsize = NULL, drop = TRUE ) f %laplacian% var
f |
array of |
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. See |
params |
|
coordinates |
coordinate system to use. One of: |
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. |
drop |
if |
The Laplacian is a differential operator given by the divergence of the
gradient of a scalar-valued function , resulting in a scalar value giving
the flux density of the gradient flow of a function.
The
laplacian
is computed in arbitrary orthogonal coordinate systems using
the scale factors :
where . When the function
is a tensor-valued function
, the
laplacian
is computed for each scalar component:
Scalar for scalar-valued functions when drop=TRUE
, array
otherwise.
f %laplacian% var
: binary operator with default parameters.
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
Other differential operators:
curl()
,
derivative()
,
divergence()
,
gradient()
,
hessian()
,
jacobian()
### symbolic Laplacian laplacian("x^3+y^3+z^3", var = c("x","y","z")) ### numerical Laplacian in (x=1, y=1, z=1) f <- function(x, y, z) x^3+y^3+z^3 laplacian(f = f, var = c(x=1, y=1, z=1)) ### vectorized interface f <- function(x) sum(x^3) laplacian(f = f, var = c(1, 1, 1)) ### symbolic vector-valued functions f <- array(c("x^2","x*y","x*y","y^2"), dim = c(2,2)) laplacian(f = f, var = c("x","y")) ### numerical vector-valued functions f <- function(x, y) array(c(x^2,x*y,x*y,y^2), dim = c(2,2)) laplacian(f = f, var = c(x=0,y=0)) ### binary operator "x^3+y^3+z^3" %laplacian% c("x","y","z")
### symbolic Laplacian laplacian("x^3+y^3+z^3", var = c("x","y","z")) ### numerical Laplacian in (x=1, y=1, z=1) f <- function(x, y, z) x^3+y^3+z^3 laplacian(f = f, var = c(x=1, y=1, z=1)) ### vectorized interface f <- function(x) sum(x^3) laplacian(f = f, var = c(1, 1, 1)) ### symbolic vector-valued functions f <- array(c("x^2","x*y","x*y","y^2"), dim = c(2,2)) laplacian(f = f, var = c("x","y")) ### numerical vector-valued functions f <- function(x, y) array(c(x^2,x*y,x*y,y^2), dim = c(2,2)) laplacian(f = f, var = c(x=0,y=0)) ### binary operator "x^3+y^3+z^3" %laplacian% c("x","y","z")
Multiplies two numeric
or character
matrices, if they are conformable. If one argument is a vector, it will be promoted to either a row or column matrix to make the two arguments conformable. If both are vectors of the same length, it will return the inner product (as a matrix
).
mx(x, y) x %mx% y
mx(x, y) x %mx% y
x |
|
y |
|
matrix
.
x %mx% y
: binary operator.
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
Other matrix algebra:
mxdet()
,
mxinv()
,
mxtr()
### numeric inner product x <- 1:4 mx(x, x) ### symbolic inner product x <- letters[1:4] mx(x, x) ### numeric matrix product x <- letters[1:4] y <- diag(4) mx(x, y) ### symbolic matrix product x <- array(1:12, dim = c(3,4)) y <- letters[1:4] mx(x, y) ### binary operator x <- array(1:12, dim = c(3,4)) y <- letters[1:4] x %mx% y
### numeric inner product x <- 1:4 mx(x, x) ### symbolic inner product x <- letters[1:4] mx(x, x) ### numeric matrix product x <- letters[1:4] y <- diag(4) mx(x, y) ### symbolic matrix product x <- array(1:12, dim = c(3,4)) y <- letters[1:4] mx(x, y) ### binary operator x <- array(1:12, dim = c(3,4)) y <- letters[1:4] x %mx% y
Computes the determinant of a numeric
or character
matrix.
mxdet(x)
mxdet(x)
x |
|
numeric
or character
.
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
Other matrix algebra:
mxinv()
,
mxtr()
,
mx()
### numeric matrix x <- matrix(1:4, nrow = 2) mxdet(x) ### symbolic matrix x <- matrix(letters[1:4], nrow = 2) mxdet(x)
### numeric matrix x <- matrix(1:4, nrow = 2) mxdet(x) ### symbolic matrix x <- matrix(letters[1:4], nrow = 2) mxdet(x)
Computes the inverse of a numeric
or character
matrix.
mxinv(x)
mxinv(x)
x |
|
numeric
or character
matrix.
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
Other matrix algebra:
mxdet()
,
mxtr()
,
mx()
### numeric matrix x <- matrix(1:4, nrow = 2, byrow = TRUE) mxinv(x) ### symbolic matrix x <- matrix(letters[1:4], nrow = 2, byrow = TRUE) mxinv(x)
### numeric matrix x <- matrix(1:4, nrow = 2, byrow = TRUE) mxinv(x) ### symbolic matrix x <- matrix(letters[1:4], nrow = 2, byrow = TRUE) mxinv(x)
Computes the trace of a numeric
or character
matrix.
mxtr(x)
mxtr(x)
x |
|
numeric
or character
.
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
Other matrix algebra:
mxdet()
,
mxinv()
,
mx()
### numeric matrix x <- matrix(1:4, nrow = 2) mxtr(x) ### character matrix x <- matrix(letters[1:4], nrow = 2) mxtr(x)
### numeric matrix x <- matrix(1:4, nrow = 2) mxtr(x) ### character matrix x <- matrix(letters[1:4], nrow = 2) mxtr(x)
Solves a numerical or symbolic system of ordinary differential equations.
ode( f, var, times, timevar = NULL, params = list(), method = "rk4", drop = FALSE )
ode( f, var, times, timevar = NULL, params = list(), method = "rk4", drop = FALSE )
f |
vector of |
var |
vector giving the initial conditions. See examples. |
times |
discretization sequence, the first value represents the initial time. |
timevar |
the time variable used by |
params |
|
method |
the solver to use. One of |
drop |
if |
Vector of final solutions if drop=TRUE
, otherwise a matrix
with as many
rows as elements in times
and as many columns as elements in var
.
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
Other integrals:
integral()
## ================================================== ## Example: symbolic system ## System: dx = x dt ## Initial: x0 = 1 ## ================================================== f <- "x" var <- c(x=1) times <- seq(0, 2*pi, by=0.001) x <- ode(f, var, times) plot(times, x, type = "l") ## ================================================== ## Example: time dependent system ## System: dx = cos(t) dt ## Initial: x0 = 0 ## ================================================== f <- "cos(t)" var <- c(x=0) times <- seq(0, 2*pi, by=0.001) x <- ode(f, var, times, timevar = "t") plot(times, x, type = "l") ## ================================================== ## Example: multivariate time dependent system ## System: dx = x dt ## dy = x*(1+cos(10*t)) dt ## Initial: x0 = 1 ## y0 = 1 ## ================================================== f <- c("x", "x*(1+cos(10*t))") var <- c(x=1, y=1) times <- seq(0, 2*pi, by=0.001) x <- ode(f, var, times, timevar = "t") matplot(times, x, type = "l", lty = 1, col = 1:2) ## ================================================== ## Example: numerical system ## System: dx = x dt ## dy = y dt ## Initial: x0 = 1 ## y0 = 2 ## ================================================== f <- function(x, y) c(x, y) var <- c(x=1, y=2) times <- seq(0, 2*pi, by=0.001) x <- ode(f, var, times) matplot(times, x, type = "l", lty = 1, col = 1:2) ## ================================================== ## Example: vectorized interface ## System: dx = x dt ## dy = y dt ## dz = y*(1+cos(10*t)) dt ## Initial: x0 = 1 ## y0 = 2 ## z0 = 2 ## ================================================== f <- function(x, t) c(x[1], x[2], x[2]*(1+cos(10*t))) var <- c(1,2,2) times <- seq(0, 2*pi, by=0.001) x <- ode(f, var, times, timevar = "t") matplot(times, x, type = "l", lty = 1, col = 1:3)
## ================================================== ## Example: symbolic system ## System: dx = x dt ## Initial: x0 = 1 ## ================================================== f <- "x" var <- c(x=1) times <- seq(0, 2*pi, by=0.001) x <- ode(f, var, times) plot(times, x, type = "l") ## ================================================== ## Example: time dependent system ## System: dx = cos(t) dt ## Initial: x0 = 0 ## ================================================== f <- "cos(t)" var <- c(x=0) times <- seq(0, 2*pi, by=0.001) x <- ode(f, var, times, timevar = "t") plot(times, x, type = "l") ## ================================================== ## Example: multivariate time dependent system ## System: dx = x dt ## dy = x*(1+cos(10*t)) dt ## Initial: x0 = 1 ## y0 = 1 ## ================================================== f <- c("x", "x*(1+cos(10*t))") var <- c(x=1, y=1) times <- seq(0, 2*pi, by=0.001) x <- ode(f, var, times, timevar = "t") matplot(times, x, type = "l", lty = 1, col = 1:2) ## ================================================== ## Example: numerical system ## System: dx = x dt ## dy = y dt ## Initial: x0 = 1 ## y0 = 2 ## ================================================== f <- function(x, y) c(x, y) var <- c(x=1, y=2) times <- seq(0, 2*pi, by=0.001) x <- ode(f, var, times) matplot(times, x, type = "l", lty = 1, col = 1:2) ## ================================================== ## Example: vectorized interface ## System: dx = x dt ## dy = y dt ## dz = y*(1+cos(10*t)) dt ## Initial: x0 = 1 ## y0 = 2 ## z0 = 2 ## ================================================== f <- function(x, t) c(x[1], x[2], x[2]*(1+cos(10*t))) var <- c(1,2,2) times <- seq(0, 2*pi, by=0.001) x <- ode(f, var, times, timevar = "t") matplot(times, x, type = "l", lty = 1, col = 1:3)
Provides fast algorithms for generating integer partitions.
partitions(n, max = 0, length = 0, perm = FALSE, fill = FALSE, equal = T)
partitions(n, max = 0, length = 0, perm = FALSE, fill = FALSE, equal = T)
n |
positive integer. |
max |
maximum integer in the partitions. |
length |
maximum number of elements in the partitions. |
perm |
logical. Permute partitions? |
fill |
logical. Fill partitions with zeros to match |
equal |
logical. Return only partition of |
list
of partitions, or matrix
if length>0
and fill=TRUE
.
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
### partitions of 4 partitions(4) ### partitions of 4 and permute partitions(4, perm = TRUE) ### partitions of 4 with max element equal to 2 partitions(4, max = 2) ### partitions of 4 with 2 elements partitions(4, length = 2) ### partitions of 4 with 3 elements, fill with zeros partitions(4, length = 3, fill = TRUE) ### partitions of 4 with 2 elements, fill with zeros and permute partitions(4, length = 2, fill = TRUE, perm = TRUE) ### partitions of all integers less or equal to 3 partitions(3, equal = FALSE) ### partitions of all integers less or equal to 3, fill to 2 elements and permute partitions(3, equal = FALSE, length = 2, fill = TRUE, perm = TRUE)
### partitions of 4 partitions(4) ### partitions of 4 and permute partitions(4, perm = TRUE) ### partitions of 4 with max element equal to 2 partitions(4, max = 2) ### partitions of 4 with 2 elements partitions(4, length = 2) ### partitions of 4 with 3 elements, fill with zeros partitions(4, length = 3, fill = TRUE) ### partitions of 4 with 2 elements, fill with zeros and permute partitions(4, length = 2, fill = TRUE, perm = TRUE) ### partitions of all integers less or equal to 3 partitions(3, equal = FALSE) ### partitions of all integers less or equal to 3, fill to 2 elements and permute partitions(3, equal = FALSE, length = 2, fill = TRUE, perm = TRUE)
Computes the Taylor series of functions
or characters
.
taylor( f, var, params = list(), order = 1, accuracy = 4, stepsize = NULL, zero = 1e-07 )
taylor( f, var, params = list(), order = 1, accuracy = 4, stepsize = NULL, zero = 1e-07 )
f |
|
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 |
params |
|
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. |
list
with components:
the Taylor series.
the approximation order.
data.frame
containing the variables, coefficients and degrees of each term in the Taylor series.
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
Other polynomials:
hermite()
Other derivatives:
derivative()
### univariate taylor series (in x=0) taylor("exp(x)", var = "x", order = 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) ### multivariate taylor series (in x=0 and y=1) taylor("x*(y-1)", var = c(x=0, y=1), order = 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) ### vectorized interface f <- function(x) prod(x) taylor(f, var = c(0,0,0), order = 3)
### univariate taylor series (in x=0) taylor("exp(x)", var = "x", order = 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) ### multivariate taylor series (in x=0 and y=1) taylor("x*(y-1)", var = c(x=0, y=1), order = 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) ### vectorized interface f <- function(x) prod(x) taylor(f, var = c(0,0,0), order = 3)
Wraps characters
in round brackets.
wrap(x)
wrap(x)
x |
|
Characters are automatically wrapped when performing basic symbolic operations to prevent unwanted results. E.g.:
instead of
To disable this behaviour run options(calculus.auto.wrap = FALSE)
.
character
.
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
Other utilities:
c2e()
,
e2c()
,
evaluate()
### wrap characters wrap("a+b") ### wrap array of characters wrap(array(letters[1:9], dim = c(3,3)))
### wrap characters wrap("a+b") ### wrap array of characters wrap(array(letters[1:9], dim = c(3,3)))