/*1This file is part of t8code.2t8code is a C library to manage a collection (a forest) of multiple3connected adaptive space-trees of general element classes in parallel.45Copyright (C) 2023 the developers67t8code is free software; you can redistribute it and/or modify8it under the terms of the GNU General Public License as published by9the Free Software Foundation; either version 2 of the License, or10(at your option) any later version.1112t8code is distributed in the hope that it will be useful,13but WITHOUT ANY WARRANTY; without even the implied warranty of14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15GNU General Public License for more details.1617You should have received a copy of the GNU General Public License18along with t8code; if not, write to the Free Software Foundation, Inc.,1951 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.20*/2122/** \file t8_mat.h23* Collection of useful 3x3 matrices, matrix-matrix and matrix-vector operations.24*/2526#ifndef T8_MAT_H27#define T8_MAT_H2829#include <math.h>3031/** Initialize given 3x3 matrix as rotation matrix around the x-axis with given angle.32* \param [in,out] mat 3x3-matrix.33* \param [in] angle Rotation angle in radians.34*/35static inline void36t8_mat_init_xrot (double mat[3][3], const double angle)37{38/* first row */39mat[0][0] = 1.0;40mat[0][1] = 0.0;41mat[0][2] = 0.0;4243/* second row */44mat[1][0] = 0.0;45mat[1][1] = cos (angle);46mat[1][2] = -sin (angle);4748/* third row */49mat[2][0] = 0.0;50mat[2][1] = sin (angle);51mat[2][2] = cos (angle);52}5354/** Initialize given 3x3 matrix as rotation matrix around the y-axis with given angle.55* \param [in,out] mat 3x3-matrix.56* \param [in] angle Rotation angle in radians.57*/58static inline void59t8_mat_init_yrot (double mat[3][3], const double angle)60{61/* first row */62mat[0][0] = cos (angle);63mat[0][1] = 0.0;64mat[0][2] = sin (angle);6566/* second row */67mat[1][0] = 0.0;68mat[1][1] = 1.0;69mat[1][2] = 0.0;7071/* third row */72mat[2][0] = -sin (angle);73mat[2][1] = 0.0;74mat[2][2] = cos (angle);75}7677/** Initialize given 3x3 matrix as rotation matrix around the z-axis with given angle.78* \param [in,out] mat 3x3-matrix.79* \param [in] angle Rotation angle in radians.80*/81static inline void82t8_mat_init_zrot (double mat[3][3], const double angle)83{84/* first row */85mat[0][0] = cos (angle);86mat[0][1] = -sin (angle);87mat[0][2] = 0.0;8889/* second row */90mat[1][0] = sin (angle);91mat[1][1] = cos (angle);92mat[1][2] = 0.0;9394/* third row */95mat[2][0] = 0.0;96mat[2][1] = 0.0;97mat[2][2] = 1.0;98}99100/** Apply matrix-matrix multiplication: b = M*a.101* \param [in] mat 3x3-matrix.102* \param [in] a 3-vector.103* \param [in,out] b 3-vector.104*/105static inline void106t8_mat_mult_vec (const double mat[3][3], const double a[3], double b[3])107{108b[0] = mat[0][0] * a[0] + mat[0][1] * a[1] + mat[0][2] * a[2];109b[1] = mat[1][0] * a[0] + mat[1][1] * a[1] + mat[1][2] * a[2];110b[2] = mat[2][0] * a[0] + mat[2][1] * a[1] + mat[2][2] * a[2];111}112113/** Apply matrix-matrix multiplication: C = A*B.114* \param [in] A 3x3-matrix.115* \param [in] B 3x3-matrix.116* \param [in,out] C 3x3-matrix.117*/118static inline void119t8_mat_mult_mat (const double A[3][3], const double B[3][3], double C[3][3])120{121for (int i = 0; i < 3; i++) {122for (int j = 0; j < 3; j++) {123C[i][j] = 0.0;124}125}126127for (int i = 0; i < 3; i++) {128for (int j = 0; j < 3; j++) {129for (int k = 0; k < 3; k++) {130C[i][j] = C[i][j] + A[i][k] * B[k][j];131}132}133}134}135136#endif /* !T8_MAT_H */137138139