OpenKalman
make_triangular_matrix.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 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_MAKE_TRIANGULAR_MATRIX_HPP
17 #define OPENKALMAN_MAKE_TRIANGULAR_MATRIX_HPP
18 
19 namespace OpenKalman
20 {
26 #ifdef __cpp_concepts
27  template<TriangleType t = TriangleType::lower, indexible Arg> requires
29  constexpr triangular_matrix<t> decltype(auto)
30 #else
31  template<TriangleType t = TriangleType::lower, typename Arg, std::enable_if_t<indexible<Arg> and
32  (t == TriangleType::lower or t == TriangleType::upper or t == TriangleType::diagonal), int> = 0>
33  constexpr decltype(auto)
34 #endif
36  {
37  if constexpr (triangular_matrix<Arg, t>)
38  {
39  // If Arg is already triangular of type t, pass through to the result
40  return std::forward<Arg>(arg);
41  }
42  else if constexpr (t == TriangleType::diagonal)
43  {
44  return to_diagonal(diagonal_of(std::forward<Arg>(arg)));
45  }
46  else if constexpr (triangular_matrix<Arg, TriangleType::any> and not triangular_matrix<Arg, t>)
47  {
48  // Arg is the opposite triangle of t.
49  return make_triangular_matrix<TriangleType::diagonal>(std::forward<Arg>(arg));
50  }
51  else if constexpr (hermitian_adapter<Arg>)
52  {
53  if constexpr (hermitian_adapter<Arg, static_cast<HermitianAdapterType>(t)>)
54  return make_triangular_matrix<t>(nested_object(std::forward<Arg>(arg)));
55  else
56  return make_triangular_matrix<t>(adjoint(nested_object(std::forward<Arg>(arg))));
57  }
58  else if constexpr (interface::make_triangular_matrix_defined_for<Arg, t, Arg&&>)
59  {
61  return Traits::template make_triangular_matrix<t>(std::forward<Arg>(arg));
62  }
63  else // Default behavior if interface function not defined:
64  {
65  return TriangularAdapter<Arg, t> {std::forward<Arg>(arg)};
66  }
67  }
68 
69 } // namespace OpenKalman
70 
71 #endif //OPENKALMAN_MAKE_TRIANGULAR_MATRIX_HPP
constexpr bool hermitian_adapter
Specifies that a type is a hermitian matrix adapter of a particular type.
Definition: hermitian_adapter.hpp:54
A triangular_adapter, where components above or below the diagonal (or both) are zero.
Definition: forward-class-declarations.hpp:257
decltype(auto) constexpr to_diagonal(Arg &&arg)
Convert an indexible object into a diagonal matrix.
Definition: to_diagonal.hpp:32
An upper-right triangular matrix.
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
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
decltype(auto) constexpr adjoint(Arg &&arg)
Take the adjoint of a matrix.
Definition: adjoint.hpp:33
A diagonal matrix (both a lower-left and an upper-right triangular matrix).
decltype(auto) constexpr nested_object(Arg &&arg)
Retrieve a nested object of Arg, if it exists.
Definition: nested_object.hpp:34
A lower-left triangular matrix.
decltype(auto) constexpr make_triangular_matrix(Arg &&arg)
Create a triangular_matrix from a general matrix.
Definition: make_triangular_matrix.hpp:35