如何在Android Studio中覆盖/更新Jama矩阵而不会出现任何错误? [英] How to overwrite/update a Jama Matrix in Android Studio without any error?
问题描述
Jama矩阵在我的代码(矩阵计算类)中定义如下:
Jama Matrices are defined in my code (Matrix computation class) as follows:
private Matrix A;
private Matrix B;
private Matrix 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 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.
矩阵C
的初始化和计算如下:
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);
在这里, MainActivity类每秒调用一次 Matrix计算类,并相应地更新矩阵B
.但是,该代码仅在第一次迭代时运行良好,并在稍后抛出错误,如下所示:
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.
经过一番挖掘,我发现这是由于矩阵覆盖(矩阵B
和C
)引起的.我的代码中的矩阵不能为static
或final
.当矩阵不是静态的时候,有什么方法可以使用Jama矩阵吗?在Matrix Studio中,是否有替代Jama进行Matrix操作?
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?
推荐答案
查看JAMA随附的源代码,在该位置抛出IllegalArgumentException:
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;
}
因此,显然C.times(B)
失败,这意味着C
和B
的尺寸不一致.从下面的行中我们还可以看到,C = new Matrix(2,2)
确实具有2x2的大小,这意味着B
必须具有错误的大小.
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.
我不明白您所说的矩阵覆盖"是什么意思? C = A.plus(C.times(B))
确实在内部分配了Matrix
的新实例.这与static
或final
无关.
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
.
请提供完整的最小示例.遗漏了很多部分,例如B初始化的地方.
Please provide a complete, minimal example. There are many parts missing, like the place where B is initialized.
这篇关于如何在Android Studio中覆盖/更新Jama矩阵而不会出现任何错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!