-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fmt::formatter specializations (#203)
- Loading branch information
Showing
3 changed files
with
170 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright (c) Sleipnir contributors | ||
|
||
#pragma once | ||
|
||
#include <concepts> | ||
|
||
#include <Eigen/Core> | ||
#include <Eigen/SparseCore> | ||
#include <fmt/format.h> | ||
|
||
#include "sleipnir/autodiff/Variable.hpp" | ||
#include "sleipnir/autodiff/VariableBlock.hpp" | ||
#include "sleipnir/autodiff/VariableMatrix.hpp" | ||
|
||
// FIXME: Doxygen gives internal inconsistency errors: | ||
// scope for class sleipnir::fmt::formatter< Derived, CharT > not found! | ||
// scope for class sleipnir::fmt::formatter< sleipnir::Variable > not found! | ||
// scope for class sleipnir::fmt::formatter< T > not found! | ||
|
||
//! @cond Doxygen_Suppress | ||
|
||
/** | ||
* Formatter for classes derived from Eigen::MatrixBase<Derived> or | ||
* Eigen::SparseCompressedBase<Derived>. | ||
*/ | ||
template <typename Derived, typename CharT> | ||
requires std::derived_from<Derived, Eigen::MatrixBase<Derived>> || | ||
std::derived_from<Derived, Eigen::SparseCompressedBase<Derived>> | ||
struct fmt::formatter<Derived, CharT> { | ||
constexpr auto parse(fmt::format_parse_context& ctx) { | ||
return m_underlying.parse(ctx); | ||
} | ||
|
||
auto format(const Derived& mat, fmt::format_context& ctx) const { | ||
auto out = ctx.out(); | ||
|
||
for (int row = 0; row < mat.rows(); ++row) { | ||
for (int col = 0; col < mat.cols(); ++col) { | ||
out = fmt::format_to(out, " "); | ||
out = m_underlying.format(mat.coeff(row, col), ctx); | ||
} | ||
|
||
if (row < mat.rows() - 1) { | ||
out = fmt::format_to(out, "\n"); | ||
} | ||
} | ||
|
||
return out; | ||
} | ||
|
||
private: | ||
fmt::formatter<typename Derived::Scalar, CharT> m_underlying; | ||
}; | ||
|
||
/** | ||
* Formatter for sleipnir::Variable. | ||
*/ | ||
template <> | ||
struct fmt::formatter<sleipnir::Variable> { | ||
constexpr auto parse(fmt::format_parse_context& ctx) { | ||
return m_underlying.parse(ctx); | ||
} | ||
|
||
auto format(const sleipnir::Variable& variable, | ||
fmt::format_context& ctx) const { | ||
return m_underlying.format(variable.Value(), ctx); | ||
} | ||
|
||
private: | ||
fmt::formatter<double> m_underlying; | ||
}; | ||
|
||
/** | ||
* Formatter for sleipnir::VariableBlock or sleipnir::VariableMatrix. | ||
*/ | ||
template <typename T> | ||
requires std::same_as<T, sleipnir::VariableBlock<sleipnir::VariableMatrix>> || | ||
std::same_as<T, sleipnir::VariableMatrix> | ||
struct fmt::formatter<T> { | ||
constexpr auto parse(fmt::format_parse_context& ctx) { | ||
return m_underlying.parse(ctx); | ||
} | ||
|
||
auto format(const T& mat, fmt::format_context& ctx) const { | ||
auto out = ctx.out(); | ||
|
||
for (int row = 0; row < mat.Rows(); ++row) { | ||
for (int col = 0; col < mat.Cols(); ++col) { | ||
out = fmt::format_to(out, " "); | ||
out = m_underlying.format(mat(row, col).Value(), ctx); | ||
} | ||
|
||
if (row < mat.Rows() - 1) { | ||
out = fmt::format_to(out, "\n"); | ||
} | ||
} | ||
|
||
return out; | ||
} | ||
|
||
private: | ||
fmt::formatter<double> m_underlying; | ||
}; | ||
|
||
//! @endcond |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) Sleipnir contributors | ||
|
||
#include <vector> | ||
|
||
#include <fmt/format.h> | ||
#include <gtest/gtest.h> | ||
#include <sleipnir/Formatters.hpp> | ||
|
||
TEST(FormattersTest, Eigen) { | ||
Eigen::Matrix<double, 3, 2> A{{0.0, 1.0}, {2.0, 3.0}, {4.0, 5.0}}; | ||
EXPECT_EQ( | ||
" 0.000000 1.000000\n" | ||
" 2.000000 3.000000\n" | ||
" 4.000000 5.000000", | ||
fmt::format("{:f}", A)); | ||
|
||
Eigen::MatrixXd B{{0.0, 1.0}, {2.0, 3.0}, {4.0, 5.0}}; | ||
EXPECT_EQ( | ||
" 0.000000 1.000000\n" | ||
" 2.000000 3.000000\n" | ||
" 4.000000 5.000000", | ||
fmt::format("{:f}", B)); | ||
|
||
Eigen::SparseMatrix<double> C{3, 2}; | ||
std::vector<Eigen::Triplet<double>> triplets; | ||
triplets.emplace_back(0, 1, 1.0); | ||
triplets.emplace_back(1, 0, 2.0); | ||
triplets.emplace_back(1, 1, 3.0); | ||
triplets.emplace_back(2, 0, 4.0); | ||
triplets.emplace_back(2, 1, 5.0); | ||
C.setFromTriplets(triplets.begin(), triplets.end()); | ||
EXPECT_EQ( | ||
" 0.000000 1.000000\n" | ||
" 2.000000 3.000000\n" | ||
" 4.000000 5.000000", | ||
fmt::format("{:f}", C)); | ||
} | ||
|
||
TEST(FormattersTest, Variable) { | ||
EXPECT_EQ("4.000000", fmt::format("{:f}", sleipnir::Variable{4.0})); | ||
} | ||
|
||
TEST(FormattersTest, VariableMatrix) { | ||
Eigen::Matrix<double, 3, 2> A{{0.0, 1.0}, {2.0, 3.0}, {4.0, 5.0}}; | ||
|
||
sleipnir::VariableMatrix B{3, 2}; | ||
B = A; | ||
EXPECT_EQ( | ||
" 0.000000 1.000000\n" | ||
" 2.000000 3.000000\n" | ||
" 4.000000 5.000000", | ||
fmt::format("{:f}", B)); | ||
|
||
sleipnir::VariableBlock<sleipnir::VariableMatrix> C{B}; | ||
EXPECT_EQ( | ||
" 0.000000 1.000000\n" | ||
" 2.000000 3.000000\n" | ||
" 4.000000 5.000000", | ||
fmt::format("{:f}", C)); | ||
} |