Expression Templates Library (ETL)
det.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2014-2023 Baptiste Wicht
3 // Distributed under the terms of the MIT License.
4 // (See accompanying file LICENSE or copy at
5 // http://opensource.org/licenses/MIT)
6 //=======================================================================
7 
13 #pragma once
14 
15 namespace etl {
16 
20 template <etl_expr AT, etl_expr LT, etl_expr UT, etl_expr PT>
21 bool lu(const AT& A, LT& L, UT& U, PT& P);
22 
23 namespace impl {
24 
25 namespace standard {
26 
31 template <etl_expr AT>
32 value_t<AT> det(const AT& A) {
33  using T = value_t<AT>;
34 
35  const auto n = etl::dim<0>(A);
36 
37  if (is_permutation_matrix(A)) {
38  size_t t = 0;
39 
40  for (size_t i = 0; i < n; ++i) {
41  for (size_t j = 0; j < n; ++j) {
42  if (A(i, j) != 0.0 && i != j) {
43  ++t;
44  }
45  }
46  }
47 
48  return std::pow(T(-1.0), t - 1);
49  }
50 
51  if (is_triangular(A)) {
52  T det(1.0);
53 
54  for (size_t i = 0; i < n; ++i) {
55  det *= A(i, i);
56  }
57 
58  return det;
59  }
60 
61  auto L = force_temporary_dim_only(A);
62  auto U = force_temporary_dim_only(A);
63  auto P = force_temporary_dim_only(A);
64 
65  etl::lu(A, L, U, P);
66 
67  return det(L) * det(U) * det(P);
68 }
69 
70 } //end of namespace standard
71 } //end of namespace impl
72 } //end of namespace etl
auto pow(E &&value, T v)
Apply pow(x, v) on each element x of the ETL expression.
Definition: expression_builder.hpp:127
bool is_triangular(E &&expr)
Indicates if the given expression is a triangular matrix or not.
Definition: adapters.hpp:308
value_t< AT > det(const AT &A)
Compute the determinant of the given matrix.
Definition: det.hpp:32
bool is_permutation_matrix(E &&expr)
Indicates if the given expression represents a permutation matrix.
Definition: globals.hpp:112
Root namespace for the ETL library.
Definition: adapter.hpp:15
bool lu(const AT &A, LT &L, UT &U, PT &P)
Decomposition the matrix so that P * A = L * U.
Definition: globals.hpp:308
typename decay_traits< E >::value_type value_t
Traits to extract the value type out of an ETL type.
Definition: tmp.hpp:81
decltype(auto) force_temporary_dim_only(E &&expr)
Force a temporary out of the expression with the same dimensions, but the content is not defined...
Definition: temporary.hpp:156