| 28 |
#define SYMMAT_H_ |
#define SYMMAT_H_ |
| 29 |
|
|
| 30 |
#include <assert.h> |
#include <assert.h> |
| 31 |
#include "../utility_data_structure/nrutil_nr.hh" |
#include <stddef.h> |
|
|
|
|
using namespace std; |
|
| 32 |
|
|
| 33 |
// Class of a symmmetric matrix (a_ij) determined by m_mat as below. |
// Class of a symmmetric matrix (a_ij) determined by m_mat as below. |
| 34 |
// m_mat[0] m_mat[1] m_mat[2] |
// m_mat[0] m_mat[1] m_mat[2] |
| 38 |
class SymMat |
class SymMat |
| 39 |
{ |
{ |
| 40 |
private: |
private: |
| 41 |
const Int4 ISIZE; |
const int ISIZE; |
| 42 |
const Int4 NUM_ELEMENT; |
const int NUM_ELEMENT; |
| 43 |
T* m_mat; |
T* m_mat; |
| 44 |
|
|
| 45 |
inline T& operator[](const int&); |
inline T& operator[](const int&); |
| 46 |
inline const T& operator[](const int&) const; |
inline const T& operator[](const int&) const; |
| 47 |
|
|
| 48 |
public: |
public: |
| 49 |
SymMat(const Int4& isize); |
SymMat(const int& isize); |
| 50 |
SymMat(const Int4& isize, const T&); |
SymMat(const int& isize, const T&); |
| 51 |
SymMat(const SymMat<T>&); // copy constructor |
SymMat(const SymMat<T>&); // copy constructor |
| 52 |
~SymMat(); |
~SymMat(); |
| 53 |
|
|
| 62 |
inline T& operator()(const int&, const int&); |
inline T& operator()(const int&, const int&); |
| 63 |
inline const T& operator()(const int&, const int&) const; |
inline const T& operator()(const int&, const int&) const; |
| 64 |
|
|
| 65 |
inline const Int4& size() const { return ISIZE; }; |
inline const int& size() const { return ISIZE; }; |
| 66 |
|
|
| 67 |
// for GUI |
// for GUI |
| 68 |
const T *getref_m_mat() const {return m_mat;} |
const T *getref_m_mat() const {return m_mat;} |
| 70 |
}; |
}; |
| 71 |
|
|
| 72 |
template <class T> |
template <class T> |
| 73 |
SymMat<T>::SymMat(const Int4& isize) : ISIZE(isize), NUM_ELEMENT(isize*(isize+1)/2), m_mat(NULL) |
SymMat<T>::SymMat(const int& isize) : ISIZE(isize), NUM_ELEMENT(isize*(isize+1)/2), m_mat(NULL) |
| 74 |
{ |
{ |
| 75 |
assert( isize >= 0 ); |
assert( isize >= 0 ); |
| 76 |
if( isize > 0 ) m_mat = new T[NUM_ELEMENT]; |
if( isize > 0 ) m_mat = new T[NUM_ELEMENT]; |
| 77 |
} |
} |
| 78 |
|
|
| 79 |
template <class T> |
template <class T> |
| 80 |
SymMat<T>::SymMat(const Int4& isize, const T& a) : ISIZE(isize), NUM_ELEMENT(isize*(isize+1)/2), m_mat(NULL) |
SymMat<T>::SymMat(const int& isize, const T& a) : ISIZE(isize), NUM_ELEMENT(isize*(isize+1)/2), m_mat(NULL) |
| 81 |
{ |
{ |
| 82 |
assert( isize >= 0 ); |
assert( isize >= 0 ); |
| 83 |
if( isize > 0 ) m_mat = new T[NUM_ELEMENT]; |
if( isize > 0 ) m_mat = new T[NUM_ELEMENT]; |
| 192 |
} |
} |
| 193 |
|
|
| 194 |
|
|
| 195 |
inline Int4 put_index(const int& ISIZE, const int& j, const int& k) |
inline int put_index(const int& ISIZE, const int& j, const int& k) |
| 196 |
{ |
{ |
| 197 |
if( j < k ) return j*(ISIZE*2-j-1)/2 + k; |
if( j < k ) return j*(ISIZE*2-j-1)/2 + k; |
| 198 |
else return k*(ISIZE*2-k-1)/2 + j; |
else return k*(ISIZE*2-k-1)/2 + j; |
| 216 |
} |
} |
| 217 |
|
|
| 218 |
|
|
| 219 |
inline Double Determinant3(const SymMat<Double>& rhs) |
inline double Determinant3(const SymMat<double>& rhs) |
| 220 |
{ |
{ |
| 221 |
assert( rhs.size() == 3 ); |
assert( rhs.size() == 3 ); |
| 222 |
|
|
| 226 |
} |
} |
| 227 |
|
|
| 228 |
|
|
| 229 |
inline SymMat<Double> Inverse3(const SymMat<Double>& rhs) |
inline SymMat<double> Inverse3(const SymMat<double>& rhs) |
| 230 |
{ |
{ |
| 231 |
assert( rhs.size() == 3 ); |
assert( rhs.size() == 3 ); |
| 232 |
|
|
| 233 |
const Double det01 = rhs(0,0)*rhs(1,1)-rhs(0,1)*rhs(1,0); |
const double det01 = rhs(0,0)*rhs(1,1)-rhs(0,1)*rhs(1,0); |
| 234 |
const Double det02 = rhs(0,0)*rhs(2,2)-rhs(0,2)*rhs(2,0); |
const double det02 = rhs(0,0)*rhs(2,2)-rhs(0,2)*rhs(2,0); |
| 235 |
const Double det12 = rhs(1,1)*rhs(2,2)-rhs(1,2)*rhs(2,1); |
const double det12 = rhs(1,1)*rhs(2,2)-rhs(1,2)*rhs(2,1); |
| 236 |
const Double det01_02 = rhs(0,0)*rhs(1,2)-rhs(0,2)*rhs(1,0); |
const double det01_02 = rhs(0,0)*rhs(1,2)-rhs(0,2)*rhs(1,0); |
| 237 |
const Double det01_12 = rhs(0,1)*rhs(1,2)-rhs(0,2)*rhs(1,1); |
const double det01_12 = rhs(0,1)*rhs(1,2)-rhs(0,2)*rhs(1,1); |
| 238 |
const Double det02_12 = rhs(0,1)*rhs(2,2)-rhs(0,2)*rhs(2,1); |
const double det02_12 = rhs(0,1)*rhs(2,2)-rhs(0,2)*rhs(2,1); |
| 239 |
|
|
| 240 |
const Double det = 1.0/(rhs(0,0)*det12 - rhs(0,1)*det02_12 + rhs(0,2)*det01_12); |
const double det = 1.0/(rhs(0,0)*det12 - rhs(0,1)*det02_12 + rhs(0,2)*det01_12); |
| 241 |
|
|
| 242 |
SymMat<Double> ans(3); |
SymMat<double> ans(3); |
| 243 |
ans(0,0) = det12; |
ans(0,0) = det12; |
| 244 |
ans(1,1) = det02; |
ans(1,1) = det02; |
| 245 |
ans(2,2) = det01; |
ans(2,2) = det01; |