如何在没有任何错误的情况下覆盖/更新 Android Studio 中的 Jama Matrix? [英] How to overwrite/update a Jama Matrix in Android Studio without any error?

查看:20
本文介绍了如何在没有任何错误的情况下覆盖/更新 Android Studio 中的 Jama Matrix?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Jama 矩阵在我的代码(矩阵计算类)中定义如下:

私有矩阵A;私有矩阵 B;私有矩阵 C;

矩阵A初始化如下:

A = new Matrix(2,2);A.set(0,0,1.5);A.set(0,1,0.0);A.set(1,0,0.0);A.set(1,1,1.5);

Matrix B 是一个 2*2 矩阵,初始化为单位矩阵,并且每秒被 MainActivity 类中的下一个相同大小的矩阵更新.

Matrix C 初始化和计算如下:

 if(C!=null)C = A.plus(C.times(B));别的 {C = 新矩阵(2,2);C.set(0,0,1);C.set(0,1,0.0);C.set(1,0,0.0);C.set(1,1,1);

这里,矩阵计算类每秒被MainActivity类调用,矩阵B会相应地更新.但是,该代码仅在第一次迭代时运行良好,并在稍后抛出错误,如下所示:

java.lang.IllegalArgumentException:矩阵内部维度必须一致.

经过一番挖掘,我发现这是由于矩阵覆盖(矩阵BC)引起的.我的代码中的矩阵不能是 staticfinal.当矩阵不是静态的时,有没有办法使用 Jama 矩阵?android studio 有没有替代Jama 的矩阵操作?

解决方案

查看 JAMA 提供的源代码,在那个地方抛出了 IllegalArgumentException:

/** 线性代数矩阵乘法,A * B@param B 另一个矩阵@return 矩阵乘积,A * B@exception IllegalArgumentException 矩阵内部尺寸必须一致.*/公共矩阵时间(矩阵 B){如果(B.m != n){throw new IllegalArgumentException("矩阵内部维度必须一致.");}矩阵 X = 新矩阵(m,B.n);double[][] C = X.getArray();double[] Bcolj = new double[n];for (int j = 0; j 

所以看起来C.times(B)失败了,这意味着CB维度不一致.我们还可以从下面的行中看到,C = new Matrix(2,2) 确实有 2x2 大小,这意味着 B 的大小肯定是错误的.>

我不明白您所说的矩阵覆盖"是什么意思?C = A.plus(C.times(B)) 确实在内部分配了一个新的 Matrix 实例.这与 staticfinal 无关.

请提供完整的最小示例.少了很多地方,比如B初始化的地方.

Jama Matrices are defined in my code (Matrix computation class) as follows:

private Matrix A;
private Matrix B;
private Matrix C;

The matrix A is initialized as follows:

A = new Matrix(2,2);
A.set(0,0,1.5);
A.set(0,1,0.0);
A.set(1,0,0.0);
A.set(1,1,1.5);

Matrix B is a 2*2 matrix, initialized as an identity matrix and is updated every second by the next matrix of the same size from the MainActivity class.

Matrix C is initialized and computed as follows:

 if(C!=null)
 C = A.plus(C.times(B));
 else {
 C = new Matrix(2,2);
 C.set(0,0,1);
 C.set(0,1,0.0);
 C.set(1,0,0.0);
 C.set(1,1,1);

Here, the Matrix computation class is called by the MainActivity class every second and matrix B is updated accordingly. However, the code runs well for only the first iteration and throws an error in later as follows:

java.lang.IllegalArgumentException: Matrix inner dimensions must agree.

After some digging, I found that it is caused due to matrix overwriting (Matrix B and C). The matrices in my code cannot be static or final. Is there any way to use the Jama matrix when the matrices are not static? Are there any alternatives to Jama in the android studio for Matrix operation?

解决方案

Looking at the source code provided with JAMA, the IllegalArgumentException is thrown at that place:

   /** Linear algebraic matrix multiplication, A * B
   @param B    another matrix
   @return     Matrix product, A * B
   @exception  IllegalArgumentException Matrix inner dimensions must agree.
   */

   public Matrix times (Matrix B) {
      if (B.m != n) {
         throw new IllegalArgumentException("Matrix inner dimensions must agree.");
      }
      Matrix X = new Matrix(m,B.n);
      double[][] C = X.getArray();
      double[] Bcolj = new double[n];
      for (int j = 0; j < B.n; j++) {
         for (int k = 0; k < n; k++) {
            Bcolj[k] = B.A[k][j];
         }
         for (int i = 0; i < m; i++) {
            double[] Arowi = A[i];
            double s = 0;
            for (int k = 0; k < n; k++) {
               s += Arowi[k]*Bcolj[k];
            }
            C[i][j] = s;
         }
      }
      return X;
   }

So appearently C.times(B) fails, which means that C and B dimensions do not agree. As we can also see from the line below, C = new Matrix(2,2) does have 2x2 size, which means that B must have a wrong size.

I do not understand what you mean with "matrix overwriting"? C = A.plus(C.times(B)) does allocate a new instance of Matrix internally. This has nothing to do with static or final.

Please provide a complete, minimal example. There are many parts missing, like the place where B is initialized.

这篇关于如何在没有任何错误的情况下覆盖/更新 Android Studio 中的 Jama Matrix?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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