Check if a matrix is numeric, square, and meets the specified conditions
Source:R/check_matrix.R
check-matrix-square.Rd
check_matrix_square()
and check_is_matrix_square()
functions check whether a given object is a numeric square matrix.
check_matrix_square()
raises an error if the matrix is not
numeric, not a matrix, or not square, while check_is_matrix_square()
returns TRUE
if the matrix is valid and square, and FALSE
otherwise.
Usage
check_matrix_square(mat, is_numeric = TRUE, par_name = "mat")
check_is_matrix_square(mat, is_numeric)
Value
check_matrix_square()
: Returns the matrix if valid, otherwise raises an error message.check_is_matrix_square()
: Logical value,TRUE
if the matrix is numeric, square, and meets the conditions, andFALSE
otherwise.
Examples
# Examples for check_matrix_square()
# ----------------------------------
mat <- matrix(1:9, nrow = 3)
print(mat)
#> [,1] [,2] [,3]
#> [1,] 1 4 7
#> [2,] 2 5 8
#> [3,] 3 6 9
check_matrix_square(mat)
#> [,1] [,2] [,3]
#> [1,] 1 4 7
#> [2,] 2 5 8
#> [3,] 3 6 9
mat2 <- matrix(1:6, nrow = 2)
print(mat2)
#> [,1] [,2] [,3]
#> [1,] 1 3 5
#> [2,] 2 4 6
try(check_matrix_square(mat2))
#> Error in check_matrix_square(mat2) :
#> Expected 'mat' to be a square matrix but got a 2 by 3 matrix
mat3 <- matrix("a", nrow = 3, ncol = 3)
print(mat3)
#> [,1] [,2] [,3]
#> [1,] "a" "a" "a"
#> [2,] "a" "a" "a"
#> [3,] "a" "a" "a"
try(check_matrix_square(mat3))
#> Error in check_matrix_square(mat3) :
#> Expected 'mat' to be numeric but got matrix array
# Examples for check_is_matrix_square()
# -------------------------------------
check_is_matrix_square(mat1)
#> [1] FALSE
try(check_is_matrix_square(mat2))
#> [1] FALSE
try(check_is_matrix_square(mat3))
#> [1] FALSE