ANIMA  4.0
animaCholeskyDecomposition.cxx
Go to the documentation of this file.
2 #include <iostream>
3 #include <limits>
4 
5 namespace anima
6 {
7 
9 {
10  m_InputMatrix = val;
11  m_MatrixSize = val.rows();
12 }
13 
15 {
16  m_LMatrix.set_size(m_MatrixSize, m_MatrixSize);
17  m_LMatrix.set_identity();
18  m_DMatrix.set_size(m_MatrixSize);
19 \
20  for (unsigned int i = 0;i < m_MatrixSize;++i)
21  {
22  m_DMatrix[i] = m_InputMatrix.get(i,i);
23  for (unsigned int j = 0;j < i;++j)
24  {
25  double tmpVal = m_LMatrix.get(i,j);
26  m_DMatrix[i] -= tmpVal * tmpVal * m_DMatrix[j];
27  }
28 
29  for (unsigned int j = i + 1;j < m_MatrixSize;++j)
30  {
31  double diffVal = 0;
32  for (unsigned int k = 0;k < i;++k)
33  diffVal += m_LMatrix.get(j,k) * m_LMatrix.get(i,k) * m_DMatrix[k];
34 
35  double lVal = (m_InputMatrix.get(j,i) - diffVal) / m_DMatrix[i];
36  m_LMatrix.put(j,i,lVal);
37  }
38  }
39 }
40 
42 {
43  double minVal = m_DMatrix[0];
44  double maxVal = m_DMatrix[0];
45 
46  for (unsigned int i = 1;i < m_MatrixSize;++i)
47  {
48  double absVal = std::abs(m_DMatrix[i]);
49  if (minVal > absVal)
50  minVal = absVal;
51 
52  if (maxVal < absVal)
53  maxVal = absVal;
54  }
55 
56  if (minVal == 0.0)
57  return std::numeric_limits<double>::max();
58 
59  return maxVal / minVal;
60 }
61 
63 {
64  m_ProblemSolution = b;
65  SolveLinearSystemInPlace(m_ProblemSolution);
66  return m_ProblemSolution;
67 }
68 
70 {
71  for (unsigned int i = 1;i < m_MatrixSize;++i)
72  {
73  for (unsigned int j = 0;j < i;++j)
74  b[i] -= m_LMatrix.get(i,j) * b[j];
75  }
76 
77  for (unsigned int i = 0;i < m_MatrixSize;++i)
78  b[i] /= m_DMatrix[i];
79 
80  for (int i = m_MatrixSize - 2;i >= 0;--i)
81  {
82  for (unsigned int j = i + 1;j < m_MatrixSize;++j)
83  b[i] -= m_LMatrix.get(j,i) * b[j];
84  }
85 }
86 
88 {
89  double a = 1.0;
90  m_WorkVector = x;
91 
92  for (unsigned int i = 0;i < m_MatrixSize;++i)
93  {
94  double p = m_WorkVector[i];
95  double oldDValue = m_DMatrix[i];
96  m_DMatrix[i] += a * p * p;
97  double b = p * a / m_DMatrix[i];
98  a *= oldDValue / m_DMatrix[i];
99 
100  for (unsigned int j = i + 1;j < m_MatrixSize;++j)
101  {
102  m_WorkVector[j] -= p * m_LMatrix.get(j,i);
103  m_LMatrix(j,i) += b * m_WorkVector[j];
104  }
105  }
106 }
107 
109 {
110  for (unsigned int i = 0;i < m_MatrixSize;++i)
111  {
112  for (unsigned int j = i;j < m_MatrixSize;++j)
113  {
114  double iValue = 0.0;
115  for (unsigned int k = 0;k <= i;++k)
116  iValue += m_LMatrix.get(i,k) * m_DMatrix[k] * m_LMatrix.get(j,k);
117 
118  m_InputMatrix.put(i,j,iValue);
119  if (i != j)
120  m_InputMatrix.put(j,i,iValue);
121  }
122  }
123 }
124 
125 }
double GetConditionNumber()
Get condition number from decomposition.
void PerformDecomposition()
Performs LDL decomposition from input matrix.
void SetInputMatrix(const MatrixType &matrix)
Set input matrix to decompose.
void SolveLinearSystemInPlace(VectorType &b)
Solves linear system Ax=b and outputs result in input b variable.
void Recompose()
Recompose input matrix from LDL decomposition.
VectorType & SolveLinearSystem(const VectorType &b)
Solves linear system Ax=b and outputs result in new variable.
void Update(const VectorType &x)
Update decomposition with x so that LDL matches A + x x^T.