Skip to contents

stm_hadamard() generates a Hadamard matrix of size n, where n must be a power of 2. The matrix is recursively built by combining smaller Hadamard matrices.

Usage

stm_hadamard(n)

Arguments

n

An integer specifying the size of the matrix. It must be a power of 2.

Value

A Hadamard matrix of size n x n.

Details

The function checks whether n is a valid power of 2. If n is not a power of 2, an error is raised. The function starts with a 1x1 matrix and recursively expands it by creating larger matrices by joining smaller ones.

Examples

stm_hadamard(4)
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    1    1    1
#> [2,]    1   -1    1   -1
#> [3,]    1    1   -1   -1
#> [4,]    1   -1   -1    1
stm_hadamard(8)
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
#> [1,]    1    1    1    1    1    1    1    1
#> [2,]    1   -1    1   -1    1   -1    1   -1
#> [3,]    1    1   -1   -1    1    1   -1   -1
#> [4,]    1   -1   -1    1    1   -1   -1    1
#> [5,]    1    1    1    1   -1   -1   -1   -1
#> [6,]    1   -1    1   -1   -1    1   -1    1
#> [7,]    1    1   -1   -1   -1   -1    1    1
#> [8,]    1   -1   -1    1   -1    1    1   -1
stm_hadamard(16)
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
#>  [1,]    1    1    1    1    1    1    1    1    1     1     1     1     1
#>  [2,]    1   -1    1   -1    1   -1    1   -1    1    -1     1    -1     1
#>  [3,]    1    1   -1   -1    1    1   -1   -1    1     1    -1    -1     1
#>  [4,]    1   -1   -1    1    1   -1   -1    1    1    -1    -1     1     1
#>  [5,]    1    1    1    1   -1   -1   -1   -1    1     1     1     1    -1
#>  [6,]    1   -1    1   -1   -1    1   -1    1    1    -1     1    -1    -1
#>  [7,]    1    1   -1   -1   -1   -1    1    1    1     1    -1    -1    -1
#>  [8,]    1   -1   -1    1   -1    1    1   -1    1    -1    -1     1    -1
#>  [9,]    1    1    1    1    1    1    1    1   -1    -1    -1    -1    -1
#> [10,]    1   -1    1   -1    1   -1    1   -1   -1     1    -1     1    -1
#> [11,]    1    1   -1   -1    1    1   -1   -1   -1    -1     1     1    -1
#> [12,]    1   -1   -1    1    1   -1   -1    1   -1     1     1    -1    -1
#> [13,]    1    1    1    1   -1   -1   -1   -1   -1    -1    -1    -1     1
#> [14,]    1   -1    1   -1   -1    1   -1    1   -1     1    -1     1     1
#> [15,]    1    1   -1   -1   -1   -1    1    1   -1    -1     1     1     1
#> [16,]    1   -1   -1    1   -1    1    1   -1   -1     1     1    -1     1
#>       [,14] [,15] [,16]
#>  [1,]     1     1     1
#>  [2,]    -1     1    -1
#>  [3,]     1    -1    -1
#>  [4,]    -1    -1     1
#>  [5,]    -1    -1    -1
#>  [6,]     1    -1     1
#>  [7,]    -1     1     1
#>  [8,]     1     1    -1
#>  [9,]    -1    -1    -1
#> [10,]     1    -1     1
#> [11,]    -1     1     1
#> [12,]     1     1    -1
#> [13,]     1     1     1
#> [14,]    -1     1    -1
#> [15,]     1    -1    -1
#> [16,]    -1    -1     1