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 113 by tim, Tue Oct 19 23:01:03 2004 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   /*
2 < * Copyright (C) 2000-2004  Object Oriented Parallel Simulation Engine (OOPSE) project
3 < *
4 < * Contact: oopse@oopse.org
5 < *
6 < * This program is free software; you can redistribute it and/or
7 < * modify it under the terms of the GNU Lesser General Public License
8 < * as published by the Free Software Foundation; either version 2.1
9 < * of the License, or (at your option) any later version.
10 < * All we ask is that proper credit is given for our work, which includes
11 < * - but is not limited to - adding the above copyright notice to the beginning
12 < * of your source code files, and to any copyright notice that you may distribute
13 < * with programs based on this work.
14 < *
15 < * This program is distributed in the hope that it will be useful,
16 < * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 < * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 < * GNU Lesser General Public License for more details.
19 < *
20 < * You should have received a copy of the GNU Lesser General Public License
21 < * along with this program; if not, write to the Free Software
22 < * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
2 > * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved.
3   *
4 + * The University of Notre Dame grants you ("Licensee") a
5 + * non-exclusive, royalty free, license to use, modify and
6 + * redistribute this software in source and binary code form, provided
7 + * that the following conditions are met:
8 + *
9 + * 1. Redistributions of source code must retain the above copyright
10 + *    notice, this list of conditions and the following disclaimer.
11 + *
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.
16 + *
17 + * This software is provided "AS IS," without a warranty of any
18 + * kind. All express or implied conditions, representations and
19 + * warranties, including any implied warranty of merchantability,
20 + * fitness for a particular purpose or non-infringement, are hereby
21 + * excluded.  The University of Notre Dame and its licensors shall not
22 + * be liable for any damages suffered by licensee as a result of
23 + * using, modifying or distributing the software or its
24 + * derivatives. In no event will the University of Notre Dame or its
25 + * licensors be liable for any lost revenue, profit or data, or for
26 + * direct, indirect, special, consequential, incidental or punitive
27 + * damages, however caused and regardless of the theory of liability,
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 <
42 >
43   /**
44   * @file SquareMatrix3.hpp
45   * @author Teng Lin
# Line 31 | Line 48
48   */
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 OpenMD {
57  
58 < namespace oopse {
58 >  template<typename Real>
59 >  class SquareMatrix3 : public SquareMatrix<Real, 3> {
60 >  public:
61  
62 <    template<typename Real>
63 <    class SquareMatrix3 : public SquareMatrix<Real, 3> {
43 <        public:
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 <            /** copy  constructor */
70 <            SquareMatrix3(const SquareMatrix<Real, 3>& m)  : SquareMatrix<Real, 3>(m) {
71 <            }
69 >    /** Constructs and initializes every element of this matrix to a scalar */
70 >    SquareMatrix3(Real s) : SquareMatrix<Real,3>(s){
71 >    }
72  
73 <            SquareMatrix3( const Vector3<Real>& eulerAngles) {
74 <                setupRotMat(eulerAngles);
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 >    }
81              
82 <            SquareMatrix3(Real phi, Real theta, Real psi) {
83 <                setupRotMat(phi, theta, psi);
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 >    }
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  
78            /**
79             * Sets this matrix to a rotation matrix by three euler angles
80             * @ param euler
81             */
82            void setupRotMat(const Vector3<Real>& eulerAngles) {
83                setupRotMat(eulerAngles[0], eulerAngles[1], eulerAngles[2]);
84            }
107  
108 <            /**
109 <             * Sets this matrix to a rotation matrix by three euler angles
110 <             * @param phi
111 <             * @param theta
90 <             * @psi theta
91 <             */
92 <            void setupRotMat(Real phi, Real theta, Real psi) {
93 <                Real sphi, stheta, spsi;
94 <                Real cphi, ctheta, cpsi;
108 >    SquareMatrix3<Real>& operator =(const Quaternion<Real>& q) {
109 >      this->setupRotMat(q);
110 >      return *this;
111 >    }
112  
113 <                sphi = sin(phi);
114 <                stheta = sin(theta);
115 <                spsi = sin(psi);
116 <                cphi = cos(phi);
117 <                ctheta = cos(theta);
118 <                cpsi = cos(psi);
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 <                data_[0][0] = cpsi * cphi - ctheta * sphi * spsi;
123 <                data_[0][1] = cpsi * sphi + ctheta * cphi * spsi;
124 <                data_[0][2] = spsi * stheta;
125 <                
126 <                data_[1][0] = -spsi * ctheta - ctheta * sphi * cpsi;
127 <                data_[1][1] = -spsi * stheta + ctheta * cphi * cpsi;
128 <                data_[1][2] = cpsi * stheta;
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 <                data_[2][0] = stheta * sphi;
133 <                data_[2][1] = -stheta * cphi;
134 <                data_[2][2] = ctheta;
135 <            }
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;
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;
146  
147 <            /**
148 <             * Sets this matrix to a rotation matrix by quaternion
149 <             * @param quat
150 <            */
121 <            void setupRotMat(const Quaternion<Real>& quat) {
122 <                setupRotMat(quat.w(), quat.x(), quat.y(), quat.z());
123 <            }
147 >      this->data_[2][0] = stheta * sphi;
148 >      this->data_[2][1] = -stheta * cphi;
149 >      this->data_[2][2] = ctheta;
150 >    }
151  
125            /**
126             * Sets this matrix to a rotation matrix by quaternion
127             * @param w the first element
128             * @param x the second element
129             * @param y the third element
130             * @param z the fourth element
131            */
132            void setupRotMat(Real w, Real x, Real y, Real z) {
133                Quaternion<Real> q(w, x, y, z);
134                *this = q.toRotationMatrix3();
135            }
152  
153 <            /**
154 <             * Returns the quaternion from this rotation matrix
155 <             * @return the quaternion from this rotation matrix
156 <             * @exception invalid rotation matrix
157 <            */            
158 <            Quaternion<Real> toQuaternion() {
159 <                Quaternion<Real> q;
144 <                Real t, s;
145 <                Real ad1, ad2, ad3;    
146 <                t = data_[0][0] + data_[1][1] + data_[2][2] + 1.0;
147 <
148 <                if( t > 0.0 ){
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 <                    s = 0.5 / sqrt( t );
162 <                    q[0] = 0.25 / s;
163 <                    q[1] = (data_[1][2] - data_[2][1]) * s;
164 <                    q[2] = (data_[2][0] - data_[0][2]) * s;
165 <                    q[3] = (data_[0][1] - data_[1][0]) * s;
166 <                } else {
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 <                    ad1 = fabs( data_[0][0] );
174 <                    ad2 = fabs( data_[1][1] );
175 <                    ad3 = fabs( data_[2][2] );
173 >    void setupSkewMat(Vector3<Real> v) {
174 >        setupSkewMat(v[0], v[1], v[2]);
175 >    }
176  
177 <                    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  
163                        s = 2.0 * sqrt( 1.0 + data_[0][0] - data_[1][1] - data_[2][2] );
164                        q[0] = (data_[1][2] + data_[2][1]) / s;
165                        q[1] = 0.5 / s;
166                        q[2] = (data_[0][1] + data_[1][0]) / s;
167                        q[3] = (data_[0][2] + data_[2][0]) / s;
168                    } else if ( ad2 >= ad1 && ad2 >= ad3 ) {
169                        s = sqrt( 1.0 + data_[1][1] - data_[0][0] - data_[2][2] ) * 2.0;
170                        q[0] = (data_[0][2] + data_[2][0]) / s;
171                        q[1] = (data_[0][1] + data_[1][0]) / s;
172                        q[2] = 0.5 / s;
173                        q[3] = (data_[1][2] + data_[2][1]) / s;
174                    } else {
191  
192 <                        s = sqrt( 1.0 + data_[2][2] - data_[0][0] - data_[1][1] ) * 2.0;
193 <                        q[0] = (data_[0][1] + data_[1][0]) / s;
194 <                        q[1] = (data_[0][2] + data_[2][0]) / s;
195 <                        q[2] = (data_[1][2] + data_[2][1]) / s;
196 <                        q[3] = 0.5 / 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,theta,psi,eps;
256 <                Real ctheta,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,data_[2][2])));
264 <                ctheta = 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(-data_[1][0], data_[0][0]);  
280 <                }
281 <                // we only have one unique solution
282 <                else{    
283 <                    phi = atan2(data_[2][0], -data_[2][1]);
284 <                    psi = atan2(data_[0][2], 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 = data_[0][0] * (data_[1][1] * data_[2][2] - data_[1][2] * data_[2][1]);
306 <                y = data_[0][1] * (data_[1][2] * data_[2][0] - data_[1][0] * data_[2][2]);
307 <                z = data_[0][2] * (data_[1][0] * data_[2][1] - data_[1][1] * 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 >    }
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() {
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) = data_[1][1]*data_[2][2] - data_[1][2]*data_[2][1];
336 <                m(1, 0) = data_[1][2]*data_[2][0] - data_[1][0]*data_[2][2];
337 <                m(2, 0) = data_[1][0]*data_[2][1] - data_[1][1]*data_[2][0];
338 <                m(0, 1) = data_[2][1]*data_[0][2] - data_[2][2]*data_[0][1];
269 <                m(1, 1) = data_[2][2]*data_[0][0] - data_[2][0]*data_[0][2];
270 <                m(2, 1) = data_[2][0]*data_[0][1] - data_[2][1]*data_[0][0];
271 <                m(0, 2) = data_[0][1]*data_[1][2] - data_[0][2]*data_[1][1];
272 <                m(1, 2) = data_[0][2]*data_[1][0] - data_[0][0]*data_[1][2];
273 <                m(2, 2) = data_[0][0]*data_[1][1] - data_[0][1]*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 + */
359 +      } else {
360  
361 <            void diagonalize(SquareMatrix3<Real>& a, Vector3<Real>& w, SquareMatrix3<Real>& v) {
362 <                int i,j,k,maxI;
363 <                Real tmp, maxVal;
364 <                Vector3<Real> v_maxI, v_k, v_j;
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 <                // diagonalize using Jacobi
372 <                jacobi(a, w, v);
371 >          m /= det;
372 >        }
373 >      return m;
374 >    }
375  
376 <                // if all the eigenvalues are the same, return identity matrix
377 <                if (w[0] == w[1] && w[0] == w[2] ) {
289 <                      v = SquareMatrix3<Real>::identity();
290 <                      return;
291 <                }
292 <
293 <                // transpose temporarily, it makes it easier to sort the eigenvectors
294 <                v = v.transpose();
376 >    SquareMatrix3<Real> transpose() const{
377 >      SquareMatrix3<Real> result;
378                  
379 <                // if two eigenvalues are the same, re-orthogonalize to optimally line
380 <                // up the eigenvectors with the x, y, and z axes
381 <                for (i = 0; i < 3; i++) {
299 <                    if (w((i+1)%3) == w((i+2)%3)) {// two eigenvalues are the same
300 <                    // find maximum element of the independant eigenvector
301 <                    maxVal = fabs(v(i, 0));
302 <                    maxI = 0;
303 <                    for (j = 1; j < 3; j++) {
304 <                        if (maxVal < (tmp = fabs(v(i, j)))){
305 <                            maxVal = tmp;
306 <                            maxI = j;
307 <                        }
308 <                    }
309 <                    
310 <                    // swap the eigenvector into its proper position
311 <                    if (maxI != i) {
312 <                        tmp = w(maxI);
313 <                        w(maxI) = w(i);
314 <                        w(i) = tmp;
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 <                        v.swapRow(i, maxI);
384 <                    }
385 <                    // maximum element of eigenvector should be positive
386 <                    if (v(maxI, maxI) < 0) {
387 <                        v(maxI, 0) = -v(maxI, 0);
388 <                        v(maxI, 1) = -v(maxI, 1);
389 <                        v(maxI, 2) = -v(maxI, 2);
390 <                    }
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 <                    // re-orthogonalize the other two eigenvectors
402 <                    j = (maxI+1)%3;
327 <                    k = (maxI+2)%3;
401 >  Program:   Visualization Toolkit
402 >  Module:    $RCSfile: SquareMatrix3.hpp,v $
403  
404 <                    v(j, 0) = 0.0;
405 <                    v(j, 1) = 0.0;
406 <                    v(j, 2) = 0.0;
332 <                    v(j, j) = 1.0;
404 >  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
405 >  All rights reserved.
406 >  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
407  
408 <                    /** @todo */
409 <                    v_maxI = v.getRow(maxI);
410 <                    v_j = v.getRow(j);
337 <                    v_k = cross(v_maxI, v_j);
338 <                    v_k.normalize();
339 <                    v_j = cross(v_k, v_maxI);
340 <                    v.setRow(j, v_j);
341 <                    v.setRow(k, v_k);
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;
419  
420 <                    // transpose vectors back to columns
421 <                    v = v.transpose();
422 <                    return;
423 <                    }
424 <                }
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 <                // the three eigenvalues are different, just sort the eigenvectors
429 <                // to align them with the x, y, and z axes
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 >        }
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;
450  
451 <                // find the vector with the largest x element, make that vector
452 <                // the first vector
453 <                maxVal = fabs(v(0, 0));
454 <                maxI = 0;
455 <                for (i = 1; i < 3; i++) {
456 <                    if (maxVal < (tmp = fabs(v(i, 0)))) {
457 <                        maxVal = tmp;
458 <                        maxI = i;
361 <                    }
362 <                }
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 <                // swap eigenvalue and eigenvector
461 <                if (maxI != 0) {
462 <                    tmp = w(maxI);
367 <                    w(maxI) = w(0);
368 <                    w(0) = tmp;
369 <                    v.swapRow(maxI, 0);
370 <                }
371 <                // do the same for the y element
372 <                if (fabs(v(1, 1)) < fabs(v(2, 1))) {
373 <                    tmp = w(2);
374 <                    w(2) = w(1);
375 <                    w(1) = tmp;
376 <                    v.swapRow(2, 1);
377 <                }
460 >        // re-orthogonalize the other two eigenvectors
461 >        j = (maxI+1)%3;
462 >        k = (maxI+2)%3;
463  
464 <                // ensure that the sign of the eigenvectors is correct
465 <                for (i = 0; i < 2; i++) {
466 <                    if (v(i, i) < 0) {
467 <                        v(i, 0) = -v(i, 0);
383 <                        v(i, 1) = -v(i, 1);
384 <                        v(i, 2) = -v(i, 2);
385 <                    }
386 <                }
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 <                // set sign of final eigenvector to ensure that determinant is positive
470 <                if (v.determinant() < 0) {
471 <                    v(2, 0) = -v(2, 0);
472 <                    v(2, 1) = -v(2, 1);
473 <                    v(2, 2) = -v(2, 2);
474 <                }
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  
395                // transpose the eigenvectors back again
396                v = v.transpose();
397                return ;
398            }
399    };
478  
479 <    typedef SquareMatrix3<double> Mat3x3d;
480 <    typedef SquareMatrix3<double> RotMat3x3d;
479 >        // transpose vectors back to columns
480 >        v = v.transpose();
481 >        return;
482 >      }
483 >    }
484  
485 < } //namespace oopse
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 >    }
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 >    }
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 >    }
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);
528 >    }
529 >
530 >    // transpose the eigenvectors back again
531 >    v = v.transpose();
532 >    return ;
533 >  }
534 >
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 >    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 >    return result;
551 >  }
552 >
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<RealType> Mat3x3d;
568 >  typedef SquareMatrix3<RealType> RotMat3x3d;
569 >
570 > } //namespace OpenMD
571   #endif // MATH_SQUAREMATRIX_HPP
572 +

Comparing:
trunk/src/math/SquareMatrix3.hpp (property svn:keywords), Revision 113 by tim, Tue Oct 19 23:01:03 2004 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