Close Navigation
Learn more about IBKR accounts
R: Symbolic Matrix Multiplications

R: Symbolic Matrix Multiplications

Posted March 6, 2024
Sang-Heon Lee
SHLee AI Financial Model

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

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
m <- 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

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))"

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(1, 2), 
                    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

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"
> 

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

Originally posted on SHLee AI Financial Model blog.

Join The Conversation

If you have a general question, it may already be covered in our FAQs. If you have an account-specific question or concern, please reach out to Client Services.

Leave a Reply

Your email address will not be published. Required fields are marked *

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from SHLee AI Financial Model and is being posted with its permission. The views expressed in this material are solely those of the author and/or SHLee AI Financial Model and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.