我想计算矩阵的LU分解并从中提取线性组合。
我首先使用Armadillo here库问了一个问题,但正如一个评论所指出的那样,Armadillo无法处理模数计算。
因此,我从头开始使用素数模量开发了LU,这是我获得的,但仍然有一个我看不到的错误。
这是我现在拥有的代码。 (不要过多考虑Matrix类,它只是暂时封装vector<vector<int>>
的一种方法。
Matrix* Matrix::triangulation(Matrix & ident)
{
unsigned int n = getNbLines();
unsigned int m = getNbColumns();
vector<vector<int>> mat = getMat();
vector<vector<int>> identity = ident.getMat();
vector<vector<int>> lower;
vector<vector<int>> upper;
/* ------------------------------------------------------------ */
/**
* @brief
* This code initialize a 'lower' matrix of size 'n x n'.
* The matrix is fill with only '0'.
*/
for(unsigned int i = 0; i < n; i++) {
vector<int> v(m);
lower.push_back(v);
for(unsigned int j = 0; j < m; j++) lower[i][j] = 0;
}
/**
* @brief
* This code initialize an 'upper' matrix of size 'n x m'.
* The matrix is fill with only '0'.
*/
for(unsigned int i = 0; i < n; i++) {
vector<int> v(m);
upper.push_back(v);
for(unsigned int j = 0; j < m; j++) upper[i][j] = 0;
}
/**
* @brief
* This code initialize an 'identity' matrix of size 'm x m'.
* The matrix is fill with only '0'.
*/
for(unsigned int i = 0; i < m; i++) {
vector<int> v2(m);
identity.push_back(v2);
for(unsigned int j = 0; j < m; j++) identity[i][j] = 0;
identity[i][i] = 1;
}
/* ------------------------------------------------------------ */
// Decomposing matrix into Upper and Lower triangular matrix
for (unsigned int i = 0; i < n; i++) {
// Upper Triangular
for (unsigned int k = 0; k < m; k++) {
// Summation of L(i, j) * U(j, k)
int sum = 0;
for (unsigned int j = 0; j < n; j++)
sum = sum + ((lower[i][j] * upper[j][k]));
// Evaluating U(i, k)
upper[i][k] = (mat[i][k] - sum) % prime;
identity[i][k] = (mat[i][k] - sum) % prime;
}
// Lower Triangular
for (unsigned int k = 0; k < n; k++) {
if (i == k) {
lower[i][i] = 1; // Diagonal as 1
}
else {
// Summation of L(k, j) * U(j, i)
int sum = 0;
for (unsigned int j = 0; j < n; j++)
sum = sum + ((lower[k][j] * upper[j][i]));
// Evaluating L(k, i)
lower[k][i] = (((mat[k][i] - sum)) / upper[i][i]) % prime;
identity[k][i] = (((mat[k][i] - sum)) / upper[i][i]) % prime;
}
}
}
ident.setMat(identity);
return new Matrix(lower,prime);
}
我用对象Matrix mat({ { 2, 1, 3, 2, 0}, { 4, 3, 0, 1, 1 }},5);
称呼它
所以基本上,我希望所有分解都在模数5中进行LU分解(尤其是下三角矩阵)。
它可以提取下矩阵,但是,线性组合(仅对单位矩阵执行的所有操作)不正确。 这是我要获得的解释的踪迹:
c |-------------------------------------------------------------------------------------------------------|
c | Prime Number: 5
c |-------------------------------------------------------------------------------------------------------|
c | Input Matrix:
2 1 3 2 0
4 3 0 1 1
c |-------------------------------------------------------------------------------------------------------|
c | Lower Matrix:
1 0 0 0 0
2 1 0 0 0
c |-------------------------------------------------------------------------------------------------------|
c | Linear Combination Matrix:
2 0 3 2 0
0 1 0 3 1
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
c |-------------------------------------------------------------------------------------------------------|
c | Expected Solution:
3 2 3 0 3
0 1 1 3 4
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
c |-------------------------------------------------------------------------------------------------------|
c | Explanations:
c | 3 * c1 + 0 * c2 + 0 * c3 + 0 * c4 + 0 * c5 = c1 of Lower-Matrix
c | 2 * c1 + 1 * c2 + 0 * c3 + 0 * c4 + 0 * c5 = c2 of Lower-Matrix
c | 3 * c1 + 1 * c2 + 1 * c3 + 0 * c4 + 0 * c5 = c3 of Lower-Matrix
c | 0 * c1 + 3 * c2 + 0 * c3 + 1 * c4 + 0 * c5 = c4 of Lower-Matrix
c | 3 * c1 + 4 * c2 + 0 * c3 + 0 * c4 + 1 * c5 = c5 of Lower-Matrix
c +=======================================================================================================+
作为一个小总结:
问题:将修改应用于单位矩阵的方式中的错误是什么?为什么我的输出中没有正确的线性组合?
编辑
清晰了解正常情况。但是我所做的算法(LU分解)与我手工执行的算法不完全相同,即使它会导致相同的结果。这是真正的麻烦...
答案 0 :(得分:2)
让我把评论提出一个实际的答案:加法和乘法模质数会达到您的期望(请注意),而减法有一个陷阱,模数将为负输入返回负结果(例如(-3)%5 = = -3),对于除法,您不能仅使用整数除法,而必须实际实现乘法的逆运算(有关提示,请参阅前面链接的问题中的德莫斯涅斯的答案)。
注意:除非您溢出,否则如果prime * prime> INT_MAX,您也有乘法的麻烦