MercuryDPM  Beta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Matrix.cc
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 #include "Matrix.h"
27 #include "ExtendedMath.h"
28 
33 {
34 }
35 
42 Matrix3D::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)
43 {
44  XX = xx;
45  XY = xy;
46  XZ = xz;
47  YX = yx;
48  YY = yy;
49  YZ = yz;
50  ZX = zx;
51  ZY = zy;
52  ZZ = zz;
53 }
54 
59 {
60  XX = XY = XZ = YX = YY = YZ = ZX = ZY = ZZ = 0.0;
61 }
62 
69 {
70  return (XX + YY + ZZ) / 3;
71 }
72 
79 {
80  return Matrix3D(XX + A.XX, XY + A.XY, XZ + A.XZ,
81  YX + A.YX, YY + A.YY, YZ + A.YZ,
82  ZX + A.ZX, ZY + A.ZY, ZZ + A.ZZ);
83 }
84 
92 {
93  return Matrix3D(XX - A.XX, XY - A.XY, XZ - A.XZ,
94  YX - A.YX, YY - A.YY, YZ - A.YZ,
95  ZX - A.ZX, ZY - A.ZY, ZZ - A.ZZ);
96 }
97 
104 {
105  return Matrix3D(XX + a, XY + a, XZ + a,
106  YX + a, YY + a, YZ + a,
107  ZX + a, ZY + a, ZZ + a);
108 }
109 
116 {
117  return Matrix3D(XX - a, XY - a, XZ - a,
118  YX - a, YY - a, YZ - a,
119  ZX - a, ZY - a, ZZ - a);
120 }
121 
128 {
129  return Matrix3D(XX * a, XY * a, XZ * a,
130  YX * a, YY * a, YZ * a,
131  ZX * a, ZY * a, ZZ * a);
132 }
133 
140 {
141  return Vec3D(XX * a.X + XY * a.Y + XZ * a.Z,
142  YX * a.X + YY * a.Y + YZ * a.Z,
143  ZX * a.X + ZY * a.Y + ZZ * a.Z);
144 }
145 
147 {
148  return Matrix3D(XX * a.XX + XY * a.YX + XZ * a.ZX,
149  YX * a.XX + YY * a.YX + YZ * a.ZX,
150  ZX * a.XX + ZY * a.YX + ZZ * a.ZX,
151  XX * a.XY + XY * a.YY + XZ * a.ZY,
152  YX * a.XY + YY * a.YY + YZ * a.ZY,
153  ZX * a.XY + ZY * a.YY + ZZ * a.ZY,
154  XX * a.XZ + XY * a.YZ + XZ * a.ZZ,
155  YX * a.XZ + YY * a.YZ + YZ * a.ZZ,
156  ZX * a.XZ + ZY * a.YZ + ZZ * a.ZZ
157  );
158 }
159 
166 {
167  return Matrix3D(XX / a, XY / a, XZ / a,
168  YX / a, YY / a, YZ / a,
169  ZX / a, ZY / a, ZZ / a);
170 }
171 
178 std::ostream& operator <<(std::ostream& os, const Matrix3D& A)
179 {
180  os << A.XX << ' ' << A.XY << ' ' << A.XZ << ' '
181  << A.YX << ' ' << A.YY << ' ' << A.YZ << ' '
182  << A.ZX << ' ' << A.ZY << ' ' << A.ZZ;
183  return os;
184 }
185 
192 std::istream& operator >>(std::istream& is, Matrix3D& A)
193 {
194  is >> A.XX >> A.XY >> A.XZ >> A.YX >> A.YY >> A.YZ >> A.ZX >> A.ZY >> A.ZZ;
195  return is;
196 }
197 
204 {
205  XX += A.XX;
206  XY += A.XY;
207  XZ += A.XZ;
208  YX += A.YX;
209  YY += A.YY;
210  YZ += A.YZ;
211  ZX += A.ZX;
212  ZY += A.ZY;
213  ZZ += A.ZZ;
214  return *this;
215 }
216 
223 {
224  XX -= A.XX;
225  XY -= A.XY;
226  XZ -= A.XZ;
227  YX -= A.YX;
228  YY -= A.YY;
229  YZ -= A.YZ;
230  ZX -= A.ZX;
231  ZY -= A.ZY;
232  ZZ -= A.ZZ;
233  return *this;
234 }
235 
242 {
243  XX /= a;
244  XY /= a;
245  XZ /= a;
246  YX /= a;
247  YY /= a;
248  YZ /= a;
249  ZX /= a;
250  ZY /= a;
251  ZZ /= a;
252  return *this;
253 }
254 
261 {
265 }
266 
273 {
274  return Matrix3D(std::sqrt(A.XX), std::sqrt(A.XY), std::sqrt(A.XZ),
275  std::sqrt(A.YX), std::sqrt(A.YY), std::sqrt(A.YZ),
276  std::sqrt(A.ZX), std::sqrt(A.ZY), std::sqrt(A.ZZ));
277 }
278 
286 {
287  return Matrix3D(a.X * b.X, a.X * b.Y, a.X * b.Z,
288  a.Y * b.X, a.Y * b.Y, a.Y * b.Z,
289  a.Z * b.X, a.Z * b.Y, a.Z * b.Z);
290 }
291 
300 {
301  return Matrix3D(
302  a.Y * B.ZX - a.Z * B.YX, a.Y * B.ZY - a.Z * B.YY, a.Y * B.ZZ - a.Z * B.YZ,
303  a.Z * B.XX - a.X * B.ZX, a.Z * B.XY - a.X * B.ZY, a.Z * B.XZ - a.X * B.ZZ,
304  a.X * B.YX - a.Y * B.XX, a.X * B.YY - a.Y * B.XY, a.X * B.YZ - a.Y * B.XZ);
305 }
Mdouble XY
Definition: Matrix.h:42
Matrix3D operator-(const Matrix3D &A) const
Matrix subtraction.
Definition: Matrix.cc:91
double trace() const
Mean of the diagonal elements.
Definition: Matrix.cc:68
Matrix3D & operator+=(const Matrix3D &A)
Matrix addition.
Definition: Matrix.cc:203
Mdouble X
the vector components
Definition: Vector.h:52
Matrix3D & operator/=(const Mdouble a)
Scalar division.
Definition: Matrix.cc:241
Mdouble ZY
Definition: Matrix.h:42
Mdouble XZ
Definition: Matrix.h:42
Mdouble ZX
Definition: Matrix.h:42
double Mdouble
Matrix3D operator/(const Mdouble a) const
Scalar division.
Definition: Matrix.cc:165
Mdouble ZZ
Definition: Matrix.h:42
T square(T val)
squares a number
Definition: ExtendedMath.h:91
Mdouble YZ
Definition: Matrix.h:42
static Matrix3D dyadic(const Vec3D &a, const Vec3D &b)
Calculates the dyadic product of a two Vec3D: .
Definition: Matrix.cc:285
Matrix3D operator*(const Mdouble a) const
Scalar multiplication.
Definition: Matrix.cc:127
static Matrix3D cross(const Vec3D &a, const Matrix3D &b)
'Special' cross product; CP of vector with each column of a matrix
Definition: Matrix.cc:299
Mdouble YX
Definition: Matrix.h:42
Matrix3D operator+(const Matrix3D &A) const
Matrix addition.
Definition: Matrix.cc:78
Mdouble Y
Definition: Vector.h:52
Matrix3D & operator-=(const Matrix3D &A)
Matrix substraction.
Definition: Matrix.cc:222
static Matrix3D sqrt(const Matrix3D &A)
Calculates the pointwise square root.
Definition: Matrix.cc:272
std::ostream & operator<<(std::ostream &os, const Matrix3D &A)
Definition: Matrix.cc:178
Mdouble XX
all nine matrix elements
Definition: Matrix.h:42
void setZero()
Sets all elements to zero.
Definition: Matrix.cc:58
Mdouble YY
Definition: Matrix.h:42
Implementation of a 3D matrix.
Definition: Matrix.h:36
Implementation of a 3D vector (by Vitaliy).
Definition: Vector.h:45
std::istream & operator>>(std::istream &is, Matrix3D &A)
Definition: Matrix.cc:192
Mdouble Z
Definition: Vector.h:52
Matrix3D()
default constructor
Definition: Matrix.cc:32
static Matrix3D square(const Matrix3D &A)
Calculates the pointwise square.
Definition: Matrix.cc:260