OpenKalman
determinant.hpp
Go to the documentation of this file.
1 /* This file is part of OpenKalman, a header-only C++ library for
2  * Kalman filters and other recursive filters.
3  *
4  * Copyright (c) 2022-2023 Christopher Lee Ogden <ogden@gatech.edu>
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
9  */
10 
16 #ifndef OPENKALMAN_DETERMINANT_HPP
17 #define OPENKALMAN_DETERMINANT_HPP
18 
19 
20 namespace OpenKalman
21 {
22  namespace detail
23  {
24  template<typename Arg>
25  inline void error_if_argument_to_determinant_is_not_square(const Arg& arg)
26  {
27  if constexpr (has_dynamic_dimensions<Arg>) if (not is_square_shaped(arg))
28  throw std::domain_error {"Argument to 'determinant' is not a square matrix"};
29  }
30  } // namespace detail
31 
32 
37 #ifdef __cpp_concepts
38  template<square_shaped<Applicability::permitted> Arg> requires (max_tensor_order_v<Arg> <= 2)
39  constexpr std::convertible_to<scalar_type_of_t<Arg>> auto
40 #else
41  template<typename Arg, std::enable_if_t<square_shaped<Arg, Applicability::permitted> and (max_tensor_order_v<Arg> <= 2), int> = 0>
42  constexpr auto
43 #endif
44  determinant(Arg&& arg)
45  {
46  using Scalar = scalar_type_of_t<Arg>;
47  constexpr auto ix = []{ if constexpr (dynamic_dimension<Arg, 0>) return 1; else return 0; }();
48 
49  if constexpr (identity_matrix<Arg> or empty_object<Arg>)
50  {
51  detail::error_if_argument_to_determinant_is_not_square(arg);
52  return values::Fixed<Scalar, 1>{};
53  }
54  else if constexpr (dimension_size_of_index_is<Arg, 0, 1> or dimension_size_of_index_is<Arg, 1, 1>)
55  {
56  // At least one of the dimensions is 1.
57  detail::error_if_argument_to_determinant_is_not_square(arg);
58  return internal::get_singular_component(std::forward<Arg>(arg));
59  }
60  else if constexpr (constant_matrix<Arg> and not dynamic_dimension<Arg, ix> and index_dimension_of_v<Arg, ix> >= 2)
61  {
62  detail::error_if_argument_to_determinant_is_not_square(arg);
63  return values::Fixed<Scalar, 0>{};
64  }
65  else if constexpr (constant_diagonal_matrix<Arg>)
66  {
67  detail::error_if_argument_to_determinant_is_not_square(arg);
68  return values::pow(constant_diagonal_coefficient{arg}, values::cast_to<Scalar>(get_index_dimension_of<ix>(arg)))();
69  }
70  else if constexpr (triangular_matrix<Arg>) // Includes the diagonal case.
71  {
72  detail::error_if_argument_to_determinant_is_not_square(arg);
73  return reduce(std::multiplies<Scalar>{}, diagonal_of(std::forward<Arg>(arg)));
74  }
75  else if constexpr (constant_matrix<Arg>) // Arg could be empty or 1D at runtime, so we need to check.
76  {
77  auto d = is_square_shaped(arg);
78  if (not d) throw std::invalid_argument{"Argument of 'determinant' is not a square matrix."};
79  else if (get_dimension(*d) >= 2) return static_cast<Scalar>(0);
80  else if (get_dimension(*d) == 1) return static_cast<Scalar>(internal::get_singular_component(std::forward<Arg>(arg))); // 1D matrix
81  else return static_cast<Scalar>(1); // empty matrix
82  }
83  else
84  {
86  }
87  }
88 
89 } // namespace OpenKalman
90 
91 #endif //OPENKALMAN_DETERMINANT_HPP
typename scalar_type_of< T >::type scalar_type_of_t
helper template for scalar_type_of.
Definition: scalar_type_of.hpp:54
constexpr auto is_square_shaped(const T &t)
Determine whether an object is square_shaped at runtime.
Definition: is_square_shaped.hpp:63
Definition: tuple_reverse.hpp:103
decltype(auto) constexpr reduce(BinaryFunction &&b, Arg &&arg)
Perform a partial reduction based on an associative binary function, across one or more indices...
Definition: reduce.hpp:143
The root namespace for OpenKalman.
Definition: basics.hpp:34
An interface to various routines from the linear algebra library associated with indexible object T...
Definition: library_interface.hpp:37
The constant associated with T, assuming T is a constant_diagonal_matrix.
Definition: constant_diagonal_coefficient.hpp:32
constexpr auto determinant(Arg &&arg)
Take the determinant of a matrix.
Definition: determinant.hpp:44
decltype(auto) constexpr diagonal_of(Arg &&arg)
Extract a column vector (or column slice for rank>2 tensors) comprising the diagonal elements...
Definition: diagonal_of.hpp:33
Definition: Fixed.hpp:36