Develop and Download Open Source Software

Browse Subversion Repository

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 33 - (show annotations) (download) (as text)
Wed Sep 7 04:38:51 2016 UTC (7 years, 6 months ago) by rtomiyasu
File MIME type: text/x-c++hdr
File size: 6559 byte(s)
The output format for base-centered monoclinic cells was corrected.
1 /*
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 #include <assert.h>
31 #include <stddef.h>
32
33 // Class of a symmmetric matrix (a_ij) determined by m_mat as below.
34 // m_mat[0] m_mat[1] m_mat[2]
35 // (a_ij) = m_mat[1] m_mat[3] m_mat[4]
36 // m_mat[2] m_mat[4] m_mat[5]
37 template <class T>
38 class SymMat
39 {
40 private:
41 const int ISIZE;
42 const int NUM_ELEMENT;
43 T* m_mat;
44
45 inline T& operator[](const int&);
46 inline const T& operator[](const int&) const;
47
48 public:
49 SymMat(const int& isize);
50 SymMat(const int& isize, const T&);
51 SymMat(const SymMat<T>&); // copy constructor
52 ~SymMat();
53
54 SymMat<T>& operator=(const T&);
55 SymMat<T>& operator=(const SymMat<T>&);
56 SymMat<T>& operator+=(const SymMat<T>&);
57 SymMat<T>& operator-=(const SymMat<T>&);
58 SymMat<T>& operator*=(const T&);
59 template<class U>
60 SymMat<T>& operator/=(const U&);
61
62 inline T& operator()(const int&, const int&);
63 inline const T& operator()(const int&, const int&) const;
64
65 inline const int& size() const { return ISIZE; };
66
67 // for GUI
68 const T *getref_m_mat() const {return m_mat;}
69 T *getref_m_mat() {return m_mat;}
70 };
71
72 template <class T>
73 SymMat<T>::SymMat(const int& isize) : ISIZE(isize), NUM_ELEMENT(isize*(isize+1)/2), m_mat(NULL)
74 {
75 assert( isize >= 0 );
76 if( isize > 0 ) m_mat = new T[NUM_ELEMENT];
77 }
78
79 template <class T>
80 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 );
83 if( isize > 0 ) m_mat = new T[NUM_ELEMENT];
84 for(int k=0; k<NUM_ELEMENT; k++) m_mat[k]=a;
85 }
86
87 template <class T>
88 SymMat<T>::SymMat(const SymMat<T>&rhs) : ISIZE(rhs.ISIZE), NUM_ELEMENT(ISIZE*(ISIZE+1)/2), m_mat(NULL)
89 {
90 if( ISIZE > 0 ) m_mat = new T[NUM_ELEMENT];
91 for(int k=0; k<NUM_ELEMENT; k++) m_mat[k]=rhs.m_mat[k];
92 }
93
94 template <class T>
95 SymMat<T>::~SymMat()
96 {
97 delete[] (m_mat);
98 }
99
100 template <class T>
101 SymMat<T>& SymMat<T>::operator=(const T& a)
102 {
103 for(int k=0; k<NUM_ELEMENT; k++) m_mat[k]=a;
104 return *this;
105 }
106
107 template <class T>
108 SymMat<T>& SymMat<T>::operator=(const SymMat<T>& rhs)
109 {
110 if(this != &rhs)
111 {
112 assert( ISIZE == rhs.ISIZE );
113 for(int k=0; k<NUM_ELEMENT; k++) m_mat[k]=rhs.m_mat[k];
114 }
115 return *this;
116 }
117
118 template <class T>
119 SymMat<T>& SymMat<T>::operator+=(const SymMat<T>& rhs)
120 {
121 assert( ISIZE == rhs.ISIZE );
122 for(int k=0; k<NUM_ELEMENT; k++) m_mat[k]+=rhs.m_mat[k];
123 return *this;
124 }
125
126 template <class T>
127 SymMat<T>& SymMat<T>::operator-=(const SymMat<T>& rhs)
128 {
129 assert( ISIZE == rhs.ISIZE );
130 for(int k=0; k<NUM_ELEMENT; k++) m_mat[k]-=rhs.m_mat[k];
131 return *this;
132 }
133
134 template <class T>
135 SymMat<T>& SymMat<T>::operator*=(const T& a)
136 {
137 for(int k=0; k<NUM_ELEMENT; k++) m_mat[k]*=a;
138 return *this;
139 }
140
141 template <class T> template<class U>
142 SymMat<T>& SymMat<T>::operator/=(const U& a)
143 {
144 for(int k=0; k<NUM_ELEMENT; k++) m_mat[k]/=a;
145 return *this;
146 }
147
148 template <class T>
149 inline SymMat<T> operator+(const SymMat<T>& lhs, const SymMat<T>& rhs)
150 {
151 SymMat<T> ans(lhs);
152 ans += rhs;
153 return ans;
154 }
155
156 template <class T>
157 inline SymMat<T> operator-(const SymMat<T>& lhs, const SymMat<T>& rhs)
158 {
159 SymMat<T> ans(lhs);
160 ans -= rhs;
161 return ans;
162 }
163
164 template <class T>
165 inline SymMat<T> operator*(const SymMat<T>& lhs, const T& a)
166 {
167 SymMat<T> ans(lhs);
168 ans *= a;
169 return ans;
170 }
171
172 template <class T, class U>
173 inline SymMat<T> operator/(const SymMat<T>& lhs, const U& a)
174 {
175 SymMat<T> ans(lhs);
176 ans /= a;
177 return ans;
178 }
179
180 template <class T>
181 inline T& SymMat<T>::operator[](const int& j)
182 {
183 assert( 0 <= j && j < NUM_ELEMENT );
184 return m_mat[j];
185 }
186
187 template <class T>
188 inline const T& SymMat<T>::operator[](const int& j) const
189 {
190 assert( 0 <= j && j < NUM_ELEMENT );
191 return m_mat[j];
192 }
193
194
195 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;
198 else return k*(ISIZE*2-k-1)/2 + j;
199 }
200
201
202 template <class T>
203 inline T& SymMat<T>::operator()(const int& j, const int& k)
204 {
205 // assert( 0 <= j && j < ISIZE );
206 // assert( 0 <= k && k < ISIZE );
207 return m_mat[put_index(ISIZE, j, k)];
208 }
209
210 template <class T>
211 inline const T& SymMat<T>::operator()(const int& j, const int& k) const
212 {
213 // assert( 0 <= j && j < ISIZE );
214 // assert( 0 <= k && k < ISIZE );
215 return m_mat[put_index(ISIZE, j, k)];
216 }
217
218
219 inline double Determinant3(const SymMat<double>& rhs)
220 {
221 assert( rhs.size() == 3 );
222
223 return rhs(0,0)*rhs(1,1)*rhs(2,2)
224 - 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)
225 + rhs(0,1)*rhs(0,2)*rhs(1,2)*2.0;
226 }
227
228
229 inline SymMat<double> Inverse3(const SymMat<double>& rhs)
230 {
231 assert( rhs.size() == 3 );
232
233 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);
235 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);
237 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);
239
240 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);
243 ans(0,0) = det12;
244 ans(1,1) = det02;
245 ans(2,2) = det01;
246 ans(0,1) = -det02_12;
247 ans(0,2) = det01_12;
248 ans(1,2) = -det01_02;
249
250 ans *= det;
251 return ans;
252 }
253
254 #endif /*SymMat_H_*/

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