SPH
Tensor.h
Go to the documentation of this file.
1 #pragma once
2 
7 
9 
11 
13 class Tensor {
14 private:
15  Vector v[3]; // rows
16 
17 public:
18  Tensor() = default;
19 
20  Tensor(const Float value)
21  : v{ Vector(value), Vector(value), Vector(value) } {}
22 
24  Tensor(const Vector& v1, const Vector& v2, const Vector& v3)
25  : v{ v1, v2, v3 } {}
26 
29  INLINE Float& operator()(const Size i, const Size j) {
30  SPH_ASSERT(i < 3 && j < 3, i, j);
31  return v[i][j];
32  }
33 
34  INLINE Float operator()(const Size i, const Size j) const {
35  SPH_ASSERT(i < 3 && j < 3, i, j);
36  return v[i][j];
37  }
38 
39  INLINE Vector column(const Size idx) const {
40  SPH_ASSERT(idx < 3, idx);
41  return Vector(v[0][idx], v[1][idx], v[2][idx]);
42  }
43 
44  INLINE Vector row(const Size idx) const {
45  SPH_ASSERT(idx < 3, idx);
46  return v[idx];
47  }
48 
50  return Tensor(column(0), column(1), column(2));
51  } /*
52 
53  INLINE Float determinant() const {
54  return v[0][0] * (v[1][1] * v[2][2] - v[2][1] * v[1][2]) -
55  v[0][1] * (v[1][0] * v[2][2] - v[1][2] * v[2][0]) +
56  v[0][2] * (v[1][0] * v[2][1] - v[1][1] * v[2][0]);
57  }
58 
59  Tensor inverse() const {
60  const Float det = this->determinant();
61  SPH_ASSERT(det != 0._f);
62  const Float invdet = 1._f / det;
63 
64  Tensor inv;
65  inv(0, 0) = (v[1][1] * v[2][2] - v[2][1] * v[1][2]) * invdet;
66  inv(0, 1) = (v[0][2] * v[2][1] - v[0][1] * v[2][2]) * invdet;
67  inv(0, 2) = (v[0][1] * v[1][2] - v[0][2] * v[1][1]) * invdet;
68  inv(1, 0) = (v[1][2] * v[2][0] - v[1][0] * v[2][2]) * invdet;
69  inv(1, 1) = (v[0][0] * v[2][2] - v[0][2] * v[2][0]) * invdet;
70  inv(1, 2) = (v[1][0] * v[0][2] - v[0][0] * v[1][2]) * invdet;
71  inv(2, 0) = (v[1][0] * v[2][1] - v[2][0] * v[1][1]) * invdet;
72  inv(2, 1) = (v[2][0] * v[0][1] - v[0][0] * v[2][1]) * invdet;
73  inv(2, 2) = (v[0][0] * v[1][1] - v[1][0] * v[0][1]) * invdet;
74  return inv;
75  }*/
76 
77  static Tensor null() {
78  return Tensor(0._f);
79  }
80 
81  static Tensor identity() {
82  return Tensor(Vector(1._f, 0._f, 0._f), Vector(0._f, 1._f, 0._f), Vector(0._f, 0._f, 1._f));
83  }
84 
85 
86  INLINE Tensor operator+(const Tensor& other) const {
87  return Tensor(v[0] + other.v[0], v[1] + other.v[1], v[2] + other.v[2]);
88  }
89 
90  INLINE Tensor operator-(const Tensor& other) const {
91  return Tensor(v[0] - other.v[0], v[1] - other.v[1], v[2] - other.v[2]);
92  }
93 
95  INLINE Tensor operator*(const Tensor& other) const {
96  Tensor result;
97  for (Size i = 0; i < 3; ++i) {
98  for (Size j = 0; j < 3; ++j) {
99  result(i, j) = dot(this->row(i), other.column(j));
100  }
101  }
102  return result;
103  }
104 
105  INLINE Vector operator*(const Vector& u) const {
106  return Vector(dot(v[0], u), dot(v[1], u), dot(v[2], u));
107  }
108 
109  INLINE friend Tensor operator*(const Tensor& t, const Float v) {
110  return Tensor(t.row(0) * v, t.row(1) * v, t.row(2) * v);
111  }
112 
113  INLINE friend Tensor operator*(const Float v, const Tensor& t) {
114  return t * v;
115  }
116 
117  INLINE Tensor& operator+=(const Tensor& other) {
118  v[0] += other.v[0];
119  v[1] += other.v[1];
120  v[2] += other.v[2];
121  return *this;
122  }
123 
124  INLINE Tensor& operator-=(const Tensor& other) {
125  v[0] -= other.v[0];
126  v[1] -= other.v[1];
127  v[2] -= other.v[2];
128  return *this;
129  }
130 
132  INLINE Tensor& operator*=(const Float value) {
133  v[0] *= value;
134  v[1] *= value;
135  v[2] *= value;
136  return *this;
137  }
138 
139  INLINE Tensor& operator/=(const Float value) {
140  SPH_ASSERT(value != 0._f);
141  v[0] /= value;
142  v[1] /= value;
143  v[2] /= value;
144  return *this;
145  }
146 
147  INLINE bool operator==(const Tensor& other) const {
148  return v[0] == other.v[0] && v[1] == other.v[1] && v[2] == other.v[2];
149  }
150 
151  INLINE bool operator!=(const Tensor& other) const {
152  return !(*this == other);
153  }
154 
155  friend std::ostream& operator<<(std::ostream& stream, const Tensor& t) {
156  stream << t.row(0) << std::endl << t.row(1) << std::endl << t.row(2);
157  return stream;
158  }
159 };
160 
161 template <>
163  return AffineMatrix(t.row(0), t.row(1), t.row(2));
164 }
165 
166 template <>
168  return Tensor(t.row(0), t.row(1), t.row(2));
169 }
170 
171 template <>
173  return Tensor(t.row(0), t.row(1), t.row(2));
174 }
175 
178  return SymmetricTensor(Vector(t(0, 0), t(1, 1), t(2, 2)),
179  0.5_f * Vector(t(0, 1) + t(1, 0), t(0, 2) + t(2, 0), t(1, 2) + t(2, 1)));
180 }
181 
183 INLINE Tensor outer(const Vector& r1, const Vector& r2) {
184  return Tensor(r1[0] * r2, r1[1] * r2, r1[2] * r2);
185 }
186 
188 INLINE bool almostEqual(const Tensor& t1, const Tensor& t2, const Float eps = EPS) {
189  return almostEqual(t1.row(0), t2.row(0), eps) && almostEqual(t1.row(1), t2.row(1), eps) &&
190  almostEqual(t1.row(2), t2.row(2), eps);
191 }
192 
195 template <>
196 INLINE Float norm(const Tensor& t) {
197  const Vector v = max(t.row(0), t.row(1), t.row(2));
198  SPH_ASSERT(isReal(v));
199  return norm(v);
200 }
201 
203 template <>
205  const Vector v = max(t.row(0), t.row(1), t.row(2));
206  return normSqr(v);
207 }
208 
210 template <>
211 INLINE auto abs(const Tensor& t) {
212  return Tensor(abs(t.row(0)), abs(t.row(1)), abs(t.row(2)));
213 }
214 
216 template <>
218  return min(minElement(t.row(0)), minElement(t.row(1)), minElement(t.row(2)));
219 }
220 
222 template <>
224  return max(maxElement(t.row(0)), maxElement(t.row(1)), maxElement(t.row(2)));
225 }
226 
228 template <>
229 INLINE Tensor min(const Tensor& t1, const Tensor& t2) {
230  return Tensor(min(t1.row(0), t2.row(0)), min(t1.row(1), t2.row(1)), min(t1.row(2), t2.row(2)));
231 }
232 
234 template <>
235 INLINE Tensor max(const Tensor& t1, const Tensor& t2) {
236  return Tensor(max(t1.row(0), t2.row(0)), max(t1.row(1), t2.row(1)), max(t1.row(2), t2.row(2)));
237 }
238 
240 template <>
241 INLINE Tensor clamp(const Tensor& t, const Interval& range) {
242  return Tensor(clamp(t.row(0), range), clamp(t.row(1), range), clamp(t.row(2), range));
243 }
244 
245 template <>
246 INLINE bool isReal(const Tensor& t) {
247  return isReal(t.row(0)) && isReal(t.row(1)) && isReal(t.row(2));
248 }
249 
250 template <>
253 }
254 
255 
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
#define NOT_IMPLEMENTED
Helper macro marking missing implementation.
Definition: Assert.h:100
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
double Float
Precision used withing the code. Use Float instead of float or double where precision is important.
Definition: Globals.h:13
constexpr Float EPS
Definition: MathUtils.h:30
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
Basic algebra for symmetric 2nd order tensors.
INLINE SymmetricTensor symmetrize(const Tensor &t)
Computes a symmetrized tensor 1/2*(t + t^T)
Definition: Tensor.h:177
INLINE AffineMatrix convert(const Tensor &t)
Definition: Tensor.h:162
INLINE Tensor outer(const Vector &r1, const Vector &r2)
Outer product.
Definition: Tensor.h:183
INLINE Float maxElement(const Tensor &t)
Returns the maximal element of the tensor.
Definition: Tensor.h:223
INLINE bool isReal(const Tensor &t)
Definition: Tensor.h:246
INLINE Tensor max(const Tensor &t1, const Tensor &t2)
Component-wise maximum of two tensors.
Definition: Tensor.h:235
INLINE auto abs(const Tensor &t)
Returns the tensor of absolute values.
Definition: Tensor.h:211
INLINE Float norm(const Tensor &t)
Definition: Tensor.h:196
INLINE Tensor clamp(const Tensor &t, const Interval &range)
Clamping all components by range.
Definition: Tensor.h:241
INLINE bool almostEqual(const Tensor &t1, const Tensor &t2, const Float eps=EPS)
Checks if two tensors are equal to some given accuracy.
Definition: Tensor.h:188
INLINE Float minElement(const Tensor &t)
Returns the minimal element of the tensor.
Definition: Tensor.h:217
INLINE Tensor min(const Tensor &t1, const Tensor &t2)
Component-wise minimum of two tensors.
Definition: Tensor.h:229
INLINE Float normSqr(const Tensor &t)
Arbitrary squared norm of the tensor.
Definition: Tensor.h:204
INLINE StaticArray< Float, 6 > getComponents(const Tensor &)
Definition: Tensor.h:251
BasicVector< Float > Vector
Definition: Vector.h:539
INLINE float dot(const BasicVector< float > &v1, const BasicVector< float > &v2)
Make sure the vector is trivially constructible and destructible, needed for fast initialization of a...
Definition: Vector.h:548
INLINE Vector row(const Size idx) const
Definition: AffineMatrix.h:44
Object representing a 1D interval of real numbers.
Definition: Interval.h:17
Array with fixed number of allocated elements.
Definition: StaticArray.h:19
Symmetric tensor of 2nd order.
INLINE Vector row(const Size idx) const
Returns a row of the matrix.
Generic 2nd-order tensor with 9 independent components.
Definition: Tensor.h:13
INLINE friend Tensor operator*(const Tensor &t, const Float v)
Definition: Tensor.h:109
INLINE Tensor operator-(const Tensor &other) const
Definition: Tensor.h:90
Tensor()=default
INLINE Tensor & operator+=(const Tensor &other)
Definition: Tensor.h:117
INLINE Tensor & operator*=(const Float value)
Definition: Tensor.h:132
INLINE Vector row(const Size idx) const
Definition: Tensor.h:44
static Tensor identity()
Definition: Tensor.h:81
INLINE Vector operator*(const Vector &u) const
Definition: Tensor.h:105
INLINE Float & operator()(const Size i, const Size j)
Definition: Tensor.h:29
Tensor(const Vector &v1, const Vector &v2, const Vector &v3)
Construct the matrix from vectors as rows.
Definition: Tensor.h:24
INLINE friend Tensor operator*(const Float v, const Tensor &t)
Definition: Tensor.h:113
INLINE Tensor transpose() const
Definition: Tensor.h:49
INLINE Vector column(const Size idx) const
Definition: Tensor.h:39
INLINE Tensor operator*(const Tensor &other) const
Matrix multiplication.
Definition: Tensor.h:95
INLINE Float operator()(const Size i, const Size j) const
Definition: Tensor.h:34
INLINE Tensor operator+(const Tensor &other) const
Definition: Tensor.h:86
INLINE bool operator!=(const Tensor &other) const
Definition: Tensor.h:151
INLINE Tensor & operator-=(const Tensor &other)
Definition: Tensor.h:124
INLINE Tensor & operator/=(const Float value)
Definition: Tensor.h:139
Tensor(const Float value)
Definition: Tensor.h:20
INLINE bool operator==(const Tensor &other) const
Definition: Tensor.h:147
friend std::ostream & operator<<(std::ostream &stream, const Tensor &t)
Definition: Tensor.h:155