Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
DLR-AMR
GitHub Repository: DLR-AMR/t8code
Path: blob/main/src/t8_mat.h
898 views
1
/*
2
This file is part of t8code.
3
t8code is a C library to manage a collection (a forest) of multiple
4
connected adaptive space-trees of general element classes in parallel.
5
6
Copyright (C) 2023 the developers
7
8
t8code is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2 of the License, or
11
(at your option) any later version.
12
13
t8code is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
17
18
You should have received a copy of the GNU General Public License
19
along with t8code; if not, write to the Free Software Foundation, Inc.,
20
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
*/
22
23
/** \file t8_mat.h
24
* Collection of useful 3x3 matrices, matrix-matrix and matrix-vector operations.
25
*/
26
27
#ifndef T8_MAT_H
28
#define T8_MAT_H
29
30
#include <math.h>
31
32
/** Initialize given 3x3 matrix as rotation matrix around the x-axis with given angle.
33
* \param [in,out] mat 3x3-matrix.
34
* \param [in] angle Rotation angle in radians.
35
*/
36
static inline void
37
t8_mat_init_xrot (double mat[3][3], const double angle)
38
{
39
/* first row */
40
mat[0][0] = 1.0;
41
mat[0][1] = 0.0;
42
mat[0][2] = 0.0;
43
44
/* second row */
45
mat[1][0] = 0.0;
46
mat[1][1] = cos (angle);
47
mat[1][2] = -sin (angle);
48
49
/* third row */
50
mat[2][0] = 0.0;
51
mat[2][1] = sin (angle);
52
mat[2][2] = cos (angle);
53
}
54
55
/** Initialize given 3x3 matrix as rotation matrix around the y-axis with given angle.
56
* \param [in,out] mat 3x3-matrix.
57
* \param [in] angle Rotation angle in radians.
58
*/
59
static inline void
60
t8_mat_init_yrot (double mat[3][3], const double angle)
61
{
62
/* first row */
63
mat[0][0] = cos (angle);
64
mat[0][1] = 0.0;
65
mat[0][2] = sin (angle);
66
67
/* second row */
68
mat[1][0] = 0.0;
69
mat[1][1] = 1.0;
70
mat[1][2] = 0.0;
71
72
/* third row */
73
mat[2][0] = -sin (angle);
74
mat[2][1] = 0.0;
75
mat[2][2] = cos (angle);
76
}
77
78
/** Initialize given 3x3 matrix as rotation matrix around the z-axis with given angle.
79
* \param [in,out] mat 3x3-matrix.
80
* \param [in] angle Rotation angle in radians.
81
*/
82
static inline void
83
t8_mat_init_zrot (double mat[3][3], const double angle)
84
{
85
/* first row */
86
mat[0][0] = cos (angle);
87
mat[0][1] = -sin (angle);
88
mat[0][2] = 0.0;
89
90
/* second row */
91
mat[1][0] = sin (angle);
92
mat[1][1] = cos (angle);
93
mat[1][2] = 0.0;
94
95
/* third row */
96
mat[2][0] = 0.0;
97
mat[2][1] = 0.0;
98
mat[2][2] = 1.0;
99
}
100
101
/** Apply matrix-matrix multiplication: b = M*a.
102
* \param [in] mat 3x3-matrix.
103
* \param [in] a 3-vector.
104
* \param [in,out] b 3-vector.
105
*/
106
static inline void
107
t8_mat_mult_vec (const double mat[3][3], const double a[3], double b[3])
108
{
109
b[0] = mat[0][0] * a[0] + mat[0][1] * a[1] + mat[0][2] * a[2];
110
b[1] = mat[1][0] * a[0] + mat[1][1] * a[1] + mat[1][2] * a[2];
111
b[2] = mat[2][0] * a[0] + mat[2][1] * a[1] + mat[2][2] * a[2];
112
}
113
114
/** Apply matrix-matrix multiplication: C = A*B.
115
* \param [in] A 3x3-matrix.
116
* \param [in] B 3x3-matrix.
117
* \param [in,out] C 3x3-matrix.
118
*/
119
static inline void
120
t8_mat_mult_mat (const double A[3][3], const double B[3][3], double C[3][3])
121
{
122
for (int i = 0; i < 3; i++) {
123
for (int j = 0; j < 3; j++) {
124
C[i][j] = 0.0;
125
}
126
}
127
128
for (int i = 0; i < 3; i++) {
129
for (int j = 0; j < 3; j++) {
130
for (int k = 0; k < 3; k++) {
131
C[i][j] = C[i][j] + A[i][k] * B[k][j];
132
}
133
}
134
}
135
}
136
137
#endif /* !T8_MAT_H */
138
139