SCOREC core
Parallel unstructured mesh tools
mth_def.h
1 /******************************************************************************
2 
3  Copyright 2015 Scientific Computation Research Center,
4  Rensselaer Polytechnic Institute. All rights reserved.
5 
6  This work is open source software, licensed under the terms of the
7  BSD license as described in the LICENSE file in the top-level directory.
8 
9 *******************************************************************************/
10 
11 #ifndef MTH_DEF_H
12 #define MTH_DEF_H
13 
14 #include "mth.h"
15 
16 namespace mth {
17 
18 template <class T, unsigned M>
19 T norm(Vector<T,M> const& a)
20 {
21  return sqrt(a * a);
22 }
23 
24 template <class T>
26 {
27  Vector3<T> r;
28  r(0) = a(1)*b(2) - a(2)*b(1);
29  r(1) = a(2)*b(0) - a(0)*b(2);
30  r(2) = a(0)*b(1) - a(1)*b(0);
31  return r;
32 }
33 
34 template <class T>
36 {
37  Matrix3x3<T> r(
38  0, -a(2), a(1),
39  a(2), 0, -a(0),
40  -a(1), a(0), 0);
41  return r;
42 }
43 
44 template <class T, unsigned N>
46 {
47  return b*((a*b)/(b*b));
48 }
49 
50 template <class T, unsigned N>
52 {
53  return a - project(a, b);
54 }
55 
56 template <class T, unsigned M, unsigned N>
57 void transpose(Matrix<T,M,N> const& a,
58  Matrix<T,N,M>& b)
59 {
60  unsigned m = a.cols();
61  unsigned n = a.rows();
62  b.resize(m, n);
63  for (unsigned i=0; i < m; ++i)
64  for (unsigned j=0; j < n; ++j)
65  b(i,j) = a(j,i);
66 }
67 
68 template <class T>
70 {
71  return a(0,0)*a(1,1) - a(1,0)*a(0,1);
72 }
73 
74 template <class T>
76 {
77  return
78  a(0,0) * (a(1,1)*a(2,2) - a(2,1)*a(1,2)) -
79  a(0,1) * (a(1,0)*a(2,2) - a(2,0)*a(1,2)) +
80  a(0,2) * (a(1,0)*a(2,1) - a(2,0)*a(1,1));
81 }
82 
83 template <class T>
85 {
86  Matrix<T,2,2> r;
87  r(0,0) = a(1,1); r(0,1) = -a(0,1);
88  r(1,0) = -a(1,0); r(1,1) = a(0,0);
89  return r / determinant(a);
90 }
91 
92 template <class T>
94 {
95  Matrix<T,3,3> r;
96  Matrix<T,3,3> x = transpose(a);
97  r[0] = cross(x[1], x[2]);
98  r[1] = cross(x[2], x[0]);
99  r[2] = cross(x[0], x[1]);
100  return r / determinant(a);
101 }
102 
103 template <class T>
104 T trace(Tensor<T> const& a)
105 {
106  unsigned dim = a.dim();
107  T t = a(0,0);
108  for (unsigned i=1; i < dim; ++i)
109  t += a(i,i);
110  return t;
111 }
112 
113 template <class T>
114 T norm(Tensor<T> const& a)
115 {
116  T n = 0.0;
117  for (unsigned i=0; i < a.dim(); ++i)
118  for (unsigned j=0; j < a.dim(); ++j)
119  n += a(i,j)*a(i,j);
120  return std::sqrt(n);
121 }
122 
123 template <class T>
124 T det2x2(Tensor<T> const& a)
125 {
126  return a(0,0)*a(1,1) - a(1,0)*a(0,1);
127 }
128 
129 template <class T>
130 T det3x3(Tensor<T> const& a)
131 {
132  return
133  a(0,0) * (a(1,1)*a(2,2) - a(2,1)*a(1,2)) -
134  a(0,1) * (a(1,0)*a(2,2) - a(2,0)*a(1,2)) +
135  a(0,2) * (a(1,0)*a(2,1) - a(2,0)*a(1,1));
136 }
137 
138 template <class T>
140 {
141  T det = T(0.0);
142  switch(a.dim())
143  {
144  case 2:
145  det = det2x2(a);
146  break;
147  case 3:
148  det = det3x3(a);
149  break;
150  }
151  return det;
152 }
153 
154 template <class T>
155 void transpose(Tensor<T> const& a, Tensor<T>& r)
156 {
157  r.resize(a.dim());
158  for (unsigned i=0; i < a.dim(); ++i)
159  for (unsigned j=0; j < a.dim(); ++j)
160  r(j,i) = a(i,j);
161 }
162 
163 template <class T>
164 void inverse2x2(Tensor<T> const& a, Tensor<T>& r)
165 {
166  r(0,0) = a(1,1); r(0,1) = -a(0,1);
167  r(1,0) = -a(1,0); r(1,1) = a(0,0);
168  r /= determinant(a);
169 }
170 
171 template <class T>
172 void inverse3x3(Tensor<T> const& a, Tensor<T>& r)
173 {
174  r(0,0) = a(2,2)*a(1,1) - a(2,1)*a(1,2);
175  r(0,1) = a(2,1)*a(0,2) - a(2,2)*a(0,1);
176  r(0,2) = a(1,2)*a(0,1) - a(1,1)*a(0,2);
177  r(1,0) = a(2,0)*a(1,2) - a(2,2)*a(1,0);
178  r(1,1) = a(2,2)*a(0,0) - a(2,0)*a(0,2);
179  r(1,2) = a(1,0)*a(0,2) - a(1,2)*a(0,0);
180  r(2,0) = a(2,1)*a(1,0) - a(2,0)*a(1,1);
181  r(2,1) = a(2,0)*a(0,1) - a(2,1)*a(0,0);
182  r(2,2) = a(1,1)*a(0,0) - a(1,0)*a(0,1);
183  r /= determinant(a);
184 }
185 
186 template <class T>
187 void inverse(Tensor<T> const& a, Tensor<T>& r)
188 {
189  r.resize(a.dim());
190  switch(a.dim())
191  {
192  case 2:
193  inverse2x2(a, r);
194  break;
195  case 3:
196  inverse3x3(a, r);
197  break;
198  }
199 }
200 
201 template <class T>
202 void deviatoric(Tensor<T> const& a, Tensor<T>& r)
203 {
204  r = a;
205  T p = trace(a) / a.dim();
206  for (unsigned i=0; i < a.dim(); ++i)
207  r(i,i) -= p;
208 }
209 
210 template <class T>
211 Tensor<T> eye(unsigned d)
212 {
213  Tensor<T> r(d);
214  r.zero();
215  for (unsigned i=0; i < d; ++i)
216  r(i,i) = 1.0;
217  return r;
218 }
219 
220 template <class T, unsigned M, unsigned N>
221 void multiply(Matrix<T,M,N> const& a, Vector<T,N> const& b,
222  Vector<T,M>& c)
223 {
224  unsigned m = a.rows();
225  unsigned n = a.cols();
226  c.resize(m);
227  for (unsigned i = 0; i < m; ++i) {
228  c(i) = 0;
229  for (unsigned j = 0; j < n; ++j)
230  c(i) += a(i,j) * b(j);
231  }
232 }
233 
234 template <class T, unsigned M, unsigned N, unsigned O>
235 void multiply(Matrix<T,M,O> const& a, Matrix<T,O,N> const& b,
236  Matrix<T,M,N>& c)
237 {
238  unsigned m = a.rows();
239  unsigned n = b.cols();
240  unsigned o = a.cols();
241  c.resize(m, n);
242  for (unsigned i = 0; i < m; ++i)
243  for (unsigned j = 0; j < n; ++j) {
244  c(i,j) = 0;
245  for (unsigned l = 0; l < o; ++l)
246  c(i,j) += a(i,l) * b(l,j);
247  }
248 }
249 
250 }
251 
252 #endif
convenience wrapper over Matrix<T,3,3>
Definition: mthMatrix.h:195
compile-time (static) matrix
Definition: mthMatrix.h:24
void zero()
zero a matrix
Definition: mthMatrix.h:101
unsigned rows() const
get the number of rows
Definition: mthMatrix.h:33
unsigned cols() const
get the number of columns
Definition: mthMatrix.h:35
run-time (dynamic) tensor
Definition: mthTensor.h:29
void resize(unsigned d)
resize this tensor to dimension d
Definition: mthTensor.h:106
unsigned dim() const
get the dimension of this tensor
Definition: mthTensor.h:123
convenience wrapper over apf::Vector<3>
Definition: mthVector.h:196
templated math functions
All MTH functions are contained in this namespace.
Vector3< T > cross(Vector< T, 3 > const &a, Vector< T, 3 > const &b)
returns vector cross product
Definition: mth_def.h:25
Matrix< T, 2, 2 > inverse(Matrix< T, 2, 2 > const &a)
invert a 2 by 2 matrix
Definition: mth_def.h:84
double sqrt(double A)
wrapper for standard sqrt function
Definition: mthAD.h:588
Vector< T, N > reject(Vector< T, N > const &a, Vector< T, N > const &b)
vector rejection
Definition: mth_def.h:51
T trace(Tensor< T > const &a)
trace of a tensor
Definition: mth_def.h:104
T norm(Tensor< T > const &a)
Frobenius norm of a tensor.
Definition: mth_def.h:114
T determinant(Matrix< T, 2, 2 > const &a)
determinant of a 2 by 2 matrix
Definition: mth_def.h:69
void transpose(Matrix< T, M, N > const &a, Matrix< T, N, M > &b)
transpose of a static matrix
Definition: mth_def.h:57
Tensor< T > eye(unsigned d)
identity tensor
Definition: mth_def.h:211
Vector< T, N > project(Vector< T, N > const &a, Vector< T, N > const &b)
returns vector a projected onto vector b
Definition: mth_def.h:45