Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /Conograph/trunk/src/utility_data_structure/SymMat.hh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 25 - (hide annotations) (download) (as text)
Mon Jul 7 02:35:51 2014 UTC (9 years, 8 months ago) by rtomiyasu
File MIME type: text/x-c++hdr
File size: 6621 byte(s)
Source codes of version 0.9.99
1 rtomiyasu 3 /*
2     * The MIT License
3    
4     Conograph (powder auto-indexing program)
5    
6     Copyright (c) <2012> <Ryoko Oishi-Tomiyasu, KEK>
7    
8     Permission is hereby granted, free of charge, to any person obtaining a copy
9     of this software and associated documentation files (the "Software"), to deal
10     in the Software without restriction, including without limitation the rights
11     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12     copies of the Software, and to permit persons to whom the Software is
13     furnished to do so, subject to the following conditions:
14    
15     The above copyright notice and this permission notice shall be included in
16     all copies or substantial portions of the Software.
17    
18     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24     THE SOFTWARE.
25     *
26     */
27     #ifndef SYMMAT_H_
28     #define SYMMAT_H_
29    
30 rtomiyasu 25 #include <assert.h>
31     #include "../utility_data_structure/nrutil_nr.hh"
32 rtomiyasu 3
33     using namespace std;
34    
35     // Class of a symmmetric matrix (a_ij) determined by m_mat as below.
36     // m_mat[0] m_mat[1] m_mat[2]
37 rtomiyasu 25 // (a_ij) = m_mat[1] m_mat[3] m_mat[4]
38 rtomiyasu 3 // m_mat[2] m_mat[4] m_mat[5]
39     template <class T>
40     class SymMat
41     {
42     private:
43     const Int4 ISIZE;
44     const Int4 NUM_ELEMENT;
45     T* m_mat;
46    
47     inline T& operator[](const int&);
48     inline const T& operator[](const int&) const;
49    
50     public:
51     SymMat(const Int4& isize);
52     SymMat(const Int4& isize, const T&);
53     SymMat(const SymMat<T>&); // copy constructor
54     ~SymMat();
55    
56     SymMat<T>& operator=(const T&);
57     SymMat<T>& operator=(const SymMat<T>&);
58     SymMat<T>& operator+=(const SymMat<T>&);
59     SymMat<T>& operator-=(const SymMat<T>&);
60     SymMat<T>& operator*=(const T&);
61     template<class U>
62     SymMat<T>& operator/=(const U&);
63    
64     inline T& operator()(const int&, const int&);
65     inline const T& operator()(const int&, const int&) const;
66    
67     inline const Int4& size() const { return ISIZE; };
68    
69     // for GUI
70     const T *getref_m_mat() const {return m_mat;}
71     T *getref_m_mat() {return m_mat;}
72     };
73    
74     template <class T>
75     SymMat<T>::SymMat(const Int4& isize) : ISIZE(isize), NUM_ELEMENT(isize*(isize+1)/2), m_mat(NULL)
76     {
77     assert( isize >= 0 );
78     if( isize > 0 ) m_mat = new T[NUM_ELEMENT];
79     }
80    
81     template <class T>
82     SymMat<T>::SymMat(const Int4& isize, const T& a) : ISIZE(isize), NUM_ELEMENT(isize*(isize+1)/2), m_mat(NULL)
83     {
84     assert( isize >= 0 );
85     if( isize > 0 ) m_mat = new T[NUM_ELEMENT];
86     for(int k=0; k<NUM_ELEMENT; k++) m_mat[k]=a;
87     }
88    
89     template <class T>
90     SymMat<T>::SymMat(const SymMat<T>&rhs) : ISIZE(rhs.ISIZE), NUM_ELEMENT(ISIZE*(ISIZE+1)/2), m_mat(NULL)
91     {
92     if( ISIZE > 0 ) m_mat = new T[NUM_ELEMENT];
93     for(int k=0; k<NUM_ELEMENT; k++) m_mat[k]=rhs.m_mat[k];
94     }
95    
96     template <class T>
97     SymMat<T>::~SymMat()
98     {
99     delete[] (m_mat);
100     }
101    
102     template <class T>
103     SymMat<T>& SymMat<T>::operator=(const T& a)
104     {
105     for(int k=0; k<NUM_ELEMENT; k++) m_mat[k]=a;
106     return *this;
107     }
108    
109     template <class T>
110     SymMat<T>& SymMat<T>::operator=(const SymMat<T>& rhs)
111     {
112     if(this != &rhs)
113     {
114     assert( ISIZE == rhs.ISIZE );
115     for(int k=0; k<NUM_ELEMENT; k++) m_mat[k]=rhs.m_mat[k];
116     }
117     return *this;
118     }
119    
120     template <class T>
121     SymMat<T>& SymMat<T>::operator+=(const SymMat<T>& rhs)
122     {
123     assert( ISIZE == rhs.ISIZE );
124     for(int k=0; k<NUM_ELEMENT; k++) m_mat[k]+=rhs.m_mat[k];
125     return *this;
126     }
127    
128     template <class T>
129     SymMat<T>& SymMat<T>::operator-=(const SymMat<T>& rhs)
130     {
131     assert( ISIZE == rhs.ISIZE );
132     for(int k=0; k<NUM_ELEMENT; k++) m_mat[k]-=rhs.m_mat[k];
133     return *this;
134     }
135    
136     template <class T>
137     SymMat<T>& SymMat<T>::operator*=(const T& a)
138     {
139     for(int k=0; k<NUM_ELEMENT; k++) m_mat[k]*=a;
140     return *this;
141     }
142    
143     template <class T> template<class U>
144     SymMat<T>& SymMat<T>::operator/=(const U& a)
145     {
146     for(int k=0; k<NUM_ELEMENT; k++) m_mat[k]/=a;
147     return *this;
148     }
149    
150     template <class T>
151     inline SymMat<T> operator+(const SymMat<T>& lhs, const SymMat<T>& rhs)
152     {
153     SymMat<T> ans(lhs);
154     ans += rhs;
155     return ans;
156     }
157    
158     template <class T>
159     inline SymMat<T> operator-(const SymMat<T>& lhs, const SymMat<T>& rhs)
160     {
161     SymMat<T> ans(lhs);
162     ans -= rhs;
163     return ans;
164     }
165    
166     template <class T>
167     inline SymMat<T> operator*(const SymMat<T>& lhs, const T& a)
168     {
169     SymMat<T> ans(lhs);
170     ans *= a;
171     return ans;
172     }
173    
174     template <class T, class U>
175     inline SymMat<T> operator/(const SymMat<T>& lhs, const U& a)
176     {
177     SymMat<T> ans(lhs);
178     ans /= a;
179     return ans;
180     }
181    
182     template <class T>
183     inline T& SymMat<T>::operator[](const int& j)
184     {
185     assert( 0 <= j && j < NUM_ELEMENT );
186     return m_mat[j];
187     }
188    
189     template <class T>
190     inline const T& SymMat<T>::operator[](const int& j) const
191     {
192     assert( 0 <= j && j < NUM_ELEMENT );
193     return m_mat[j];
194     }
195    
196    
197     inline Int4 put_index(const int& ISIZE, const int& j, const int& k)
198     {
199     if( j < k ) return j*(ISIZE*2-j-1)/2 + k;
200     else return k*(ISIZE*2-k-1)/2 + j;
201     }
202    
203    
204     template <class T>
205     inline T& SymMat<T>::operator()(const int& j, const int& k)
206     {
207     // assert( 0 <= j && j < ISIZE );
208     // assert( 0 <= k && k < ISIZE );
209     return m_mat[put_index(ISIZE, j, k)];
210     }
211    
212     template <class T>
213     inline const T& SymMat<T>::operator()(const int& j, const int& k) const
214     {
215     // assert( 0 <= j && j < ISIZE );
216     // assert( 0 <= k && k < ISIZE );
217     return m_mat[put_index(ISIZE, j, k)];
218     }
219    
220    
221     inline Double Determinant3(const SymMat<Double>& rhs)
222     {
223     assert( rhs.size() == 3 );
224    
225     return rhs(0,0)*rhs(1,1)*rhs(2,2)
226     - rhs(1,2)*rhs(1,2)*rhs(0,0) - rhs(0,2)*rhs(0,2)*rhs(1,1) - rhs(0,1)*rhs(0,1)*rhs(2,2)
227     + rhs(0,1)*rhs(0,2)*rhs(1,2)*2.0;
228     }
229    
230    
231     inline SymMat<Double> Inverse3(const SymMat<Double>& rhs)
232     {
233     assert( rhs.size() == 3 );
234    
235     const Double det01 = rhs(0,0)*rhs(1,1)-rhs(0,1)*rhs(1,0);
236     const Double det02 = rhs(0,0)*rhs(2,2)-rhs(0,2)*rhs(2,0);
237     const Double det12 = rhs(1,1)*rhs(2,2)-rhs(1,2)*rhs(2,1);
238     const Double det01_02 = rhs(0,0)*rhs(1,2)-rhs(0,2)*rhs(1,0);
239     const Double det01_12 = rhs(0,1)*rhs(1,2)-rhs(0,2)*rhs(1,1);
240     const Double det02_12 = rhs(0,1)*rhs(2,2)-rhs(0,2)*rhs(2,1);
241    
242     const Double det = 1.0/(rhs(0,0)*det12 - rhs(0,1)*det02_12 + rhs(0,2)*det01_12);
243    
244     SymMat<Double> ans(3);
245     ans(0,0) = det12;
246     ans(1,1) = det02;
247     ans(2,2) = det01;
248     ans(0,1) = -det02_12;
249     ans(0,2) = det01_12;
250     ans(1,2) = -det01_02;
251    
252     ans *= det;
253     return ans;
254     }
255    
256     #endif /*SymMat_H_*/

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26