Learn R Programming

calculus (version 0.3.0)

diagonal: Tensor Diagonals

Description

Functions to extract or replace the diagonals of an array, or construct a diagonal array.

Usage

diagonal(x = 1, dim = rep(2, 2))

diagonal(x) <- value

Arguments

x

an array, or vector giving the values of the diagonal entries.

dim

the dimensions of the (square) array.

value

vector giving the values of the diagonal entries.

Value

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.

Functions

  • diagonal<-: set diagonals.

References

Guidotti, E. (2020). "calculus: High dimensional numerical and symbolic calculus in R". https://arxiv.org/abs/2101.00086

See Also

Other tensor algebra: contraction(), delta(), einstein(), epsilon(), index()

Examples

Run this code
# NOT RUN {
### 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

# }

Run the code above in your browser using DataLab