MercuryDPM  0.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Matrix.h
Go to the documentation of this file.
1 //Copyright (c) 2013-2014, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #ifndef MATRIX_H
27 #define MATRIX_H
28 
29 #include <cmath>
30 #include <sstream>
31 #include "Vector.h"
32 
35 class Matrix3D
36 {
37 public:
38 
39  Mdouble XX, XY, XZ, YX, YY, YZ, ZX, ZY, ZZ;
40 
41  inline Matrix3D(void){}
42 
43  inline Matrix3D (const Mdouble xx, const Mdouble xy, const Mdouble xz, const Mdouble yx, const Mdouble yy, const Mdouble yz, const Mdouble zx, const Mdouble zy, const Mdouble zz)
44  {
45  XX = xx; XY = xy; XZ = xz;
46  YX = yx; YY = yy; YZ = yz;
47  ZX = zx; ZY = zy; ZZ = zz;
48  }
49 
50  inline void set_zero ()
51  {
52  XX = XY = XZ = YX = YY = YZ = ZX = ZY = ZZ = 0.0;
53  }
54 
55  inline Mdouble trace () const
56  {
57  return (XX+YY+ZZ)/3;
58  }
59 
60  inline Matrix3D operator + (const Matrix3D &A) const
61  {
62  return Matrix3D(XX + A.XX, XY + A.XY, XZ + A.XZ,
63  YX + A.YX, YY + A.YY, YZ + A.YZ,
64  ZX + A.ZX, ZY + A.ZY, ZZ + A.ZZ);
65  }
66 
67  inline Matrix3D operator - (const Matrix3D &A) const
68  {
69  return Matrix3D(XX - A.XX, XY - A.XY, XZ - A.XZ,
70  YX - A.YX, YY - A.YY, YZ - A.YZ,
71  ZX - A.ZX, ZY - A.ZY, ZZ - A.ZZ);
72  }
73 
74  inline Matrix3D operator + (const Mdouble A) const
75  {
76  return Matrix3D(XX + A, XY + A, XZ + A,
77  YX + A, YY + A, YZ + A,
78  ZX + A, ZY + A, ZZ + A);
79  }
80 
81  inline Matrix3D operator - (const Mdouble A) const
82  {
83  return Matrix3D(XX - A, XY - A, XZ - A,
84  YX - A, YY - A, YZ - A,
85  ZX - A, ZY - A, ZZ - A);
86  }
87 
88  friend inline Vec3D operator * (const Matrix3D A, const Vec3D B)
89  {
90  return Vec3D(A.XX*B.X+A.XY*B.Y+A.XZ*B.Z,
91  A.YX*B.X+A.YY*B.Y+A.YZ*B.Z,
92  A.ZX*B.X+A.ZY*B.Y+A.ZZ*B.Z);
93  }
94 
95  inline Matrix3D operator * (const Mdouble A) const
96  {
97  return Matrix3D(XX * A, XY * A, XZ * A,
98  YX * A, YY * A, YZ * A,
99  ZX * A, ZY * A, ZZ * A);
100  }
101 
102  inline Vec3D operator * (const Vec3D A) const
103  {
104  return Vec3D(XX * A.X + XY * A.Y + XZ * A.Z,
105  YX * A.X + YY * A.Y + YZ * A.Z,
106  ZX * A.X + ZY * A.Y + ZZ * A.Z );
107  }
108 
109  inline Matrix3D operator / (const Mdouble A) const
110  {
111  return Matrix3D(XX / A, XY / A, XZ / A,
112  YX / A, YY / A, YZ / A,
113  ZX / A, ZY / A, ZZ / A);
114  }
115 
116  friend inline std::ostream& operator<<(std::ostream& os, const Matrix3D &A)
117  {
118  os << A.XX << ' ' << A.XY << ' ' << A.XZ << ' '
119  << A.YX << ' ' << A.YY << ' ' << A.YZ << ' '
120  << A.ZX << ' ' << A.ZY << ' ' << A.ZZ;
121  return os;
122  }
123 
124  friend inline std::istream& operator>>(std::istream& is, Matrix3D &A)
125  {
126  is >> A.XX >> A.XY >> A.XZ >> A.YX >> A.YY >> A.YZ >> A.ZX >> A.ZY >> A.ZZ;
127  return is;
128  }
129 
130  inline Matrix3D& operator+=(const Matrix3D &A)
131  {
132  XX += A.XX;
133  XY += A.XY;
134  XZ += A.XZ;
135  YX += A.YX;
136  YY += A.YY;
137  YZ += A.YZ;
138  ZX += A.ZX;
139  ZY += A.ZY;
140  ZZ += A.ZZ;
141  return *this;
142  }
143 
144  inline Matrix3D& operator-=(const Matrix3D &A)
145  {
146  XX -= A.XX;
147  XY -= A.XY;
148  XZ -= A.XZ;
149  YX -= A.YX;
150  YY -= A.YY;
151  YZ -= A.YZ;
152  ZX -= A.ZX;
153  ZY -= A.ZY;
154  ZZ -= A.ZZ;
155  return *this;
156  }
157 
158  inline Matrix3D& operator/=(const Mdouble A)
159  {
160  XX /= A;
161  XY /= A;
162  XZ /= A;
163  YX /= A;
164  YY /= A;
165  YZ /= A;
166  ZX /= A;
167  ZY /= A;
168  ZZ /= A;
169  return *this;
170  }
171 
172  // Pointwise square
173  friend inline Matrix3D square(const Matrix3D &A)
174  {
175  return Matrix3D(sqr(A.XX), sqr(A.XY), sqr(A.XZ),
176  sqr(A.YX), sqr(A.YY), sqr(A.YZ),
177  sqr(A.ZX), sqr(A.ZY), sqr(A.ZZ));
178  }
179 
180  // Pointwise square root
181  friend inline Matrix3D sqrt(const Matrix3D &A)
182  {
183  return Matrix3D(sqrt(A.XX), sqrt(A.XY), sqrt(A.XZ),
184  sqrt(A.YX), sqrt(A.YY), sqrt(A.YZ),
185  sqrt(A.ZX), sqrt(A.ZY), sqrt(A.ZZ));
186  }
187 
188 };
189 
192  return Matrix3D(A.X*B.X, A.X*B.Y, A.X*B.Z,
193  A.Y*B.X, A.Y*B.Y, A.Y*B.Z,
194  A.Z*B.X, A.Z*B.Y, A.Z*B.Z);
195 }
196 
197 // Cross product
198 Matrix3D Cross(const Vec3D &A, const Matrix3D &B)
199 {
200  return Matrix3D(
201  A.Y*B.ZX-A.Z*B.YX, A.Y*B.ZY-A.Z*B.YY, A.Y*B.ZZ-A.Z*B.YZ,
202  A.Z*B.XX-A.X*B.ZX, A.Z*B.XY-A.X*B.ZY, A.Z*B.XZ-A.X*B.ZZ,
203  A.X*B.YX-A.Y*B.XX, A.X*B.YY-A.Y*B.XY, A.X*B.YZ-A.Y*B.XZ);
204 }
205 
206 #endif
Mdouble XY
Definition: Matrix.h:39
Matrix3D(void)
Definition: Matrix.h:41
Matrix3D operator-(const Matrix3D &A) const
Definition: Matrix.h:67
Mdouble trace() const
Definition: Matrix.h:55
Mdouble X
Definition: Vector.h:44
Mdouble ZY
Definition: Matrix.h:39
Mdouble XZ
Definition: Matrix.h:39
Matrix3D & operator/=(const Mdouble A)
Definition: Matrix.h:158
Mdouble ZX
Definition: Matrix.h:39
Matrix3D Cross(const Vec3D &A, const Matrix3D &B)
Definition: Matrix.h:198
#define sqr(a)
Definition: ExtendedMath.h:36
Mdouble ZZ
Definition: Matrix.h:39
Mdouble YZ
Definition: Matrix.h:39
Matrix3D(const Mdouble xx, const Mdouble xy, const Mdouble xz, const Mdouble yx, const Mdouble yy, const Mdouble yz, const Mdouble zx, const Mdouble zy, const Mdouble zz)
Definition: Matrix.h:43
friend Vec3D operator*(const Matrix3D A, const Vec3D B)
Definition: Matrix.h:88
Matrix3D operator/(const Mdouble A) const
Definition: Matrix.h:109
Matrix3D & operator+=(const Matrix3D &A)
Definition: Matrix.h:130
friend Matrix3D sqrt(const Matrix3D &A)
Definition: Matrix.h:181
void set_zero()
Definition: Matrix.h:50
friend Matrix3D square(const Matrix3D &A)
Definition: Matrix.h:173
double Mdouble
Definition: ExtendedMath.h:33
Matrix3D Dyadic(Vec3D A, Vec3D B)
calculates the dyadic product ( )
Definition: Matrix.h:191
Mdouble YX
Definition: Matrix.h:39
Matrix3D operator+(const Matrix3D &A) const
Definition: Matrix.h:60
friend std::ostream & operator<<(std::ostream &os, const Matrix3D &A)
Definition: Matrix.h:116
Mdouble Y
Definition: Vector.h:44
Mdouble XX
Definition: Matrix.h:39
Matrix3D & operator-=(const Matrix3D &A)
Definition: Matrix.h:144
Mdouble YY
Definition: Matrix.h:39
Implementation of a 3D matrix.
Definition: Matrix.h:35
Implementation of a 3D vector (by Vitaliy).
Definition: Vector.h:40
Mdouble Z
Definition: Vector.h:44
friend std::istream & operator>>(std::istream &is, Matrix3D &A)
Definition: Matrix.h:124