Learn R Programming

GPUmatrix (version 1.0.2)

diag: diag

Description

This function mimics the base function 'diag' to operate on gpu.matrix-class objects: "extract or replace the diagonal of a matrix, or constructs a diagonal matrix."

Usage

# S4 method for gpu.matrix.tensorflow
diag(x)
# S4 method for gpu.matrix.torch
diag(x)
# S4 method for gpu.matrix.tensorflow,numeric
diag(x) <- value
# S4 method for gpu.matrix.torch,numeric
diag(x) <- value

Value

Output corresponding to the base function diag: If input x is a gpu.matrix-class object then diag{x} returns a numeric object with the diagonal of the matrix x (this output is not a gpu.matrix-class object).

The replacement form diag(x) <- value sets the diagonal of the matrix x to the given value(s).

Arguments

x

a gpu.matrix.

value

either a single value or a vector of length equal to that of the current diagonal.

See Also

For more information see: diag

Examples

Run this code

if(installTorch()){

  a <- gpu.matrix(rnorm(9),nrow=3,ncol=3)

  diag(a) #shows the diagonal of matrix a

  diag(a) <- c(10,0,100) #set the diagonal of matrix a
  a

}





Run the code above in your browser using DataLab