SynAPSE  0.6.0
Synergistic Analysis of Phase Space Evolution
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
DecompQR-inl.hh
1 template<typename T> DecompQR<T>::DecompQR()
2  : _Q(0), _R(0) {}
3 
4 template<typename T> DecompQR<T>::DecompQR(const std::vector<std::vector<T>>& mat)
5  : _Q(0), _R(0) {
6 
7  try {
9  } catch ( Exceptions::Exception& e ) {
10  throw(Exceptions::Exception(Exceptions::nonRecoverable,
11  "Could not initialize"+std::string(e.what()),
12  "DecompQR<T>::DecompQR"));
13  }
14 }
15 
16 template<typename T> DecompQR<T>::DecompQR(const DecompQR& decQR) {
17  *this = decQR;
18 }
19 
20 template<typename T> DecompQR<T>& DecompQR<T>::operator=(const DecompQR<T>& decQR) {
21  if ( this == &decQR )
22  return *this;
23 
24  _Q = decQR._Q;
25  _R = decQR._R;
26  return *this;
27 }
28 
29 template<typename T> DecompQR<T>::~DecompQR() {}
30 
31 template<typename T> void DecompQR<T>::DecomposeGramSchmidt(const std::vector<std::vector<T>>& mat) {
32 
33  // Check the input matrix
34  Assert::IsNotEmpty("DecompQR<T>::DecomposeGramSchmidt", "Matrix", mat);
35  Assert::IsSquare("DecompQR<T>::DecomposeGramSchmidt", "Matrix", mat);
36 
37  // Fill the matrices P, L and U with default values in case of singularity
38  size_t n = mat.size();
39  _Q = NewMatrix(n, n);
40  _R = NewMatrix(n, n);
41  size_t i, j, k;
42  for (i = 0; i < n; i++) {
43  for (j = 0; j < n; j++) {
44  _Q[i][j] = 0;
45  _R[i][j] = 0;
46  }
47  }
48 
49  // For each matrix column k = 0, ..., n-1, find the unscaled Q matrix
50  T fact, norm;
51  std::vector<T> matcol(n), Qcol(n);
52  for (k = 0; k < n; k++) {
53 
54  // Extract the k^th column of the input matrix and set the k^th column of Q
55  for (j = 0; j < n; j++) {
56  matcol[j] = mat[j][k];
57  _Q[j][k] = matcol[j];
58  }
59 
60  // For each i, find the i^th column of Q and project the current column on it.
61  // Increment by substracting the projection from the current column
62  for (i = 0; i < k; i++) {
63  for (j = 0; j < n; j++)
64  Qcol[j] = _Q[j][i];
65  fact = DotProduct(matcol, Qcol)/DotProduct(Qcol, Qcol);
66 
67  for (j = 0; j < n; j++)
68  _Q[j][k] -= fact*Qcol[j];
69  }
70  }
71 
72  // Normalize the columns
73  for (k = 0; k < n; k++) {
74  for (j = 0; j < n; j++)
75  Qcol[j] = _Q[j][k];
76 
77  norm = sqrt(DotProduct(Qcol, Qcol));
78  for (j = 0; j < n; j++)
79  _Q[j][k] /= norm;
80  }
81 
82  // For each matrix row k = 0, ..., n-1, compute R = Q^T*M
83  for (k = 0; k < n; k++)
84  for (j = k; j < n; j++)
85  for (i = 0; i < n; i++)
86  _R[k][j] += _Q[i][k]*mat[i][j];
87 }
88 
89 template<typename T> std::vector<std::vector<T>> DecompQR<T>::NewMatrix(size_t m, size_t n) {
90 
91  std::vector<std::vector<T>> mat(m);
92  size_t i;
93  for (i = 0; i < m; i++)
94  mat[i].resize(n);
95 
96  return mat;
97 }
98 
99 template<typename T> T DecompQR<T>::DotProduct(const std::vector<T>& a, const std::vector<T>& b) {
100 
101  size_t i;
102  T product(0);
103  for (i = 0; i < a.size(); i++)
104  product += a[i]*b[i];
105 
106  return product;
107 }
void DecomposeGramSchmidt(const std::vector< std::vector< T >> &mat)
Decomposes a square matrix into a parity matrix P, an orthogonal matrix Q and a right triangular matr...
Definition: DecompQR-inl.hh:31
const char * what() const
Return char buffer of message+" at "+location, memory remains owned by Exception. ...
Definition: Exception.hh:52
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 > > _R
R Matrix.
Definition: DecompQR.hh:73
std::vector< std::vector< T > > _Q
Q Matrix.
Definition: DecompQR.hh:72
Global exception object for all non-standard exceptions thrown by the code.
Definition: Exception.hh:30
std::vector< std::vector< T > > NewMatrix(size_t m, size_t n)
Returns a matrix of m rows and n columns.
Definition: DecompQR-inl.hh:89
Defines the QR decomposition algorithm of a square matrix.
Definition: DecompQR.hh:15
DecompQR & operator=(const DecompQR &decQR)
Equality operator.
Definition: DecompQR-inl.hh:20
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
DecompQR()
Default constructor, initilizes an empty matrix of dimension 0x0.
Definition: DecompQR-inl.hh:1
T DotProduct(const std::vector< T > &a, const std::vector< T > &b)
Returns the dot produt of two vectors.
Definition: DecompQR-inl.hh:99
~DecompQR()
Destructor.
Definition: DecompQR-inl.hh:29