计算R中稀疏矩阵的特征向量(Computing eigenvectors of a sparse matrix in R)
我试图计算R中大的稀疏矩阵的
m
第一特征向量。使用eigen()
是不现实的,因为大意味着N> 10 6 。到目前为止,我发现我应该使用
igraph
包中的ARPACK,它可以处理稀疏矩阵。 但是我不能让它在一个非常简单的(3x3)矩阵上工作:library(Matrix) library(igraph) TestDiag <- Diagonal(3, 3:1) TestMatrix <- t(sparseMatrix(i = c(1, 1, 2, 2, 3), j = c(1, 2, 1, 2, 3), x = c(3/5, 4/5, -4/5, 3/5, 1))) TestMultipliedMatrix <- t(TestMatrix) %*% TestDiag %*% TestMatrix
然后使用
arpack()
函数帮助示例中给出的代码来提取2个第一个特征向量:func <- function(x, extra=NULL) { as.vector(TestMultipliedMatrix %*% x) } arpack(func, options=list(n = 3, nev = 2, ncv = 3, sym=TRUE, which="LM", maxiter=200), complex = FALSE)
我收到一条错误消息:
Error in arpack(func, options = list(n = 3, nev = 2, ncv = 3, sym = TRUE, : At arpack.c:1156 : ARPACK error, NCV must be greater than NEV and less than or equal to N
我不明白这个错误,因为这里ncv(3)大于nev(2),并且等于N(3)。
我是否犯了一些愚蠢的错误,或者有更好的方法来计算R中稀疏矩阵的特征向量?
更新
此错误显然是由于
arpack()
函数中的大写/小写NCV和NEV的错误。任何解决这个bug的建议(我试着看一下包代码,但这对我来说太复杂了)或者以其他方式计算特征向量是值得欢迎的。
I am trying to compute the
m
first eigenvectors of a large sparse matrix in R. Usingeigen()
is not realistic because large means N > 106 here.So far I figured out that I should use ARPACK from the
igraph
package, which can deal with sparse matrices. However I can't get it to work on a very simple (3x3) matrix:library(Matrix) library(igraph) TestDiag <- Diagonal(3, 3:1) TestMatrix <- t(sparseMatrix(i = c(1, 1, 2, 2, 3), j = c(1, 2, 1, 2, 3), x = c(3/5, 4/5, -4/5, 3/5, 1))) TestMultipliedMatrix <- t(TestMatrix) %*% TestDiag %*% TestMatrix
And then using the code given in example of the help of the
arpack()
function to extract the 2 first eigenvectors :func <- function(x, extra=NULL) { as.vector(TestMultipliedMatrix %*% x) } arpack(func, options=list(n = 3, nev = 2, ncv = 3, sym=TRUE, which="LM", maxiter=200), complex = FALSE)
I get an error message:
Error in arpack(func, options = list(n = 3, nev = 2, ncv = 3, sym = TRUE, : At arpack.c:1156 : ARPACK error, NCV must be greater than NEV and less than or equal to N
I don't understand this error, as ncv (3) is greater than nev (2) here, and equal to N (3).
Am I making some stupid mistake or is there a better way to compute eigenvectors of a sparse matrix in R?
Update
This error is apparently due to a bug in the
arpack()
function with uppercase / lowercase NCV and NEV.Any suggestions to solve the bug (I tried to have a look at the package code but it is far too complex for me to understand) or compute the eigenvectors in an other way are welcome.
原文:https://stackoverflow.com/questions/16966748
最满意答案
.NET中没有内置数据库来跟踪特定语言字母表中出现的字母。 这将是一个非常大的。 而一个有争议的国家,即使是像荷兰语这样简单字母的国家也有发言者不同意Ÿ有向字是否在字母表中以及它出现在什么位置。 前南斯拉夫有两个字母,战争已经过去了。 瑞典在不久前加入了W,这是一个多变的,被万维网逼迫。 对于像中国人和韩国人这样的语言而言,这是一个相当不实用的问题
在一般情况下,您不希望必须解决此问题。
There's no database built into .NET that keeps track of the letters that appear in the alphabet of a particular language. It would be a very large one. And a controversial one, even a country with a simple alphabet like Dutch has speakers that don't agree whether the Ÿ digraph is in the alphabet or not and at what position it appears. The former Yugoslavia had two alphabets, wars have been fought over it. And a changeable one, Swedish added W not long ago, forced to by the World Wide Web. And a rather unpractical one for a languages like Chinese and Korean.
You do not want to have to solve this problem in the general case.
相关问答
更多-
我可以使用awk将所有小写字母转换为大写字母吗?(Can I use awk to convert all the lower-case letters into upper-case?)[2023-08-04]
尝试这个: awk '{ print toupper($0) }' <<< "your string" 使用文件: awk '{ print toupper($0) }' yourfile.txt Try this: awk '{ print toupper($0) }' <<< "your string" Using a file: awk '{ print toupper($0) }' yourfile.txt -
通过阅读inputmask jquery插件文档 ,有两件事情迫使文本输入内容为小写。 pastedValue = pastedValue.toLowerCase(); 将任何复制粘贴的文本更改为小写。 casing: "lower"将任何类型化的文本更改为小写。 删除这两行,我们留下了下面的内容,它适用于你的jsfiddle。 $(document).ready(function(){ //email mask $("#u_email").inputmask({ m ...
-
为什么不能将大写字母用于定义值的模式匹配?(Why can't upper-case letters be used for pattern matching for define values?)[2021-10-19]
因为Scala的设计者更喜欢允许以大写字母开头的标识符这样使用(并且允许两者都会令人困惑): val A = 1 2 match { case A => true case _ => false } // returns false, because 2 != A 请注意,小写你会得到 val a = 1 2 match { case a => true case _ => false } // returns true 因为case a绑定了一个名为a的新变量。 一个非常常见的情况 ... -
toupper将返回大写值而不是就地修改值。 正如你的代码应该是这样的: word[j] = toupper(word[j]); toupper will return the uppercase value instead of modifying the value in-place. As such your code should read: word[j] = toupper(word[j]);
-
只需将if语句更改为 if achar == 'h' or achar == 'H': 如果你想计算所有'h'和'H'。 Just change if statement to if achar == 'h' or achar == 'H': If you want to count all 'h' and 'H'.
-
引用W3C : URL通常区分大小写(机器名称除外)。 可能存在URL或URL的一部分,其中大小写无关紧要,但识别这些可能并不容易。 用户应始终认为URL区分大小写。 我会说不,保持小写。 Quoting the W3C: URLs in general are case-sensitive (with the exception of machine names). There may be URLs, or parts of URLs, where case doesn't matter, but id ...
-
尝试这样的JavaScript验证: if (value.match(/^[A-Z]*$/)) { // matches } else { // doesn't match } 并在服务器端验证在PHP中: if (preg_match("/^[A-Z]*$/", $value)) { // matches } else { // doesn't match } 进行额外的服务器端检查总是一个好主意,因为javascript检查可以轻松绕过。 Try something ...
-
.NET中没有内置数据库来跟踪特定语言字母表中出现的字母。 这将是一个非常大的。 而一个有争议的国家,即使是像荷兰语这样简单字母的国家也有发言者不同意Ÿ有向字是否在字母表中以及它出现在什么位置。 前南斯拉夫有两个字母,战争已经过去了。 瑞典在不久前加入了W,这是一个多变的,被万维网逼迫。 对于像中国人和韩国人这样的语言而言,这是一个相当不实用的问题 在一般情况下,您不希望必须解决此问题。 There's no database built into .NET that keeps track of the ...
-
let textFieldOutput = "Wait a moment, please." let newString = textFieldOutput.uppercased() //The string is now "WAIT A MOMENT, PLEASE." let textFieldOutput = "Wait a moment, please." let newString = textFieldOutput.uppercased() //The string is now "WAIT ...
-
在for循环中需要第二个“if”的情况来检查大写字母。 您已将p和j初始化为大写范围 char p = 'a', j = 'z'; 然后,通过减去小写的'a'来确定最终数组中的字母索引 x = s[c][i] - 'a'; 现在用大写做同样的事情! 请记住,大写字母是完全不同的ASCII值 所以初始化一些字符: char uppercaseA = 'A', uppercaseZ = 'Z'; 添加第二个if语句(或将其与第一个if语句结合使用): else if ((s[c][i] >= upperc ...