ug4
Loading...
Searching...
No Matches
math_matrix_functions.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009-2015: G-CSC, Goethe University Frankfurt
3 * Author: Andreas Vogel
4 *
5 * This file is part of UG4.
6 *
7 * UG4 is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License version 3 (as published by the
9 * Free Software Foundation) with the following additional attribution
10 * requirements (according to LGPL/GPL v3 §7):
11 *
12 * (1) The following notice must be displayed in the Appropriate Legal Notices
13 * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 *
15 * (2) The following notice must be displayed at a prominent place in the
16 * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 *
18 * (3) The following bibliography is recommended for citation and must be
19 * preserved in all covered files:
20 * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 * parallel geometric multigrid solver on hierarchically distributed grids.
22 * Computing and visualization in science 16, 4 (2013), 151-164"
23 * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 * flexible software system for simulating pde based models on high performance
25 * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU Lesser General Public License for more details.
31 */
32
33#ifndef __H__UG__COMMON__MATH_MATRIX_FUNCTIONS__
34#define __H__UG__COMMON__MATH_MATRIX_FUNCTIONS__
35
36#include "math_matrix.h"
37
38namespace ug{
39
42
44// Addition of Matrices
46
48// mOut = m1 + m2
49template <typename matrix_t>
50inline void
51MatAdd(matrix_t& mOut, const matrix_t& m1, const matrix_t& m2);
52
54// Subtraction of Matrices
56
58// mOut = m1 - m2
59template <typename matrix_t>
60inline void
61MatSubtract(matrix_t& mOut, const matrix_t& m1, const matrix_t& m2);
62
64// Multiplication of Matrices
66
68// mOut = m1 * m2
69template <size_t N, size_t M, size_t L, typename T>
70inline void
71MatMultiply(MathMatrix<N, M, T>& mOut,
72 const MathMatrix<N, L, T>& m1, const MathMatrix<L, M, T>& m2);
73
75// mOut = m1 * m2 * m3
76template <size_t N, size_t M, size_t L, size_t P, typename T>
77inline void
78MatMultiply(MathMatrix<N, M, T>& mOut, const MathMatrix<N, L, T>& m1,
79 const MathMatrix<L, P, T>& m2, const MathMatrix<P, M, T>& m3);
80
82// mOut = m1^T * m2^T
83template <size_t N, size_t M, size_t L, typename T>
84inline void
85MatMultiplyTransposed(MathMatrix<N, M, T>& mOut,
86 const MathMatrix<L, N, T>& m1, const MathMatrix<M, L, T>& m2);
87
89// mOut = m^T * m
90template <size_t N, size_t M, typename T>
91inline void
92MatMultiplyMTM(MathMatrix<N, N, T>& mOut, const MathMatrix<M, N, T>& m);
93
95// mOut = m * m^T
96template <size_t N, size_t M, typename T>
97inline void
98MatMultiplyMMT(MathMatrix<M, M, T>& mOut, const MathMatrix<M, N, T>& m);
99
101// mOut = m1 * m2^T
102template <size_t N, size_t M, size_t L, typename T>
103inline void
104MatMultiplyMBT(MathMatrix<N, M, T>& mOut, const MathMatrix<N, L, T>& m1,
105 const MathMatrix<M, L, T>& m2);
106
108// mOut = m1^T * m2
109template <size_t N, size_t M, size_t L, typename T>
110inline void
111MatMultiplyMTB(MathMatrix<N, M, T>& mOut, const MathMatrix<L, N, T>& m1,
112 const MathMatrix<L, M, T>& m2);
113
116// mOut = m1 * m2 * m1^T
117template <size_t N, size_t M, typename T>
118inline void
119MatMultiplyMBMT(MathMatrix<N, N, T>& mOut, const MathMatrix<N, M, T>& m1,
120 const MathMatrix<M, M, T>& m2);
121
124// mOut = m1^T * m2 * m1
125template <size_t N, size_t M, typename T>
126inline void
127MatMultiplyMTBM(MathMatrix<N, N, T>& mOut, const MathMatrix<M, N, T>& m1,
128 const MathMatrix<M, M, T>& m2);
129
131// "Contraction" for Matrices (note: contraction is usually known regarding tensors!)
133
134template <typename matrix_t>
135inline typename matrix_t::value_type
136MatContraction(const matrix_t& m1, const matrix_t& m2);
137
139// "Deviator" and trace for Matrices
141
142template <typename matrix_t>
143inline typename matrix_t::value_type
144MatDeviatorTrace(const matrix_t& m, matrix_t& dev);
145
147// Scaling of Matrices
149
151// mOut = s * m
152template <typename matrix_t>
153inline void
154MatScale(matrix_t& mOut, typename matrix_t::value_type s, const matrix_t& m);
155
157// mOut += s * m
158template <typename matrix_t>
159inline void
160MatScaleAppend(matrix_t& mOut, typename matrix_t::value_type s, const matrix_t& m);
161
163// Transposed of Matrix
165
167template <size_t N, size_t M, typename T>
168inline void
169Transpose(MathMatrix<N,M,T>& mOut, const MathMatrix<M,N,T>& m);
170
172template <typename matrix_t>
173inline void
174Transpose(matrix_t& m);
175
177// Determinant of Matrix
179
181
188template <size_t N, typename T>
189inline typename MathMatrix<N,N,T>::value_type
190Determinant(const MathMatrix<N,N,T>& m);
191
192template <typename T>
193inline typename MathMatrix<1,1,T>::value_type
194Determinant(const MathMatrix<1,1,T>& m);
195
196template <typename T>
197inline typename MathMatrix<2,2,T>::value_type
198Determinant(const MathMatrix<2,2,T>& m);
199
200template <typename T>
201inline typename MathMatrix<3,3,T>::value_type
202Determinant(const MathMatrix<3,3,T>& m);
204
206// Gram Determinant of Matrix
208
210
216template <size_t N, size_t M, typename T>
217inline typename MathMatrix<N,M,T>::value_type
218GramDeterminant(const MathMatrix<N,M,T>& m);
219
220template <typename T>
221inline typename MathMatrix<1,1,T>::value_type
222GramDeterminant(const MathMatrix<1,1,T>& m);
223
224template <typename T>
225inline typename MathMatrix<2,2,T>::value_type
226GramDeterminant(const MathMatrix<2,2,T>& m);
227
228template <typename T>
229inline typename MathMatrix<3,3,T>::value_type
230GramDeterminant(const MathMatrix<3,3,T>& m);
232
234// Square root of Gram Determinant of Matrix
236
238
245template <size_t N, size_t M, typename T>
246inline typename MathMatrix<N,M,T>::value_type
247SqrtGramDeterminant(const MathMatrix<N,M,T>& m);
248
249template <typename T>
250inline typename MathMatrix<1,1,T>::value_type
251SqrtGramDeterminant(const MathMatrix<1,1,T>& m);
252
253template <typename T>
254inline typename MathMatrix<2,2,T>::value_type
255SqrtGramDeterminant(const MathMatrix<2,2,T>& m);
256
257template <typename T>
258inline typename MathMatrix<3,3,T>::value_type
259SqrtGramDeterminant(const MathMatrix<3,3,T>& m);
261
263// Inverse of Matrix
265
267
275template <size_t N, size_t M, typename T>
276inline typename MathMatrix<N,M,T>::value_type
277Inverse(MathMatrix<N,M,T>& mOut, const MathMatrix<M,N,T>& m);
278
279template <typename T>
280inline typename MathMatrix<1,1,T>::value_type
281Inverse(MathMatrix<1,1,T>& mOut, const MathMatrix<1,1,T>& m);
282
283template <typename T>
284inline typename MathMatrix<2,2,T>::value_type
285Inverse(MathMatrix<2,2,T>& mOut, const MathMatrix<2,2,T>& m);
286
287template <typename T>
288inline typename MathMatrix<3,3,T>::value_type
289Inverse(MathMatrix<3,3,T>& mOut, const MathMatrix<3,3,T>& m);
291
293// Inverse Transposed of Matrix
295
297
305template <size_t N, size_t M, typename T>
306inline typename MathMatrix<N,M,T>::value_type
307InverseTransposed(MathMatrix<N,M,T>& mOut, const MathMatrix<M,N,T>& m);
308
309template <typename T>
310inline typename MathMatrix<1,1,T>::value_type
311InverseTransposed(MathMatrix<1,1,T>& mOut, const MathMatrix<1,1,T>& m);
312
313template <typename T>
314inline typename MathMatrix<2,2,T>::value_type
315InverseTransposed(MathMatrix<2,2,T>& mOut, const MathMatrix<2,2,T>& m);
316
317template <typename T>
318inline typename MathMatrix<3,3,T>::value_type
319InverseTransposed(MathMatrix<3,3,T>& mOut, const MathMatrix<3,3,T>& m);
321
323// Right-Inverse of Matrix
325
327
346template <size_t N, size_t M, typename T>
347inline typename MathMatrix<N,M,T>::value_type
348RightInverse(MathMatrix<N,M,T>& mOut, const MathMatrix<M,N,T>& m);
349
350template <typename T>
351inline typename MathMatrix<1,1,T>::value_type
352RightInverse(MathMatrix<1,1>& mOut, const MathMatrix<1,1>& m);
353
354template <typename T>
355inline typename MathMatrix<2,2,T>::value_type
356RightInverse(MathMatrix<2,2>& mOut, const MathMatrix<2,2>& m);
357
358template <typename T>
359inline typename MathMatrix<3,3,T>::value_type
360RightInverse(MathMatrix<3,3>& mOut, const MathMatrix<3,3>& m);
362
364// Left-Inverse of Matrix
366
368
387template <size_t N, size_t M, typename T>
388inline typename MathMatrix<N,M,T>::value_type
389LeftInverse(MathMatrix<N,M,T>& mOut, const MathMatrix<M,N,T>& m);
390
391template <typename T>
392inline typename MathMatrix<1,1,T>::value_type
393LeftInverse(MathMatrix<1,1>& mOut, const MathMatrix<1,1>& m);
394
395template <typename T>
396inline typename MathMatrix<2,2,T>::value_type
397LeftInverse(MathMatrix<2,2>& mOut, const MathMatrix<2,2>& m);
398
399template <typename T>
400inline typename MathMatrix<3,3,T>::value_type
401LeftInverse(MathMatrix<3,3>& mOut, const MathMatrix<3,3>& m);
403
405// Generalized-Inverse of Matrix
407
420template<size_t N, size_t M, typename T>
421inline typename MathMatrix<N,M,T>::value_type
422GeneralizedInverse(MathMatrix<N,M,T>& mOut, const MathMatrix<M,N,T>& m);
424
426// Trace of Matrix
428
430
434template <typename T>
435inline typename MathMatrix<1,1,T>::value_type
436Trace(const MathMatrix<1,1,T>& m);
437
438template <typename T>
439inline typename MathMatrix<2,2,T>::value_type
440Trace(const MathMatrix<2,2,T>& m);
441
442template <typename T>
443inline typename MathMatrix<3,3,T>::value_type
444Trace(const MathMatrix<3,3,T>& m);
446
448// Scalar operations for Matrices
450
452template <typename matrix_t>
453inline void
454MatSet(matrix_t& mInOut, typename matrix_t::value_type s);
455
457template <typename matrix_t>
458inline void
459MatDiagSet(matrix_t& mInOut, typename matrix_t::value_type s);
460
462template <typename matrix_t>
463inline void
464MatAdd(matrix_t& mOut, const matrix_t& m, typename matrix_t::value_type s);
465
467template <typename matrix_t>
468inline void
469MatSubtract(matrix_t& mOut, const matrix_t& m, typename matrix_t::value_type s);
470
472template <typename matrix_t>
473inline void
474MatDivide(matrix_t& mOut, const matrix_t& m, typename matrix_t::value_type s);
475
477template <typename matrix_t>
478inline void
479MatMultiply(matrix_t& mOut, const matrix_t& m, typename matrix_t::value_type s);
480
482template <typename matrix_t>
483inline void
484MatIdentity(matrix_t& mOut);
485
487
489template <typename matrix_t>
490inline void
491MatRotationX(matrix_t& mOut, typename matrix_t::value_type rads);
492
494
496template <typename matrix_t>
497inline void
498MatRotationY(matrix_t& mOut, typename matrix_t::value_type rads);
499
501
503template <typename matrix_t>
504inline void
505MatRotationZ(matrix_t& mOut, typename matrix_t::value_type rads);
506
510template <typename matrix_t>
511inline void
512MatRotationYawPitchRoll(matrix_t& mOut,
513 typename matrix_t::value_type yaw,
514 typename matrix_t::value_type pitch,
515 typename matrix_t::value_type roll);
516
521
522template <typename matrix_t, typename vector_t>
523inline void
524MatHouseholder(matrix_t& mOut, const vector_t& orthoVec);
525
527// Norms for Matrices
529
530template <typename matrix_t>
531inline typename matrix_t::value_type
532MatFrobeniusNormSq(matrix_t& m);
533
534template <typename matrix_t>
535inline typename matrix_t::value_type
536MatFrobeniusNorm(matrix_t& m);
537
538template <typename matrix_t>
539inline typename matrix_t::value_type
540MatOneNorm(matrix_t& m);
541
542template <typename matrix_t>
543inline typename matrix_t::value_type
544MatInftyNorm(matrix_t& m);
545
546template <typename matrix_t>
547inline typename matrix_t::value_type
548MatMaxNorm(matrix_t& m);
549
551template <size_t N, size_t M, typename T>
552inline typename MathMatrix<N,M,T>::value_type
553MaxAbsEigenvalue(const MathMatrix<M,N,T>& m);
554
556template <size_t N, size_t M, typename T>
557inline typename MathMatrix<N,M,T>::value_type
559
560
561// end group math_matrix
563
564} //end of namespace
565
566// include a general, but not very fast implementation of the declared methods above.
568
569#endif /* __H__UG__COMMON__MATH_MATRIX_FUNCTIONS__ */
A class for fixed size, dense matrices.
Definition math_matrix.h:63
T value_type
Definition math_matrix.h:65
void MatMultiplyMMT(MathMatrix< M, M, T > &mOut, const MathMatrix< M, N, T > &m)
multiply a matrix with its transposed and stores the result in a second one
Definition math_matrix_functions_common_impl.hpp:164
MathMatrix< N, M, T >::value_type MinAbsEigenvalue(const MathMatrix< M, N, T > &m)
Computes minimum eigenvalue of a (symmetric) matrix.
matrix_t::value_type MatDeviatorTrace(const matrix_t &m, matrix_t &dev)
Definition math_matrix_functions_common_impl.hpp:295
MathMatrix< N, M, T >::value_type SqrtGramDeterminant(const MathMatrix< N, M, T > &m)
Square root of Gram Determinant of a matrix.
Definition math_matrix_functions_common_impl.hpp:508
matrix_t::value_type MatContraction(const matrix_t &m1, const matrix_t &m2)
Definition math_matrix_functions_common_impl.hpp:275
MathMatrix< N, M, T >::value_type RightInverse(MathMatrix< N, M, T > &mOut, const MathMatrix< M, N, T > &m)
Right-Inverse of a Matrix.
Definition math_matrix_functions_common_impl.hpp:679
void MatMultiplyMBMT(MathMatrix< N, N, T > &mOut, const MathMatrix< N, M, T > &m1, const MathMatrix< M, M, T > &m2)
Definition math_matrix_functions_common_impl.hpp:223
void MatRotationX(matrix_t &mOut, typename matrix_t::value_type rads)
Fills the matrix with a matrix that rotates around the x-axis in 3 dimensions.
Definition math_matrix_functions_common_impl.hpp:873
void Transpose(MathMatrix< N, M, T > &mOut, const MathMatrix< M, N, T > &m)
transpose a matrix
Definition math_matrix_functions_common_impl.hpp:345
MathMatrix< 1, 1, T >::value_type Trace(const MathMatrix< 1, 1, T > &m)
Trace of a Matrix.
Definition math_matrix_functions_common_impl.hpp:769
matrix_t::value_type MatInftyNorm(matrix_t &m)
Definition math_matrix_functions_common_impl.hpp:1006
void MatMultiplyMTB(MathMatrix< N, M, T > &mOut, const MathMatrix< L, N, T > &m1, const MathMatrix< L, M, T > &m2)
multiply the transposed of a matrix with a matrix and stores the result in mOut
Definition math_matrix_functions_common_impl.hpp:205
MathMatrix< N, M, T >::value_type GeneralizedInverse(MathMatrix< N, M, T > &mOut, const MathMatrix< M, N, T > &m)
Definition math_matrix_functions_common_impl.hpp:751
void MatMultiplyMBT(MathMatrix< N, M, T > &mOut, const MathMatrix< N, L, T > &m1, const MathMatrix< M, L, T > &m2)
multiply a matrix with the transposed of a second one and stores the result in mOut
Definition math_matrix_functions_common_impl.hpp:187
void MatMultiplyTransposed(MathMatrix< N, M, T > &mOut, const MathMatrix< L, N, T > &m1, const MathMatrix< M, L, T > &m2)
multiply two transposed matrices and stores the result in a third one
Definition math_matrix_functions_common_impl.hpp:125
void MatIdentity(matrix_t &mOut)
Fills the matrix with the identity matrix.
Definition math_matrix_functions_common_impl.hpp:865
void MatSet(matrix_t &mInOut, typename matrix_t::value_type s)
Set each matrix entry to a scalar (componentwise)
Definition math_matrix_functions_common_impl.hpp:794
MathMatrix< N, N, T >::value_type Determinant(const MathMatrix< N, N, T > &m)
Determinant of a matrix.
Definition math_matrix_functions_common_impl.hpp:382
void MatDiagSet(matrix_t &mInOut, typename matrix_t::value_type s)
Set diagonal entries of a matrix to a scalar (other entries are not changed)
Definition math_matrix_functions_common_impl.hpp:806
void MatRotationZ(matrix_t &mOut, typename matrix_t::value_type rads)
Fills the matrix with a matrix that rotates around the y-axis in 2 or 3 dimensions.
Definition math_matrix_functions_common_impl.hpp:903
matrix_t::value_type MatOneNorm(matrix_t &m)
Definition math_matrix_functions_common_impl.hpp:988
void MatRotationY(matrix_t &mOut, typename matrix_t::value_type rads)
Fills the matrix with a matrix that rotates around the y-axis in 3 dimensions.
Definition math_matrix_functions_common_impl.hpp:888
matrix_t::value_type MatFrobeniusNormSq(matrix_t &m)
Definition math_matrix_functions_common_impl.hpp:966
matrix_t::value_type MatMaxNorm(matrix_t &m)
Definition math_matrix_functions_common_impl.hpp:1024
void MatMultiplyMTM(MathMatrix< N, N, T > &mOut, const MathMatrix< M, N, T > &m)
multiply a transposed matrix with itself and stores the result in a second one
Definition math_matrix_functions_common_impl.hpp:141
MathMatrix< N, M, T >::value_type GramDeterminant(const MathMatrix< N, M, T > &m)
Gram Determinant of a matrix.
Definition math_matrix_functions_common_impl.hpp:444
void MatAdd(matrix_t &mOut, const matrix_t &m1, const matrix_t &m2)
adds two matrices and stores the result in a third one
Definition math_matrix_functions_common_impl.hpp:52
void MatScale(matrix_t &mOut, typename matrix_t::value_type s, const matrix_t &m)
scales a matrix_t
Definition math_matrix_functions_common_impl.hpp:317
void MatRotationYawPitchRoll(matrix_t &mOut, typename matrix_t::value_type yaw, typename matrix_t::value_type pitch, typename matrix_t::value_type roll)
Creates a rotation matrix given yaw, pitch and roll in radiants.
Definition math_matrix_functions_common_impl.hpp:919
MathMatrix< N, M, T >::value_type LeftInverse(MathMatrix< N, M, T > &mOut, const MathMatrix< M, N, T > &m)
Left-Inverse of a Matrix.
Definition math_matrix_functions_common_impl.hpp:715
matrix_t::value_type MatFrobeniusNorm(matrix_t &m)
Definition math_matrix_functions_common_impl.hpp:981
MathMatrix< N, M, T >::value_type InverseTransposed(MathMatrix< N, M, T > &mOut, const MathMatrix< M, N, T > &m)
Transposed-Inverse of a Matrix (= Inverse-Transposed of a Matrix)
Definition math_matrix_functions_common_impl.hpp:622
void MatMultiply(MathMatrix< N, M, T > &mOut, const MathMatrix< N, L, T > &m1, const MathMatrix< L, M, T > &m2)
multiply two matrices and stores the result in a third one
Definition math_matrix_functions_common_impl.hpp:85
void MatMultiplyMTBM(MathMatrix< N, N, T > &mOut, const MathMatrix< M, N, T > &m1, const MathMatrix< M, M, T > &m2)
Definition math_matrix_functions_common_impl.hpp:247
MathMatrix< N, M, T >::value_type Inverse(MathMatrix< N, M, T > &mOut, const MathMatrix< M, N, T > &m)
Inverse of a matrix.
Definition math_matrix_functions_common_impl.hpp:560
void MatSubtract(matrix_t &mOut, const matrix_t &m1, const matrix_t &m2)
subtracts m2 from m1 and stores the result in a mOut
Definition math_matrix_functions_common_impl.hpp:68
MathMatrix< N, M, T >::value_type MaxAbsEigenvalue(const MathMatrix< M, N, T > &m)
Computes maximum eigenvalue of a (symmetric) matrix.
Definition math_matrix_functions_common_impl.hpp:1041
void MatScaleAppend(matrix_t &mOut, typename matrix_t::value_type s, const matrix_t &m)
scales a matrix_t and adds to result to a second matrix
Definition math_matrix_functions_common_impl.hpp:329
void MatHouseholder(matrix_t &mOut, const vector_t &orthoVec)
Definition math_matrix_functions_common_impl.hpp:942
void MatDivide(matrix_t &mOut, const matrix_t &m, typename matrix_t::value_type s)
Devide a matrix by a scalar (componentwise)
Definition math_matrix_functions_common_impl.hpp:841
the ug namespace