Skip to contents

Gradient operation.

Usage

grad(f, gradin, opstr, var, index)

Arguments

f

A LazyTensor or a ComplexLazyTensor.

gradin

a LazyTensor, a ComplexLazyTensor encoding a matrix of ones with an inner dimension equal to 1 and indexed by the same index and with number of rows equal to the number of rows of the first x variable (in x$data).

opstr

A string formula corresponding to a reduction (like "Sum" or "Max").

var

An integer number indicating regarding to which variable/parameter (given by name or by position index starting at 0) the gradient of the formula should be computed or a one of the LazyTensor contained in f.

index

A character that should be either i or j to specify whether if the reduction is indexed by i (rows), or j (columns). When the first f variable is indexed by i (resp. j), index cannot be i (resp. j).

Value

A matrix.

Details

grad(f, gradin, opstr, var, index) returns a matrix which corresponds to the gradient (more precisely, the adjoint of the differential operator) of f with respect to the variable var and applied to gradin with compiling the corresponding reduction operator of opstr. It has an additional integer input parameter index indicating if the inner dimension corresponds to columns, i.e. index = 'j' or rows, i.e. index = 'i'.

Author

Chloe Serre-Combe, Amelie Vernay

Examples

if (FALSE) {
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 
# nrow(x) == nrow(eta)

x_i <- LazyTensor(x, index = 'i')  # LazyTensor from matrix x, indexed by 'i'
y_j <- LazyTensor(y, index = 'j')  # LazyTensor from matrix y, indexed by 'j'
eta_i <- LazyTensor(eta, index = 'i')   # LazyTensor from matrix eta, 
                                        # indexed by 'i' (like x_i)

# gradient with the formula from position
grad_xy <- grad(sqnorm2(x_i-y_j), eta_i, "Sum", var = y_j$formula, "j")  

# gradient with the formula from index
grad_xy <- grad(sqnorm2(x_i-y_j), eta_i, "Sum", var = 0, "j")     
}