MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
matrix_util.cpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: matrix_util.cpp
5  *
6  * Description: Some matrix utility function. Used when boost library is
7  * used.
8  *
9  * Version: 1.0
10  * Created: 05/10/2016 05:25:27 PM
11  * Revision: none
12  * Compiler: gcc
13  *
14  * Author: Dilawar Singh (), dilawars@ncbs.res.in
15  * Organization: NCBS Bangalore
16  *
17  * =====================================================================================
18  */
19 
20 #ifdef USE_BOOST_ODE
21 
22 #include "matrix_util.h"
23 
24 #define EPSILON 1e-9 /* Treat number below it as 0 */
25 
26 /*-----------------------------------------------------------------------------
27  * These functions computes rank of a matrix.
28  *-----------------------------------------------------------------------------*/
29 
38 void swapRows( ublas::matrix< value_type >& mat, unsigned int r1, unsigned int r2)
39 {
40  ublas::vector<value_type> temp( mat.size2() );
41  for (size_t i = 0; i < mat.size2(); i++)
42  {
43  temp[i] = mat(r1, i );
44  mat(r1, i ) = mat(r2, i );
45  }
46 
47  for (size_t i = 0; i < mat.size2(); i++)
48  mat(r2, i) = temp[i];
49 }
50 
51 
52 int reorderRows( ublas::matrix< value_type >& U, int start, int leftCol )
53 {
54  int leftMostRow = start;
55  int numReacs = U.size2() - U.size1();
56  int newLeftCol = numReacs;
57  for ( size_t i = start; i < U.size1(); ++i )
58  {
59  for ( int j = leftCol; j < numReacs; ++j )
60  {
61  if ( fabs( U(i,j )) > EPSILON )
62  {
63  if ( j < newLeftCol )
64  {
65  newLeftCol = j;
66  leftMostRow = i;
67  }
68  break;
69  }
70  }
71  }
72 
73  if ( leftMostRow != start ) // swap them.
74  swapRows( U, start, leftMostRow );
75 
76  return newLeftCol;
77 }
78 
79 void eliminateRowsBelow( ublas::matrix< value_type >& U, int start, int leftCol )
80 {
81  int numMols = U.size1();
82  double pivot = U( start, leftCol );
83  assert( fabs( pivot ) > EPSILON );
84  for ( int i = start + 1; i < numMols; ++i )
85  {
86  double factor = U(i, leftCol);
87  if( fabs ( factor ) > EPSILON )
88  {
89  factor = factor / pivot;
90  for ( size_t j = leftCol + 1; j < U.size2(); ++j )
91  {
92  double x = U(i,j);
93  double y = U( start, j );
94  x -= y * factor;
95  if ( fabs( x ) < EPSILON )
96  x = 0.0;
97  U( i, j ) = x;
98  }
99  }
100  U(i, leftCol) = 0.0;
101  }
102 }
103 
104 unsigned int rankUsingBoost( ublas::matrix<value_type>& U )
105 {
106  int numMols = U.size1();
107  int numReacs = U.size2() - numMols;
108  int i;
109  // Start out with a nonzero entry at 0,0
110  int leftCol = reorderRows( U, 0, 0 );
111 
112  for ( i = 0; i < numMols - 1; ++i )
113  {
114  eliminateRowsBelow( U, i, leftCol );
115  leftCol = reorderRows( U, i + 1, leftCol );
116  if ( leftCol == numReacs )
117  break;
118  }
119  return i + 1;
120 }
121 
122 #endif /* ----- not USE_BOOST_ODE ----- */
void swapRows(ublas::matrix< double > &mat, unsigned int r1, unsigned int r2)
Swap row r1 and r2.
void eliminateRowsBelow(ublas::matrix< double > &U, int start, int leftCol)
int reorderRows(ublas::matrix< double > &U, int start, int leftCol)
#define EPSILON
Definition: MatrixOps.h:28
unsigned int rankUsingBoost(ublas::matrix< double > &U)