Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void SLAE::LUdecomposition()
- {
- L.resize(eqcount);
- U.resize(eqcount);
- for (int i(0); i < eqcount; ++i) {
- L[i].resize(varcount);
- U[i].resize(varcount);
- }
- for (int i(0); i < eqcount; ++i) {
- for (int j(0); j < varcount; ++j) {
- if (i <= j) {
- U[i][j] = A[i][j];
- for (int t(0); t <= i; ++t) {
- U[i][j] -= L[i][t] * U[t][j];
- }
- }
- else {
- L[i][j] = A[i][j];
- for (int t(0); t < j; ++t) {
- L[i][j] -= L[i][t] * U[t][j];
- }
- L[i][j] /= U[j][j];
- }
- }
- L[i][i] = 1;
- }
- }
- void SLAE::LUsolution()
- {
- LUdecomposition();
- std::vector<double> y(varcount);
- for (int j(0); j < varcount; ++j) {
- y[j] = B[j];
- for (int i(0); i < j; ++i) {
- y[j] -= L[j][i] * y[i];
- }
- }
- for (int j(eqcount - 1); j >= 0; --j) {
- solution[j] = y[j];
- for (int i(j + 1); i < varcount; ++i) {
- solution[j] -= U[j][i] * solution[i];
- }
- solution[j] /= U[j][j];
- }
- }
- std::vector<std::vector<double> > SLAE::inverseMatrix()
- {
- LUdecomposition();
- std::vector<std::vector<double> > inv;
- inv.resize(eqcount);
- for (int i(0); i < eqcount; ++i) {
- inv[i].resize(varcount);
- }
- std::vector<double> b;
- for (int i(0); i < eqcount; ++i) {
- b.clear();
- for (int j(0); j < eqcount; ++j) {
- if (j == i) {
- b.push_back(1);
- }
- else {
- b.push_back(0);
- }
- }
- SLAE s(A, b);
- s.LUsolution();
- for (int j(0); j < eqcount; ++j) {
- inv[j][i] = s.solution[j];
- }
- }
- return inv;
- }
- void SLAE::invMatMethod()
- {
- std::vector < std::vector<double> > invA(inverseMatrix());
- solution = invA * B;
- }
- //кусочек из мэйна
- S.invMatMethod();
- cout << "Solution:" << endl;
- for (int i(0); i < n; ++i) {
- cout << "x" << i + 1 << " = " << S.solution[i] << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement