R: Symbolic Matrix Multiplications

This post shows how to perform symbolic matrix multiplications using two R libraries.

Little Forest リトル・フォレスト - Summer/Autumn


Symbolic Matrix Multiplications in R



Symbolic matrix multiplication is useful when trying to identify patterns in their products. To multiply two matrices with string elements (symbolic matrix), we can use the mx() function in the calculus R library, as follows.

library(calculus) # mx
 
# sample matrix with string elements
<- matrix(
    c("1""0""0",
      "0""a""0",
      "0","-a""a"),
    3,3, byrow = TRUE)
 
# symblic matrix multiplication
m2<-mx(m, m); m2
m3<-mx(m, m2); m3
 
cs

The output is satisfactory, but its length is excessive. A more concise representation would enhance its utility.

> # symblic matrix multiplication
> m2<-mx(m, m); m2
     [,1]        [,2]                      [,3]       
[1,] "(1) * (1)" "0"                       "0"        
[2,] "0"         "(a) * (a)"               "0"        
[3,] "0"         "(-a) * (a) + (a) * (-a)" "(a) * (a)"
> m3<-mx(m, m2); m3
     [,1]                [,2]                                                   [,3]               
[1,] "(1) * ((1) * (1))" "0"                                                    "0"                
[2,] "0"                 "(a) * ((a) * (a))"                                    "0"                
[3,] "0"                 "(-a) * ((a) * (a)) + (a) * ((-a) * (a) + (a) * (-a))" "(a) * ((a) * (a))"
> 
 
cs


To compress the results, I make a user-defined function by utilizing the yacas(Expand("string")) function in the Ryacas0 R library, as demonstrated below.

library(Ryacas0)  # yacas
 
# Function to apply yacas expression to each element 
# and replace with as.character(xx)
apply_yacas_and_replace <- function(mat) {
    ret_mat <- matrix(nrow = nrow(mat), ncol = ncol(mat))
    
    for (i in 1:nrow(mat)) {
        for (j in 1:ncol(mat)) {
            # Apply yacas expression to each element
            expr <- yacas(paste("Expand(", mat[i, j], ")"
                                sep = ""))
            ret_mat[i, j] <- as.character(expr)
        }
    }
 
    # delete spaces
    ret_mat<- apply(ret_mat, c(12), 
                    function(x) gsub("\\s""", x))
    
    return(ret_mat)
}
 
# Apply yacas expression and replace in the matrix
m2_2 <- apply_yacas_and_replace(m2); m2_2
m3_2 <- apply_yacas_and_replace(m3); m3_2
 
cs

The outcomes now appear visually appealing, facilitating a more straightforward identification of potential patterns.

> # Apply yacas expression and replace in the matrix
> m2_2 <- apply_yacas_and_replace(m2); m2_2
     [,1] [,2]     [,3
[1,] "1"  "0"      "0"  
[2,] "0"  "a^2"    "0"  
[3,] "0"  "-2*a^2" "a^2"
> m3_2 <- apply_yacas_and_replace(m3); m3_2
     [,1] [,2]     [,3
[1,] "1"  "0"      "0"  
[2,] "0"  "a^3"    "0"  
[3,] "0"  "-3*a^3" "a^3"
> 
cs


In summary, the collaboration between these two R libraries has yielded relatively satisfactory results for symbolic matrix multiplication.



No comments:

Post a Comment