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-2024 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 #include <type_traits>
20 #include <tuple>
21 
22 namespace OpenKalman::interface
23 {
32 #ifdef __cpp_concepts
33  template<typename LibraryObject>
34 #else
35  template<typename LibraryObject, typename>
36 #endif
38  {
39 #ifdef DOXYGEN_SHOULD_SKIP_THIS
40 
48  template<typename Derived>
49  using LibraryBase = std::monostate;
50 
51 
60 #ifdef __cpp_lib_ranges
61  template<indexible Arg, std::ranges::input_range Indices> requires values::index<std::ranges::range_value_t<Indices>>
62  static constexpr values::scalar decltype(auto)
63 #else
64  template<typename Arg, typename Indices>
65  static constexpr decltype(auto)
66 #endif
67  get_component(Arg&& arg, const Indices& indices) = delete;
68 
69 
78 #ifdef __cpp_lib_ranges
79  template<indexible Arg, std::ranges::input_range Indices> requires values::index<std::ranges::range_value_t<Indices>>
80 #else
81  template<typename Arg, typename Indices>
82 #endif
83  static void
84  set_component(Arg& arg, const scalar_type_of_t<Arg>& s, const Indices& indices) = delete;
85 
86 
95 #ifdef __cpp_concepts
96  static decltype(auto)
97  to_native_matrix(auto&& arg) = delete;
98 #else
99  template<typename Arg>
100  static decltype(auto)
101  to_native_matrix(Arg&& arg) = delete;
102 #endif
103 
104 
110 #ifdef __cpp_concepts
111  template<writable To, indexible From>
112 #else
113  template<typename To, typename From, std::enable_if_t<indexible<To> and indexible<From>, int> = 0>
114 #endif
115  static void
116  assign(M& a, From&& b) = delete;
117 
118 
128 #ifdef __cpp_concepts
129  template<Layout layout, values::number Scalar> requires (layout != Layout::stride)
130  static auto
131  make_default(coordinates::euclidean_pattern_collection auto&& descriptors) = delete;
132 #else
133  template<Layout layout, typename Scalar, typename Descriptors>
134  static auto
135  make_default(Descriptors&& descriptors) = delete;
136 #endif
137 
138 
146 #ifdef __cpp_concepts
147  template<Layout layout, writable Arg> requires (layout == Layout::right) or (layout == Layout::left)
148  static void
149  fill_components(Arg& arg, const std::convertible_to<scalar_type_of_t<Arg>> auto ... scalars) = delete;
150 #else
151  template<Layout layout, typename Arg, typename...Scalars, std::enable_if_t<
152  writable<Arg> and (layout == Layout::right) or (layout == Layout::left), int> = 0>
153  static void
154  fill_components(Arg& arg, const Scalars...scalars) = delete;
155 #endif
156 
157 
165 #ifdef __cpp_concepts
166  static constexpr constant_matrix auto
167  make_constant(const values::scalar auto& c, coordinates::euclidean_pattern_collection auto&& d) = delete;
168 #else
169  template<typename C, typename D>
170  static constexpr auto
171  make_constant(const C& c, D&& d) = delete;
172 #endif
173 
174 
182 #ifdef __cpp_concepts
183  template<values::number Scalar>
184  static constexpr identity_matrix auto
185  make_identity_matrix(coordinates::euclidean_pattern_collection auto&& d) = delete;
186 #else
187  template<typename Scalar, typename D>
188  static constexpr auto
189  make_identity_matrix(D&& d) = delete;
190 #endif
191 
192 
201 #ifdef __cpp_concepts
202  template<TriangleType t>
203  static constexpr auto
204  make_triangular_matrix(indexible auto&& arg) = delete;
205 #else
206  template<TriangleType t, typename Arg>
207  static constexpr auto
208  make_triangular_matrix(Arg&& arg) = delete;
209 #endif
210 
211 
220 #ifdef __cpp_concepts
221  template<HermitianAdapterType t>
222  static constexpr auto
223  make_hermitian_adapter(indexible auto&& arg) = delete;
224 #else
225  template<HermitianAdapterType t, typename Arg>
226  static constexpr auto
227  make_hermitian_adapter(Arg&& arg) = delete;
228 #endif
229 
230 
237  #ifdef __cpp_concepts
238  template<indexible Arg>
239  static constexpr indexible auto
240  #else
241  template<typename Arg>
242  static constexpr auto
243  #endif
244  to_euclidean(Arg&& arg) = delete;
245 
246 
254  #ifdef __cpp_concepts
255  template<indexible Arg, coordinates::pattern V>
256  static constexpr indexible auto
257  #else
258  template<typename Arg, typename V>
259  static constexpr auto
260  #endif
261  from_euclidean(Arg&& arg, const V& v) = delete;
262 
263 
269  #ifdef __cpp_concepts
270  template<indexible Arg>
271  static constexpr indexible auto
272  #else
273  template<typename Arg>
274  static constexpr auto
275  #endif
276  wrap_angles(Arg&& arg) = delete;
277 
278 
284 #ifdef __cpp_concepts
285  template<indexible Arg, values::index...Begin, values::index...Size> requires
286  (index_count_v<Arg> == sizeof...(Begin)) and (index_count_v<Arg> == sizeof...(Size))
287  static indexible decltype(auto)
288 #else
289  template<typename Arg, typename...Begin, typename...Size>
290  static decltype(auto)
291 #endif
292  get_slice(Arg&& arg, const std::tuple<Begin...>& begin, const std::tuple<Size...>& size) = delete;
293 
294 
304 #ifdef __cpp_concepts
305  template<writable Arg, indexible Block, values::index...Begin> requires
306  (index_count_v<Block> == sizeof...(Begin)) and (index_count_v<Arg> == sizeof...(Begin))
307 #else
308  template<typename Arg, typename Block, typename...Begin>
309 #endif
310  static void
311  set_slice(Arg& arg, Block&& block, const Begin&...begin) = delete;
312 
313 
323 #ifdef __cpp_concepts
324  template<TriangleType t>
325  static void
326  set_triangle(writable auto& a, indexible auto&& b) = delete;
327 #else
328  template<TriangleType t, typename A, typename B, std::enable_if_t<writable<A> and indexible<B>, int> = 0>
329  static void
330  set_triangle(A& a, B&& b) = delete;
331 #endif
332 
333 
340 #ifdef __cpp_concepts
341  static constexpr diagonal_matrix auto
342  to_diagonal(indexible auto&& arg) = delete;
343 #else
344  template<typename Arg>
345  static constexpr auto
346  to_diagonal(Arg&& arg) = delete;
347 #endif
348 
349 
359 #ifdef __cpp_concepts
360  static constexpr indexible auto
361  diagonal_of(square_shaped<Applicability::permitted> auto&& arg) = delete;
362 #else
363  template<typename Arg>
364  static constexpr auto
365  diagonal_of(Arg&& arg) = delete;
366 #endif
367 
368 
376 #ifdef __cpp_concepts
377  static indexible auto
378  broadcast(indexible auto&& arg, const values::index auto&...factors) = delete;
379 #else
380  template<typename Arg, typename...Factors>
381  static auto
382  broadcast(Arg&& arg, const Factors&...factors) = delete;
383 #endif
384 
385 
400 #ifdef __cpp_concepts
401  template<coordinates::pattern...Ds, typename Operation, indexible...Args> requires
402  std::invocable<Operation&&, scalar_type_of_t<Args>...> or
403  std::invocable<Operation&&, scalar_type_of_t<Args>..., std::conditional_t<true, std::size_t, Ds>...>
404  static compatible_with_vector_space_descriptor_collection<std::tuple<Ds...>> auto
405 #else
406  template<typename...Ds, typename Operation, typename...Args>
407  static auto
408 #endif
409  n_ary_operation(const std::tuple<Ds...>& d_tup, Operation&& op, Args&&...args) = delete;
410 
411 
422 #ifdef __cpp_concepts
423  template<std::size_t...indices, typename BinaryFunction, indexible Arg>
424 #else
425  template<std::size_t...indices, typename BinaryFunction, typename Arg>
426 #endif
427  static constexpr auto
428  reduce(BinaryFunction&& op, Arg&& arg) = delete;
429 
430 
435 #ifdef __cpp_concepts
436  template<indexible Arg>
437  static constexpr vector_space_descriptors_may_match_with<Arg> auto
438 #else
439  template<typename Arg>
440  static constexpr auto
441 #endif
442  conjugate(Arg&& arg) = delete;
443 
444 
449 #ifdef __cpp_concepts
450  template<indexible Arg>
451  static constexpr compatible_with_vector_space_descriptor_collection<std::tuple<vector_space_descriptor_of_t<Arg, 1>, vector_space_descriptor_of_t<Arg, 0>>> auto
452 #else
453  template<typename Arg>
454  static constexpr auto
455 #endif
456  transpose(Arg&& arg) = delete;
457 
458 
464 #ifdef __cpp_concepts
465  template<indexible Arg>
466  static constexpr compatible_with_vector_space_descriptor_collection<std::tuple<vector_space_descriptor_of_t<Arg, 1>, vector_space_descriptor_of_t<Arg, 0>>> auto
467 #else
468  template<typename Arg>
469  static constexpr auto
470 #endif
471  adjoint(Arg&& arg) = delete;
472 
473 
478 #ifdef __cpp_concepts
479  template<square_shaped<Applicability::permitted> Arg>
480  static constexpr std::convertible_to<scalar_type_of_t<Arg>> auto
481 #else
482  template<typename Arg>
483  static constexpr auto
484 #endif
485  determinant(Arg&& arg) = delete;
486 
487 
494 #ifdef __cpp_concepts
495  template<indexible Arg, vector_space_descriptors_may_match_with<Arg>...Args>
496  static constexpr vector_space_descriptors_may_match_with<Arg> auto
497 #else
498  template<typename Arg, typename...Args>
499  static constexpr auto
500 #endif
501  sum(Arg&& arg, Args&&...args) = delete;
502 
503 
510 #ifdef __cpp_concepts
511  template<indexible Arg, values::scalar S> requires
512  requires(S s) { {values::to_number(s)} -> std::convertible_to<scalar_type_of_t<Arg>>; }
513  static constexpr vector_space_descriptors_may_match_with<Arg> auto
514 #else
515  template<typename Arg, typename S>
516  static constexpr auto
517 #endif
518  scalar_product(Arg&& arg, S&& s) = delete;
519 
520 
527 #ifdef __cpp_concepts
528  template<indexible Arg, values::scalar S> requires
529  requires(S s) { {values::to_number(s)} -> std::convertible_to<scalar_type_of_t<Arg>>; }
530  static constexpr vector_space_descriptors_may_match_with<Arg> auto
531 #else
532  template<typename Arg, typename S>
533  static constexpr auto
534 #endif
535  scalar_quotient(Arg&& arg, S&& s) = delete;
536 
537 
543 #ifdef __cpp_concepts
544  template<indexible A, vector_space_descriptors_may_match_with<A> B>
545  static constexpr compatible_with_vector_space_descriptor_collection<std::tuple(vector_space_descriptor_of_t<A, 1>, vector_space_descriptor_of_t<B, 1>>> auto
546 #else
547  template<typename A, typename B>
548  static constexpr auto
549 #endif
550  contract(A&& a, B&& b) = delete;
551 
552 
559 #ifdef __cpp_concepts
560  template<bool on_the_right, writable A, vector_space_descriptors_may_match_with<A> B>
561 #else
562  template<bool on_the_right, typename A, typename B>
563 #endif
564  static constexpr A&
565  contract_in_place(A& a, B&& b) = delete;
566 
567 
576 #ifdef __cpp_concepts
577  template<TriangleType triangle_type>
578  static constexpr triangular_matrix<triangle_type> auto
579  cholesky_factor(indexible auto&& a) = delete;
580 #else
581  template<TriangleType triangle_type, typename Arg>
582  static constexpr auto
583  cholesky_factor(Arg&& a) = delete;
584 #endif
585 
586 
600 #ifdef __cpp_concepts
601  template<HermitianAdapterType significant_triangle, indexible A, indexible U>
602  static hermitian_matrix decltype(auto)
603 #else
604  template<HermitianAdapterType significant_triangle, typename A, typename U>
605  static decltype(auto)
606 #endif
607  rank_update_hermitian(A&& a, U&& u, const scalar_type_of_t<A>& alpha) = delete;
608 
609 
626 #ifdef __cpp_concepts
627  template<TriangleType triangle, indexible A, indexible U> requires
628  (triangle == TriangleType::lower) or (triangle == TriangleType::upper)
629  static triangular_matrix<triangle> decltype(auto)
630 #else
631  template<TriangleType triangle, typename A, typename U>
632  static decltype(auto)
633 #endif
634  rank_update_triangular(A&& a, U&& u, const scalar_type_of_t<A>& alpha) = delete;
635 
636 
651 #ifdef __cpp_concepts
652  template<bool must_be_unique = false, bool must_be_exact = false, indexible A, indexible B>
653  static compatible_with_vector_space_descriptor_collection<std::tuple<vector_space_descriptor_of_t<A, 1>, vector_space_descriptor_of_t<B, 1>>> auto
654  solve(indexible auto&& a, indexible auto&& b) = delete;
655 #else
656  template<bool must_be_unique = false, bool must_be_exact = false, typename A, typename B>
657  static auto
658  solve(A&& a, B&& b) = delete;
659 #endif
660 
661 
668 #ifdef __cpp_concepts
669  static constexpr triangular_matrix<TriangleType::lower> auto
670  LQ_decomposition(indexible auto&& arg) = delete;
671 #else
672  template<typename Arg>
673  static constexpr auto
674  LQ_decomposition(Arg&& arg) = delete;
675 #endif
676 
677 
684 #ifdef __cpp_concepts
685  static constexpr triangular_matrix<TriangleType::upper> auto
686  QR_decomposition(indexible auto&& arg) = delete;
687 #else
688  template<typename Arg>
689  static constexpr auto
690  QR_decomposition(Arg&& arg) = delete;
691 #endif
692 
693 #endif // DOXYGEN_SHOULD_SKIP_THIS
694  };
695 
696 
697 } // namespace OpenKalman::interface
698 
699 
700 #endif //OPENKALMAN_LIBRARY_INTERFACE_HPP
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:319
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
Row-major storage (C or C++ style): contiguous storage in which the right-most index has a stride of ...
constexpr bool constant_matrix
Specifies that all components of an object are the same constant value.
Definition: constant_matrix.hpp:31
constexpr bool diagonal_matrix
Specifies that a type is a diagonal matrix or tensor.
Definition: diagonal_matrix.hpp:32
Arg && set_component(Arg &&arg, const scalar_type_of_t< Arg > &s, const Indices &indices)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: set_component.hpp:51
decltype(auto) constexpr conjugate(Arg &&arg)
Take the conjugate of a matrix.
Definition: conjugate.hpp:33
decltype(auto) constexpr get_slice(Arg &&arg, const std::tuple< Offset... > &offsets, const std::tuple< Extent... > &extents)
Extract a slice from a matrix or tensor.
Definition: get_slice.hpp:101
typename scalar_type_of< T >::type scalar_type_of_t
helper template for scalar_type_of.
Definition: scalar_type_of.hpp:54
constexpr bool indexible
T is a generalized tensor type.
Definition: indexible.hpp:32
decltype(auto) to_native_matrix(Arg &&arg)
If it isn&#39;t already, convert Arg to a native object in the library associated with LibraryObject...
Definition: to_native_matrix.hpp:35
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 to_diagonal(Arg &&arg)
Convert an indexible object into a diagonal matrix.
Definition: to_diagonal.hpp:32
An upper-right triangular matrix.
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
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 auto to_number(Arg arg)
Convert any values::value to a values::number.
Definition: to_number.hpp:34
decltype(auto) constexpr transpose(Arg &&arg)
Take the transpose of a matrix.
Definition: transpose.hpp:58
An interface to various routines from the linear algebra library associated with indexible object T...
Definition: library_interface.hpp:37
Column-major storage (Fortran, Matlab, or Eigen style): contiguous storage in which the left-most ext...
constexpr bool identity_matrix
Specifies that a type is an identity matrix.
Definition: identity_matrix.hpp:45
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:33
A generalization of the above: a custom stride is specified for each index.
constexpr bool size
T is either an index representing a size, or void which represents that there is no size...
Definition: size.hpp:32
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
decltype(auto) constexpr adjoint(Arg &&arg)
Take the adjoint of a matrix.
Definition: adjoint.hpp:33
typename vector_space_descriptor_of< T, N >::type vector_space_descriptor_of_t
helper template for vector_space_descriptor_of.
Definition: vector_space_descriptor_of.hpp:56
Arg && fill_components(Arg &&arg, S...s)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: fill_components.hpp:44
Layout
The layout format of a multidimensional array.
Definition: global-definitions.hpp:47
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
constexpr bool index
T is an index value.
Definition: index.hpp:56
constexpr auto make_constant(C &&c, Descriptors &&descriptors)
Make a constant object based on a particular library object.
Definition: make_constant.hpp:37
constexpr To && assign(To &&a, From &&b)
Assign a writable object from an indexible object.
Definition: assign.hpp:51
decltype(auto) constexpr get_component(Arg &&arg, const Indices &indices)
Get a component of an object at a particular set of indices.
Definition: get_component.hpp:54
constexpr bool compatible_with_vector_space_descriptor_collection
indexible T is compatible with pattern_collection D.
Definition: compatible_with_vector_space_descriptor_collection.hpp:74
A lower-left triangular matrix.
A matrix with typed rows and columns.
Definition: forward-class-declarations.hpp:448
constexpr bool hermitian_matrix
Specifies that a type is a hermitian matrix (assuming it is square_shaped).
Definition: hermitian_matrix.hpp:50
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