Jpp  15.0.0-rc.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JMatrix2S.hh
Go to the documentation of this file.
1 #ifndef __JMATRIX2S__
2 #define __JMATRIX2S__
3 
4 #include <cmath>
5 #include <algorithm>
6 
7 #include "JMath/JMatrix2D.hh"
8 #include "JLang/JException.hh"
9 
10 
11 /**
12  * \author mdejong
13  */
14 
15 namespace JMATH {}
16 namespace JPP { using namespace JMATH; }
17 
18 namespace JMATH {
19 
21 
22 
23  /**
24  * 2 x 2 symmetric matrix
25  */
26  class JMatrix2S :
27  public JMatrix2D
28  {
29  public:
30  /**
31  * Default constructor.
32  */
34  JMatrix2D()
35  {}
36 
37 
38  /**
39  * Contructor.
40  *
41  * \param A matrix
42  */
43  JMatrix2S(const JMatrix2D& A) :
44  JMatrix2D(A)
45  {}
46 
47 
48  /**
49  * Contructor.\n
50  * The upper triangle is internally set.
51  *
52  * \param __a00 (0,0)
53  * \param __a10 (1,0)
54  * \param __a11 (1,1)
55  */
56  JMatrix2S(const double __a00,
57  const double __a10, const double __a11) :
58  JMatrix2D(__a00, __a10,
59  __a10, __a11)
60  {}
61 
62 
63  /**
64  * Invert matrix
65  */
66  void invert()
67  {
68  using std::swap;
69 
70  // LDU decomposition
71 
72  int p0 = 0; // permute row 0
73 
74  double val;
75 
76  val = a00;
77 
78  if (fabs(a10) > fabs(val)) {
79 
80  p0 = 1;
81  val = a10;
82 
83  swap(a00,a10);
84  swap(a01,a11);
85  }
86 
87  if (val == 0) {
88  throw JDivisionByZero("LDU decomposition zero pivot");
89  }
90 
91  a10 /= val;
92  a11 -= a10 * a01;
93 
94  // invert D
95 
96  if (a11 == 0) {
97  throw JDivisionByZero("D matrix not invertable");
98  }
99 
100  a00 = 1.0 / a00;
101  a11 = 1.0 / a11;
102 
103  // invert U
104 
105  a01 *= -a00;
106  a01 *= a11;
107 
108  // invert L
109 
110  a10 = -a10;
111 
112  // U^-1 x L^-1
113 
114  a00 += a01 * a10;
115  a10 *= a11;
116 
117  switch (p0) {
118 
119  case 1:
120  swap(a00,a01);
121  swap(a10,a11);
122  break;
123  }
124  }
125  };
126 }
127 
128 #endif
Exceptions.
JMatrix2S(const JMatrix2D &A)
Contructor.
Definition: JMatrix2S.hh:43
2 x 2 symmetric matrix
Definition: JMatrix2S.hh:26
JMatrix2S(const double __a00, const double __a10, const double __a11)
Contructor.
Definition: JMatrix2S.hh:56
void invert()
Invert matrix.
Definition: JMatrix2S.hh:66
JMatrix2S()
Default constructor.
Definition: JMatrix2S.hh:33
2 x 2 matrix
Exception for division by zero.
Definition: JException.hh:270
source $JPP_DIR setenv csh $JPP_DIR eval JShellParser o a A