OpenKalman
library_interface.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-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_LIBRARY_INTERFACE_HPP
17 #define OPENKALMAN_LIBRARY_INTERFACE_HPP
18 
19 #ifdef DOXYGEN_SHOULD_SKIP_THIS
20 #include "patterns/patterns.hpp"
25 #endif
26 
27 namespace OpenKalman::interface
28 {
37 #ifdef __cpp_concepts
38  template<typename T>
39 #else
40  template<typename T, typename = void>
41 #endif
43  {
44 #ifdef DOXYGEN_SHOULD_SKIP_THIS
45 
53  template<typename Derived>
54  using library_base = std::monostate;
55 
56 
63  static constexpr auto
64  copy_from = [](T& t, indexible auto&& other) -> void
65  {
66  // ...
67  };
68 
69 
74  static constexpr auto
75  to_diagonal = []<typename Arg> requires std::same_as<std::remove_cvref_t<Arg>, T>
76  (Arg&& arg) -> diagonal_matrix auto
77  {
78  // ...
79  };
80 
81 
92  static constexpr auto
93  diagonal_of = []<typename Arg> requires std::same_as<std::remove_cvref_t<Arg>, T>
94  (Arg&& arg) -> indexible auto
95  {
96  // ...
97  };
98 
99 
105  static constexpr auto
106  conjugate = []<typename Arg> requires std::same_as<std::remove_cvref_t<Arg>, T>
107  (Arg&& arg) -> constexpr patterns_may_match_with<Arg>
108  {
109  // ...
110  };
111 
112 
121  template<std::size_t indexa, std::size_t indexb>
122  static constexpr auto
123  transpose = []<typename Arg> requires
124  std::same_as<std::remove_cvref_t<Arg>, T> and
125  dimension_size_of_index_is<indexa, index_dimension_of_v<T, indexb>, &stdex::is_eq, applicability::permitted>
126  (Arg&& arg) -> dimension_size_of_index_is<indexa, index_dimension_of_v<T, indexb>, &stdex::is_eq, applicability::permitted> decltype(auto)
127  {
128  // ...
129  };
130 
131 
137  static constexpr auto
138  conjugate_transpose = []<typename Arg> requires std::same_as<std::remove_cvref_t<Arg>, T>
139  (Arg&& arg) -> dimension_size_of_index_is<0, index_dimension_of_v<T, 1>, &stdex::is_eq, applicability::permitted> decltype(auto)
140  {
141  // ...
142  };
143 
144 
153  template<triangle_type tri>
154  static constexpr auto
155  make_triangular_matrix = []<typename Arg> requires std::same_as<std::remove_cvref_t<Arg>, T>
156  (Arg&& arg) -> triangular_matrix<tri> decltype(auto)
157  {
158  // ...
159  };
160 
161 
170  template<HermitianAdapterType h>
171  static constexpr auto
172  make_hermitian_adapter = []<typename Arg> requires std::same_as<std::remove_cvref_t<Arg>, T>
173  (Arg&& arg) -> hermitian_matrix<h> decltype(auto)
174  {
175  // ...
176  };
177 
178 
187  //template<internal::layout_mapping_policy layout, values::number Scalar> requires
188  // std::same_as<layout, stdex::layout_left> or std::same_as<layout, stdex::layout_right>
189  //static constexpr auto
190  //make_default = [](const patterns::euclidean_pattern_collection auto& descriptors) -> indexible auto
191  //{
192  // // ...
193  //};
194 
195 
203  //template<internal::layout_mapping_policy layout> requires
204  // (std::same_as<layout, stdex::layout_left> or std::same_as<layout, stdex::layout_right>) and writable<T>
205  //static constexpr auto
206  //fill_components = []<typename Scalars> requires std::convertible_to<collections::common_collection_type_t<Scalars>, element_type_of_t<T>>
207  // (T& t, const collections::collection auto& scalars) -> void
208  //{
209  // // ...
210  //};
211 
212 
219  static constexpr auto
220  to_euclidean = []<typename Arg> requires std::same_as<std::remove_cvref_t<Arg>, T>
221  (Arg&& arg) -> has_untyped_index<0> decltype(auto)
222  {
223  // ...
224  };
225 
226 
234  static constexpr auto
235  from_euclidean = []<typename Arg> requires std::same_as<std::remove_cvref_t<Arg>, T> and all_fixed_indices_are_euclidean<T>
236  (Arg&& arg, const has_untyped_index<0> auto& v) -> indexible decltype(auto)
237  {
238  // ...
239  };
240 
241 
247  static constexpr auto
248  wrap_angles = []<typename Arg> requires std::same_as<std::remove_cvref_t<Arg>, T>
249  (Arg&& arg) -> std::convertible_to<const T&> decltype(auto)
250  {
251  // ...
252  };
253 
254 
260  static constexpr auto
261  slice = []<typename Arg> requires std::same_as<std::remove_cvref_t<Arg>, T>
262  (Arg&& arg, const index_collection_for<Arg> auto& begin, const collections::index_collection auto& size)
263  -> indexible decltype(auto)
264  {
265  // ...
266  };
267 
268 
277  static constexpr auto
278  set_slice = [] requires writable<T>
279  (T& t, indexible auto&& block, const index_collection_for<T> auto& offsets) -> void
280  {
281  // ...
282  };
283 
284 
293  template<triangle_type tri>
294  static constexpr auto
295  set_triangle = [] requires writable<T>
296  (T& t, patterns_may_match_with<T> auto&& b) -> void
297  {
298  // ...
299  };
300 
301 
309  static constexpr auto
310  broadcast = []<typename Arg> requires std::same_as<std::remove_cvref_t<Arg>, T>
311  (Arg&& arg, const collections::index_collection auto& factors) -> indexible auto
312  {
313  // ...
314  };
315 
316 
331  static constexpr auto
332  n_ary_operation = []<patterns::pattern_collection Patterns, typename Operation,
333  compares_with_pattern_collection<Patterns>...Args>
334  requires
335  std::invocable<Operation&&, element_type_of_t<Args>...> or
336  std::invocable<Operation&&, std::array<std::size_t, collections::size_of_v<Patterns>>, element_type_of_t<Args>...> or
337  std::invocable<Operation&&, std::vector<std::size_t>, element_type_of_t<Args>...>
338  (const Patterns& patterns, Operation&& op, Args&&...args)
339  -> compares_with_pattern_collection<Patterns> auto
340  {
341  // ...
342  };
343 
344 
355  template<std::size_t...indices, typename BinaryFunction>
356  static constexpr auto
357  reduce = []<typename Arg, typename BinaryFunction>
358  requires std::same_as<std::remove_cvref_t<Arg>, T> and
359  std::invocable<BinaryFunction, element_type_of_t<Arg>, element_type_of_t<Arg>>
360  (Arg&& arg, BinaryFunction&& binary_function)
361  {
362  // ...
363  };
364 
365 
370  static constexpr auto
371  determinant = [] requires square_shaped<T, 2, applicability::permitted>
372  (const T& t) -> std::convertible_to<element_type_of_t<T>> auto
373  {
374  // ...
375  };
376 
377 
383  static constexpr auto
384  sum = []<typename...Args> requires patterns_may_match_with<Args...>
385  (Args&&...args) -> patterns_may_match_with<Args...> decltype(auto)
386  {
387  // ...
388  };
389 
390 
396  static constexpr auto
397  scalar_product = [](const T& t, std::convertible_to<element_type_of_t<T>> const auto& s)
398  -> patterns_may_match_with<T> decltype(auto)
399  {
400  // ...
401  };
402 
403 
409  static constexpr auto
410  scalar_quotient = [](const T& t, std::convertible_to<element_type_of_t<T>> const auto& s)
411  -> patterns_may_match_with<T> decltype(auto)
412  {
413  // ...
414  };
415 
416 
422  static constexpr auto
423  contract = []
424  (const T& a, const dimension_size_of_index_is<0, index_dimension_of<T, 1>, &stdex::is_eq, applicability::permitted> auto& b)
425  -> dimension_size_of_index_is<0, index_dimension_of_v<T, 0>, &stdex::is_eq, applicability::permitted> auto
426  {
427  // ...
428  };
429 
430 
436  template<bool on_the_right>
437  static constexpr auto
438  contract_in_place = [](T& t, patterns_may_match_with<T> auto&& b) -> void
439  {
440  // ...
441  };
442 
443 
452  template<triangle_type tri>
453  static constexpr auto
454  cholesky_factor = [](const T& t) -> triangular_matrix<tri> auto
455  {
456  // ...
457  };
458 
459 
473  template<HermitianAdapterType significant_triangle>
474  static constexpr auto
475  rank_update_hermitian = []<typename A, typename U>
476  requires std::same_as<std::remove_cvref_t<A>, T> and
477  dimension_size_of_index_is<U, 0, index_dimension_of_v<A, 0>, &stdex::is_eq, applicability::permitted> and
478  dimension_size_of_index_is<U, 0, index_dimension_of_v<A, 1>, &stdex::is_eq, applicability::permitted> and
479  std::convertible_to<element_type_of_t<U>, const element_type_of_t<A>>
480  (A&& a, const U& u, const element_type_of_t<A>& alpha)
481  -> hermitian_matrix decltype(auto)
482  {
483  // ...
484  };
485 
486 
503  template<triangle_type triangle> requires (triangle == triangle_type::lower) or (triangle == triangle_type::upper)
504  static constexpr auto
505  rank_update_triangular = []<typename A, typename U>
506  requires std::same_as<std::remove_cvref_t<A>, T> and
507  dimension_size_of_index_is<U, 0, index_dimension_of_v<A, 0>, &stdex::is_eq, applicability::permitted> and
508  dimension_size_of_index_is<U, 0, index_dimension_of_v<A, 1>, &stdex::is_eq, applicability::permitted> and
509  std::convertible_to<element_type_of_t<U>, const element_type_of_t<A>>
510  (A&& a, const U& u, const element_type_of_t<decltype(a)>& alpha)
511  -> triangular_matrix<triangle> decltype(auto)
512  {
513  // ...
514  };
515 
516 
531  template<bool must_be_unique = false, bool must_be_exact = false>
532  static constexpr auto
533  solve = []
534  (const T& a, const dimension_size_of_index_is<0, index_dimension_of_v<A, 0>, &stdex::is_eq, applicability::permitted> auto& b)
535  -> indexible auto
536  {
537  // ...
538  };
539 
540 
547  static constexpr auto
548  LQ_decomposition = []<typename Arg> requires std::same_as<std::remove_cvref_t<Arg>, T>
549  (Arg&& arg) -> triangular_matrix<triangle_type::lower> auto
550  {
551  // ...
552  };
553 
554 
561  static constexpr auto
562  QR_decomposition = []<typename Arg> requires std::same_as<std::remove_cvref_t<Arg>, T>
563  (Arg&& arg) -> triangular_matrix<triangle_type::upper> auto
564  {
565  // ...
566  };
567 
568 #endif // DOXYGEN_SHOULD_SKIP_THIS
569  };
570 
571 
572 }
573 
574 
575 #endif
Definition for internal::slice_specifier.
decltype(auto) constexpr from_euclidean(Arg &&arg, const V &v)
Project the Euclidean vector space associated with index 0 to patterns::pattern v after applying dire...
Definition: from_euclidean.hpp:35
constexpr auto n_ary_operation(const std::tuple< Ds... > &d_tup, Operation &&operation, Args &&...args)
Perform a component-wise n-ary operation, using broadcasting to match the size of a pattern matrix...
Definition: n_ary_operation.hpp:325
constexpr bool pattern_collection
An object describing a sized collection of /ref sized_pattern objects.
Definition: pattern_collection.hpp:66
decltype(auto) constexpr contract(A &&a, B &&b)
Matrix multiplication of A * B.
Definition: contract.hpp:54
Definition: basics.hpp:41
decltype(auto) rank_update_hermitian(A &&a, U &&u, scalar_type_of_t< A > alpha=1)
Do a rank update on a hermitian matrix.
Definition: rank_update_hermitian.hpp:45
Definition for layout_mapping_policy.
constexpr bool diagonal_matrix
Specifies that a type is a diagonal matrix or tensor.
Definition: diagonal_matrix.hpp:32
A lower-left triangular matrix.
decltype(auto) constexpr conjugate(Arg &&arg)
Take the complex conjugate of an indexible object.
Definition: conjugate.hpp:44
The concept, trait, or restraint is permitted, but whether it applies is not necessarily known at com...
constexpr bool indexible
T is a multidimensional array type.
Definition: indexible.hpp:32
decltype(auto) constexpr QR_decomposition(A &&a)
Perform a QR decomposition of matrix A=Q[U,0], U is a upper-triangular matrix, and Q is orthogonal...
Definition: QR_decomposition.hpp:33
decltype(auto) constexpr conjugate_transpose(Arg &&arg)
Take the conjugate-transpose of an indexible_object.
Definition: conjugate_transpose.hpp:35
decltype(auto) constexpr copy_from(Dest &&dest, Source &&source)
Copy elements from one object to another.
Definition: copy_from.hpp:59
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
Enumerations relating to linear algebra.
decltype(auto) constexpr broadcast(Arg &&arg, const Factors &...factors)
Broadcast an object by replicating it by factors specified for each index.
Definition: broadcast.hpp:49
constexpr bool hermitian_matrix
Specifies that a type is a hermitian matrix.
Definition: hermitian_matrix.hpp:59
Definition for element_type_of.
constexpr bool patterns_may_match_with
Specifies that indexible objects may have equivalent extents and patterns.
Definition: patterns_may_match_with.hpp:57
constexpr bool dimension_size_of_index_is
Specifies that a given index of T has a specified size.
Definition: dimension_size_of_index_is.hpp:35
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
constexpr A && contract_in_place(A &&a, B &&b)
In-place matrix multiplication of A * B, storing the result in A.
Definition: contract_in_place.hpp:38
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:44
constexpr auto solve(A &&a, B &&b)
Solve the equation AX = B for X, which may or may not be a unique solution.
Definition: solve.hpp:87
constexpr auto determinant(Arg &&arg)
Take the determinant of a matrix.
Definition: determinant.hpp:44
constexpr bool size
T is either an index representing a size, or unbounded_size_t, which indicates that the size is unbou...
Definition: size.hpp:71
decltype(auto) constexpr to_euclidean(Arg &&arg)
Project the vector space associated with index 0 to a Euclidean space for applying directional statis...
Definition: to_euclidean.hpp:38
constexpr detail::slice_adapter slice
a RangeAdapterObject associated with slice_view.
Definition: slice.hpp:423
An upper-right triangular matrix.
decltype(auto) constexpr sum(Ts &&...ts)
Element-by-element sum of one or more objects.
Definition: sum.hpp:112
decltype(auto) constexpr LQ_decomposition(A &&a)
Perform an LQ decomposition of matrix A=[L,0]Q, L is a lower-triangular matrix, and Q is orthogonal...
Definition: LQ_decomposition.hpp:33
decltype(auto) rank_update_triangular(A &&a, U &&u, scalar_type_of_t< A > alpha=1)
Do a rank update on triangular matrix.
Definition: rank_update_triangular.hpp:48
decltype(auto) constexpr to_diagonal(Arg &&arg, P &&p)
Convert a column vector (or any other array with a 1D second index) into a diagonal_matrix.
Definition: to_diagonal.hpp:46
typename element_type_of< T >::type element_type_of_t
helper template for element_type_of.
Definition: element_type_of.hpp:54
A matrix with typed rows and columns.
Definition: forward-class-declarations.hpp:292
decltype(auto) constexpr make_triangular_matrix(Arg &&arg)
Create a triangular_matrix from a general matrix.
Definition: make_triangular_matrix.hpp:35
decltype(auto) constexpr cholesky_factor(A &&a)
Take the Cholesky factor of a matrix.
Definition: cholesky_factor.hpp:38
constexpr Arg && set_slice(Arg &&arg, Block &&block, const Begin &...begin)
Assign an object to a particular slice of a matrix or tensor.
Definition: set_slice.hpp:56