tensor2.h
1 KB
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
#ifndef STIM_TENSOR2_H
#define STIM_TENSOR2_H
#include "matrix_sym.h"
namespace stim {
/*This class represents a symmetric rank-2 2D tensor, useful for structure tensors
*/
template<typename T>
class tensor2 : public matrix_sym<T, 2> {
protected:
public:
//calculate the eigenvectors and eigenvalues of the tensor
CUDA_CALLABLE void eig(stim::matrix<T, 2>& v, stim::matrix<T, 2>& lambda) {
lambda = 0; //initialize the eigenvalue matrix to zero
T t = M[0] + M[2]; //calculate the trace of the tensor
T d = M[0] * M[2] - M[1] * M[1]; //calculate the determinant of the tensor
lambda(0, 0) = t / 2 + sqrt(t*t / 4 - d);
lambda(1, 1) = t / 2 - sqrt(t*t / 4 - d);
if (M[1] == 0) {
v = stim::matrix<T, 2>::identity();
}
else {
v(0, 0) = lambda(0, 0) - d;
v(0, 1) = lambda(1, 1) - d;
v(1, 0) = v(1, 1) = M[1];
}
}
CUDA_CALLABLE tensor2<T> operator=(stim::matrix_sym<T, 2> rhs){
stim::matrix_sym<T, 2>::operator=(rhs);
return *this;
}
};
} //end namespace stim
#endif