如何修复 Jama 中的 ArrayIndexOutOfBounds 错误?
我正在使用 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();
}
}
I am using jama libarary for matrix. I used following matrix but when i tried to get S, it gave me error.
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
When I tried to get S it produce following error.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Jama.SingularValueDecomposition.getS(SingularValueDecomposition.java:507)
at SVD2.main(SVD2.java:19)
Here is the code
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于Jama奇异值分解,行数不得小于列数。也许您应该尝试对您提供的矩阵转置进行 SVD 处理。
编辑:以下是来自
SingularValueDecomposition.java
的相关代码:S
被构造为nx n
数组,因此ArrayIndexOutOfBoundsException
唯一可能的来源是对this.s[i]
的引用。s
的空间在SingularValueDecomposition
构造函数中初始化(并且没有其他地方),如下所示:因此 Jama 实现将适用于 2x3 输入(与他们在类中所说的相矛盾) javadoc)。但我敢打赌它不适用于 2x4 输入。
For Jama's singular value decomposition, the number of rows must not be less than the number of columns. Maybe you should try SVD on the transpose of the matrix you provided.
EDIT: Here's the relevant code from
SingularValueDecomposition.java
:S
is constructed to be ann x n
array, so the only possible source of anArrayIndexOutOfBoundsException
is from the reference tothis.s[i]
.Space for
s
is initialized in theSingularValueDecomposition
constructor (amd no where else) like this:So the Jama implementation will work for a 2x3 input (contradicting what they say in the class javadoc). But I bet it won't work for a 2x4 input.
您能给我们展示访问矩阵的代码吗?您得到的异常清楚地表明您正在尝试在底层数组的合法范围之外进行索引。
Could you show us the code that is accessing the matrix? The exception you get clearly indicates that you are trying to index outside of the legal bounds of the underlying array.
这是一个 3x11 的数组。事实上,您收到 i = 4 的索引越界异常,这表明您的行数在某处指定不正确。
另一个像 Apache Commons Math 这样的库可能会有所帮助,但我不认为该库是这里的问题。真正的问题是你对 SVD 缺乏了解。
It's a 3x11 array. The fact that you're getting an index out of bounds exception for i = 4 suggests to me that your row count is specified incorrectly somewhere.
Another library like Apache Commons Math might help, but I don't believe the library is the issue here. It's your lack of understanding of SVD that's the real problem.