revision: v0.14
Matrix3D Class Reference

Implementation of a 3D matrix. More...

#include <Matrix.h>

Public Member Functions

 Matrix3D ()
 default constructor More...
 
 Matrix3D (Mdouble xx, Mdouble xy, Mdouble xz, Mdouble yx, Mdouble yy, Mdouble yz, Mdouble zx, Mdouble zy, Mdouble zz)
 Alternative constructor, which let you define all elements. More...
 
 Matrix3D (const SmallMatrix< 3, 3 > &matrix)
 Alternative constructor, which takes a matrix of the same size. More...
 
void setZero ()
 Sets all elements to zero. More...
 
double trace () const
 Sum of the diagonal elements. More...
 
Vec3D diag () const
 The diagonal elements. More...
 
double deviator () const
 Deviator. More...
 
Matrix3D operator+ (const Matrix3D &A) const
 Matrix addition. More...
 
Matrix3D operator- (const Matrix3D &A) const
 Matrix subtraction. More...
 
Matrix3D operator+ (Mdouble a) const
 Scalar addition. More...
 
Matrix3D operator- (Mdouble a) const
 Scalar subtraction. More...
 
Matrix3D operator* (Mdouble a) const
 Scalar multiplication. More...
 
Vec3D operator* (const Vec3D &a) const
 Vector multiplication. More...
 
Matrix3D operator* (const Matrix3D &a) const
 Matrix multiplication. More...
 
Matrix3D operator/ (Mdouble a) const
 Scalar division. More...
 
Matrix3Doperator+= (const Matrix3D &A)
 Matrix addition. More...
 
Matrix3Doperator-= (const Matrix3D &A)
 Matrix substraction. More...
 
Matrix3Doperator/= (Mdouble a)
 Scalar division. More...
 
Vec3D ldivide (const Vec3D &b)
 A.ldivide(b) computes the solution x to A*x=b. More...
 
Matrix3D getCylindricalTensorField (const Vec3D &p) const
 Returns the matrix in cylindrical coordinates. More...
 

Static Public Member Functions

static Matrix3D square (const Matrix3D &A)
 Calculates the pointwise square. More...
 
static Matrix3D sqrt (const Matrix3D &A)
 Calculates the pointwise square root. More...
 
static Matrix3D dyadic (const Vec3D &a, const Vec3D &b)
 Calculates the dyadic product of a two Vec3D: \(a \otimes b\). More...
 
static Matrix3D cross (const Vec3D &a, const Matrix3D &b)
 'Special' cross product; CP of vector with each column of a matrix More...
 
static Matrix3D inverse (const Matrix3D &A)
 Computes the inverse of a matrix. More...
 

Public Attributes

Mdouble XX
 all nine matrix elements More...
 
Mdouble XY
 
Mdouble XZ
 
Mdouble YX
 
Mdouble YY
 
Mdouble YZ
 
Mdouble ZX
 
Mdouble ZY
 
Mdouble ZZ
 

Friends

std::ostream & operator<< (std::ostream &os, const Matrix3D &A)
 Add elements to ostream. More...
 
std::istream & operator>> (std::istream &is, Matrix3D &A)
 Add elements to istream. More...
 

Detailed Description

Implementation of a 3D matrix.

Constructor & Destructor Documentation

◆ Matrix3D() [1/3]

Matrix3D::Matrix3D ( )

default constructor

default constructor, which is empty (i.e., only creates the object)

35 {
36  setZero();
37 }

References setZero().

Referenced by cross(), dyadic(), getCylindricalTensorField(), operator*(), operator+(), operator-(), operator/(), sqrt(), and square().

◆ Matrix3D() [2/3]

Matrix3D::Matrix3D ( Mdouble  xx,
Mdouble  xy,
Mdouble  xz,
Mdouble  yx,
Mdouble  yy,
Mdouble  yz,
Mdouble  zx,
Mdouble  zy,
Mdouble  zz 
)

Alternative constructor, which let you define all elements.

Alternative constructor. Let's you specify ALL 9 elements of the 3x3 matrix.

Parameters
[in][all]xx/xy/xz /yx/yy/yz /zx/zy/zz are all nine elements (left-to-right, top-to-bottom) of the 3D matrix.
47 {
48  XX = xx;
49  XY = xy;
50  XZ = xz;
51  YX = yx;
52  YY = yy;
53  YZ = yz;
54  ZX = zx;
55  ZY = zy;
56  ZZ = zz;
57 }

References XX, XY, XZ, YX, YY, YZ, ZX, ZY, and ZZ.

◆ Matrix3D() [3/3]

Matrix3D::Matrix3D ( const SmallMatrix< 3, 3 > &  matrix)

Alternative constructor, which takes a matrix of the same size.

60 {
61  XX = matrix(0, 0);
62  XY = matrix(0, 1);
63  XZ = matrix(0, 2);
64  YX = matrix(1, 0);
65  YY = matrix(1, 1);
66  YZ = matrix(1, 2);
67  ZX = matrix(2, 0);
68  ZY = matrix(2, 1);
69  ZZ = matrix(2, 2);
70 }

References XX, XY, XZ, YX, YY, YZ, ZX, ZY, and ZZ.

Member Function Documentation

◆ cross()

Matrix3D Matrix3D::cross ( const Vec3D a,
const Matrix3D B 
)
static

'Special' cross product; CP of vector with each column of a matrix

Returns a matrix, who's columns are the inner product of a given vector with the corresponding columns of a given matrix

Parameters
[in]avector
[in]Bmatrix
Returns
Resulting matrix
338 {
339  return Matrix3D(
340  a.Y * B.ZX - a.Z * B.YX, a.Y * B.ZY - a.Z * B.YY, a.Y * B.ZZ - a.Z * B.YZ,
341  a.Z * B.XX - a.X * B.ZX, a.Z * B.XY - a.X * B.ZY, a.Z * B.XZ - a.X * B.ZZ,
342  a.X * B.YX - a.Y * B.XX, a.X * B.YY - a.Y * B.XY, a.X * B.YZ - a.Y * B.XZ);
343 }

References Matrix3D(), Vec3D::X, XX, XY, XZ, Vec3D::Y, YX, YY, YZ, Vec3D::Z, ZX, ZY, and ZZ.

◆ deviator()

Mdouble Matrix3D::deviator ( ) const

Deviator.

Returns an invariant of the deviatoric tensor, scaled such that it is equal to shear stress for the stress tensor.

Returns
resulting scalar
104 {
105  const Mdouble P = trace() / 3.0;
106  return std::sqrt(0.5 * (mathsFunc::square(XX - P) + mathsFunc::square(YY - P) + mathsFunc::square(ZZ - P)) +
108 }

References mathsFunc::square(), trace(), XX, XY, XZ, YX, YY, YZ, ZX, ZY, and ZZ.

◆ diag()

Vec3D Matrix3D::diag ( ) const

The diagonal elements.

Returns the sum of the diagonal elements of the matrix.

Returns
The trace of the matrix divided by 3 as an Mdouble
94 {
95  return Vec3D(XX, YY, ZZ);
96 }

References XX, YY, and ZZ.

◆ dyadic()

Matrix3D Matrix3D::dyadic ( const Vec3D a,
const Vec3D b 
)
static

Calculates the dyadic product of a two Vec3D: \(a \otimes b\).

Dyadic product of two vectors

Parameters
[in]afirst vector
[in]bsecond vector
Returns
Resulting matrix
324 {
325  return Matrix3D(a.X * b.X, a.X * b.Y, a.X * b.Z,
326  a.Y * b.X, a.Y * b.Y, a.Y * b.Z,
327  a.Z * b.X, a.Z * b.Y, a.Z * b.Z);
328 }

References Matrix3D(), Vec3D::X, Vec3D::Y, and Vec3D::Z.

Referenced by CGFields::GradVelocityField::addParticleDifferentialStatistics(), DPMBase::getKineticStress(), DPMBase::getStaticStress(), main(), and CGFields::StandardFields::setFields().

◆ getCylindricalTensorField()

Matrix3D Matrix3D::getCylindricalTensorField ( const Vec3D p) const

Returns the matrix in cylindrical coordinates.

Transforms the (Cartesian) vector to cylindrical coordinates. See https://en.wikipedia.org/wiki/Vector_fields_in_cylindrical_and_spherical_coordinates

Returns
Transformed vector
395 {
396  //define sin(A)=y/r, cos(A)=x/r
397  const Mdouble r = std::sqrt(p.X * p.X + p.Y * p.Y);
398  Mdouble s = p.Y / r;
399  Mdouble c = p.X / r;
400  if (r == 0)
401  {
402  s = 0;
403  c = 1;
404  }
405  const Matrix3D M = Matrix3D(XX * c + XY * s, -XX * s + XY * c, XZ,
406  YX * c + YY * s, -YX * s + YY * c, YZ,
407  ZX * c + ZY * s, -ZX * s + ZY * c, ZZ);
408  return Matrix3D(M.XX * c + M.YX * s, -M.XX * s + M.YX * c, M.ZX,
409  M.XY * c + M.YY * s, -M.XY * s + M.YY * c, M.ZY,
410  M.XZ * c + M.YZ * s, -M.XZ * s + M.YZ * c, M.ZZ);
411 }

References Matrix3D(), Vec3D::X, XX, XY, XZ, Vec3D::Y, YX, YY, YZ, ZX, ZY, and ZZ.

Referenced by main(), and CGFields::StandardFields::setCylindricalFields().

◆ inverse()

Matrix3D Matrix3D::inverse ( const Matrix3D A)
static

Computes the inverse of a matrix.

Parameters
[in]AMatrix that should be inverted.
Returns
Inverse of matrix A.
350 {
351  Matrix3D result;
352  Mdouble det;
353  det = 1. / (A.XX * (A.YY * A.ZZ - A.YZ * A.ZY)
354  + A.XY * (A.YZ * A.ZX - A.YX * A.ZZ)
355  + A.XZ * (A.YX * A.ZY - A.YY * A.ZX));
356  result.XX = (A.YY * A.ZZ - A.YZ * A.ZY) * det;
357  result.XY = -(A.XY * A.ZZ - A.XZ * A.ZY) * det;
358  result.XZ = (A.XY * A.YZ - A.XZ * A.YY) * det;
359  result.YX = -(A.YX * A.ZZ - A.YZ * A.ZX) * det;
360  result.YY = (A.XX * A.ZZ - A.XZ * A.ZX) * det;
361  result.YZ = -(A.XX * A.YZ - A.XZ * A.YX) * det;
362  result.ZX = (A.YX * A.ZY - A.YY * A.ZX) * det;
363  result.ZY = -(A.XX * A.ZY - A.XY * A.ZX) * det;
364  result.ZZ = (A.XX * A.YY - A.XY * A.YX) * det;
365  return result;
366 }

References A, XX, XY, XZ, YX, YY, YZ, ZX, ZY, and ZZ.

◆ ldivide()

Vec3D Matrix3D::ldivide ( const Vec3D b)

A.ldivide(b) computes the solution x to A*x=b.

Solve the linear system Ax = b: A.ldivide(b) computes the solution x to A*x=b.

Parameters
[in]bRight-handside in Ax = b, as a const-reference to a Vec3D
Returns
The solution x of Ax = b as a Vec3D.
374 {
375  Mdouble invdet = 1. / (XX * (YY * ZZ - YZ * ZY)
376  + XY * (YZ * ZX - YX * ZZ)
377  + XZ * (YX * ZY - YY * ZX));
378  return Vec3D(+(XY * YZ - YY * XZ) * b.Z
379  - (ZY * YZ - ZZ * YY) * b.X
380  + (ZY * XZ - ZZ * XY) * b.Y,
381  -(XX * YZ - XZ * YX) * b.Z
382  + (ZX * YZ - ZZ * YX) * b.X
383  - (ZX * XZ - XX * ZZ) * b.Y,
384  +(XX * YY - YX * XY) * b.Z
385  - (ZX * YY - YX * ZY) * b.X
386  + (ZX * XY - XX * ZY) * b.Y) * invdet;
387 }

References Vec3D::X, XX, XY, XZ, Vec3D::Y, YX, YY, YZ, Vec3D::Z, ZX, ZY, and ZZ.

Referenced by BasicIntersectionOfWalls::getDistanceAndNormal().

◆ operator*() [1/3]

Matrix3D Matrix3D::operator* ( const Matrix3D a) const

Matrix multiplication.

Todo:
check
185 {
186  return Matrix3D(XX * a.XX + XY * a.YX + XZ * a.ZX,
187  XX * a.XY + XY * a.YY + XZ * a.ZY,
188  XX * a.XZ + XY * a.YZ + XZ * a.ZZ,
189  YX * a.XX + YY * a.YX + YZ * a.ZX,
190  YX * a.XY + YY * a.YY + YZ * a.ZY,
191  YX * a.XZ + YY * a.YZ + YZ * a.ZZ,
192  ZX * a.XX + ZY * a.YX + ZZ * a.ZX,
193  ZX * a.XY + ZY * a.YY + ZZ * a.ZY,
194  ZX * a.XZ + ZY * a.YZ + ZZ * a.ZZ
195  );
196 }

References Matrix3D(), XX, XY, XZ, YX, YY, YZ, ZX, ZY, and ZZ.

◆ operator*() [2/3]

Vec3D Matrix3D::operator* ( const Vec3D a) const

Vector multiplication.

Multiplication with vector

Parameters
[in]aVector to be multiplied with
Returns
Resulting vector
177 {
178  return Vec3D(XX * a.X + XY * a.Y + XZ * a.Z,
179  YX * a.X + YY * a.Y + YZ * a.Z,
180  ZX * a.X + ZY * a.Y + ZZ * a.Z);
181 }

References Vec3D::X, XX, XY, XZ, Vec3D::Y, YX, YY, YZ, Vec3D::Z, ZX, ZY, and ZZ.

◆ operator*() [3/3]

Matrix3D Matrix3D::operator* ( Mdouble  a) const

Scalar multiplication.

Multiplication with scalar

Parameters
[in]aScalar to be multiplied with
Returns
resulting matrix
165 {
166  return Matrix3D(XX * a, XY * a, XZ * a,
167  YX * a, YY * a, YZ * a,
168  ZX * a, ZY * a, ZZ * a);
169 }

References Matrix3D(), XX, XY, XZ, YX, YY, YZ, ZX, ZY, and ZZ.

◆ operator+() [1/2]

Matrix3D Matrix3D::operator+ ( const Matrix3D A) const

Matrix addition.

Addition of this matrix with given one

Parameters
[in]AMatrix to be added
Returns
resulting matrix
116 {
117  return Matrix3D(XX + A.XX, XY + A.XY, XZ + A.XZ,
118  YX + A.YX, YY + A.YY, YZ + A.YZ,
119  ZX + A.ZX, ZY + A.ZY, ZZ + A.ZZ);
120 }

References A, Matrix3D(), XX, XY, XZ, YX, YY, YZ, ZX, ZY, and ZZ.

◆ operator+() [2/2]

Matrix3D Matrix3D::operator+ ( Mdouble  a) const

Scalar addition.

Addition of scalar

Parameters
[in]aScalar to be added
Returns
resulting matrix
141 {
142  return Matrix3D(XX + a, XY + a, XZ + a,
143  YX + a, YY + a, YZ + a,
144  ZX + a, ZY + a, ZZ + a);
145 }

References Matrix3D(), XX, XY, XZ, YX, YY, YZ, ZX, ZY, and ZZ.

◆ operator+=()

Matrix3D & Matrix3D::operator+= ( const Matrix3D A)

Matrix addition.

Adds all elements of a given matrix to its own

Parameters
[in]A3D matrix to be added
Returns
(reference to) resulting (this) matrix
242 {
243  XX += A.XX;
244  XY += A.XY;
245  XZ += A.XZ;
246  YX += A.YX;
247  YY += A.YY;
248  YZ += A.YZ;
249  ZX += A.ZX;
250  ZY += A.ZY;
251  ZZ += A.ZZ;
252  return *this;
253 }

References A, XX, XY, XZ, YX, YY, YZ, ZX, ZY, and ZZ.

◆ operator-() [1/2]

Matrix3D Matrix3D::operator- ( const Matrix3D A) const

Matrix subtraction.

Substraction of given matrix from this one

Parameters
[in]AMatrix to be subtracted
Returns
resulting matrix
129 {
130  return Matrix3D(XX - A.XX, XY - A.XY, XZ - A.XZ,
131  YX - A.YX, YY - A.YY, YZ - A.YZ,
132  ZX - A.ZX, ZY - A.ZY, ZZ - A.ZZ);
133 }

References A, Matrix3D(), XX, XY, XZ, YX, YY, YZ, ZX, ZY, and ZZ.

◆ operator-() [2/2]

Matrix3D Matrix3D::operator- ( Mdouble  a) const

Scalar subtraction.

Substraction of scalar

Parameters
[in]aScalar to be subtracted
Returns
resulting matrix
153 {
154  return Matrix3D(XX - a, XY - a, XZ - a,
155  YX - a, YY - a, YZ - a,
156  ZX - a, ZY - a, ZZ - a);
157 }

References Matrix3D(), XX, XY, XZ, YX, YY, YZ, ZX, ZY, and ZZ.

◆ operator-=()

Matrix3D & Matrix3D::operator-= ( const Matrix3D A)

Matrix substraction.

Substract all elements of a given matrix from its own

Parameters
[in]A3D matrix to be subtracted
Returns
(reference to) resulting (this) matrix
261 {
262  XX -= A.XX;
263  XY -= A.XY;
264  XZ -= A.XZ;
265  YX -= A.YX;
266  YY -= A.YY;
267  YZ -= A.YZ;
268  ZX -= A.ZX;
269  ZY -= A.ZY;
270  ZZ -= A.ZZ;
271  return *this;
272 }

References A, XX, XY, XZ, YX, YY, YZ, ZX, ZY, and ZZ.

◆ operator/()

Matrix3D Matrix3D::operator/ ( Mdouble  a) const

Scalar division.

Division by a scalar

Parameters
[in]ascalar to be divided by
Returns
resulting matrix
204 {
205  return Matrix3D(XX / a, XY / a, XZ / a,
206  YX / a, YY / a, YZ / a,
207  ZX / a, ZY / a, ZZ / a);
208 }

References Matrix3D(), XX, XY, XZ, YX, YY, YZ, ZX, ZY, and ZZ.

◆ operator/=()

Matrix3D & Matrix3D::operator/= ( Mdouble  a)

Scalar division.

Division by a scalar

Parameters
[in]ascalar to be divided by
Returns
(reference to) resulting (this) matrix
280 {
281  XX /= a;
282  XY /= a;
283  XZ /= a;
284  YX /= a;
285  YY /= a;
286  YZ /= a;
287  ZX /= a;
288  ZY /= a;
289  ZZ /= a;
290  return *this;
291 }

References XX, XY, XZ, YX, YY, YZ, ZX, ZY, and ZZ.

◆ setZero()

void Matrix3D::setZero ( )

Sets all elements to zero.

Sets all elements to zero.

76 {
77  XX = XY = XZ = YX = YY = YZ = ZX = ZY = ZZ = 0.0;
78 }

References XX, XY, XZ, YX, YY, YZ, ZX, ZY, and ZZ.

Referenced by Matrix3D(), CGFields::GradVelocityField::setZero(), CGFields::StandardFields::setZero(), and StressStrainControlBoundary::StressStrainControlBoundary().

◆ sqrt()

Matrix3D Matrix3D::sqrt ( const Matrix3D A)
static

Calculates the pointwise square root.

Takes the square root of all the elements in given matrix

Parameters
[in]AMatrix to be pointwise square rooted
Returns
Resulting matrix
311 {
312  return Matrix3D(std::sqrt(A.XX), std::sqrt(A.XY), std::sqrt(A.XZ),
313  std::sqrt(A.YX), std::sqrt(A.YY), std::sqrt(A.YZ),
314  std::sqrt(A.ZX), std::sqrt(A.ZY), std::sqrt(A.ZZ));
315 }

References A, and Matrix3D().

◆ square()

Matrix3D Matrix3D::square ( const Matrix3D A)
static

Calculates the pointwise square.

Squares all the elements in given matrix

Parameters
[in]AMatrix to be pointwise squared
Returns
Resulting matrix

References A, Matrix3D(), and mathsFunc::square().

Referenced by CGFields::GradVelocityField::getSquared(), and CGFields::StandardFields::getSquared().

◆ trace()

Mdouble Matrix3D::trace ( ) const

Sum of the diagonal elements.

Returns the sum of the diagonal elements of the matrix.

Returns
The trace of the matrix divided by 3 as an Mdouble
85 {
86  return XX + YY + ZZ;
87 }

References XX, YY, and ZZ.

Referenced by deviator().

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const Matrix3D A 
)
friend

Add elements to ostream.

Adds all elements of a matrix to an ostream

Parameters
[out]osoutput stream
[in]A3D matrix
Returns
output stream with matrix elements added
217 {
218  os << A.XX << ' ' << A.XY << ' ' << A.XZ << ' '
219  << A.YX << ' ' << A.YY << ' ' << A.YZ << ' '
220  << A.ZX << ' ' << A.ZY << ' ' << A.ZZ;
221  return os;
222 }

◆ operator>>

std::istream& operator>> ( std::istream &  is,
Matrix3D A 
)
friend

Add elements to istream.

Reads the elements of a matrix from an istream

Parameters
[in,out]isinput stream
[out]A3D matrix
Returns
is input stream after the read operation.
231 {
232  is >> A.XX >> A.XY >> A.XZ >> A.YX >> A.YY >> A.YZ >> A.ZX >> A.ZY >> A.ZZ;
233  return is;
234 }

Member Data Documentation

◆ XX

◆ XY

◆ XZ

◆ YX

◆ YY

◆ YZ

◆ ZX

◆ ZY

◆ ZZ


The documentation for this class was generated from the following files:
Matrix3D::trace
double trace() const
Sum of the diagonal elements.
Definition: Matrix.cc:84
mathsFunc::square
T square(const T val)
squares a number
Definition: ExtendedMath.h:106
Matrix3D::YZ
Mdouble YZ
Definition: Matrix.h:43
Matrix3D::XX
Mdouble XX
all nine matrix elements
Definition: Matrix.h:43
Matrix3D::XZ
Mdouble XZ
Definition: Matrix.h:43
Matrix3D::YX
Mdouble YX
Definition: Matrix.h:43
Vec3D::X
Mdouble X
the vector components
Definition: Vector.h:65
Vec3D
Definition: Vector.h:50
A
@ A
Definition: StatisticsVector.h:42
Mdouble
double Mdouble
Definition: GeneralDefine.h:34
Matrix3D::YY
Mdouble YY
Definition: Matrix.h:43
Matrix3D::ZX
Mdouble ZX
Definition: Matrix.h:43
Matrix3D
Implementation of a 3D matrix.
Definition: Matrix.h:38
Matrix3D::ZY
Mdouble ZY
Definition: Matrix.h:43
Matrix3D::setZero
void setZero()
Sets all elements to zero.
Definition: Matrix.cc:75
Matrix3D::ZZ
Mdouble ZZ
Definition: Matrix.h:43
Vec3D::Y
Mdouble Y
Definition: Vector.h:65
Matrix3D::XY
Mdouble XY
Definition: Matrix.h:43
Matrix3D::Matrix3D
Matrix3D()
default constructor
Definition: Matrix.cc:34
Vec3D::Z
Mdouble Z
Definition: Vector.h:65