Skip to contents

stm_vander() creates a Vandermonde matrix from a given vector. It raises each element of the vector to the power of integers from 0 to n-1, where n is the length of the vector. The user can specify the number of columns in the matrix and whether the matrix should be in increasing order.

Usage

stm_vander(vec, ncols = NULL, is_increasing = FALSE)

Arguments

vec

A numeric vector for which the Vandermonde matrix will be created.

ncols

Optional. An integer specifying the number of columns in the matrix. Default is NULL, which keeps all columns.

is_increasing

A logical value indicating whether the matrix should be in increasing order. Default is FALSE.

Value

A matrix representing the Vandermonde matrix, either in the default order or reversed if is_increasing = TRUE.

Details

The function generates a Vandermonde matrix using the outer product, where the elements of vec are raised to successive powers from 0 to n-1. The user can specify the number of columns using the ncols parameter. The matrix can also be returned in increasing order if is_increasing is set to TRUE.

Examples

v <- c(1, 2, 3, 4)
print(v)
#> [1] 1 2 3 4
stm_vander(v)
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    1    1    1
#> [2,]    8    4    2    1
#> [3,]   27    9    3    1
#> [4,]   64   16    4    1
stm_vander(v, is_increasing = TRUE)
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    1    1    1
#> [2,]    1    2    4    8
#> [3,]    1    3    9   27
#> [4,]    1    4   16   64