calculus (version 0.1.0)

diag: Tensor Diagonals

Description

Extracts or replace the diagonal of a tensor, or construct a diagonal tensor.

Usage

diag(x, dim = 2, value = 1)

diag(x) <- value

Arguments

x

array, vector or integer.

dim

the dimension of the tensor.

value

the value for the diagonal elements.

Value

array diagonals.

Functions

  • diag: get diagonals.

  • diag<-: set diagonals.

Examples

Run this code
# NOT RUN {
# construct a diagonal 2x2 matrix
diag(2)

# construct a diagonal 2x2x2 tensor
diag(2, dim = 3)

# construct a diagonal 2x2x2 tensor with values 3 and 4
diag(2, dim = 3, value = c(3,4))
  
# construct a diagonal 3x3 matrix with values 1,2,3
diag(1:3) 
  
# extract diagonals 
x <- diag(1:4, dim = 3)
diag(x)

# replace diagonals
x <- diag(1:4, dim = 3)
diag(x) <- c(5,6,7,8)
x

# }

Run the code above in your browser using DataLab