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 "coordinates/coordinates.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 
77  static constexpr auto
78  make_constant = [](const values::scalar auto& c, coordinates::euclidean_pattern_collection auto&& d)
79  -> constexpr_matrix auto
80  {
81  // ...
82  };
83 
84 
91  static constexpr auto
92  to_diagonal = []<typename Arg> requires std::same_as<std::remove_cvref_t<Arg>, T>
93  (Arg&& arg) -> diagonal_matrix auto
94  {
95  // ...
96  };
97 
98 
104  static constexpr auto
105  conjugate = []<typename Arg> requires std::same_as<std::remove_cvref_t<Arg>, T>
106  (Arg&& arg) -> constexpr patterns_may_match_with<Arg>
107  {
108  // ...
109  };
110 
111 
118  static constexpr auto
119  transpose = []<typename Arg> requires
120  std::same_as<std::remove_cvref_t<Arg>, T> and
121  dimension_size_of_index_is<0, index_dimension_of_v<T, 1>, &stdex::is_eq, applicability::permitted>
122  (Arg&& arg) -> dimension_size_of_index_is<0, index_dimension_of_v<T, 1>, &stdex::is_eq, applicability::permitted> decltype(auto)
123  {
124  // ...
125  };
126 
127 
134  template<std::size_t indexa, std::size_t indexb>
135  static constexpr auto
136  transpose = []<typename Arg> requires
137  std::same_as<std::remove_cvref_t<Arg>, T> and
138  dimension_size_of_index_is<indexa, index_dimension_of_v<T, indexb>, &stdex::is_eq, applicability::permitted>
139  (Arg&& arg) -> dimension_size_of_index_is<indexa, index_dimension_of_v<T, indexb>, &stdex::is_eq, applicability::permitted> decltype(auto)
140  {
141  // ...
142  };
143 
144 
151  static constexpr auto
152  adjoint = []<typename Arg> requires std::same_as<std::remove_cvref_t<Arg>, T>
153  (Arg&& arg) -> dimension_size_of_index_is<0, index_dimension_of_v<T, 1>, &stdex::is_eq, applicability::permitted> decltype(auto)
154  {
155  // ...
156  };
157 
158 
165  template<std::size_t indexa, std::size_t indexb>
166  static constexpr auto
167  adjoint = []<typename Arg> requires
168  std::same_as<std::remove_cvref_t<Arg>, T> and
169  dimension_size_of_index_is<indexa, index_dimension_of_v<T, indexb>, &stdex::is_eq, applicability::permitted>
170  (Arg&& arg) -> dimension_size_of_index_is<indexa, index_dimension_of_v<T, indexb>, &stdex::is_eq, applicability::permitted> decltype(auto)
171  {
172  // ...
173  };
174 
175 
183  template<values::number Scalar>
184  static constexpr auto
185  make_identity_matrix = [](coordinates::euclidean_pattern_collection auto&& d) -> identity_matrix auto
186  {
187  // ...
188  };
189 
190 
199  template<triangle_type tri>
200  static constexpr auto
201  make_triangular_matrix = []<typename Arg> requires std::same_as<std::remove_cvref_t<Arg>, T>
202  (Arg&& arg) -> triangular_matrix<tri> decltype(auto)
203  {
204  // ...
205  };
206 
207 
216  template<HermitianAdapterType h>
217  static constexpr auto
218  make_hermitian_adapter = []<typename Arg> requires std::same_as<std::remove_cvref_t<Arg>, T>
219  (Arg&& arg) -> hermitian_matrix<h> decltype(auto)
220  {
221  // ...
222  };
223 
224 
233  //template<internal::layout_mapping_policy layout, values::number Scalar> requires
234  // std::same_as<layout, stdex::layout_left> or std::same_as<layout, stdex::layout_right>
235  //static constexpr auto
236  //make_default = [](const coordinates::euclidean_pattern_collection auto& descriptors) -> indexible auto
237  //{
238  // // ...
239  //};
240 
241 
249  //template<internal::layout_mapping_policy layout> requires
250  // (std::same_as<layout, stdex::layout_left> or std::same_as<layout, stdex::layout_right>) and writable<T>
251  //static constexpr auto
252  //fill_components = []<typename Scalars> requires std::convertible_to<collections::common_collection_type_t<Scalars>, element_type_of_t<T>>
253  // (T& t, const collections::collection auto& scalars) -> void
254  //{
255  // // ...
256  //};
257 
258 
265  static constexpr auto
266  to_euclidean = []<typename Arg> requires std::same_as<std::remove_cvref_t<Arg>, T>
267  (Arg&& arg) -> has_untyped_index<0> decltype(auto)
268  {
269  // ...
270  };
271 
272 
280  static constexpr auto
281  from_euclidean = []<typename Arg> requires std::same_as<std::remove_cvref_t<Arg>, T> and all_fixed_indices_are_euclidean<T>
282  (Arg&& arg, const has_untyped_index<0> auto& v) -> indexible decltype(auto)
283  {
284  // ...
285  };
286 
287 
293  static constexpr auto
294  wrap_angles = []<typename Arg> requires std::same_as<std::remove_cvref_t<Arg>, T>
295  (Arg&& arg) -> std::convertible_to<const T&> decltype(auto)
296  {
297  // ...
298  };
299 
300 
306  static constexpr auto
307  slice = []<typename Arg> requires std::same_as<std::remove_cvref_t<Arg>, T>
308  (Arg&& arg, const index_collection_for<Arg> auto& begin, const collections::index_collection auto& size)
309  -> indexible decltype(auto)
310  {
311  // ...
312  };
313 
314 
323  static constexpr auto
324  set_slice = [] requires writable<T>
325  (T& t, indexible auto&& block, const index_collection_for<T> auto& offsets) -> void
326  {
327  // ...
328  };
329 
330 
339  template<triangle_type tri>
340  static constexpr auto
341  set_triangle = [] requires writable<T>
342  (T& t, patterns_may_match_with<T> auto&& b) -> void
343  {
344  // ...
345  };
346 
347 
358  static constexpr auto
359  diagonal_of = []<typename Arg> requires std::same_as<std::remove_cvref_t<Arg>, T>
360  (Arg&& arg) -> indexible auto
361  {
362  // ...
363  };
364 
365 
373  static constexpr auto
374  broadcast = []<typename Arg> requires std::same_as<std::remove_cvref_t<Arg>, T>
375  (Arg&& arg, const collections::index_collection auto& factors) -> indexible auto
376  {
377  // ...
378  };
379 
380 
395  static constexpr auto
396  n_ary_operation = []<coordinates::pattern_collection Patterns, typename Operation,
397  compares_with_pattern_collection<Patterns>...Args>
398  requires
399  std::invocable<Operation&&, element_type_of_t<Args>...> or
400  std::invocable<Operation&&, std::array<std::size_t, collections::size_of_v<Patterns>>, element_type_of_t<Args>...> or
401  std::invocable<Operation&&, std::vector<std::size_t>, element_type_of_t<Args>...>
402  (const Patterns& patterns, Operation&& op, Args&&...args)
403  -> compares_with_pattern_collection<Patterns> auto
404  {
405  // ...
406  };
407 
408 
419  template<std::size_t...indices, typename BinaryFunction>
420  static constexpr auto
421  reduce = []<typename Arg, typename BinaryFunction>
422  requires std::same_as<std::remove_cvref_t<Arg>, T> and
423  std::invocable<BinaryFunction, element_type_of_t<Arg>, element_type_of_t<Arg>>
424  (Arg&& arg, BinaryFunction&& binary_function)
425  {
426  // ...
427  };
428 
429 
434  static constexpr auto
435  determinant = [] requires square_shaped<T, applicability::permitted>
436  (const T& t) -> std::convertible_to<element_type_of_t<T>> auto
437  {
438  // ...
439  };
440 
441 
447  static constexpr auto
448  sum = []<typename...Args> requires patterns_may_match_with<Args...>
449  (Args&&...args) -> patterns_may_match_with<Args...> decltype(auto)
450  {
451  // ...
452  };
453 
454 
460  static constexpr auto
461  scalar_product = [](const T& t, std::convertible_to<element_type_of_t<T>> const auto& s)
462  -> patterns_may_match_with<T> decltype(auto)
463  {
464  // ...
465  };
466 
467 
473  static constexpr auto
474  scalar_quotient = [](const T& t, std::convertible_to<element_type_of_t<T>> const auto& s)
475  -> patterns_may_match_with<T> decltype(auto)
476  {
477  // ...
478  };
479 
480 
486  static constexpr auto
487  contract = []
488  (const T& a, const dimension_size_of_index_is<0, index_dimension_of<T, 1>, &stdex::is_eq, applicability::permitted> auto& b)
489  -> dimension_size_of_index_is<0, index_dimension_of_v<T, 0>, &stdex::is_eq, applicability::permitted> auto
490  {
491  // ...
492  };
493 
494 
500  template<bool on_the_right>
501  static constexpr auto
502  contract_in_place = [](T& t, patterns_may_match_with<T> auto&& b) -> void
503  {
504  // ...
505  };
506 
507 
516  template<triangle_type tri>
517  static constexpr auto
518  cholesky_factor = [](const T& t) -> triangular_matrix<tri> auto
519  {
520  // ...
521  };
522 
523 
537  template<HermitianAdapterType significant_triangle>
538  static constexpr auto
539  rank_update_hermitian = []<typename A, typename U>
540  requires std::same_as<std::remove_cvref_t<A>, T> and
541  dimension_size_of_index_is<U, 0, index_dimension_of_v<A, 0>, &stdex::is_eq, applicability::permitted> and
542  dimension_size_of_index_is<U, 0, index_dimension_of_v<A, 1>, &stdex::is_eq, applicability::permitted> and
543  std::convertible_to<element_type_of_t<U>, const element_type_of_t<A>>
544  (A&& a, const U& u, const element_type_of_t<A>& alpha)
545  -> hermitian_matrix decltype(auto)
546  {
547  // ...
548  };
549 
550 
567  template<triangle_type triangle> requires (triangle == triangle_type::lower) or (triangle == triangle_type::upper)
568  static constexpr auto
569  rank_update_triangular = []<typename A, typename U>
570  requires std::same_as<std::remove_cvref_t<A>, T> and
571  dimension_size_of_index_is<U, 0, index_dimension_of_v<A, 0>, &stdex::is_eq, applicability::permitted> and
572  dimension_size_of_index_is<U, 0, index_dimension_of_v<A, 1>, &stdex::is_eq, applicability::permitted> and
573  std::convertible_to<element_type_of_t<U>, const element_type_of_t<A>>
574  (A&& a, const U& u, const element_type_of_t<decltype(a)>& alpha)
575  -> triangular_matrix<triangle> decltype(auto)
576  {
577  // ...
578  };
579 
580 
595  template<bool must_be_unique = false, bool must_be_exact = false>
596  static constexpr auto
597  solve = []
598  (const T& a, const dimension_size_of_index_is<0, index_dimension_of_v<A, 0>, &stdex::is_eq, applicability::permitted> auto& b)
599  -> indexible auto
600  {
601  // ...
602  };
603 
604 
611  static constexpr auto
612  LQ_decomposition = []<typename Arg> requires std::same_as<std::remove_cvref_t<Arg>, T>
613  (Arg&& arg) -> triangular_matrix<triangle_type::lower> auto
614  {
615  // ...
616  };
617 
618 
625  static constexpr auto
626  QR_decomposition = []<typename Arg> requires std::same_as<std::remove_cvref_t<Arg>, T>
627  (Arg&& arg) -> triangular_matrix<triangle_type::upper> auto
628  {
629  // ...
630  };
631 
632 #endif // DOXYGEN_SHOULD_SKIP_THIS
633  };
634 
635 
636 }
637 
638 
639 #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 coordinates::pattern v after applying d...
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
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 copy_from(Dest &&dest, Source &&source)
Copy elements from one object to another.
Definition: copy_from.hpp:59
decltype(auto) constexpr to_diagonal(Arg &&arg)
Convert an indexible object into a diagonal matrix.
Definition: to_diagonal.hpp:33
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 Ts may have equivalent dimensions and vector-space types...
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
constexpr bool identity_matrix
Specifies that a type is known at compile time to be a rank-2 or lower identity matrix.
Definition: identity_matrix.hpp:50
decltype(auto) constexpr transpose(Arg &&arg)
Swap any two indices of an indexible_object.
Definition: transpose.hpp:163
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
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
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:36
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:65
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:364
An upper-right triangular matrix.
constexpr bool pattern_collection
An object describing a collection of /ref pattern objects.
Definition: pattern_collection.hpp:65
decltype(auto) constexpr sum(Ts &&...ts)
Element-by-element sum of one or more objects.
Definition: sum.hpp:112
constexpr bool euclidean_pattern_collection
An object describing a collection of /ref euclidean_pattern objects.
Definition: euclidean_pattern_collection.hpp:65
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
constexpr auto make_constant(C c, stdex::extents< IndexType, Extents... > extents)
Make an indexible object in which every element is a constant value.
Definition: make_constant.hpp:39
decltype(auto) constexpr adjoint(Arg &&arg)
Take the conjugate-transpose of an indexible_object.
Definition: adjoint.hpp:35
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