Handling Symbolic Matrices in R as in Matlab

丶灬走出姿态 提交于 2019-12-10 18:05:23

问题


I am failing to appropriately work with a Symbolic Matrix in R, with both rSymPy and Ryacas. In Matlab this is easy. I am looking for suggestions how to do this in R and get a similar output as in Matlab. To do this in Matlab I need the 'Symbolic Toolbox'.

In this example I wish to generate a Symbolic transition probability matrix "P", which is say 5 x 5, and has elements P11, P12,..., P55. I then want to use this matrix for multiplication (with itself and other matrices) and potentially perform also other operations.

(1) Matlab - Generate Symbolic Matrix -> OK

P = sym('P%d%d', [5 5])

(1) R - Get Symbolic Matrix -> OK

library(rSymPy)
P<-matrix(nrow=5, ncol=5)
for (i in 1:5){
  for (j in 1:5) {
P[i,j]<-paste0(Sym("P"), i, j) # tried Var("P") also
  }
}

then I want to mulitply these matrices.

(2) Matlab - Multiply Symbolic Matrix -> OK

P*P 

works just fine.

(2) R - Multiply Symbolic Matrix -> Problem

sympy("P * P")
Need Help Here!

does not work. Many other attempts did not work either. I need suggestions on how to do this correctly and get the same output as, for example, in (2) Matlab.

Additional Task:

(3) Matlab - sym() each element separately -> OK

Moreover I would ask for a suggestion how to Sym() a single element of the matrix P in R as in Matlab.

for i = 1:5;
    for j = 1:5;
    P = sprintf('P%d%d',i,j); 
    assignin('caller',P,sym(P));
    end;
end;

(3) R - Sym() each element separately -> Problem

Need Help Here!

Thx for all suggestions!


回答1:


When I did this recently, I ended up writing a function to convert R matrices to the format required for Python (see the 2x2 matrix given in the example on page 3 of https://cran.r-project.org/web/packages/rSymPy/rSymPy.pdf).

Let's first start with defining the matrix P in R:

library(rSymPy)

P<-matrix(nrow=5, ncol=5)
for (i in 1:5){
  for (j in 1:5) {
    P[i,j] = Var(paste0("P", i, j)) # Declare variable in matrix first
  }
}

I have just taken your code and adjusted it slightly. Specifically, for each element of the matrix P, create a SymPy variable for this element by doing Var(paste0("P", i, j)). For example, when i = j = 1, we will be creating the Sympy variable P11. The output of Var is the string "P11" which we store in P[1,1].

At this time, we have defined an R matrix P where P is given by:

     [,1]  [,2]  [,3]  [,4]  [,5] 
[1,] "P11" "P12" "P13" "P14" "P15"
[2,] "P21" "P22" "P23" "P24" "P25"
[3,] "P31" "P32" "P33" "P34" "P35"
[4,] "P41" "P42" "P43" "P44" "P45"
[5,] "P51" "P52" "P53" "P54" "P55"

We have created a SymPy variable for each element of P, but we haven't defined the symbolic matrix as yet. This is our next step.

We define the function to convert the R matrix to Python's format:

# Converts matrix in R to Python's format
mat2py <- function(x){
  str = lapply(1:nrow(x), function(i) paste0(x[i,], collapse = ", "))
  str = paste0("Matrix([", paste0("[", unlist(str), "]", collapse = ", "), "])")
  return (str)
}

You use the above function as follows to define the symbolic matrix P:

cat(sympy(paste0("P = ", mat2py(P))), "\n")

# [P11, P12, P13, P14, P15]
# [P21, P22, P23, P24, P25]
# [P31, P32, P33, P34, P35]
# [P41, P42, P43, P44, P45]
# [P51, P52, P53, P54, P55]

(I am just using cat(..., "\n") to print the matrix nicely in R).

Finally, compute P*P as follows:

cat(sympy("P*P"), "\n")



回答2:


And this works with rSymPy:

library(rSymPy)

a1 <- Var("a1")
a2 <- Var("a2")
a3 <- Var("a3")
a4 <- Var("a4")

A <- Matrix(List(a1, a2), List(a3, a4))

A*A
#[1] "[a2*a3 + a1**2, a1*a2 + a2*a4]\n[a1*a3 + a3*a4, a2*a3 + a4**2]"


来源:https://stackoverflow.com/questions/39820161/handling-symbolic-matrices-in-r-as-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!