22namespace VectorizedTensor
25constexpr int size(
int const displacement_dim)
27 if (displacement_dim == 1)
31 if (displacement_dim == 2)
35 if (displacement_dim == 3)
40 "Cannot convert displacement dimension {} to vectorized tensor size.",
45template <
typename VectorizedTensor>
48 static_assert(VectorizedTensor::ColsAtCompileTime == 1);
49 constexpr int rows = VectorizedTensor::RowsAtCompileTime;
63 "Cannot convert vectorized tensor of size {} to displacement "
71template <
int DisplacementDim>
72using Type = Eigen::Matrix<double,
size(DisplacementDim), 1, Eigen::ColMajor>;
76template <
int DisplacementDim>
80 0 < DisplacementDim && DisplacementDim <= 3,
81 "Identity is implemented only for displacement dimension 1, 2, or 3.");
83 size(DisplacementDim), 1,
84 [](Eigen::Index
const row, [[maybe_unused]] Eigen::Index
const col)
87 if constexpr (DisplacementDim == 1)
91 if constexpr (DisplacementDim == 2)
93 if (row == 0 || row == 3 || row == 4)
98 if constexpr (DisplacementDim == 3)
100 if (row == 0 || row == 4 || row == 8)
110template <
typename Derived>
114 static_assert(0 < displacement_dim && displacement_dim <= 3,
115 "Vectorized tensor determinant is implemented only for "
116 "displacement dimension 1, 2, or 3.");
118 if constexpr (displacement_dim == 1)
120 return tensor[0] * tensor[1] * tensor[2];
122 if constexpr (displacement_dim == 2)
124 Eigen::Map<Eigen::Matrix2d const>
const top_left{
125 tensor.derived().data()};
127 return top_left.determinant() * tensor[4];
129 if constexpr (displacement_dim == 3)
131 return Eigen::Map<Eigen::Matrix3d const>(tensor.derived().data())
151template <
int DisplacementDim,
typename Derived>
154 static_assert(0 < DisplacementDim && DisplacementDim <= 3,
155 "Conversion to displacement dimension other than 1, 2, or 3 "
158 constexpr int rows = Derived::RowsAtCompileTime;
159 constexpr int cols = Derived::ColsAtCompileTime;
160 static_assert(rows == 3 || rows == Eigen::Dynamic);
161 static_assert(cols == 3 || cols == Eigen::Dynamic);
162 if (tensor.rows() != 3 || tensor.cols() != 3)
165 "Incorrect tensor size, must be 3x3, but tensor is {:d}x{:d}.",
166 tensor.rows(), tensor.cols());
169 if constexpr (DisplacementDim == 1)
174 "Cannot convert a tensor with non-zero off-diagonal elements "
175 "to a 1d vectorized tensor representation.");
177 return tensor.diagonal();
179 if constexpr (DisplacementDim == 2)
184 "Cannot convert a tensor with non-zero elements at (0, 2), (1, "
185 "2), (2, 0), and (2, 1) positions to a 2d vectorized tensor "
189 result.template head<4>() =
190 tensor.template block<2, 2>(0, 0).reshaped();
191 result(4) = tensor(2, 2);
194 if constexpr (DisplacementDim == 3)
196 return tensor.reshaped();
199 "Not all cases handled in the VectorizedTensor::toVector() function.");
203template <
int DisplacementDim>
207 DisplacementDim == 1 || DisplacementDim == 2 || DisplacementDim == 3,
208 "Conversion from displacement dimension other than 1, 2, or 3 "
211 using Matrix = Eigen::Matrix3d;
212 if constexpr (DisplacementDim == 1)
214 return tensor.asDiagonal();
216 if constexpr (DisplacementDim == 2)
218 Matrix m = Matrix::Zero();
219 m.template block<2, 2>(0, 0) =
220 Eigen::Map<Eigen::Matrix<double, 2, 2>
const>(tensor.data());
224 if constexpr (DisplacementDim == 3)
226 return Eigen::Map<Matrix const>(tensor.data());
constexpr int dimension()
Displacement dimension of a vectorized tensor.
bool isTensorConvertibleTo2d(Eigen::Matrix3d const &tensor)
double determinant(Eigen::MatrixBase< Derived > const &tensor)
Computes determinant of a vectorized tensor.
constexpr int size(int const displacement_dim)
Vectorized tensor size for given displacement dimension.
bool isTensorConvertibleTo1d(Eigen::Matrix3d const &tensor)
Only a diagonal tensor can be converted to a 1d vectorized tensor.
Eigen::Matrix< double, size(DisplacementDim), 1, Eigen::ColMajor > Type
constexpr auto identity()
Eigen::Matrix3d toTensor(Type< DisplacementDim > const &tensor)
Converts a vectorized tensor to a 3x3 matrix.
Type< DisplacementDim > toVector(Eigen::MatrixBase< Derived > const &tensor)