Skip to contents

stm_hessenberg() computes the Hessenberg form of a square matrix, optionally also returning the orthogonal matrix \( Q \) used in the transformation. This is useful in numerical linear algebra, particularly for simplifying matrix operations such as eigenvalue calculations.

Usage

stm_hessenberg(mat, calc_q = FALSE, digits = NULL)

Arguments

mat

A numeric square matrix to be transformed into its Hessenberg form.

calc_q

Optional. A logical indicating whether to return the orthogonal matrix \( Q \) in addition to the Hessenberg matrix. Default is FALSE, meaning only the Hessenberg matrix is returned.

digits

Optional. The number of digits to round the result to. If NULL, no rounding is performed.

Value

If calc_q = FALSE, a matrix representing the Hessenberg form of mat. If calc_q = TRUE, a list containing two elements: H (the Hessenberg matrix) and Q (the orthogonal matrix \( Q \)).

Details

The function uses the pracma::hessenberg() function to compute the Hessenberg form of a square matrix. Optionally, it also returns the orthogonal matrix used in the transformation, which is useful for certain numerical methods.

Examples

A <- matrix(
  c(4, 1, 2, 2, 3, 1, 1, 0, 2), nrow = 3, byrow = TRUE
)
print(A)
#>      [,1] [,2] [,3]
#> [1,]    4    1    2
#> [2,]    2    3    1
#> [3,]    1    0    2
stm_hessenberg(A)
#>           [,1]      [,2]      [,3]
#> [1,]  4.000000 -1.788854  1.341641
#> [2,] -2.236068  3.200000 -0.400000
#> [3,]  0.000000  0.600000  1.800000
stm_hessenberg(A, calc_q = TRUE)
#> $h
#>           [,1]      [,2]      [,3]
#> [1,]  4.000000 -1.788854  1.341641
#> [2,] -2.236068  3.200000 -0.400000
#> [3,]  0.000000  0.600000  1.800000
#> 
#> $q
#>      [,1]       [,2]       [,3]
#> [1,]    1  0.0000000  0.0000000
#> [2,]    0 -0.8944272 -0.4472136
#> [3,]    0 -0.4472136  0.8944272
#> 
stm_hessenberg(A, calc_q = TRUE, digits = 3)
#> $h
#>        [,1]   [,2]   [,3]
#> [1,]  4.000 -1.789  1.342
#> [2,] -2.236  3.200 -0.400
#> [3,]  0.000  0.600  1.800
#> 
#> $q
#>      [,1]   [,2]   [,3]
#> [1,]    1  0.000  0.000
#> [2,]    0 -0.894 -0.447
#> [3,]    0 -0.447  0.894
#>