keops_grad: Compute the gradient of a rkeops operator

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/generic-keops_grad.R

Description

The function keops_grad defines a new operator that is a partial derivative from a previously defined KeOps operator supplied as input regarding a specified input variable of this operator.

Usage

1
keops_grad(operator, var)

Arguments

operator

a function returned by keops_kernel implementing a formula.

var

a text string or an integer number indicating regarding to which variable/parameter (given by name or by position starting at 0) the gradient of the formula should be computed.

Details

The use of the function keops_grad is detailed in the vignettes. Run browseVignettes("rkeops") to access the vignettes.

KeOps gradient operators are defined based on KeOps formula and on operator Grad. The function keops_grad is a wrapper to define a new formula deriving the gradient of the formula associated to a previously defined operator. The user just needs to choose regarding which variable (given by name or by position starting at 0), they want to compute the partial derivative.

The function keops_grad then calls the function keops_kernel() to compile a new operator corresponding to the partial derivative of the input operator.

To decide regarding which variable the input operator should be derived, you can specify its name or its position starting as 0 with the input parameter var.

Value

a function that can be used to compute the value of the formula on actual data. This function takes as input a list of data corresponding to the formula arguments and return the computed values (generally a vector or a matrix depending on the reduction). It has an additional integer input parameter inner_dim indicating if the inner dimension (c.f. browseVignettes("rkeops")) corresponds to columns, i.e. inner_dim=1 (default), or rows, i.e. inner_dim=0, in the data.

Author(s)

Ghislain Durif

See Also

keops_kernel()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
## Not run: 
set_rkeops_options()

# defining an operator (reduction on squared distance)
formula <- "Sum_Reduction(SqNorm2(x-y), 0)"
args <- c("x=Vi(0,3)", "y=Vj(1,3)")
op <- keops_kernel(formula, args)
# defining its gradient regarding x
grad_op <- keops_grad(op, var="x")

# data
nx <- 100
ny <- 150
x <- matrix(runif(nx*3), nrow=nx, ncol=3)     # matrix 100 x 3
y <- matrix(runif(ny*3), nrow=ny, ncol=3)     # matrix 150 x 3
eta <- matrix(runif(nx*1), nrow=nx, ncol=1)   # matrix 100 x 1

# computation
input <- list(x, y, eta)
res <- grad_op(input)

# OR you can directly define gradient in a formula
# defining a formula with a Gradient
formula <- "Grad(Sum_Reduction(SqNorm2(x-y), 0), x, eta)"
args <- c("x=Vi(0,3)", "y=Vj(1,3)", "eta=Vi(2,1)")
# compiling the corresponding operator
op <- keops_kernel(formula, args)

# data
nx <- 100
ny <- 150
x <- matrix(runif(nx*3), nrow=nx, ncol=3)     # matrix 100 x 3
y <- matrix(runif(ny*3), nrow=ny, ncol=3)     # matrix 150 x 3
eta <- matrix(runif(nx*1), nrow=nx, ncol=1)   # matrix 100 x 1

# computation
input <- list(x, y, eta)
res <- op(input)

## End(Not run)

rkeops documentation built on Feb. 17, 2021, 5:08 p.m.