我正在尝试使用R的solve()函数来找到线性方程组的解。系数矩阵是2X2。我的下面的代码写入一个R文件,我写完后执行。
在致电solve
之前,我会检查行列式是否不是0:
int prod1 = x * y_2;
int prod2 = x_2 * y;
int determinant = prod1 - prod2;
if (determinant != 0) {
strcat(Q, "A = array(c(");
strcat(Q, numx);
strcat(Q, ", ");
strcat(Q, numx_2);
strcat(Q, ", ");
strcat(Q, numy);
strcat(Q, ", ");
strcat(Q, numy_2);
strcat(Q, "), dim = c(2,2,1))\n");
strcat(Q, "b = c(");
strcat(Q, numz);
strcat(Q, ", ");
strcat(Q, numz_2);
strcat(Q, ")\n");
strcat(Q, "solve(A[,,1],b)\n");
}
然后我fputs
Q进入一个R文件,然后我的程序打开并计算命令。但是,我的if语句似乎没有正确捕获奇异矩阵。
我得到像这样的输出
Execution halted
Error in solve.default(A[, , 1], b) :
Lapack routine dgesv: system is exactly singular: U[2,2] = 0
Calls: solve -> solve.default
//repeat above line for another 60 lines//
the value of determinant: 1
the value of determinant: 1
the value of determinant: 1the value of determinant: 1
the value of determinant: 1the value of determinant: 1
the value of determinant: 1the value of determinant: 1
the value of determinant: 1the value of determinant: 1
the value of determinant: 2the value of determinant: 2
the value of determinant: 2the value of determinant: 2
//this continues for another 20 lines
Error in solve.default(A[, , 1], b) :
Lapack routine dgesv: system is exactly singular: U[2,2] = 0
Calls: solve -> solve.default
Execution halted
//this continue again till I CTRL + C
我怎么不捕捉奇异矩阵?