Skip to content

Commit

Permalink
[linalg] split physical from indexed linalg
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancoisCarouge committed Dec 27, 2024
1 parent 7650614 commit e69ea21
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 95 deletions.
3 changes: 2 additions & 1 deletion support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org> ]]

add_subdirectory("eigen")
add_subdirectory("indexed")
add_subdirectory("main")
add_subdirectory("mp_units")
add_subdirectory("naive")
add_subdirectory("physical")
add_subdirectory("quantity")

add_library(kalman_options INTERFACE)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org> ]]

add_library(kalman_physical_linalg INTERFACE)
add_library(kalman_linalg_indexed INTERFACE)
target_sources(
kalman_physical_linalg INTERFACE FILE_SET "unit_headers" TYPE "HEADERS" FILES
"fcarouge/physical_linalg.hpp")
target_link_libraries(kalman_physical_linalg INTERFACE kalman)
kalman_linalg_indexed INTERFACE FILE_SET "unit_headers" TYPE "HEADERS" FILES
"fcarouge/indexed_linalg.hpp")
target_link_libraries(kalman_linalg_indexed INTERFACE kalman)
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org> */

#ifndef FCAROUGE_PHYSICAL_LINALG_HPP
#define FCAROUGE_PHYSICAL_LINALG_HPP
#ifndef FCAROUGE_INDEXED_LINALG_HPP
#define FCAROUGE_INDEXED_LINALG_HPP

//! @file
//! @brief Physically typed linear algebra implementation.
//! @brief Index typed linear algebra implementation.
//!
//! @details Matrix, vectors, and named algebraic values.

Expand All @@ -66,9 +66,9 @@ using element_t =
//! @name Algebraic Types
//! @{

//! @brief Physical matrix.
//! @brief Indexed matrix.
//!
//! @details Compose a matrix into a physical matrix. Supports type safety. Unit
//! @details Compose a matrix into a indexed matrix. Supports type safety. Unit
//! safety. Row and column indexes provide each index type.
//!
//! @tparam Matrix The underlying linear algebra matrix.
Expand All @@ -77,35 +77,34 @@ using element_t =
//!
//! @todo Explore various types of indexes: arithmetic, units, frames, etc...
template <typename Matrix, typename RowIndexes, typename ColumnIndexes>
class physical_matrix {
class indexed_matrix {
public:
inline constexpr physical_matrix() = default;
inline constexpr indexed_matrix() = default;

inline constexpr physical_matrix(const physical_matrix &other) = default;
inline constexpr indexed_matrix(const indexed_matrix &other) = default;

inline constexpr physical_matrix &
operator=(const physical_matrix &other) = default;
inline constexpr indexed_matrix &
operator=(const indexed_matrix &other) = default;

inline constexpr physical_matrix(physical_matrix &&other) = default;
inline constexpr indexed_matrix(indexed_matrix &&other) = default;

inline constexpr physical_matrix &
operator=(physical_matrix &&other) = default;
inline constexpr indexed_matrix &operator=(indexed_matrix &&other) = default;

//! @todo Is this function safe? Correct?
//! @todo Add other move assignement function?
template <typename M, typename R, typename C>
inline constexpr physical_matrix &
operator=(const physical_matrix<M, R, C> &other) {
inline constexpr indexed_matrix &
operator=(const indexed_matrix<M, R, C> &other) {
data = other.data;
return *this;
}

//! @todo Is this function safe? Correct?
template <eigen Type>
explicit inline constexpr physical_matrix(const Type &other) : data{other} {}
//!@todo Is this function safe? Correct?
// template <Matrix Type>
explicit inline constexpr indexed_matrix(const Matrix &other) : data{other} {}

//! @todo Can the tuple packing be avoided altogether?
explicit inline constexpr physical_matrix(const auto &...elements)
explicit inline constexpr indexed_matrix(const auto &...elements)
requires(std::tuple_size_v<ColumnIndexes> == 1 &&
sizeof...(elements) == std::tuple_size_v<RowIndexes>)
{
Expand All @@ -118,7 +117,7 @@ class physical_matrix {
}

//! @todo Can the tuple packing be avoided altogether?
explicit inline constexpr physical_matrix(const auto &...elements)
explicit inline constexpr indexed_matrix(const auto &...elements)
requires(std::tuple_size_v<RowIndexes> == 1 &&
std::tuple_size_v<ColumnIndexes> != 1 &&
sizeof...(elements) == std::tuple_size_v<ColumnIndexes>)
Expand All @@ -134,7 +133,7 @@ class physical_matrix {
//! @todo Fix the conversion index, perhaps with constexpr loop, and size
//! verification?
template <typename Type>
inline constexpr explicit physical_matrix(
inline constexpr explicit indexed_matrix(
std::initializer_list<std::initializer_list<Type>> rows) {
for (std::size_t i{0}; const auto &row : rows) {
for (std::size_t j{0}; const auto &element : row) {
Expand All @@ -147,7 +146,8 @@ class physical_matrix {

//! @todo Is this function safe? Correct?
template <typename Type>
explicit inline constexpr physical_matrix(const Type &other)
requires requires(Type value) { value.data; }
explicit inline constexpr indexed_matrix(const Type &other)
: data{other.data} {}

template <auto Index>
Expand Down Expand Up @@ -205,38 +205,38 @@ using one_column = std::tuple<placeholder_index>;

//! @brief Column vector.
template <typename Matrix, typename... RowIndexes>
using physical_column_vector =
physical_matrix<Matrix, std::tuple<RowIndexes...>, one_column>;
using indexed_column_vector =
indexed_matrix<Matrix, std::tuple<RowIndexes...>, one_column>;

//! @brief Row vector.
template <typename Matrix, typename... ColumnIndexes>
using physical_row_vector =
physical_matrix<Matrix, one_row, std::tuple<ColumnIndexes...>>;
using indexed_row_vector =
indexed_matrix<Matrix, one_row, std::tuple<ColumnIndexes...>>;

//! @brief Specialization of the evaluation type.
//!
//! @note Implementation not needed.
template <template <typename, typename, typename> typename PhysicalMatrix,
template <template <typename, typename, typename> typename IndexedMatrix,
typename Matrix, typename RowIndexes, typename ColumnIndexes>
struct evaluater<PhysicalMatrix<Matrix, RowIndexes, ColumnIndexes>> {
struct evaluater<IndexedMatrix<Matrix, RowIndexes, ColumnIndexes>> {
[[nodiscard]] inline constexpr auto operator()() const
-> PhysicalMatrix<evaluate<Matrix>, RowIndexes, ColumnIndexes>;
-> IndexedMatrix<evaluate<Matrix>, RowIndexes, ColumnIndexes>;
};

//! @brief Specialization of the transposer.
template <template <typename, typename, typename> typename PhysicalMatrix,
template <template <typename, typename, typename> typename IndexedMatrix,
typename Matrix, typename RowIndexes, typename ColumnIndexes>
requires requires(PhysicalMatrix<Matrix, RowIndexes, ColumnIndexes> m) {
requires requires(IndexedMatrix<Matrix, RowIndexes, ColumnIndexes> m) {
m.data;
}
struct transposer<PhysicalMatrix<Matrix, RowIndexes, ColumnIndexes>> {
struct transposer<IndexedMatrix<Matrix, RowIndexes, ColumnIndexes>> {
[[nodiscard]] inline constexpr auto operator()(
const PhysicalMatrix<Matrix, RowIndexes, ColumnIndexes> &value) const {
const IndexedMatrix<Matrix, RowIndexes, ColumnIndexes> &value) const {

evaluate<Matrix> result{value.data};

return PhysicalMatrix<evaluate<transpose<Matrix>>, ColumnIndexes,
RowIndexes>{t(result)};
return IndexedMatrix<evaluate<transpose<Matrix>>, ColumnIndexes,
RowIndexes>{t(result)};
}
};

Expand All @@ -245,85 +245,85 @@ struct transposer<PhysicalMatrix<Matrix, RowIndexes, ColumnIndexes>> {
//! @name Algebraic Named Values
//! @{

//! @brief The identity matrix physical specialization.
//! @brief The identity matrix indexed specialization.
//!
//! @todo The identity doesn't really make sense for matrices without units?
//! Unless it's the matrix without units? Even then, the identity matrix is
//! supposed to be square? What's the name of this thing then?
template <typename Matrix, typename RowIndexes, typename ColumnIndexes>
inline physical_matrix<Matrix, RowIndexes, ColumnIndexes>
identity<physical_matrix<Matrix, RowIndexes, ColumnIndexes>>{
inline indexed_matrix<Matrix, RowIndexes, ColumnIndexes>
identity<indexed_matrix<Matrix, RowIndexes, ColumnIndexes>>{
identity<Matrix>};

//! @brief The zero matrix physical specialization.
//! @brief The zero matrix indexed specialization.
template <typename Matrix, typename RowIndexes, typename ColumnIndexes>
inline physical_matrix<Matrix, RowIndexes, ColumnIndexes>
zero<physical_matrix<Matrix, RowIndexes, ColumnIndexes>>{zero<Matrix>};
inline indexed_matrix<Matrix, RowIndexes, ColumnIndexes>
zero<indexed_matrix<Matrix, RowIndexes, ColumnIndexes>>{zero<Matrix>};

//! @}

template <typename Matrix1, typename Matrix2, typename RowIndexes,
typename ColumnIndexes>
[[nodiscard]] inline constexpr auto
operator+(const physical_matrix<Matrix1, RowIndexes, ColumnIndexes> &lhs,
const physical_matrix<Matrix2, RowIndexes, ColumnIndexes> &rhs) {
operator+(const indexed_matrix<Matrix1, RowIndexes, ColumnIndexes> &lhs,
const indexed_matrix<Matrix2, RowIndexes, ColumnIndexes> &rhs) {
auto result{lhs.data + rhs.data};

return physical_matrix<decltype(result), RowIndexes, ColumnIndexes>{result};
return indexed_matrix<decltype(result), RowIndexes, ColumnIndexes>{result};
}

template <typename Matrix1, typename Matrix2, typename RowIndexes,
typename ColumnIndexes>
[[nodiscard]] inline constexpr auto
operator-(const physical_matrix<Matrix1, RowIndexes, ColumnIndexes> &lhs,
const physical_matrix<Matrix2, RowIndexes, ColumnIndexes> &rhs) {
operator-(const indexed_matrix<Matrix1, RowIndexes, ColumnIndexes> &lhs,
const indexed_matrix<Matrix2, RowIndexes, ColumnIndexes> &rhs) {
auto result{lhs.data - rhs.data};

return physical_matrix<decltype(result), RowIndexes, ColumnIndexes>{result};
return indexed_matrix<decltype(result), RowIndexes, ColumnIndexes>{result};
}

template <typename Matrix1, typename Matrix2, typename RowIndexes,
typename Indexes, typename ColumnIndexes>
[[nodiscard]] inline constexpr auto
operator*(const physical_matrix<Matrix1, RowIndexes, Indexes> &lhs,
const physical_matrix<Matrix2, Indexes, ColumnIndexes> &rhs) {
operator*(const indexed_matrix<Matrix1, RowIndexes, Indexes> &lhs,
const indexed_matrix<Matrix2, Indexes, ColumnIndexes> &rhs) {
auto result{lhs.data * rhs.data};

return physical_matrix<decltype(result), RowIndexes, ColumnIndexes>{result};
return indexed_matrix<decltype(result), RowIndexes, ColumnIndexes>{result};
}

template <typename Scalar, typename Matrix, typename RowIndexes,
typename ColumnIndexes>
[[nodiscard]] inline constexpr auto
operator*(Scalar lhs,
const physical_matrix<Matrix, RowIndexes, ColumnIndexes> &rhs) {
return physical_matrix<Matrix, RowIndexes, ColumnIndexes>{lhs * rhs.data};
const indexed_matrix<Matrix, RowIndexes, ColumnIndexes> &rhs) {
return indexed_matrix<Matrix, RowIndexes, ColumnIndexes>{lhs * rhs.data};
}

template <typename Matrix1, typename Matrix2, typename RowIndexes1,
typename RowIndexes2, typename ColumnIndexes>
[[nodiscard]] inline constexpr auto
operator/(const physical_matrix<Matrix1, RowIndexes1, ColumnIndexes> &lhs,
const physical_matrix<Matrix2, RowIndexes2, ColumnIndexes> &rhs) {
operator/(const indexed_matrix<Matrix1, RowIndexes1, ColumnIndexes> &lhs,
const indexed_matrix<Matrix2, RowIndexes2, ColumnIndexes> &rhs) {
auto result{lhs.data / rhs.data};

return physical_matrix<decltype(result), RowIndexes1, RowIndexes2>{result};
return indexed_matrix<decltype(result), RowIndexes1, RowIndexes2>{result};
}
} // namespace fcarouge

//! @brief Specialization of the standard formatter for the physical linear
//! @brief Specialization of the standard formatter for the indexed linear
//! algebra matrix.
template <typename Matrix, typename RowIndexes, typename ColumnIndexes,
typename Char>
struct std::formatter<
fcarouge::physical_matrix<Matrix, RowIndexes, ColumnIndexes>, Char> {
fcarouge::indexed_matrix<Matrix, RowIndexes, ColumnIndexes>, Char> {
constexpr auto parse(std::basic_format_parse_context<Char> &parse_context) {
return parse_context.begin();
}

template <typename OutputIterator>
constexpr auto format(
const fcarouge::physical_matrix<Matrix, RowIndexes, ColumnIndexes> &value,
const fcarouge::indexed_matrix<Matrix, RowIndexes, ColumnIndexes> &value,
std::basic_format_context<OutputIterator, Char> &format_context) const
-> OutputIterator {
format_context.advance_to(std::format_to(format_context.out(), "["));
Expand Down Expand Up @@ -364,7 +364,7 @@ struct std::formatter<

template <typename OutputIterator>
constexpr auto format(
const fcarouge::physical_matrix<Matrix, RowIndexes, ColumnIndexes> &value,
const fcarouge::indexed_matrix<Matrix, RowIndexes, ColumnIndexes> &value,
std::basic_format_context<OutputIterator, Char> &format_context) const
-> OutputIterator
requires(std::tuple_size_v<RowIndexes> == 1 &&
Expand Down Expand Up @@ -394,7 +394,7 @@ struct std::formatter<

template <typename OutputIterator>
constexpr auto format(
const fcarouge::physical_matrix<Matrix, RowIndexes, ColumnIndexes> &value,
const fcarouge::indexed_matrix<Matrix, RowIndexes, ColumnIndexes> &value,
std::basic_format_context<OutputIterator, Char> &format_context) const
-> OutputIterator
requires(std::tuple_size_v<RowIndexes> == 1 &&
Expand All @@ -411,4 +411,4 @@ struct std::formatter<
}
};

#endif // FCAROUGE_PHYSICAL_LINALG_HPP
#endif // FCAROUGE_INDEXED_LINALG_HPP
15 changes: 0 additions & 15 deletions support/mp_units/fcarouge/unit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,6 @@ using quantity = mp_units::quantity<Reference, Representation>;
template <mp_units::Quantity Type>
inline constexpr auto identity<Type>{Type::one()};

//! @brief Physical linear algebra matrix unit index type.
//!
//! @todo Constraint indexes with a concept: scalar/underlying, type, conversion
//! members?
template <auto Reference> struct quantity_index {
using scalar = double;
using type = quantity<Reference, scalar>;

//! @todo Can we do without this structure altogether with the help of
//! https://mpusz.github.io/mp-units/latest/users_guide/use_cases/interoperability_with_other_libraries/
[[nodiscard]] static constexpr auto convert(const auto &value) -> scalar {
return value.numerical_value_in(value.unit);
}
};

using mp_units::si::metre;
using mp_units::si::second;
using mp_units::si::unit_symbols::m;
Expand Down
45 changes: 45 additions & 0 deletions support/quantity/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#[[ __ _ __ __ _ _
| |/ / /\ | | | \/ | /\ | \ | |
| ' / / \ | | | \ / | / \ | \| |
| < / /\ \ | | | |\/| | / /\ \ | . ` |
| . \ / ____ \| |____| | | |/ ____ \| |\ |
|_|\_\/_/ \_\______|_| |_/_/ \_\_| \_|
Kalman Filter
Version 0.4.0
https://github.com/FrancoisCarouge/Kalman
SPDX-License-Identifier: Unlicense
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org> ]]

add_library(kalman_linalg_quantity INTERFACE)
target_sources(
kalman_linalg_quantity INTERFACE FILE_SET "linalg_headers" TYPE "HEADERS"
FILES "fcarouge/linalg.hpp")
target_link_libraries(
kalman_linalg_quantity INTERFACE kalman kalman_linalg_eigen
kalman_linalg_indexed)
Loading

0 comments on commit e69ea21

Please sign in to comment.