-
Notifications
You must be signed in to change notification settings - Fork 0
/
UsageExample.cpp
57 lines (46 loc) · 1.95 KB
/
UsageExample.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "Tensor.hpp"
#include "TensorOperations.hpp"
#include "TypeChecks.hpp"
#include <array>
#include <iostream>
int main() {
using TwoTensor = tensoralgebra::Tensor<2, double, 2>;
// By default a tensor is 3 dimensional (in applications this is the most
// common due to our world being 3 dimensional) and dataype double.
// Hence the following is the same as Tensor<1, double, 3>:
using OneTensor = tensoralgebra::Tensor<1>;
// Initialisation works with nested initialiser lists:
TwoTensor tensor = {{1., 2.}, {3., 4.}};
TwoTensor inverse_metric = 2.;
OneTensor vector = {1., 2., 3.};
// Evaluation is done lazily. For example, the following is _not_ evaluated:
auto unevaluated = 3 * tensor + 1 / exp(tensor) - sin(tensor);
// Expressions are evaluated when writing output:
std::cout << "Result of 3*tensor + 1/exp(tensor) - sin(tensor): "
<< unevaluated << ".\n";
// ... when assigning to another tensor
TwoTensor tensor1 = unevaluated;
// ... when applying as many indices as given by the rank:
auto evaluated = unevaluated[0][0];
// ... but not if the number of supplied indices is smaller than the rank
auto unevaluated1 = unevaluated[0];
// Basic tensor operations for differential geometry are supported
// e.g. the trace with respect to a given inverse metric
std::cout << "Trace: " << tensoralgebra::trace(tensor1, inverse_metric)
<< std::endl;
// ... the outer product
std::cout << "Outer: " << tensoralgebra::outer(vector, vector) << std::endl;
// ... or the dot product (with or without metric)
std::cout << "Dot: " << tensoralgebra::dot(vector, vector) << std::endl;
std::cout << "Dot: " << tensoralgebra::dot(tensor, inverse_metric)
<< std::endl;
// Tensors have an iterator for each dimension. This allows e.g. the use of
// range based for loops:
double sum = 0.;
for (auto &row : tensor) {
for (auto &element : row) {
sum += element;
}
}
return 0;
}