问题描述
我无法同时使用rSymPy和Ryacas在R中使用符号矩阵.在Matlab中,这很容易. 我正在寻找有关如何在R中执行此操作以及获得与Matlab类似的输出的建议.为此,我需要在"Matlab"中使用符号工具箱".
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'.
在此示例中,我希望生成一个符号转换概率矩阵"P",即5 x 5,并具有元素P11,P12,...,P55.然后,我想将此矩阵用于与其自身和其他矩阵的乘法运算,并可能还要执行其他运算.
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-生成符号矩阵->确定
P = sym('P%d%d', [5 5])
(1)R-获取符号矩阵->确定
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-乘以符号矩阵->确定
P*P
工作正常.
(2)R-乘以符号矩阵->问题
sympy("P * P")
Need Help Here!
不起作用.许多其他尝试也不起作用.我需要有关如何正确执行此操作并获得与(2)Matlab中相同的输出的建议.
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.
其他任务:
(3)Matlab-sym()分别将每个元素->确定
此外,我想提出一个建议,如Matlab中那样,如何对R中矩阵P的单个元素进行Sym().
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()分别每个元素->问题
Need Help Here!
感谢所有建议!
推荐答案
这适用于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]"
这篇关于在Matlab中处理R中的符号矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!