| testeigenlu.cpp |
// testeigenlu.cpp --- EIgen::PartialPivLU の利用
// https://eigen.tuxfamily.org/dox/classEigen_1_1PartialPivLU.html
#include <iostream>
#include <cmath>
// #include <Eigen/Dense>
#include <Eigen/Core>
#include <Eigen/LU>
using Eigen::MatrixXd;
using Eigen::VectorXd;
using Eigen::PartialPivLU;
using namespace std;
int main(void)
{
int n = 2;
MatrixXd a(n,n);
VectorXd b(n), x(n);
PartialPivLU<MatrixXd> lu;
// 係数行列を決める
a <<
1, 2,
3, 4;
// まず解 x を決めて
x(0) = 1.0; x(1) = 2.0;
// 対応する b を計算する
b = a * x;
// A, b を表示
cout << "a=" << endl << a << endl;
cout << "b=" << endl << b << endl;
// LU分解する
lu.compute(a);
// 連立一次方程式を解く
x=lu.solve(b);
// 結果を表示する
cout << "x=" << endl << x << endl;
}
|
% g++ -I /usr/local/include testeigenlu.cpp % ./a.out a= 1 2 3 4 b= 5 11 x= 1 2 % |