OpenKalman
transpose.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-2026 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_TRANSPOSE_HPP
17 #define OPENKALMAN_TRANSPOSE_HPP
18 
19 #include "patterns/patterns.hpp"
29 #include "linear-algebra/functions/internal/make_wrapped_mdspan.hpp"
32 
33 namespace OpenKalman
34 {
41 #ifdef __cpp_concepts
42  template<std::size_t indexa = 0, std::size_t indexb = 1, indexible Arg> requires (indexa < indexb)
43  constexpr indexible decltype(auto)
44 #else
45  template<std::size_t indexa = 0, std::size_t indexb = 1, typename Arg, std::enable_if_t<
46  indexible<Arg> and (indexa < indexb), int> = 0>
47  constexpr decltype(auto)
48 #endif
49  transpose(Arg&& arg)
50  {
51  using P = decltype(get_pattern_collection(arg));
52  if constexpr (
54  (constant_object<Arg> or
55  (indexb == 1 and
56  (diagonal_matrix<Arg> or
57  (hermitian_matrix<Arg> and not values::complex<element_type_of_t<Arg>>)))))
58  {
59  return std::forward<Arg>(arg);
60  }
61  else if constexpr (interface::transpose_defined_for<Arg&&, indexa, indexb>)
62  {
63  return interface::library_interface<stdex::remove_cvref_t<Arg>>::template transpose<indexa, indexb>(std::forward<Arg>(arg));
64  }
65  else if constexpr (constant_object<Arg>)
66  {
67  return make_constant(constant_value(arg), patterns::views::transpose<indexa, indexb>(get_pattern_collection(arg)));
68  }
69  else if constexpr (hermitian_matrix<Arg>)
70  {
71  return conjugate(std::forward<Arg>(arg));
72  }
73  else
74  {
75  auto p = patterns::views::transpose<indexa, indexb>(get_pattern_collection(arg));
76  auto n = get_mdspan(arg);
77  using nested_layout = typename std::decay_t<decltype(n)>::layout_type;
78  using extents_type = std::decay_t<decltype(to_extents(p))>;
79  if constexpr (indexb == 1 and n.rank() == 2)
80  {
81  using layout_type = stdex::linalg::layout_transpose<nested_layout>;
82  using mapping_type = typename layout_type::template mapping<extents_type>;
83  return internal::make_wrapped_mdspan(
84  std::forward<Arg>(arg),
86  mapping_type(n.mapping()),
87  n.accessor(),
88  std::move(p));
89  }
90  else
91  {
92  using nested_mapping_type = typename std::decay_t<decltype(n)>::mapping_type;
94  using mapping_type = typename layout_type::template mapping<extents_type>;
95  return internal::make_wrapped_mdspan(
96  std::forward<Arg>(arg),
98  mapping_type(n.mapping(), to_extents(p)),
99  n.accessor(),
100  std::move(p));
101  }
102  }
103  }
104 
105 
106 }
107 
108 #endif
Definition for constant_value.
Definition: language-features.hpp:228
Definition for constant_object.
Definition: transpose_mdspan_policies.hpp:29
decltype(auto) constexpr conjugate(Arg &&arg)
Take the complex conjugate of an indexible object.
Definition: conjugate.hpp:44
constexpr bool compares_with
Compares two patterns::pattern objects.
Definition: compares_with.hpp:472
constexpr bool complex
T is a value that reduces to std::complex or a custom complex type.
Definition: complex.hpp:47
constexpr bool indexible
T is a multidimensional array type.
Definition: indexible.hpp:32
decltype(auto) constexpr get_pattern_collection(T &&t)
Get the patterns::pattern_collection associated with indexible object T.
Definition: get_pattern_collection.hpp:36
Definition for diagonal_matrix.
typename pattern_collection_element< i, T >::type pattern_collection_element_t
Helper template for collection_element.
Definition: pattern_collection_element.hpp:58
Definition for element_type_of.
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:42
decltype(auto) constexpr transpose(Arg &&arg)
Swap any two indices of an indexible_object.
Definition: transpose.hpp:49
Definition of get_mdspan function.
constexpr auto make_constant(C c, P &&p)
Make an indexible object in which every element is a constant value.
Definition: make_constant.hpp:41
constexpr auto constant_value(T &&t)
The constant value associated with a constant_object or constant_diagonal_object. ...
Definition: constant_value.hpp:37
Definition of get_pattern_collection function.
Definition for hermitian_matrix.
Definition for indexible.
Definition of conjugate function.
typename element_type_of< T >::type element_type_of_t
helper template for element_type_of.
Definition: element_type_of.hpp:54
Definitions for make_constant.
constexpr auto to_extents(P &&p)
Convert a pattern_collection to std::extents.
Definition: to_extents.hpp:78
decltype(auto) constexpr get_mdspan(T &&t)
Get the mdspan associated with indexible object T.
Definition: get_mdspan.hpp:35