/* thanks to Valient Gough for this example program! */
//整理 by RobinKin
#include <mtl/matrix.h>
#include <mtl/mtl.h>
#include <mtl/utils.h>
#include <mtl/lu.h>
using namespace mtl;
// don't print out the matrices once they get to this size...
#define MAX_PRINT_SIZE 5
typedef matrix<double, rectangle<>, dense<>, row_major>::type Matrix;
typedef dense1D<double> Vector;
double testMatrixError(const Matrix &A, const Matrix &AInv)
{
int size = A.nrows();
// test it
Matrix AInvA(size,size);
// AInvA = AInv * A
mult(AInv, A, AInvA);
// I = identity
typedef matrix<double, diagonal<>, packed<>, row_major>::type IdentMat;
IdentMat I(size, size, 0, 0);
mtl::set_value(I, 1.0);
// AInvA += -I
add(scaled(I, -1.0), AInvA);
if (size < MAX_PRINT_SIZE) {
std::cout << "Ainv * A - I = " << std::endl;
print_all_matrix(AInvA);
}
// find max error
double max_error = 0.0;
for(Matrix::iterator i = AInvA.begin(); i != AInvA.end(); ++i)
for(Matrix::Row::iterator j = (*i).begin(); j != (*i).end(); ++j)
if(fabs(*j) > fabs(max_error))
max_error = *j;
std::cout << "max error = " << max_error << std::endl;
return max_error;
}
void testLUSoln(const Matrix &A, const Vector &b, Vector &x)
{
// create LU decomposition
Matrix LU(A.nrows(), A.ncols());
dense1D<int> pvector(A.nrows());
copy(A, LU);
lu_factorize(LU, pvector);
// solve
//解线形方程
lu_solve(LU, pvector, b, x);
}
void testLUInv(const Matrix &A, int size)
{
// invert it
Matrix AInv(size,size);
// create LU decomposition
Matrix LU(A.nrows(), A.ncols());
dense1D<int> pvector(A.nrows());
copy(A, LU);
lu_factor(LU, pvector);
//求逆阵
// solve
lu_inverse(LU, pvector, AInv);
if(size < MAX_PRINT_SIZE) {
std::cout << "Ainv = " << std::endl;
print_all_matrix(AInv);
}
// test it
testMatrixError(A, AInv);
}
int main(int argc, char **argv)
{
typedef Matrix::size_type sizeT;
sizeT size = 3;
if(argc > 1)
size = atoi(argv[1]);
std::cout << "inverting matrix of size " << size << std::endl;
// create a random matrix and invert it. Then see how close it comes to
// identity.
Matrix A(size,size);
Vector b(size);
Vector x(size);
// initialize
for (sizeT i=0; i<A.nrows(); i++) {
for (sizeT j=0; j<A.nrows(); j++)
A(i,j) = (double)(rand() % 200 - 100) / 50.0;
b[i] = (double)(rand() % 200 - 100) / 50.0;
}
if (size < MAX_PRINT_SIZE) {
std::cout << "A = " << std::endl;
print_all_matrix(A);
}
// time LU inv
std::cout << std::endl
<< " ----------- testing inversion using LU decomposition"
<< std::endl;
testLUInv(A, size);
if (size < MAX_PRINT_SIZE) {
std::cout << "solution = ";
print_vector(x);
}
// test LU solution
mtl::set_value(x, 0.0);
testLUSoln(A, b, x);
if(size < MAX_PRINT_SIZE) {
std::cout << "solution = ";
print_vector(x);
}
if(argc == 1)
std::cout << std::endl
<< "pass size argument to program to time larger matrices."
<< std::endl;
return 0;
}
输出:
inverting matrix of size 3
A =
3x3
[
[1.66,-0.28,1.54],
[1.86,0.7,1.72],
[-1.02,-1.58,1.24]
]
//逆阵
----------- testing inversion using LU decomposition
Ainv =
3x3
[
[0.978889,-0.56949,-0.42578],
[-1.10862,0.990792,0.00251165],
[-0.607383,0.79401,0.459414]
]
Ainv * A - I =
3x3
[
[4.44089e-16,2.56739e-16,1.01481e-16],
[-5.05491e-16,-2.22045e-16,-2.75514e-16],
[-1.91335e-16,-1.27014e-16,-1.11022e-16]
]
max error = -5.05491e-16
solution = [0,0,0,]
//方程的解向量
solution = [1.00642,-0.49478,-0.980001,]
本文展示了如何利用MTL库进行矩阵的LU分解,进而求解矩阵的逆阵和线性方程组。通过创建随机矩阵,计算其逆阵并验证与单位矩阵的误差,以及利用LU分解求解线性方程,验证了解的准确性。
2万+

被折叠的 条评论
为什么被折叠?



