ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/branches/development/src/math/SquareMatrix3.hpp
(Generate patch)

Comparing:
trunk/src/math/SquareMatrix3.hpp (file contents), Revision 451 by tim, Tue Mar 29 21:00:54 2005 UTC vs.
branches/development/src/math/SquareMatrix3.hpp (file contents), Revision 1753 by gezelter, Tue Jun 12 13:20:28 2012 UTC

# Line 1 | Line 1
1 < /*
1 > /*
2   * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved.
3   *
4   * The University of Notre Dame grants you ("Licensee") a
# Line 6 | Line 6
6   * redistribute this software in source and binary code form, provided
7   * that the following conditions are met:
8   *
9 < * 1. Acknowledgement of the program authors must be made in any
10 < *    publication of scientific results based in part on use of the
11 < *    program.  An acceptable form of acknowledgement is citation of
12 < *    the article in which the program was described (Matthew
13 < *    A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher
14 < *    J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented
15 < *    Parallel Simulation Engine for Molecular Dynamics,"
16 < *    J. Comput. Chem. 26, pp. 252-271 (2005))
17 < *
18 < * 2. Redistributions of source code must retain the above copyright
9 > * 1. Redistributions of source code must retain the above copyright
10   *    notice, this list of conditions and the following disclaimer.
11   *
12 < * 3. Redistributions in binary form must reproduce the above copyright
12 > * 2. Redistributions in binary form must reproduce the above copyright
13   *    notice, this list of conditions and the following disclaimer in the
14   *    documentation and/or other materials provided with the
15   *    distribution.
# Line 37 | Line 28
28   * arising out of the use of or inability to use software, even if the
29   * University of Notre Dame has been advised of the possibility of
30   * such damages.
31 + *
32 + * SUPPORT OPEN SCIENCE!  If you use OpenMD or its source code in your
33 + * research, please cite the appropriate papers when you publish your
34 + * work.  Good starting points are:
35 + *                                                                      
36 + * [1]  Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).            
37 + * [2]  Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).          
38 + * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008).          
39 + * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010).
40 + * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
41   */
42  
43   /**
# Line 45 | Line 46
46   * @date 10/11/2004
47   * @version 1.0
48   */
49 < #ifndef MATH_SQUAREMATRIX3_HPP
49 > #ifndef MATH_SQUAREMATRIX3_HPP
50   #define  MATH_SQUAREMATRIX3_HPP
51 <
51 > #include <vector>
52   #include "Quaternion.hpp"
53   #include "SquareMatrix.hpp"
54   #include "Vector3.hpp"
55   #include "utils/NumericConstant.hpp"
56 < namespace oopse {
56 > namespace OpenMD {
57  
58 <    template<typename Real>
59 <    class SquareMatrix3 : public SquareMatrix<Real, 3> {
60 <        public:
58 >  template<typename Real>
59 >  class SquareMatrix3 : public SquareMatrix<Real, 3> {
60 >  public:
61  
62 <            typedef Real ElemType;
63 <            typedef Real* ElemPoinerType;
62 >    typedef Real ElemType;
63 >    typedef Real* ElemPoinerType;
64              
65 <            /** default constructor */
66 <            SquareMatrix3() : SquareMatrix<Real, 3>() {
67 <            }
65 >    /** default constructor */
66 >    SquareMatrix3() : SquareMatrix<Real, 3>() {
67 >    }
68  
69 <            /** Constructs and initializes every element of this matrix to a scalar */
70 <            SquareMatrix3(Real s) : SquareMatrix<Real,3>(s){
71 <            }
69 >    /** Constructs and initializes every element of this matrix to a scalar */
70 >    SquareMatrix3(Real s) : SquareMatrix<Real,3>(s){
71 >    }
72  
73 <            /** Constructs and initializes from an array */
74 <            SquareMatrix3(Real* array) : SquareMatrix<Real,3>(array){
75 <            }
73 >    /** Constructs and initializes from an array */
74 >    SquareMatrix3(Real* array) : SquareMatrix<Real,3>(array){
75 >    }
76  
77  
78 <            /** copy  constructor */
79 <            SquareMatrix3(const SquareMatrix<Real, 3>& m)  : SquareMatrix<Real, 3>(m) {
80 <            }
78 >    /** copy  constructor */
79 >    SquareMatrix3(const SquareMatrix<Real, 3>& m)  : SquareMatrix<Real, 3>(m) {
80 >    }
81              
82 <            SquareMatrix3( const Vector3<Real>& eulerAngles) {
83 <                setupRotMat(eulerAngles);
84 <            }
82 >    SquareMatrix3( const Vector3<Real>& eulerAngles) {
83 >      setupRotMat(eulerAngles);
84 >    }
85              
86 <            SquareMatrix3(Real phi, Real theta, Real psi) {
87 <                setupRotMat(phi, theta, psi);
88 <            }
86 >    SquareMatrix3(Real phi, Real theta, Real psi) {
87 >      setupRotMat(phi, theta, psi);
88 >    }
89  
90 <            SquareMatrix3(const Quaternion<Real>& q) {
91 <                setupRotMat(q);
90 >    SquareMatrix3(const Quaternion<Real>& q) {
91 >      setupRotMat(q);
92  
93 <            }
93 >    }
94  
95 <            SquareMatrix3(Real w, Real x, Real y, Real z) {
96 <                setupRotMat(w, x, y, z);
97 <            }
95 >    SquareMatrix3(Real w, Real x, Real y, Real z) {
96 >      setupRotMat(w, x, y, z);
97 >    }
98              
99 <            /** copy assignment operator */
100 <            SquareMatrix3<Real>& operator =(const SquareMatrix<Real, 3>& m) {
101 <                if (this == &m)
102 <                    return *this;
103 <                 SquareMatrix<Real, 3>::operator=(m);
104 <                 return *this;
105 <            }
99 >    /** copy assignment operator */
100 >    SquareMatrix3<Real>& operator =(const SquareMatrix<Real, 3>& m) {
101 >      if (this == &m)
102 >        return *this;
103 >      SquareMatrix<Real, 3>::operator=(m);
104 >      return *this;
105 >    }
106  
107  
108 <            SquareMatrix3<Real>& operator =(const Quaternion<Real>& q) {
109 <                this->setupRotMat(q);
110 <                return *this;
111 <            }
108 >    SquareMatrix3<Real>& operator =(const Quaternion<Real>& q) {
109 >      this->setupRotMat(q);
110 >      return *this;
111 >    }
112  
113 <            /**
114 <             * Sets this matrix to a rotation matrix by three euler angles
115 <             * @ param euler
116 <             */
117 <            void setupRotMat(const Vector3<Real>& eulerAngles) {
118 <                setupRotMat(eulerAngles[0], eulerAngles[1], eulerAngles[2]);
119 <            }
113 >    
114 >    /**
115 >     * Sets this matrix to a rotation matrix by three euler angles
116 >     * @ param euler
117 >     */
118 >    void setupRotMat(const Vector3<Real>& eulerAngles) {
119 >      setupRotMat(eulerAngles[0], eulerAngles[1], eulerAngles[2]);
120 >    }
121  
122 <            /**
123 <             * Sets this matrix to a rotation matrix by three euler angles
124 <             * @param phi
125 <             * @param theta
126 <             * @psi theta
127 <             */
128 <            void setupRotMat(Real phi, Real theta, Real psi) {
129 <                Real sphi, stheta, spsi;
130 <                Real cphi, ctheta, cpsi;
122 >    /**
123 >     * Sets this matrix to a rotation matrix by three euler angles
124 >     * @param phi
125 >     * @param theta
126 >     * @psi theta
127 >     */
128 >    void setupRotMat(Real phi, Real theta, Real psi) {
129 >      Real sphi, stheta, spsi;
130 >      Real cphi, ctheta, cpsi;
131  
132 <                sphi = sin(phi);
133 <                stheta = sin(theta);
134 <                spsi = sin(psi);
135 <                cphi = cos(phi);
136 <                ctheta = cos(theta);
137 <                cpsi = cos(psi);
132 >      sphi = sin(phi);
133 >      stheta = sin(theta);
134 >      spsi = sin(psi);
135 >      cphi = cos(phi);
136 >      ctheta = cos(theta);
137 >      cpsi = cos(psi);
138  
139 <                this->data_[0][0] = cpsi * cphi - ctheta * sphi * spsi;
140 <                this->data_[0][1] = cpsi * sphi + ctheta * cphi * spsi;
141 <                this->data_[0][2] = spsi * stheta;
139 >      this->data_[0][0] = cpsi * cphi - ctheta * sphi * spsi;
140 >      this->data_[0][1] = cpsi * sphi + ctheta * cphi * spsi;
141 >      this->data_[0][2] = spsi * stheta;
142                  
143 <                this->data_[1][0] = -spsi * ctheta - ctheta * sphi * cpsi;
144 <                this->data_[1][1] = -spsi * stheta + ctheta * cphi * cpsi;
145 <                this->data_[1][2] = cpsi * stheta;
143 >      this->data_[1][0] = -spsi * ctheta - ctheta * sphi * cpsi;
144 >      this->data_[1][1] = -spsi * stheta + ctheta * cphi * cpsi;
145 >      this->data_[1][2] = cpsi * stheta;
146  
147 <                this->data_[2][0] = stheta * sphi;
148 <                this->data_[2][1] = -stheta * cphi;
149 <                this->data_[2][2] = ctheta;
150 <            }
147 >      this->data_[2][0] = stheta * sphi;
148 >      this->data_[2][1] = -stheta * cphi;
149 >      this->data_[2][2] = ctheta;
150 >    }
151  
152  
153 <            /**
154 <             * Sets this matrix to a rotation matrix by quaternion
155 <             * @param quat
156 <            */
157 <            void setupRotMat(const Quaternion<Real>& quat) {
158 <                setupRotMat(quat.w(), quat.x(), quat.y(), quat.z());
159 <            }
153 >    /**
154 >     * Sets this matrix to a rotation matrix by quaternion
155 >     * @param quat
156 >     */
157 >    void setupRotMat(const Quaternion<Real>& quat) {
158 >      setupRotMat(quat.w(), quat.x(), quat.y(), quat.z());
159 >    }
160  
161 <            /**
162 <             * Sets this matrix to a rotation matrix by quaternion
163 <             * @param w the first element
164 <             * @param x the second element
165 <             * @param y the third element
166 <             * @param z the fourth element
167 <            */
168 <            void setupRotMat(Real w, Real x, Real y, Real z) {
169 <                Quaternion<Real> q(w, x, y, z);
170 <                *this = q.toRotationMatrix3();
171 <            }
161 >    /**
162 >     * Sets this matrix to a rotation matrix by quaternion
163 >     * @param w the first element
164 >     * @param x the second element
165 >     * @param y the third element
166 >     * @param z the fourth element
167 >     */
168 >    void setupRotMat(Real w, Real x, Real y, Real z) {
169 >      Quaternion<Real> q(w, x, y, z);
170 >      *this = q.toRotationMatrix3();
171 >    }
172  
173 <            /**
174 <             * Returns the quaternion from this rotation matrix
175 <             * @return the quaternion from this rotation matrix
174 <             * @exception invalid rotation matrix
175 <            */            
176 <            Quaternion<Real> toQuaternion() {
177 <                Quaternion<Real> q;
178 <                Real t, s;
179 <                Real ad1, ad2, ad3;    
180 <                t = this->data_[0][0] + this->data_[1][1] + this->data_[2][2] + 1.0;
173 >    void setupSkewMat(Vector3<Real> v) {
174 >        setupSkewMat(v[0], v[1], v[2]);
175 >    }
176  
177 <                if( t > NumericConstant::epsilon ){
178 <
179 <                    s = 0.5 / sqrt( t );
180 <                    q[0] = 0.25 / s;
181 <                    q[1] = (this->data_[1][2] - this->data_[2][1]) * s;
182 <                    q[2] = (this->data_[2][0] - this->data_[0][2]) * s;
183 <                    q[3] = (this->data_[0][1] - this->data_[1][0]) * s;
184 <                } else {
185 <
186 <                    ad1 = fabs( this->data_[0][0] );
187 <                    ad2 = fabs( this->data_[1][1] );
188 <                    ad3 = fabs( this->data_[2][2] );
189 <
195 <                    if( ad1 >= ad2 && ad1 >= ad3 ){
177 >    void setupSkewMat(Real v1, Real v2, Real v3) {
178 >        this->data_[0][0] = 0;
179 >        this->data_[0][1] = -v3;
180 >        this->data_[0][2] = v2;
181 >        this->data_[1][0] = v3;
182 >        this->data_[1][1] = 0;
183 >        this->data_[1][2] = -v1;
184 >        this->data_[2][0] = -v2;
185 >        this->data_[2][1] = v1;
186 >        this->data_[2][2] = 0;
187 >        
188 >        
189 >    }
190  
197                        s = 0.5 / sqrt( 1.0 + this->data_[0][0] - this->data_[1][1] - this->data_[2][2] );
198                        q[0] = (this->data_[1][2] - this->data_[2][1]) * s;
199                        q[1] = 0.25 / s;
200                        q[2] = (this->data_[0][1] + this->data_[1][0]) * s;
201                        q[3] = (this->data_[0][2] + this->data_[2][0]) * s;
202                    } else if ( ad2 >= ad1 && ad2 >= ad3 ) {
203                        s = 0.5 / sqrt( 1.0 + this->data_[1][1] - this->data_[0][0] - this->data_[2][2] );
204                        q[0] = (this->data_[2][0] - this->data_[0][2] ) * s;
205                        q[1] = (this->data_[0][1] + this->data_[1][0]) * s;
206                        q[2] = 0.25 / s;
207                        q[3] = (this->data_[1][2] + this->data_[2][1]) * s;
208                    } else {
191  
192 <                        s = 0.5 / sqrt( 1.0 + this->data_[2][2] - this->data_[0][0] - this->data_[1][1] );
193 <                        q[0] = (this->data_[0][1] - this->data_[1][0]) * s;
194 <                        q[1] = (this->data_[0][2] + this->data_[2][0]) * s;
195 <                        q[2] = (this->data_[1][2] + this->data_[2][1]) * s;
196 <                        q[3] = 0.25 / s;
197 <                    }
198 <                }            
192 >    /**
193 >     * Returns the quaternion from this rotation matrix
194 >     * @return the quaternion from this rotation matrix
195 >     * @exception invalid rotation matrix
196 >     */            
197 >    Quaternion<Real> toQuaternion() {
198 >      Quaternion<Real> q;
199 >      Real t, s;
200 >      Real ad1, ad2, ad3;    
201 >      t = this->data_[0][0] + this->data_[1][1] + this->data_[2][2] + 1.0;
202  
203 <                return q;
203 >      if( t > NumericConstant::epsilon ){
204 >
205 >        s = 0.5 / sqrt( t );
206 >        q[0] = 0.25 / s;
207 >        q[1] = (this->data_[1][2] - this->data_[2][1]) * s;
208 >        q[2] = (this->data_[2][0] - this->data_[0][2]) * s;
209 >        q[3] = (this->data_[0][1] - this->data_[1][0]) * s;
210 >      } else {
211 >
212 >        ad1 = this->data_[0][0];
213 >        ad2 = this->data_[1][1];
214 >        ad3 = this->data_[2][2];
215 >
216 >        if( ad1 >= ad2 && ad1 >= ad3 ){
217 >
218 >          s = 0.5 / sqrt( 1.0 + this->data_[0][0] - this->data_[1][1] - this->data_[2][2] );
219 >          q[0] = (this->data_[1][2] - this->data_[2][1]) * s;
220 >          q[1] = 0.25 / s;
221 >          q[2] = (this->data_[0][1] + this->data_[1][0]) * s;
222 >          q[3] = (this->data_[0][2] + this->data_[2][0]) * s;
223 >        } else if ( ad2 >= ad1 && ad2 >= ad3 ) {
224 >          s = 0.5 / sqrt( 1.0 + this->data_[1][1] - this->data_[0][0] - this->data_[2][2] );
225 >          q[0] = (this->data_[2][0] - this->data_[0][2] ) * s;
226 >          q[1] = (this->data_[0][1] + this->data_[1][0]) * s;
227 >          q[2] = 0.25 / s;
228 >          q[3] = (this->data_[1][2] + this->data_[2][1]) * s;
229 >        } else {
230 >
231 >          s = 0.5 / sqrt( 1.0 + this->data_[2][2] - this->data_[0][0] - this->data_[1][1] );
232 >          q[0] = (this->data_[0][1] - this->data_[1][0]) * s;
233 >          q[1] = (this->data_[0][2] + this->data_[2][0]) * s;
234 >          q[2] = (this->data_[1][2] + this->data_[2][1]) * s;
235 >          q[3] = 0.25 / s;
236 >        }
237 >      }            
238 >
239 >      return q;
240                  
241 <            }
241 >    }
242  
243 <            /**
244 <             * Returns the euler angles from this rotation matrix
245 <             * @return the euler angles in a vector
246 <             * @exception invalid rotation matrix
247 <             * We use so-called "x-convention", which is the most common definition.
248 <             * In this convention, the rotation given by Euler angles (phi, theta, psi), where the first
249 <             * rotation is by an angle phi about the z-axis, the second is by an angle  
250 <             * theta (0 <= theta <= 180)about the x-axis, and thethird is by an angle psi about the
251 <             * z-axis (again).
252 <            */            
253 <            Vector3<Real> toEulerAngles() {
254 <                Vector3<Real> myEuler;
255 <                Real phi;
256 <                Real theta;
257 <                Real psi;
258 <                Real ctheta;
259 <                Real stheta;
243 >    /**
244 >     * Returns the euler angles from this rotation matrix
245 >     * @return the euler angles in a vector
246 >     * @exception invalid rotation matrix
247 >     * We use so-called "x-convention", which is the most common definition.
248 >     * In this convention, the rotation given by Euler angles (phi, theta,
249 >     * psi), where the first rotation is by an angle phi about the z-axis,
250 >     * the second is by an angle theta (0 <= theta <= 180) about the x-axis,
251 >     * and the third is by an angle psi about the z-axis (again).
252 >     */            
253 >    Vector3<Real> toEulerAngles() {
254 >      Vector3<Real> myEuler;
255 >      Real phi;
256 >      Real theta;
257 >      Real psi;
258 >      Real ctheta;
259 >      Real stheta;
260                  
261 <                // set the tolerance for Euler angles and rotation elements
261 >      // set the tolerance for Euler angles and rotation elements
262  
263 <                theta = acos(std::min(1.0, std::max(-1.0,this->data_[2][2])));
264 <                ctheta = this->data_[2][2];
265 <                stheta = sqrt(1.0 - ctheta * ctheta);
263 >      theta = acos(std::min((RealType)1.0, std::max((RealType)-1.0,this->data_[2][2])));
264 >      ctheta = this->data_[2][2];
265 >      stheta = sqrt(1.0 - ctheta * ctheta);
266  
267 <                // when sin(theta) is close to 0, we need to consider singularity
268 <                // In this case, we can assign an arbitary value to phi (or psi), and then determine
269 <                // the psi (or phi) or vice-versa. We'll assume that phi always gets the rotation, and psi is 0
270 <                // in cases of singularity.  
271 <                // we use atan2 instead of atan, since atan2 will give us -Pi to Pi.
272 <                // Since 0 <= theta <= 180, sin(theta) will be always non-negative. Therefore, it never
273 <                // change the sign of both of the parameters passed to atan2.
267 >      // when sin(theta) is close to 0, we need to consider
268 >      // singularity In this case, we can assign an arbitary value to
269 >      // phi (or psi), and then determine the psi (or phi) or
270 >      // vice-versa. We'll assume that phi always gets the rotation,
271 >      // and psi is 0 in cases of singularity.
272 >      // we use atan2 instead of atan, since atan2 will give us -Pi to Pi.
273 >      // Since 0 <= theta <= 180, sin(theta) will be always
274 >      // non-negative. Therefore, it will never change the sign of both of
275 >      // the parameters passed to atan2.
276  
277 <                if (fabs(stheta) <= oopse::epsilon){
278 <                    psi = 0.0;
279 <                    phi = atan2(-this->data_[1][0], this->data_[0][0]);  
280 <                }
281 <                // we only have one unique solution
282 <                else{    
283 <                    phi = atan2(this->data_[2][0], -this->data_[2][1]);
284 <                    psi = atan2(this->data_[0][2], this->data_[1][2]);
285 <                }
277 >      if (fabs(stheta) < 1e-6){
278 >        psi = 0.0;
279 >        phi = atan2(-this->data_[1][0], this->data_[0][0]);  
280 >      }
281 >      // we only have one unique solution
282 >      else{    
283 >        phi = atan2(this->data_[2][0], -this->data_[2][1]);
284 >        psi = atan2(this->data_[0][2], this->data_[1][2]);
285 >      }
286  
287 <                //wrap phi and psi, make sure they are in the range from 0 to 2*Pi
288 <                if (phi < 0)
289 <                  phi += M_PI;
287 >      //wrap phi and psi, make sure they are in the range from 0 to 2*Pi
288 >      if (phi < 0)
289 >        phi += 2.0 * M_PI;
290  
291 <                if (psi < 0)
292 <                  psi += M_PI;
291 >      if (psi < 0)
292 >        psi += 2.0 * M_PI;
293  
294 <                myEuler[0] = phi;
295 <                myEuler[1] = theta;
296 <                myEuler[2] = psi;
294 >      myEuler[0] = phi;
295 >      myEuler[1] = theta;
296 >      myEuler[2] = psi;
297  
298 <                return myEuler;
299 <            }
298 >      return myEuler;
299 >    }
300              
301 <            /** Returns the determinant of this matrix. */
302 <            Real determinant() const {
303 <                Real x,y,z;
301 >    /** Returns the determinant of this matrix. */
302 >    Real determinant() const {
303 >      Real x,y,z;
304  
305 <                x = this->data_[0][0] * (this->data_[1][1] * this->data_[2][2] - this->data_[1][2] * this->data_[2][1]);
306 <                y = this->data_[0][1] * (this->data_[1][2] * this->data_[2][0] - this->data_[1][0] * this->data_[2][2]);
307 <                z = this->data_[0][2] * (this->data_[1][0] * this->data_[2][1] - this->data_[1][1] * this->data_[2][0]);
305 >      x = this->data_[0][0] * (this->data_[1][1] * this->data_[2][2] - this->data_[1][2] * this->data_[2][1]);
306 >      y = this->data_[0][1] * (this->data_[1][2] * this->data_[2][0] - this->data_[1][0] * this->data_[2][2]);
307 >      z = this->data_[0][2] * (this->data_[1][0] * this->data_[2][1] - this->data_[1][1] * this->data_[2][0]);
308  
309 <                return(x + y + z);
310 <            }            
309 >      return(x + y + z);
310 >    }            
311  
312 <            /** Returns the trace of this matrix. */
313 <            Real trace() const {
314 <                return this->data_[0][0] + this->data_[1][1] + this->data_[2][2];
315 <            }
312 >    /** Returns the trace of this matrix. */
313 >    Real trace() const {
314 >      return this->data_[0][0] + this->data_[1][1] + this->data_[2][2];
315 >    }
316              
317 <            /**
318 <             * Sets the value of this matrix to  the inversion of itself.
319 <             * @note since simple algorithm can be applied to inverse the 3 by 3 matrix, we hide the
320 <             * implementation of inverse in SquareMatrix class
321 <             */
322 <            SquareMatrix3<Real>  inverse() const {
323 <                SquareMatrix3<Real> m;
324 <                double det = determinant();
325 <                if (fabs(det) <= oopse::epsilon) {
326 <                //"The method was called on a matrix with |determinant| <= 1e-6.",
327 <                //"This is a runtime or a programming error in your application.");
328 <                }
317 >    /**
318 >     * Sets the value of this matrix to  the inversion of itself.
319 >     * @note since simple algorithm can be applied to inverse the 3 by 3 matrix, we hide the
320 >     * implementation of inverse in SquareMatrix class
321 >     */
322 >    SquareMatrix3<Real>  inverse() const {
323 >      SquareMatrix3<Real> m;
324 >      RealType det = determinant();
325 >      if (fabs(det) <= OpenMD::epsilon) {
326 >        //"The method was called on a matrix with |determinant| <= 1e-6.",
327 >        //"This is a runtime or a programming error in your application.");
328 >        std::vector<int> zeroDiagElementIndex;
329 >        for (int i =0; i < 3; ++i) {
330 >            if (fabs(this->data_[i][i]) <= OpenMD::epsilon) {
331 >                zeroDiagElementIndex.push_back(i);
332 >            }
333 >        }
334  
335 <                m(0, 0) = this->data_[1][1]*this->data_[2][2] - this->data_[1][2]*this->data_[2][1];
336 <                m(1, 0) = this->data_[1][2]*this->data_[2][0] - this->data_[1][0]*this->data_[2][2];
337 <                m(2, 0) = this->data_[1][0]*this->data_[2][1] - this->data_[1][1]*this->data_[2][0];
338 <                m(0, 1) = this->data_[2][1]*this->data_[0][2] - this->data_[2][2]*this->data_[0][1];
311 <                m(1, 1) = this->data_[2][2]*this->data_[0][0] - this->data_[2][0]*this->data_[0][2];
312 <                m(2, 1) = this->data_[2][0]*this->data_[0][1] - this->data_[2][1]*this->data_[0][0];
313 <                m(0, 2) = this->data_[0][1]*this->data_[1][2] - this->data_[0][2]*this->data_[1][1];
314 <                m(1, 2) = this->data_[0][2]*this->data_[1][0] - this->data_[0][0]*this->data_[1][2];
315 <                m(2, 2) = this->data_[0][0]*this->data_[1][1] - this->data_[0][1]*this->data_[1][0];
335 >        if (zeroDiagElementIndex.size() == 2) {
336 >            int index = zeroDiagElementIndex[0];
337 >            m(index, index) = 1.0 / this->data_[index][index];
338 >        }else if (zeroDiagElementIndex.size() == 1) {
339  
340 <                m /= det;
341 <                return m;
340 >            int a = (zeroDiagElementIndex[0] + 1) % 3;
341 >            int b = (zeroDiagElementIndex[0] + 2) %3;
342 >            RealType denom = this->data_[a][a] * this->data_[b][b] - this->data_[b][a]*this->data_[a][b];
343 >            m(a, a) = this->data_[b][b] /denom;
344 >            m(b, a) = -this->data_[b][a]/denom;
345 >
346 >            m(a,b) = -this->data_[a][b]/denom;
347 >            m(b, b) = this->data_[a][a]/denom;
348 >                
349 >        }
350 >      
351 > /*
352 >        for(std::vector<int>::iterator iter = zeroDiagElementIndex.begin(); iter != zeroDiagElementIndex.end() ++iter) {
353 >            if (this->data_[*iter][0] > OpenMD::epsilon || this->data_[*iter][1] ||this->data_[*iter][2] ||
354 >                this->data_[0][*iter] > OpenMD::epsilon || this->data_[1][*iter] ||this->data_[2][*iter] ) {
355 >                std::cout << "can not inverse matrix" << std::endl;
356              }
357 <            /**
358 <             * Extract the eigenvalues and eigenvectors from a 3x3 matrix.
359 <             * The eigenvectors (the columns of V) will be normalized.
323 <             * The eigenvectors are aligned optimally with the x, y, and z
324 <             * axes respectively.
325 <             * @param a symmetric matrix whose eigenvectors are to be computed. On return, the matrix is
326 <             *     overwritten            
327 <             * @param w will contain the eigenvalues of the matrix On return of this function
328 <             * @param v the columns of this matrix will contain the eigenvectors. The eigenvectors are
329 <             *    normalized and mutually orthogonal.              
330 <             * @warning a will be overwritten
331 <             */
332 <            static void diagonalize(SquareMatrix3<Real>& a, Vector3<Real>& w, SquareMatrix3<Real>& v);
333 <    };
334 < /*=========================================================================
357 >        }
358 > */
359 >      } else {
360  
361 +          m(0, 0) = this->data_[1][1]*this->data_[2][2] - this->data_[1][2]*this->data_[2][1];
362 +          m(1, 0) = this->data_[1][2]*this->data_[2][0] - this->data_[1][0]*this->data_[2][2];
363 +          m(2, 0) = this->data_[1][0]*this->data_[2][1] - this->data_[1][1]*this->data_[2][0];
364 +          m(0, 1) = this->data_[2][1]*this->data_[0][2] - this->data_[2][2]*this->data_[0][1];
365 +          m(1, 1) = this->data_[2][2]*this->data_[0][0] - this->data_[2][0]*this->data_[0][2];
366 +          m(2, 1) = this->data_[2][0]*this->data_[0][1] - this->data_[2][1]*this->data_[0][0];
367 +          m(0, 2) = this->data_[0][1]*this->data_[1][2] - this->data_[0][2]*this->data_[1][1];
368 +          m(1, 2) = this->data_[0][2]*this->data_[1][0] - this->data_[0][0]*this->data_[1][2];
369 +          m(2, 2) = this->data_[0][0]*this->data_[1][1] - this->data_[0][1]*this->data_[1][0];
370 +
371 +          m /= det;
372 +        }
373 +      return m;
374 +    }
375 +
376 +    SquareMatrix3<Real> transpose() const{
377 +      SquareMatrix3<Real> result;
378 +                
379 +      for (unsigned int i = 0; i < 3; i++)
380 +        for (unsigned int j = 0; j < 3; j++)              
381 +          result(j, i) = this->data_[i][j];
382 +
383 +      return result;
384 +    }
385 +    /**
386 +     * Extract the eigenvalues and eigenvectors from a 3x3 matrix.
387 +     * The eigenvectors (the columns of V) will be normalized.
388 +     * The eigenvectors are aligned optimally with the x, y, and z
389 +     * axes respectively.
390 +     * @param a symmetric matrix whose eigenvectors are to be computed. On return, the matrix is
391 +     *     overwritten            
392 +     * @param w will contain the eigenvalues of the matrix On return of this function
393 +     * @param v the columns of this matrix will contain the eigenvectors. The eigenvectors are
394 +     *    normalized and mutually orthogonal.              
395 +     * @warning a will be overwritten
396 +     */
397 +    static void diagonalize(SquareMatrix3<Real>& a, Vector3<Real>& w, SquareMatrix3<Real>& v);
398 +  };
399 +  /*=========================================================================
400 +
401    Program:   Visualization Toolkit
402    Module:    $RCSfile: SquareMatrix3.hpp,v $
403  
# Line 340 | Line 405 | namespace oopse {
405    All rights reserved.
406    See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
407  
408 <     This software is distributed WITHOUT ANY WARRANTY; without even
409 <     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
410 <     PURPOSE.  See the above copyright notice for more information.
408 >  This software is distributed WITHOUT ANY WARRANTY; without even
409 >  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
410 >  PURPOSE.  See the above copyright notice for more information.
411  
412 < =========================================================================*/
413 <    template<typename Real>
414 <    void SquareMatrix3<Real>::diagonalize(SquareMatrix3<Real>& a, Vector3<Real>& w,
415 <                                                                           SquareMatrix3<Real>& v) {
416 <        int i,j,k,maxI;
417 <        Real tmp, maxVal;
418 <        Vector3<Real> v_maxI, v_k, v_j;
412 >  =========================================================================*/
413 >  template<typename Real>
414 >  void SquareMatrix3<Real>::diagonalize(SquareMatrix3<Real>& a, Vector3<Real>& w,
415 >                                        SquareMatrix3<Real>& v) {
416 >    int i,j,k,maxI;
417 >    Real tmp, maxVal;
418 >    Vector3<Real> v_maxI, v_k, v_j;
419  
420 <        // diagonalize using Jacobi
421 <        jacobi(a, w, v);
422 <        // if all the eigenvalues are the same, return identity matrix
423 <        if (w[0] == w[1] && w[0] == w[2] ) {
424 <              v = SquareMatrix3<Real>::identity();
425 <              return;
426 <        }
420 >    // diagonalize using Jacobi
421 >    SquareMatrix3<Real>::jacobi(a, w, v);
422 >    // if all the eigenvalues are the same, return identity matrix
423 >    if (w[0] == w[1] && w[0] == w[2] ) {
424 >      v = SquareMatrix3<Real>::identity();
425 >      return;
426 >    }
427  
428 <        // transpose temporarily, it makes it easier to sort the eigenvectors
429 <        v = v.transpose();
428 >    // transpose temporarily, it makes it easier to sort the eigenvectors
429 >    v = v.transpose();
430          
431 <        // if two eigenvalues are the same, re-orthogonalize to optimally line
432 <        // up the eigenvectors with the x, y, and z axes
433 <        for (i = 0; i < 3; i++) {
434 <            if (w((i+1)%3) == w((i+2)%3)) {// two eigenvalues are the same
435 <            // find maximum element of the independant eigenvector
436 <            maxVal = fabs(v(i, 0));
437 <            maxI = 0;
438 <            for (j = 1; j < 3; j++) {
439 <                if (maxVal < (tmp = fabs(v(i, j)))){
440 <                    maxVal = tmp;
441 <                    maxI = j;
442 <                }
443 <            }
431 >    // if two eigenvalues are the same, re-orthogonalize to optimally line
432 >    // up the eigenvectors with the x, y, and z axes
433 >    for (i = 0; i < 3; i++) {
434 >      if (w((i+1)%3) == w((i+2)%3)) {// two eigenvalues are the same
435 >        // find maximum element of the independant eigenvector
436 >        maxVal = fabs(v(i, 0));
437 >        maxI = 0;
438 >        for (j = 1; j < 3; j++) {
439 >          if (maxVal < (tmp = fabs(v(i, j)))){
440 >            maxVal = tmp;
441 >            maxI = j;
442 >          }
443 >        }
444              
445 <            // swap the eigenvector into its proper position
446 <            if (maxI != i) {
447 <                tmp = w(maxI);
448 <                w(maxI) = w(i);
449 <                w(i) = tmp;
445 >        // swap the eigenvector into its proper position
446 >        if (maxI != i) {
447 >          tmp = w(maxI);
448 >          w(maxI) = w(i);
449 >          w(i) = tmp;
450  
451 <                v.swapRow(i, maxI);
452 <            }
453 <            // maximum element of eigenvector should be positive
454 <            if (v(maxI, maxI) < 0) {
455 <                v(maxI, 0) = -v(maxI, 0);
456 <                v(maxI, 1) = -v(maxI, 1);
457 <                v(maxI, 2) = -v(maxI, 2);
458 <            }
451 >          v.swapRow(i, maxI);
452 >        }
453 >        // maximum element of eigenvector should be positive
454 >        if (v(maxI, maxI) < 0) {
455 >          v(maxI, 0) = -v(maxI, 0);
456 >          v(maxI, 1) = -v(maxI, 1);
457 >          v(maxI, 2) = -v(maxI, 2);
458 >        }
459  
460 <            // re-orthogonalize the other two eigenvectors
461 <            j = (maxI+1)%3;
462 <            k = (maxI+2)%3;
460 >        // re-orthogonalize the other two eigenvectors
461 >        j = (maxI+1)%3;
462 >        k = (maxI+2)%3;
463  
464 <            v(j, 0) = 0.0;
465 <            v(j, 1) = 0.0;
466 <            v(j, 2) = 0.0;
467 <            v(j, j) = 1.0;
464 >        v(j, 0) = 0.0;
465 >        v(j, 1) = 0.0;
466 >        v(j, 2) = 0.0;
467 >        v(j, j) = 1.0;
468  
469 <            /** @todo */
470 <            v_maxI = v.getRow(maxI);
471 <            v_j = v.getRow(j);
472 <            v_k = cross(v_maxI, v_j);
473 <            v_k.normalize();
474 <            v_j = cross(v_k, v_maxI);
475 <            v.setRow(j, v_j);
476 <            v.setRow(k, v_k);
469 >        /** @todo */
470 >        v_maxI = v.getRow(maxI);
471 >        v_j = v.getRow(j);
472 >        v_k = cross(v_maxI, v_j);
473 >        v_k.normalize();
474 >        v_j = cross(v_k, v_maxI);
475 >        v.setRow(j, v_j);
476 >        v.setRow(k, v_k);
477  
478  
479 <            // transpose vectors back to columns
480 <            v = v.transpose();
481 <            return;
482 <            }
483 <        }
479 >        // transpose vectors back to columns
480 >        v = v.transpose();
481 >        return;
482 >      }
483 >    }
484  
485 <        // the three eigenvalues are different, just sort the eigenvectors
486 <        // to align them with the x, y, and z axes
485 >    // the three eigenvalues are different, just sort the eigenvectors
486 >    // to align them with the x, y, and z axes
487  
488 <        // find the vector with the largest x element, make that vector
489 <        // the first vector
490 <        maxVal = fabs(v(0, 0));
491 <        maxI = 0;
492 <        for (i = 1; i < 3; i++) {
493 <            if (maxVal < (tmp = fabs(v(i, 0)))) {
494 <                maxVal = tmp;
495 <                maxI = i;
496 <            }
497 <        }
488 >    // find the vector with the largest x element, make that vector
489 >    // the first vector
490 >    maxVal = fabs(v(0, 0));
491 >    maxI = 0;
492 >    for (i = 1; i < 3; i++) {
493 >      if (maxVal < (tmp = fabs(v(i, 0)))) {
494 >        maxVal = tmp;
495 >        maxI = i;
496 >      }
497 >    }
498  
499 <        // swap eigenvalue and eigenvector
500 <        if (maxI != 0) {
501 <            tmp = w(maxI);
502 <            w(maxI) = w(0);
503 <            w(0) = tmp;
504 <            v.swapRow(maxI, 0);
505 <        }
506 <        // do the same for the y element
507 <        if (fabs(v(1, 1)) < fabs(v(2, 1))) {
508 <            tmp = w(2);
509 <            w(2) = w(1);
510 <            w(1) = tmp;
511 <            v.swapRow(2, 1);
512 <        }
499 >    // swap eigenvalue and eigenvector
500 >    if (maxI != 0) {
501 >      tmp = w(maxI);
502 >      w(maxI) = w(0);
503 >      w(0) = tmp;
504 >      v.swapRow(maxI, 0);
505 >    }
506 >    // do the same for the y element
507 >    if (fabs(v(1, 1)) < fabs(v(2, 1))) {
508 >      tmp = w(2);
509 >      w(2) = w(1);
510 >      w(1) = tmp;
511 >      v.swapRow(2, 1);
512 >    }
513  
514 <        // ensure that the sign of the eigenvectors is correct
515 <        for (i = 0; i < 2; i++) {
516 <            if (v(i, i) < 0) {
517 <                v(i, 0) = -v(i, 0);
518 <                v(i, 1) = -v(i, 1);
519 <                v(i, 2) = -v(i, 2);
520 <            }
521 <        }
514 >    // ensure that the sign of the eigenvectors is correct
515 >    for (i = 0; i < 2; i++) {
516 >      if (v(i, i) < 0) {
517 >        v(i, 0) = -v(i, 0);
518 >        v(i, 1) = -v(i, 1);
519 >        v(i, 2) = -v(i, 2);
520 >      }
521 >    }
522  
523 <        // set sign of final eigenvector to ensure that determinant is positive
524 <        if (v.determinant() < 0) {
525 <            v(2, 0) = -v(2, 0);
526 <            v(2, 1) = -v(2, 1);
527 <            v(2, 2) = -v(2, 2);
463 <        }
464 <
465 <        // transpose the eigenvectors back again
466 <        v = v.transpose();
467 <        return ;
523 >    // set sign of final eigenvector to ensure that determinant is positive
524 >    if (v.determinant() < 0) {
525 >      v(2, 0) = -v(2, 0);
526 >      v(2, 1) = -v(2, 1);
527 >      v(2, 2) = -v(2, 2);
528      }
529  
530 <    /**
531 <    * Return the multiplication of two matrixes  (m1 * m2).
532 <    * @return the multiplication of two matrixes
533 <    * @param m1 the first matrix
474 <    * @param m2 the second matrix
475 <    */
476 <    template<typename Real>
477 <    inline SquareMatrix3<Real> operator *(const SquareMatrix3<Real>& m1, const SquareMatrix3<Real>& m2) {
478 <        SquareMatrix3<Real> result;
530 >    // transpose the eigenvectors back again
531 >    v = v.transpose();
532 >    return ;
533 >  }
534  
535 <            for (unsigned int i = 0; i < 3; i++)
536 <                for (unsigned int j = 0; j < 3; j++)
537 <                    for (unsigned int k = 0; k < 3; k++)
538 <                        result(i, j)  += m1(i, k) * m2(k, j);                
535 >  /**
536 >   * Return the multiplication of two matrixes  (m1 * m2).
537 >   * @return the multiplication of two matrixes
538 >   * @param m1 the first matrix
539 >   * @param m2 the second matrix
540 >   */
541 >  template<typename Real>
542 >  inline SquareMatrix3<Real> operator *(const SquareMatrix3<Real>& m1, const SquareMatrix3<Real>& m2) {
543 >    SquareMatrix3<Real> result;
544  
545 <        return result;
546 <    }
545 >    for (unsigned int i = 0; i < 3; i++)
546 >      for (unsigned int j = 0; j < 3; j++)
547 >        for (unsigned int k = 0; k < 3; k++)
548 >          result(i, j)  += m1(i, k) * m2(k, j);                
549  
550 <    template<typename Real>
551 <    inline SquareMatrix3<Real> outProduct(const Vector3<Real>& v1, const Vector3<Real>& v2) {
490 <        SquareMatrix3<Real> result;
550 >    return result;
551 >  }
552  
553 <            for (unsigned int i = 0; i < 3; i++) {
554 <                for (unsigned int j = 0; j < 3; j++) {
555 <                        result(i, j)  = v1[i] * v2[j];                
556 <                }
557 <            }
558 <            
559 <        return result;        
553 >  template<typename Real>
554 >  inline SquareMatrix3<Real> outProduct(const Vector3<Real>& v1, const Vector3<Real>& v2) {
555 >    SquareMatrix3<Real> result;
556 >
557 >    for (unsigned int i = 0; i < 3; i++) {
558 >      for (unsigned int j = 0; j < 3; j++) {
559 >        result(i, j)  = v1[i] * v2[j];                
560 >      }
561      }
562 +            
563 +    return result;        
564 +  }
565  
566      
567 <    typedef SquareMatrix3<double> Mat3x3d;
568 <    typedef SquareMatrix3<double> RotMat3x3d;
567 >  typedef SquareMatrix3<RealType> Mat3x3d;
568 >  typedef SquareMatrix3<RealType> RotMat3x3d;
569  
570 < } //namespace oopse
570 > } //namespace OpenMD
571   #endif // MATH_SQUAREMATRIX_HPP
572  

Comparing:
trunk/src/math/SquareMatrix3.hpp (property svn:keywords), Revision 451 by tim, Tue Mar 29 21:00:54 2005 UTC vs.
branches/development/src/math/SquareMatrix3.hpp (property svn:keywords), Revision 1753 by gezelter, Tue Jun 12 13:20:28 2012 UTC

# Line 0 | Line 1
1 + Author Id Revision Date

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines