Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. Matrix SqMatrix::solve(Matrix b) {
  2.   assert(b.columns()==1);
  3.   int dim=this->dimension();
  4.  
  5.   Matrix y (dim, 1);
  6.   Matrix x (dim, 1);
  7.  
  8.   //forward substitution algorithm - solve for y, Ly=b
  9.  
  10.   for(int i=0; i<dim; i++) {
  11.     double s=0;  //the sum in the equation
  12.     for(int j=0; j<i; j++) {
  13.       s+=(*this)(i, j) * y(j,0);
  14.     }
  15.     y(i, 0)=(b(i,0)-s);  // / (*this)(i, i); das sollte gleich eins werden bei L
  16.     if(y(i,0)==0.0) y(i,0)=0;
  17.   }
  18.  
  19.   //backward substitution - Ux=y, solve for x
  20.  
  21.   for(int i=this->dimension()-1; i>=0; i--) {
  22.     double s=0;
  23.     for(int j=i; j<this->dimension()-1; j++) {
  24.       s+=(*this)(i, j)*x(j,0);
  25.     }
  26.     x(i,0)=(b(i,0)-s) / (*this)(i, i);
  27.     if(x(i,0)==0.0) x(i,0)=0;
  28.   }
  29.  
  30.   return x;  
  31.    
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement