如何在R中找到矩阵的符号反转;例如:
Matrix.test <- function(x) matrix(c(x, x^2, x^3, x^4, x^5, x^6, x^7, x^8, x^9, 2*x, 3*x, 4*x, 2*x^2, 3*x^3, 4*x^4, 5*x^5), 4, 4)
我知道有一个名为'Ryacas'的包,它是'yacas'的接口,但是我不能用它来做这样的计算。 'yacas'是一个用于符号操纵数学表达式的程序。 有关详细信息,请参阅link。
谢谢
答案 0 :(得分:7)
它适用于我:
> library(Ryacas)
> x <- Sym('x')
> M <- List(List(1,x),List(x,1))
> PrettyForm(M)
/ \
| ( 1 ) ( x ) |
| |
| ( x ) ( 1 ) |
\ /
> PrettyForm(Inverse(M))
/ \
| / 1 \ / -( x ) \ |
| | ------ | | ------ | |
| | 2 | | 2 | |
| \ 1 - x / \ 1 - x / |
| |
| / -( x ) \ / 1 \ |
| | ------ | | ------ | |
| | 2 | | 2 | |
| \ 1 - x / \ 1 - x / |
\ /
答案 1 :(得分:5)
接下来:
M2 <- List(List( x, x^2, x^3, x^4),
List( x^5, x^6, x^7, x^8),
List( x^9, 2*x ,3*x , 4*x),
List(2*x^2, 3*x^3, 4*x^4, 5*x^5))
Inverse(M2)
但答案非常复杂(我只能为重新格式化前四行而烦恼):
{{(x^6*3*x*5*x^5-x^6*4*x*4*x^4+x^8*2*x*4*x^4-x^7*2*x*5*x^5+
x^7*4*x*3*x^3-x^8*3*x*3*x^3)/(x^7*3*x*5*x^5-x^7*4*x*4*x^4-
x^8*2*x*5*x^5+x^8*4*x*3*x^3-x^9*3*x*3*x^3+x^9*3*x*3*x^3+
x^8*2*x*5*x^5-x^8*4*x*3*x^3-x^7*3*x*5*x^5+x^7*4*x*4*x^4-
x^19*4*x^4+x^19*4*x^4+x^18*5*x^5-x^9*4*x*2*x^2-x^18*5*x^5+x^9*4*x*2*x^2-x^11*2*x*2*x^2+x^11*2*x*2*x^2),(x^4*3*x*3*x^3-x^4*2*x*4*x^4+x^3*2*x*5*x^5-x^3*4*x*3*x^3-x^2*3*x*5*x^5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