我正在为矩阵使用 jama 库。我使用了以下矩阵,但是当我尝试获取 S 时,它给了我错误。
1.0 1.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 11.0 1.0
1.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 12.0 2.0
1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 13.0 3.0
当我尝试获取 S 时,它产生了以下错误。
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Jama.SingularValueDecomposition.getS(SingularValueDecomposition.java:507)
at SVD2.main(SVD2.java:19)
这是代码
public class SVD2 {
public static void main(String[] args) {
double[][] vals = {
{1,1,0,1,0,0,0,0,0,11,1},
{1,0,0,0,0,0,1,0,0,12,2},
{1,1,0,0,0,0,0,0,1,13,3}
};
Matrix A = new Matrix(vals,3,11);
System.out.println("The Matrix A is ");
A.print(11, 2);
System.out.println();
System.out.println("The SVD of A is ");
SingularValueDecomposition svd = A.svd();
Matrix S = svd.getS();
}
}
最佳答案
对于 Jama 的 奇异值分解,the number of rows must not be less than the number of columns .也许您应该在您提供的矩阵的转置上尝试 SVD。
编辑:这是来自 SingularValueDecomposition.java
的相关代码:
public Matrix getS () {
Matrix X = new Matrix(n,n);
double[][] S = X.getArray();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
S[i][j] = 0.0;
}
S[i][i] = this.s[i];
}
return X;
}
S
构造为 n x n
数组,因此 ArrayIndexOutOfBoundsException
的唯一可能来源是对 this 的引用.s[i]
.
s
的空间在 SingularValueDecomposition
构造函数中初始化(amd 没有其他地方),如下所示:
s = new double [Math.min(m+1,n)];
因此 Jama 实现将适用于 2x3 输入(与他们在类 javadoc 中所说的相矛盾)。但我敢打赌它不适用于 2x4 输入。
关于java - 如何修复 Jama 中的 ArrayIndexOutOfBounds 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2112987/