Skip to content

Commit

Permalink
[linalg] avoid auto and deduce type
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancoisCarouge committed Jan 13, 2025
1 parent c8de63e commit 787bc39
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 18 deletions.
12 changes: 6 additions & 6 deletions include/fcarouge/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,12 @@ inline constexpr auto first_v{internal::first_v<Values...>};
//!
//! @details User-defined.
template <typename Type = double>
inline constexpr Type identity{internal::not_implemented<Type>{
inline constexpr void identity{internal::not_implemented<Type>{
"Implement the linear algebra identity matrix for this type."}};

//! @brief The singleton identity matrix specialization.
template <arithmetic Arithmetic>
inline constexpr Arithmetic identity<Arithmetic>{1};
inline constexpr auto identity<Arithmetic>{Arithmetic{1}};

template <typename Type>
requires requires { Type::Identity(); }
Expand All @@ -263,20 +263,20 @@ inline auto identity<Type>{Type::identity()};
//!
//! @details User-defined.
template <typename Type = double>
inline constexpr Type zero{internal::not_implemented<Type>{
inline constexpr void zero{internal::not_implemented<Type>{
"Implement the linear algebra zero matrix for this type."}};

//! @brief The singleton zero matrix specialization.
template <arithmetic Arithmetic>
inline constexpr Arithmetic zero<Arithmetic>{0};
inline constexpr auto zero<Arithmetic>{Arithmetic{0}};

template <typename Type>
requires requires { Type::Zero(); }
inline auto zero<Type>{Type::Zero()};
inline auto zero<Type>{Type{Type::Zero()}};

template <typename Type>
requires requires { Type::zero(); }
inline auto zero<Type>{Type::zero()};
inline auto zero<Type>{Type{Type::zero()}};

//! @}

Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ test(NAME "linalg_multiplication_rxc" BACKENDS "eigen" "naive")
test(NAME "linalg_multiplication_sxc" BACKENDS "eigen" "naive")
test(NAME "linalg_operator_bracket" BACKENDS "eigen" "naive")
test(NAME "linalg_operator_equality" BACKENDS "eigen" "naive")
test(NAME "linalg_zero_default" BACKENDS "eigen" "naive")
test(NAME "linalg_zero" BACKENDS "eigen" "naive")
test(NAME "printer_1x1x0")
test(NAME "printer_2x3x4" BACKENDS "eigen")
test(NAME "units_kf_1x1x0_building_height")
test(NAME "utility_zero_default")
1 change: 0 additions & 1 deletion test/kalman_f_5x4x3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ template <auto Row, auto Column> using matrix = matrix<double, Row, Column>;
[[maybe_unused]] auto test{[] {
const auto i5x5{identity<matrix<5, 5>>};
const auto z5x5{zero<matrix<5, 5>>};
const vector<3> z3{zero<vector<3>>};
kalman filter{state{vector<5>{0., 0., 0., 0., 0.}}, output<vector<4>>,
input<vector<3>>, update_types<double, float, int>,
prediction_types<int, float, double>};
Expand Down
4 changes: 4 additions & 0 deletions test/kalman_format_1x4x3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ For more information, please refer to <https://unlicense.org> */
#include <cassert>
#include <format>

#include <print> //////////////////////////////////////////////////////////////

namespace fcarouge::test {
namespace {
template <auto Size> using vector = column_vector<double, Size>;
Expand All @@ -51,6 +53,8 @@ template <auto Size> using vector = column_vector<double, Size>;
[[maybe_unused]] auto test{[] {
kalman filter{state{vector<1>{0.}}, output<vector<4>>, input<vector<3>>};

std::println("{}", filter); /////////////////////////////////////////////////

assert(std::format("{}", filter) ==
R"({"f": 1,)"
R"( "g": [1, 0, 0],)"
Expand Down
4 changes: 4 additions & 0 deletions test/kalman_format_5x4x3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ For more information, please refer to <https://unlicense.org> */
#include <cassert>
#include <format>

#include <print> /////////////////////////////////////////////////////////////

namespace fcarouge::test {
namespace {
template <auto Size> using vector = column_vector<double, Size>;
Expand All @@ -52,6 +54,8 @@ template <auto Size> using vector = column_vector<double, Size>;
kalman filter{state{vector<5>{0., 0., 0., 0., 0.}}, output<vector<4>>,
input<vector<3>>};

std::println("{}", filter); /////////////////////////////////////////////////

assert(
std::format("{}", filter) ==
R"({"f": [[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]],)"
Expand Down
6 changes: 3 additions & 3 deletions test/linalg_multiplication_sxc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ namespace fcarouge::test {
namespace {
//! @test Verifies the assignment operator.
[[maybe_unused]] auto test{[] {
matrix<double, 2, 2> a{{1.0, 2.0}, {3.0, 4.0}};
matrix<double, 2, 1> b{3.0, 4.0};
auto r{a * b};
const matrix<double, 2, 2> a{{1.0, 2.0}, {3.0, 4.0}};
const matrix<double, 2, 1> b{3.0, 4.0};
const matrix<double, 2, 1> r{a * b};

assert(r(0, 0) == 11.0);
assert(r(1, 0) == 25.0);
Expand Down
6 changes: 3 additions & 3 deletions test/linalg_operator_equality.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ namespace fcarouge::test {
namespace {
//! @test Verifies the equality operator.
[[maybe_unused]] auto test{[] {
auto m{zero<matrix<double, 5, 5>>};
auto i{identity<matrix<double, 5, 5>>};
auto z{zero<matrix<double, 5, 5>>};
const matrix<double, 5, 5> m{zero<matrix<double, 5, 5>>};
const matrix<double, 5, 5> i{identity<matrix<double, 5, 5>>};
const matrix<double, 5, 5> z{zero<matrix<double, 5, 5>>};

assert(m == z);
assert(m != i);
Expand Down
2 changes: 1 addition & 1 deletion test/linalg_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace fcarouge::test {
namespace {
//! @test Verifies the zero matrices values are null.
[[maybe_unused]] auto test{[] {
auto z{zero<matrix<double, 3, 3>>};
const matrix<double, 3, 3> z{zero<matrix<double, 3, 3>>};

assert(z(0, 0) == 0.0);
assert(z(0, 1) == 0.0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org> */

#include "fcarouge/linalg.hpp"
#include "fcarouge/utility.hpp"

#include <cassert>
#include <type_traits>
Expand All @@ -45,10 +45,11 @@ namespace fcarouge::test {
namespace {
//! @test Verifies the 1x1 zero matrix deduced default value is a null double.
[[maybe_unused]] auto test{[] {
auto z{zero<>};
const double z{zero<>};
const auto a{zero<>};

assert(z == 0.0);
static_assert(std::is_same_v<decltype(z), double>);
static_assert(std::is_same_v<decltype(a), const double>);

return 0;
}()};
Expand Down

0 comments on commit 787bc39

Please sign in to comment.