Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.39 KB | None | 0 0
  1. /*
  2.  * matrix_utils.cpp
  3.  *
  4.  *  Created on: Oct 28, 2015
  5.  *      Author: marko
  6.  */
  7.  
  8. #include <linalg/matrix_utils.h>
  9. #include <linalg/invalid_matrix_operation_exception.h>
  10. #include <cmath>
  11. #include <iostream>
  12.  
  13. using namespace std;
  14. namespace linalg {
  15.  
  16. void MatrixUtils::fwdSupstitution(Matrix& A, Matrix& b) {
  17.     if(!A.isSquare()) {
  18.         throw InvalidMatrixOperationException("Matrix A is not square");
  19.     }
  20.     if(A.cols() != b.rows() || b.cols() != 1) {
  21.         throw InvalidMatrixOperationException(
  22.                 "Matrix B should be row vector (A.cols x 1)");
  23.     }
  24.  
  25.     int N = A.rows();
  26.     for (int i = 0; i < N; ++i) {
  27.         for (int j = i + 1; j < N; ++j) {
  28.             b[j][0] -= A[j][i] * b[i][0];
  29.         }
  30.     }
  31.  
  32. }
  33.  
  34. void MatrixUtils::bkwdSupstitution(Matrix& A, Matrix& b) {
  35.     if(!A.isSquare()) {
  36.         throw InvalidMatrixOperationException("Matrix A is not square");
  37.     }
  38.     if(A.cols() != b.rows() || b.cols() != 1) {
  39.         throw InvalidMatrixOperationException(
  40.                 "Matrix B should be row vector (A.cols x 1)");
  41.     }
  42.  
  43.     int N = A.rows();
  44.     for (int i = N - 1; i >= 0; --i) {
  45.         if(fabs(A[i][i]) < EPS) {
  46.             throw InvalidMatrixOperationException(
  47.                     "Could not solve. Division by zero");
  48.         }
  49.         b[i][0] /= A[i][i];
  50.  
  51.         for (int j = 0; j < i; ++j) {
  52.             b[j][0] -= A[j][i] * b[i][0];
  53.         }
  54.     }
  55.  
  56. }
  57.  
  58. void MatrixUtils::decomposeLU(Matrix& A) {
  59.     if(!A.isSquare()) {
  60.         throw InvalidMatrixOperationException("Matrix A is not square");
  61.     }
  62.     int N = A.rows();
  63.  
  64.     for (int i = 0; i < N; ++i) {
  65.         for (int j = i + 1; j < N; ++j) {
  66.             if(fabs(A[i][i]) < EPS) {
  67.                 throw InvalidMatrixOperationException(
  68.                         "Could not decompose. 0 on main diagonal");
  69.             }
  70.             A[j][i] /= A[i][i];
  71.             for (int k = i + 1; k < N; ++k) {
  72.                 A[j][k] -= A[j][i] * A[i][k];
  73.             }
  74.         }
  75.     }
  76.  
  77. }
  78.  
  79. Matrix MatrixUtils::decomposeLUP(Matrix& A) {
  80.     if(!A.isSquare()) {
  81.         throw InvalidMatrixOperationException("Matrix A is not square");
  82.     }
  83.     int N = A.rows();
  84.     Matrix p(N, N);
  85.     p.eye();
  86.  
  87.     for (int i = 0; i < N; ++i) {
  88.         int pivot = i;
  89.         for (int j = i + 1; j < N; ++j) {
  90.             if(fabs(A[j][i]) > fabs(A[pivot][i])) {
  91.                 pivot = j;
  92.             }
  93.         }
  94.  
  95.  
  96.         p.swapRows(i, pivot);
  97.         A.swapRows(i, pivot);
  98.  
  99.         //cout << A << endl <<endl;
  100.         //cout << A[i][i] << endl;
  101.  
  102.         for (int j = i + 1; j < N; ++j) {
  103.             A[j][i] /= A[i][i];
  104.             for (int k = i + 1; k < N; ++k) {
  105.  
  106.                 //cout << A[j][k] << "  " << A[j][i] << " * " << A[i][k]<<endl;
  107.                 A[j][k] -= A[j][i] * A[i][k];
  108.             }
  109.         }
  110.  
  111.     }
  112.  
  113.     return p;
  114. }
  115.  
  116. void MatrixUtils::solveSystem(Matrix&A, Matrix& B) {
  117.     Matrix permutation = MatrixUtils::decomposeLUP(A);
  118.     B = permutation * B;
  119.  
  120.     MatrixUtils::fwdSupstitution(A, B);
  121.     MatrixUtils::bkwdSupstitution(A, B);
  122. }
  123.  
  124. std::vector<double> MatrixUtils::toVector(Matrix& m) {
  125.     std::vector<double> x;
  126.     if(m.cols() != 1) throw InvalidMatrixOperationException("Cannot convert to row vector");
  127.     for(int i = 0; i < m.rows(); ++i) {
  128.         x.emplace_back(m[i][0]);
  129.     }
  130.     return x;
  131. }
  132.  
  133. } /* namespace linalg */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement