This function computes the matrix square root of a square matrix. The sqrt of a matrix \(A\) is \(S\) such that \(A = S S\).

sqrtm(x)

Arguments

x

a square matrix.

Details

The matrix square root \(S\) of \(M\), \(S = sqrtm(M)\) is defined as one (the “principal”) \(S\) such that \(S S = S^2 = M\), (in R, all.equal( S %*% S , M )).

The method works from the Schur decomposition.

Value

A matrix ‘as x’ with the matrix sqrt of x.

References

Higham, N.~J. (2008). Functions of Matrices: Theory and Computation; Society for Industrial and Applied Mathematics, Philadelphia, PA, USA.

See also

Author

Michael Stadelmann wrote the first version.

Examples

m <- diag(2)
sqrtm(m) == m # TRUE
#>      [,1] [,2]
#> [1,] TRUE TRUE
#> [2,] TRUE TRUE

(m <- rbind(cbind(1, diag(1:3)),2))
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    1    0    0
#> [2,]    1    0    2    0
#> [3,]    1    0    0    3
#> [4,]    2    2    2    2
sm <- sqrtm(m)
sm
#>           [,1]       [,2]       [,3]       [,4]
#> [1,] 0.9047913  0.2641622 -0.3378783  0.2726584
#> [2,] 0.4716007  1.1859458  1.4115195 -0.7409765
#> [3,] 0.3411609 -0.6372572  0.6139085  1.2399735
#> [4,] 0.6309893  0.8523152  0.5144370  1.3496714
zapsmall(sm %*% sm) # Zap entries ~= 2e-16
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    1    0    0
#> [2,]    1    0    2    0
#> [3,]    1    0    0    3
#> [4,]    2    2    2    2
stopifnot(all.equal(m, sm %*% sm))