SynAPSE  0.6.0
Synergistic Analysis of Phase Space Evolution
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
DecompLU-inl.hh
1 template<typename T> DecompLU<T>::DecompLU()
2  : _fact(1), _complete(false), _P(0), _pivots(0), _L(0), _U(0) {}
3 
4 template<typename T> DecompLU<T>::DecompLU(std::vector<std::vector<T>> mat)
5  : _fact(1), _complete(false), _P(0), _pivots(0), _L(0), _U(0) {
6 
7  try {
8  DecomposeCrout(mat);
9  } catch ( Exceptions::Exception& e ) {
10  throw(Exceptions::Exception(Exceptions::nonRecoverable,
11  "Could not initialize"+std::string(e.what()),
12  "DecompLU<T>::DecompLU"));
13  }
14 }
15 
16 template<typename T> DecompLU<T>::DecompLU(const DecompLU& decLU) {
17  *this = decLU;
18 }
19 
20 template<typename T> DecompLU<T>& DecompLU<T>::operator=(const DecompLU<T>& decLU) {
21  if ( this == &decLU )
22  return *this;
23 
24  _fact = decLU._fact;
25  _complete = decLU._complete;
26  _P = decLU._P;
27  _pivots = decLU._pivots;
28  _L = decLU._L;
29  _U = decLU._U;
30  return *this;
31 }
32 
33 template<typename T> DecompLU<T>::~DecompLU() {}
34 
35 template<typename T> void DecompLU<T>::DecomposeCrout(std::vector<std::vector<T>> mat) {
36 
37  // Check the input matrix
38  Assert::IsNotEmpty("DecompLU<T>::DecomposeCrout", "Matrix", mat);
39  Assert::IsSquare("DecompLU<T>::DecomposeCrout", "Matrix", mat);
40 
41  // Use the Crout matrix decomposition algorithm to get the rest of the elements
42  size_t pivot; // Current pivot row
43  T max(0); // Maximum possible diagonal value
44 
45  // Fill the matrices P, L and U with default values in case of singularity
46  size_t n = mat.size();
47  _P = NewMatrix(n, n);
48  _L = NewMatrix(n, n);
49  _U = NewMatrix(n, n);
50  size_t i, j, k;
51  for (i = 0; i < n; i++) {
52  for (j = 0; j < n; j++) {
53  _P[i][j] = ( i == j ) ? 1 : 0;
54  _L[i][j] = 0;
55  _U[i][j] = ( i == j ) ? 1 : 0;
56  }
57  }
58 
59  _pivots.resize(n); // Pivot rows
60  for (i = 0; i < n; i++)
61  _pivots[i] = i;
62 
63  // For each matrix row k = 0, ..., n-1,
64  for (k = 0; k < n; k++) {
65 
66  // Find the pivot row, the one with the highest k^th element
67  pivot = k;
68  max = fabs(mat[k][k]);
69  for (j = k + 1; j < n; j++) {
70  if ( max < fabs(mat[j][k]) ) {
71  max = fabs(mat[j][k]);
72  pivot = j;
73  }
74  }
75 
76  // If the pivot row differs from the current row, then interchange the two rows.
77  if ( pivot != k ) {
78  std::swap(mat[pivot], mat[k]);
79  std::swap(_P[pivot], _P[k]);
80  std::swap(_pivots[pivot], _pivots[k]);
81  _fact *= -1;
82  }
83 
84  // If the matrix is singular, return true anyway but flag incomplete
85  if ( !mat[k][k] )
86  return;
87 
88  // Otherwise find the upper triangular matrix elements for row k.
89  for (j = k+1; j < n; j++)
90  mat[k][j] /= mat[k][k];
91 
92  // Update remaining matrix
93  for (i = k+1; i < n; i++)
94  for (j = k+1; j < n; j++)
95  mat[i][j] -= mat[i][k]*mat[k][j];
96 
97  }
98 
99  // Fill the matrices L and U
100  for (i = 0; i < n; i++) {
101  for (j = i; j < n; j++) {
102  // Fill the lower triangular matrix
103  _L[j][i] = mat[j][i];
104 
105  // Fill the upper triangular matrix, the diag elements are 1
106  _U[i][j] = ( i == j ) ? 1 : mat[i][j];
107  }
108  }
109 
110  _complete = true;
111 }
112 
113 template<typename T> T DecompLU<T>::Determinant() {
114  T det(1);
115  size_t n = _L.size();
116  for (size_t i = 0; i < n; i++)
117  det *= _L[i][i];
118 
119  return _fact*det;
120 }
121 
122 template<typename T> std::vector<std::vector<T>> DecompLU<T>::NewMatrix(size_t m, size_t n) {
123 
124  std::vector<std::vector<T>> mat(m);
125  size_t i;
126  for (i = 0; i < m; i++)
127  mat[i].resize(n);
128 
129  return mat;
130 }
const char * what() const
Return char buffer of message+" at "+location, memory remains owned by Exception. ...
Definition: Exception.hh:52
Defines the LU decomposition algorithm of a square matrix.
Definition: DecompLU.hh:19
std::vector< std::vector< T > > _L
L Matrix.
Definition: DecompLU.hh:83
std::vector< std::vector< T > > NewMatrix(size_t m, size_t n)
Returns a matrix of m rows and n columns.
Definition: DecompLU-inl.hh:122
void IsSquare(const std::string &location, const std::string &what, const std::vector< std::vector< T >> &matrix)
Asserts that a matrix is square.
Definition: Assert.hh:116
std::vector< std::vector< T > > _P
Permutation matrix.
Definition: DecompLU.hh:81
void DecomposeCrout(std::vector< std::vector< T >> mat)
Decomposes a square matrix into a parity matrix P, a lower triangular matrix L and an upper triangula...
Definition: DecompLU-inl.hh:35
std::vector< T > _pivots
Premutation vector (IDs of the rows)
Definition: DecompLU.hh:82
DecompLU()
Default constructor, initilizes an empty matrix of dimension 0x0.
Definition: DecompLU-inl.hh:1
T _fact
Parity of the permutation (1 or -1)
Definition: DecompLU.hh:79
Global exception object for all non-standard exceptions thrown by the code.
Definition: Exception.hh:30
~DecompLU()
Destructor.
Definition: DecompLU-inl.hh:33
void IsNotEmpty(const std::string &location, const std::string &what, const std::vector< T > &vector)
Asserts that a vector is not empty.
Definition: Assert.hh:53
std::vector< std::vector< T > > _U
U Matrix.
Definition: DecompLU.hh:84
T Determinant()
Returns the determinant of the decomposed matrix.
Definition: DecompLU-inl.hh:113
DecompLU & operator=(const DecompLU &decLU)
Equality operator.
Definition: DecompLU-inl.hh:20
bool _complete
True if the decomposition was successful.
Definition: DecompLU.hh:80