1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | //divide each rows of _b with diagonal elements inline void divGauJoElim(mxd::r _m, mxd::r _b){ i::t idx = 0; while( idx < _m.rows() ){ mxd::b b1 = _b.block( idx , 0 , 1 , _b.cols() ); mxd::b alpha11 = _m.block( idx , idx , 1 , 1 ); b1 = (1 / alpha11.value()) * b1; alpha11(0,0) = 1 ; idx++; } } //gauss-jordan elimination. inline void gauJoElim(mxd::r _m, mxd::r _b){ i::t idx = 0; while(idx < _m.rows()){ mxd::b A00 = _m.block( 0 , 0 , idx , idx ); mxd::b a10 = _m.block( idx , 0 , 1 , idx ); mxd::b A20 = _m.block( idx + 1 , 0 , _m .rows() - (idx + 1) , idx ); mxd::b a01 = _m.block( 0 , idx , idx , 1 ); mxd::b alpha11 = _m.block( idx , idx , 1 , 1 ); mxd::b a21 = _m.block( idx + 1 , idx , _m.rows() - (idx + 1) , 1 ); mxd::b A02 = _m.block( 0 , idx + 1 , idx , _m.cols() - (idx + 1) ); mxd::b a12 = _m.block( idx , idx + 1 , 1 , _m.cols() - (idx + 1) ); mxd::b A22 = _m.block( idx + 1 , idx + 1 , _m.rows() - (idx + 1) , _m.cols() - (idx + 1) ); mxd::b b0 = _b.block( 0 , 0 , idx , _b.cols() ); mxd::b b1 = _b.block( idx , 0 , 1 , _b.cols() ); mxd::b b2 = _b.block( idx + 1 , 0 , _b.rows() - (idx + 1) , _b.cols() ); a01 = a01/alpha11.value(); a21 = a21/alpha11.value(); A02 = A02 - a01 * a12; A22 = A22 - a21 * a12; b0 = b0 - a01 * b1; b2 = b2 - a21 * b1; a01 = a01.Zero( a01.rows(), a01.cols() ); a21 = a21.Zero( a21.rows(), a21.cols() ); idx++; } divGauJoElim(_m,_b); } | cs |
Main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | mxd::t m(3,3); vxd::t v(3); vxd::t v2(3); m << -2, 2, -5, 2,-3, 7, -4, 3, -7; v << -7, 11, -9; v2 << 8, -13, 9; mxd::t m2(3, 2); m2 << v, v2; gauJoElim(m,m2); co(m); co(m2); | cs |
Result :
1 2 3 4 5 6 7 8 | output = 1 0 0 0 1 0 0 0 1 output = -1 2 -2 1 1 -2 | cs |
'Linear Algebra' 카테고리의 다른 글
[강의 추천] Linear Algebra - Foundations to Frontiers (LAFF) (0) | 2019.06.09 |
---|---|
[Eigen] Day 11. Gauss-jordan elimination with pivoting (0) | 2019.06.06 |
[Eigen] Day 10. LU decomposition with pivoting (0) | 2019.06.04 |
[Eigen] Day 9. solving triangular matrix (0) | 2019.06.02 |
[Eigen] Day 9. LU decomposition (0) | 2019.06.02 |