Check if two matrices are compatible for multiplication or have the same dimensions
Source:R/check_matrices_compatible.R
check-matrices-compatible.Rd
check_matrices_compatible()
and check_is_matrices_compatible()
validate if two matrices are compatible either for matrix multiplication or
have the same dimensions.
check_matrices_compatible()
raises an error if the matrices do not
satisfy the compatibility conditions, while
check_is_matrices_compatible()
returns TRUE
if the matrices
are compatible, and FALSE
otherwise.
Usage
check_matrices_compatible(
a,
b,
is_multiplication = FALSE,
apar_name = "a",
bpar_name = "b"
)
check_is_matrices_compatible(a, b, is_multiplication = FALSE)
Arguments
- a
The first matrix to check.
- b
The second matrix to check.
- is_multiplication
Logical indicating whether to check for compatibility for matrix multiplication. Default is
FALSE
).- apar_name
A name for the parameter
a
to display in error messages.- bpar_name
A name for the parameter
b
to display in error messages.
Value
check_matrices_compatible()
: Returns a list containing the input matricesa
andb
if they are compatible, or raises an error if the matrices do not meet the compatibility conditions.check_is_matrices_compatible()
: Logical value,TRUE
if the matrices are compatible,FALSE
otherwise.
Examples
# Examples for check_matrices_compatible()
# ----------------------------------------
A <- matrix(1:6, nrow = 2)
B <- matrix(7:12, nrow = 3)
check_matrices_compatible(A, B, is_multiplication = TRUE)
#> $a
#> [,1] [,2] [,3]
#> [1,] 1 3 5
#> [2,] 2 4 6
#>
#> $b
#> [,1] [,2]
#> [1,] 7 10
#> [2,] 8 11
#> [3,] 9 12
#>
try(check_matrices_compatible(A, B))
#> Error in check_matrices_compatible(A, B) :
#> Matrix 'a' and 'b' must have the same dimensions but got 2 by 3 and 3 by 2 matrices respectively
C <- matrix(1:4, nrow = 2)
D <- matrix(5:8, nrow = 2)
check_matrices_compatible(C, D)
#> $a
#> [,1] [,2]
#> [1,] 1 3
#> [2,] 2 4
#>
#> $b
#> [,1] [,2]
#> [1,] 5 7
#> [2,] 6 8
#>
try(check_matrices_compatible(C, D, is_multiplication = TRUE))
#> $a
#> [,1] [,2]
#> [1,] 1 3
#> [2,] 2 4
#>
#> $b
#> [,1] [,2]
#> [1,] 5 7
#> [2,] 6 8
#>
try(check_matrices_compatible(A, "not a matrix"))
#> Error in check_matrix(mat = b, par_name = bpar_name) :
#> Expected 'b' to be a matrix but got character
# Examples for check_is_matrices_compatible()
# -------------------------------------------
check_is_matrices_compatible(A, B, is_multiplication = TRUE)
#> [1] TRUE
check_is_matrices_compatible(C, D)
#> [1] TRUE
check_is_matrices_compatible(C, D, is_multiplication = TRUE)
#> [1] TRUE
check_is_matrices_compatible(A, "not a matrix")
#> [1] FALSE