Skip to content

Commit 0da29ea

Browse files
committed
Added BlkMatrix::isApprox()
1 parent 1eb0f3d commit 0da29ea

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- gar/blk-matrix : added `BlkMatrix::isApprox()` (https://github.com/Simple-Robotics/aligator/pull/366)
1213
- core : added [mimalloc](https://github.com/mimalloc/mimalloc)-based memory resource (https://github.com/Simple-Robotics/aligator/pull/366)
1314
- modelling/costs : add getter and setter for cost term weights (https://github.com/Simple-Robotics/aligator/pull/359)
1415
- support for Pinocchio 4 (https://github.com/Simple-Robotics/aligator/pull/361)
@@ -18,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1819

1920
### Changed
2021

22+
- tests/gar/block-matrix.cpp : test against BunchKaufman factorization
2123
- move headers `allocator.hpp` and `arena-matrix.hpp` to `aligator/core` dir (https://github.com/Simple-Robotics/aligator/pull/362)
2224
- python: aligator now requires eigenpy version 3.10.3 at least
2325
- python: remove eigenpy version checks

include/aligator/gar/blk-matrix.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,20 @@ template <typename _MatrixType, int _N, int _M> class BlkMatrix {
171171
MatrixType &matrix() { return m_data; }
172172
const MatrixType &matrix() const { return m_data; }
173173

174+
template <typename D>
175+
inline bool isApprox(
176+
const Eigen::EigenBase<D> &other,
177+
const Scalar prec = Eigen::NumTraits<Scalar>::dummy_precision()) const {
178+
return matrix().isApprox(other, prec);
179+
}
180+
181+
template <typename D, int N2, int M2>
182+
inline bool isApprox(
183+
const BlkMatrix<D, N2, M2> &other,
184+
const Scalar prec = Eigen::NumTraits<Scalar>::dummy_precision()) const {
185+
return matrix().isApprox(other.matrix(), prec);
186+
}
187+
174188
const RowDimsType &rowDims() const { return m_rowDims; }
175189
const RowDimsType &rowIndices() const { return m_rowIndices; }
176190
const ColDimsType &colDims() const { return m_colDims; }
@@ -179,6 +193,7 @@ template <typename _MatrixType, int _N, int _M> class BlkMatrix {
179193
Index rows() const { return m_totalRows; }
180194
Index cols() const { return m_totalCols; }
181195

196+
/// @brief Take the top \c n block rows of the block matrix.
182197
auto topBlkRows(size_t n) {
183198
using OutType = BlkMatrix<Eigen::Ref<MatrixType>, -1, M>;
184199
std::vector<Index> subRowDims;
@@ -188,6 +203,8 @@ template <typename _MatrixType, int _N, int _M> class BlkMatrix {
188203
return OutType(m_data.topRows(ntr), subRowDims, m_colDims);
189204
}
190205

206+
/// @copybrief topBlkRows().
207+
/// This version returns a fixed-size block.
191208
template <size_t n> auto topBlkRows() {
192209
static_assert(n <= N,
193210
"Cannot take n block rows of matrix with <n block rows.");

tests/gar/block-matrix.cpp

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "aligator/context.hpp"
77
#include "aligator/gar/blk-matrix.hpp"
88
#include "aligator/gar/block-tridiagonal.hpp"
9+
#include "aligator/core/bunchkaufman.hpp"
910
#include <Eigen/Cholesky>
1011

1112
#include "test_util.hpp"
@@ -95,26 +96,17 @@ struct BTAG_Fixture {
9596
}
9697
};
9798

99+
constexpr double prec = 1e-12;
100+
98101
TEST_CASE_METHOD(BTAG_Fixture, "block_tridiag_solve_up_looking", "[gar]") {
99102

100103
bool ret = gar::symmetricBlockTridiagSolve(sub, diagonal, sup, vec, facs);
101104
REQUIRE(ret);
102105

103-
for (size_t i = 0; i <= N; i++) {
104-
fmt::print("rhs[{:d}] = {}\n", i, vec[i].transpose());
105-
}
106-
107-
{
108-
// alternative solve
109-
Eigen::LDLT<MatrixXs> ldlt(densemat);
110-
ldlt.solveInPlace(rhs.matrix());
106+
aligator::BunchKaufman<MatrixXs> ldlt(densemat);
107+
ldlt.solveInPlace(rhs.matrix());
111108

112-
fmt::print("Alternative solve:\n");
113-
for (size_t i = 0; i <= N; i++) {
114-
fmt::print("rhs[{:d}] = {}\n", i, rhs[i].transpose());
115-
}
116-
REQUIRE(vec.matrix().isApprox(rhs.matrix(), 1e-12));
117-
}
109+
REQUIRE(vec.isApprox(rhs, prec));
118110
}
119111

120112
TEST_CASE_METHOD(BTAG_Fixture, "block_tridiag_solve_down_looking", "[gar]") {
@@ -123,19 +115,8 @@ TEST_CASE_METHOD(BTAG_Fixture, "block_tridiag_solve_down_looking", "[gar]") {
123115
gar::symmetricBlockTridiagSolveDownLooking(sub, diagonal, sup, vec, facs);
124116
REQUIRE(ret);
125117

126-
for (size_t i = 0; i <= N; i++) {
127-
fmt::print("rhs[{:d}] = {}\n", i, vec[i].transpose());
128-
}
129-
130-
{
131-
// alternative solve
132-
Eigen::LDLT<MatrixXs> ldlt(densemat);
133-
ldlt.solveInPlace(rhs.matrix());
118+
aligator::BunchKaufman<MatrixXs> ldlt(densemat);
119+
ldlt.solveInPlace(rhs.matrix());
134120

135-
fmt::print("Alternative solve:\n");
136-
for (size_t i = 0; i <= N; i++) {
137-
fmt::print("rhs[{:d}] = {}\n", i, rhs[i].transpose());
138-
}
139-
REQUIRE(vec.matrix().isApprox(rhs.matrix(), 1e-12));
140-
}
121+
REQUIRE(vec.isApprox(rhs, prec));
141122
}

0 commit comments

Comments
 (0)