如何求解奇异矩阵? [英] How to solve singular matrices?

查看:703
本文介绍了如何求解奇异矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要solve列表中的一千多个矩阵.但是,出现错误Lapack routine dgesv: system is exactly singular.我的问题是我的输入数据是非奇异矩阵,但是按照计算,我需要对矩阵进行处理,其中一些会变得奇异.但是,由于要花很长时间(我已经尝试过),所以无法复制带有我的数据子集的示例.这是我的问题的一个基本示例(A是经过一些计算的矩阵,R是我需要做的下一次计算):

I need to solve more than thousand matrices in a list. However, I get the error Lapack routine dgesv: system is exactly singular. My problem is that my input data are non singular matrices, but following the calculations I need to do on the matrices some of them get singular. A reproducible example with a subset of my data is however not possible as it would be to long (I tried already). Here a basic example of my problem (A would be a matrix after some calculations and R the next calculation I need to do):

A=matrix(c(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1), nrow=4)
R = solve(diag(4)-A)

您是否有想法如何解决"此问题,也许还有其他功能?还是为了不产生完全不同的结果,如何非常非常简单地转换奇异矩阵?谢谢

Do you have ideas how to "solve" this problem, maybe other function? Or how to transform the singular matrices very very slightly in order to not create totally different results? Thanks

根据@Roman Susi,我包括我必须做的功能(包括所有计算):

according to @Roman Susi I include the function (with all calculations) I have to do:

function(Z, p) {
  imp <- as.vector(cbind(imp=rowSums(Z)))
  exp <- as.vector(t(cbind(exp=colSums(Z))))
  x = p + imp
  ac = p + imp - exp  
  einsdurchx = 1/as.vector(x)    
  einsdurchx[is.infinite(einsdurchx)] <- 0
  A = Z %*% diag(einsdurchx)
  R = solve(diag(length(p))-A) %*% diag(p)    
  C = ac * einsdurchx
  R_bar = diag(as.vector(C)) %*% R
  rR_bar = round(R_bar)
  return(rR_bar)
}

问题出在计算solve(diag(length(p))-A)的函数的第8行中.在这里,我可以提供Zp的新示例数据,但是在此示例中,它可以正常工作,因为我无法重新创建出现错误的示例:

The problem is in line 8 of the function calculating solve(diag(length(p))-A). Here I can provide new example data for Z and p, however in this example it works fine as I was not able to recreate an example which brings the error:

p = c(200, 1000, 100, 10)
Z = matrix(
  c(0,0,100,200,0,0,0,0,50,350,0,50,50,200,200,0),
  nrow = 4, 
  ncol = 4,
  byrow = T) 

因此,根据@Roman Susi的问题是:是否有一种方法可以更改以前的计算,以使det(diag(length(p))-A)永远不会为0变为方程式?希望您能理解我想要的内容:)想法,谢谢. Edit2 :也许更容易被问到:如何避免此函数内的奇异性(至少在第8行之前)?

So, the question according to @Roman Susi is: Is there a way to change the calculations before so that det(diag(length(p))-A) never gets 0 in order to solve the equation? I hope you can understand what I want:) Ideas, thanks. Maybe asked easier: How to avoid singularity within this function (at least before line 8)?

推荐答案

MASS软件包中的广义逆ginv可以处理奇异矩阵,但必须确定对您的问题是否有意义.

The generalized inverse ginv in the MASS package can handle singular matrices but whether it makes sense or not for your problem would have to be determined.

 library(MASS)
 ginv(diag(4) - A)

给予:

     [,1] [,2] [,3] [,4]
[1,]    0    0    0    0
[2,]    0    0    0    0
[3,]    0    0    0    0
[4,]    0    0    0    0

ibdreg软件包中还有Ginv函数.

There is also the Ginv function in the ibdreg package.

这篇关于如何求解奇异矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆