如何修复 Jama 中的 ArrayIndexOutOfBounds 错误?

发布于 2024-08-19 06:12:52 字数 1096 浏览 13 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

高冷爸爸 2024-08-26 06:12:52

对于Jama奇异值分解,行数不得小于列数。也许您应该尝试对您提供的矩阵转置进行 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 被构造为 nx n 数组,因此ArrayIndexOutOfBoundsException 唯一可能的来源是对 this.s[i] 的引用。

s 的空间在 SingularValueDecomposition 构造函数中初始化(并且没有其他地方),如下所示:

s = new double [Math.min(m+1,n)];

因此 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:

   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 is constructed to be an n x n array, so the only possible source of an ArrayIndexOutOfBoundsException is from the reference to this.s[i].

Space for s is initialized in the SingularValueDecomposition constructor (amd no where else) like this:

s = new double [Math.min(m+1,n)];

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.

临走之时 2024-08-26 06:12:52

您能给我们展示访问矩阵的代码吗?您得到的异常清楚地表明您正在尝试在底层数组的合法范围之外进行索引。

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.

请远离我 2024-08-26 06:12:52

这是一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文