PhysicsBasedAnimationToolkit 0.0.10
Cross-platform C++20 library of algorithms and data structures commonly used in computer graphics research on physically-based simulation.
Loading...
Searching...
No Matches
Determinant.h
1#ifndef PBAT_MATH_LINALG_MINI_DETERMINANT_H
2#define PBAT_MATH_LINALG_MINI_DETERMINANT_H
3
4#include "Concepts.h"
5#include "pbat/HostDevice.h"
6
7#include <type_traits>
8
9namespace pbat {
10namespace math {
11namespace linalg {
12namespace mini {
13
14template <class TMatrix>
15PBAT_HOST_DEVICE auto Determinant(TMatrix&& A)
16{
17 using MatrixType = std::remove_cvref_t<TMatrix>;
18 PBAT_MINI_CHECK_CMATRIX(MatrixType);
19 static_assert(
20 MatrixType::kRows == MatrixType::kCols,
21 "Cannot compute determinant of non-square matrix");
22 static_assert(MatrixType::kRows < 4, "Determinant of matrix of dimensions >= 4 too costly");
23 if constexpr (MatrixType::kRows == 1)
24 {
25 return A(0, 0);
26 }
27 else if constexpr (MatrixType::kRows == 2)
28 {
29 return A(0, 0) * A(1, 1) - A(0, 1) * A(1, 0);
30 }
31 else if constexpr (MatrixType::kRows == 3)
32 {
33 return A(0, 0) * (A(1, 1) * A(2, 2) - A(2, 1) * A(1, 2)) -
34 A(0, 1) * (A(1, 0) * A(2, 2) - A(2, 0) * A(1, 2)) +
35 A(0, 2) * (A(1, 0) * A(2, 1) - A(2, 0) * A(1, 1));
36 }
37 else
38 {
39 using ScalarType = typename MatrixType::ScalarType;
40 return ScalarType{0.};
41 }
42}
43
44} // namespace mini
45} // namespace linalg
46} // namespace math
47} // namespace pbat
48
49#endif // PBAT_MATH_LINALG_MINI_DETERMINANT_H
Mini linear algebra related functionality.
Definition Assign.h:12
Linear Algebra related functionality.
Definition FilterEigenvalues.h:7
Math related functionality.
Definition Concepts.h:19
The main namespace of the library.
Definition Aliases.h:15