OpenKalman
mdspan-library.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) 2025 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_INTERFACES_MDSPAN_LIBRARY_HPP
17 #define OPENKALMAN_INTERFACES_MDSPAN_LIBRARY_HPP
18 
19 #include "basics/basics.hpp"
20 #include "mdspan-object.hpp"
22 
23 namespace OpenKalman::interface
24 {
29  template<typename T, typename IndexType, std::size_t...Extents, typename LayoutPolicy, typename AccessorPolicy>
30  struct library_interface<stdex::mdspan<T, stdex::extents<IndexType, Extents...>, LayoutPolicy, AccessorPolicy>>
31  {
32  private:
33 
34  using M = stdex::mdspan<T, stdex::extents<IndexType, Extents...>, LayoutPolicy, AccessorPolicy>;
35 
36  using extents = stdex::extents<IndexType, Extents...>;
37 
38  static constexpr std::size_t rank = extents::rank();
39  static constexpr std::size_t rank_dynamic = extents::rank_dynamic();
40 
41  public:
42 
43  static constexpr auto
44  conjugate = [](auto&& m)
45  {
46  return stdex::linalg::conjugated(std::forward<decltype(m)>(m));
47  };
48 
49 
50  static constexpr auto
51 #ifdef __cpp_concepts
52  transpose = [](auto&& m) -> decltype(auto) requires (rank <= 2)
53 #else
54  transpose = [](auto&& m, std::enable_if_t<std::decay_t<decltype(m)>::rank() <= 2, int> = 0) -> decltype(auto)
55 #endif
56  {
57  if constexpr (rank == 2)
58  return stdex::linalg::transposed(std::forward<decltype(m)>(m));
59  else if constexpr (rank == 1 and rank_dynamic == 0)
60  return stdex::extents<IndexType, 1_uz, Extents...> {};
61  else if constexpr (rank == 1)
62  return stdex::extents<IndexType, 1_uz, Extents...> {m.extent(0)};
63  else // if constexpr (rank == 0)
64  return std::forward<decltype(m)>(m);
65  };
66 
67 
68  static constexpr auto
69 #ifdef __cpp_concepts
70  adjoint = [](auto&& m) -> decltype(auto) requires (rank <= 2)
71 #else
72  adjoint = [](auto&& m, std::enable_if_t<std::decay_t<decltype(m)>::rank() <= 2, int> = 0) -> decltype(auto)
73 #endif
74  {
75  if constexpr (rank == 2)
76  return stdex::linalg::conjugate_transposed(std::forward<decltype(m)>(m));
77  else
78  return conjugate(transpose(std::forward<decltype(m)>(m)));
79  };
80 
81  };
82 
83 }
84 
85 
86 #endif
Definition: basics.hpp:41
decltype(auto) constexpr conjugate(Arg &&arg)
Take the complex conjugate of an indexible object.
Definition: conjugate.hpp:44
Definition: mdspan.hpp:34
An interface to various routines from the linear algebra library associated with indexible object T...
Definition: library_interface.hpp:42
Forward declaration of library_interface, which must be defined for all objects used in OpenKalman...
decltype(auto) constexpr transpose(Arg &&arg)
Swap any two indices of an indexible_object.
Definition: transpose.hpp:163
Definition of object_traits for std::mdspan.
decltype(auto) constexpr adjoint(Arg &&arg)
Take the conjugate-transpose of an indexible_object.
Definition: adjoint.hpp:35
Basic definitions for OpenKalman as a whole.
Definition: extents.hpp:372