add_diag: Add to the diagonal of a square matrix.

Description Usage Arguments Details Value Author(s) Examples

Description

Adds a constant or a vector to the diagonal of a numeric square matrix.

Usage

1
add_diag(x, epsilon = sqrt(.Machine$double.eps), check = TRUE)

Arguments

x

A square matrix, either of type double or integer.

epsilon

Values to be added to the main diagonal of x, either a single numeric value or a numeric vector. Its type must match the type of x.

check

Should checks of the input be performed? Not doing so is faster.

Details

This function modifies the matrix x in place for getting maximum speed by avoiding making copies.

Value

Noting, the matrix is modified in place.

Author(s)

Dominik Mueller

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
d <- diag(5L)
add_diag(d, 1)
diag(d)

# Speed comparison:
d <- diag(1000L)
epsilon <- 1e-6
microbenchmark::microbenchmark(times = 100L,
add_diag(d, epsilon),
add_diag(d, epsilon, check = FALSE),
diag(d) <- diag(d) + epsilon # canonical approach
)

DominikMueller64/dmisc documentation built on May 6, 2019, 2:52 p.m.