24#include <gtsam/dllexport.h>
25#if GTSAM_ENABLE_BOOST_SERIALIZATION
26#include <boost/serialization/nvp.hpp>
36namespace serialization {
45class CompactLeafSchurKernel;
54 return column >= begin && column < end;
59 return (*
this)(std::max(I, J));
65class VerticalBlockMatrix;
83 typedef Eigen::Block<Matrix> Block;
84 typedef Eigen::Block<const Matrix> constBlock;
99 template <
typename CONTAINER>
101 bool appendOneDimension =
false)
103 fillOffsets(dimensions.begin(), dimensions.end(), appendOneDimension);
109 template <
typename ITERATOR>
111 bool appendOneDimension =
false)
113 fillOffsets(firstBlockDim, lastBlockDim, appendOneDimension);
120 template <
typename CONTAINER>
122 bool appendOneDimension =
false)
124 matrix_.resize(matrix.rows(), matrix.cols());
125 matrix_.triangularView<Eigen::Upper>() =
126 matrix.triangularView<Eigen::Upper>();
127 fillOffsets(dimensions.begin(), dimensions.end(), appendOneDimension);
129 throw std::invalid_argument(
130 "Requested to create a SymmetricBlockMatrix from a non-square "
133 throw std::invalid_argument(
134 "Requested to create a SymmetricBlockMatrix with dimensions that do "
135 "not sum to the total size of the provided matrix.");
192 return block_(J, J).selfadjointView<Eigen::Upper>();
198 return block_(J, J).selfadjointView<Eigen::Upper>();
214 return block_(I, I, J - I, J - I).selfadjointView<Eigen::Upper>();
222 return block_(I, I, J - I, J - I).triangularView<Eigen::Upper>();
229 assert(i_startBlock < j_startBlock);
230 assert(i_endBlock <= j_startBlock);
231 return block_(i_startBlock, j_startBlock, i_endBlock - i_startBlock,
232 j_endBlock - j_startBlock);
238 assert(i_startBlock < j_startBlock);
239 assert(i_endBlock <= j_startBlock);
240 return block_(i_startBlock, j_startBlock, i_endBlock - i_startBlock,
241 j_endBlock - j_startBlock);
250 template <
typename XprType>
252 block_(I, I).triangularView<Eigen::Upper>() =
258 template <
typename XprType>
264 block_(J, I) = xpr.transpose();
270 template <
typename XprType>
275 assert(dest.rows() == xpr.rows());
276 assert(dest.cols() == xpr.cols());
277 for (
DenseIndex col = 0; col < dest.cols(); ++col) {
279 dest(row, col) += xpr(row, col);
285 template <
typename XprType>
288 matrix_.block(scalarOffset, scalarOffset, xpr.rows(), xpr.cols());
289 assert(dest.rows() == dest.cols());
290 for (
DenseIndex col = 0; col < dest.cols(); ++col) {
292 dest(row, col) += xpr(row, col);
298 template <
int Dimension,
typename XprType>
302 for (
int col = 0; col < Dimension; ++col) {
303 for (
int row = 0; row <= col; ++row) {
304 dest(row, col) += xpr(row, col);
312 assert(dest.rows() == deltaDiag.size());
313 dest.diagonal().array() += deltaDiag.array();
319 dest.diagonal().array() += lambda;
324 template <
typename XprType>
327 if constexpr (XprType::SizeAtCompileTime == 1) {
329 auto destination =
block_(I, J);
330 assert(destination.rows() == 1 && destination.cols() == 1);
331 destination(0, 0) += xpr.coeff(0, 0);
333 auto destination =
block_(J, I);
334 assert(destination.rows() == 1 && destination.cols() == 1);
335 destination(0, 0) += xpr.coeff(0, 0);
339 block_(I, J).noalias() += xpr;
341 block_(J, I).noalias() += xpr.transpose();
347 template <
typename XprType>
349 const XprType& xpr) {
350 assert(rowOffset < colOffset);
351 if constexpr (XprType::SizeAtCompileTime == 1) {
352 matrix_(rowOffset, colOffset) += xpr.coeff(0, 0);
354 matrix_.block(rowOffset, colOffset, xpr.rows(), xpr.cols()) += xpr;
359 template <
int Rows,
int Cols,
typename XprType>
361 const XprType& xpr) {
362 assert(rowOffset < colOffset);
363 if constexpr (Rows == 1 && Cols == 1) {
364 assert(xpr.rows() == 1 && xpr.cols() == 1);
365 matrix_(rowOffset, colOffset) += xpr.coeff(0, 0);
374 const std::vector<DenseIndex>& blockIndices);
378 const std::vector<DenseIndex>& blockIndices,
379 const std::vector<DenseIndex>& scalarOffsets);
390 const std::vector<DenseIndex>& blockIndices,
391 const std::vector<DenseIndex>& scalarOffsets);
404 const std::vector<DenseIndex>& blockIndices,
405 const std::vector<DenseIndex>& scalarOffsets,
406 bool ownLastCrossBlock =
false);
413 const std::vector<DenseIndex>& blockIndices,
420 void updateFromOuterProductBlocks(
423 const std::vector<DenseIndex>& blockIndices,
424 const std::vector<DenseIndex>& scalarOffsets,
double alpha = 1.0);
429 full().triangularView<Eigen::Upper>() += other.
full();
438 return full().selfadjointView<Eigen::Upper>();
443 return full().selfadjointView<Eigen::Upper>();
448 template <
typename XprType>
450 full().triangularView<Eigen::Upper>() =
463 assert(beginCol < endCol);
464 assert(beginCol >= 0);
468 static_assert(Matrix::IsRowMajor == 0,
469 "setZeroColumns requires column-major storage.");
478 memset(begin, 0, (end - begin) *
sizeof(*begin));
485 void invertInPlace();
539 const std::array<DenseIndex, 4> indices =
541 return matrix_.block(indices[0], indices[1], indices[2], indices[3]);
547 const std::array<DenseIndex, 4> indices =
549 return matrix_.block(indices[0], indices[1], indices[2], indices[3]);
562 assert(blockRows >= 0);
563 assert(blockCols >= 0);
570 return {{denseI, denseJ, denseRows, denseCols}};
573 void assertInvariants()
const {
574 assert(matrix_.rows() == matrix_.cols());
575 assert(matrix_.cols() == variableColOffsets_.back());
576 assert(blockStart_ < (
DenseIndex)variableColOffsets_.size());
579 template <
typename ITERATOR>
580 void fillOffsets(ITERATOR firstBlockDim, ITERATOR lastBlockDim,
581 bool appendOneDimension) {
582 variableColOffsets_.resize((lastBlockDim - firstBlockDim) + 1 +
583 (appendOneDimension ? 1 : 0));
584 variableColOffsets_[0] = 0;
586 for (ITERATOR dim = firstBlockDim; dim != lastBlockDim; ++dim) {
587 variableColOffsets_[j + 1] = variableColOffsets_[j] + *dim;
590 if (appendOneDimension) {
591 variableColOffsets_[j + 1] = variableColOffsets_[j] + 1;
596 friend class VerticalBlockMatrix;
597 friend class internal::CompactLeafSchurKernel;
598 template <
typename SymmetricBlockMatrixType>
599 friend class SymmetricBlockMatrixBlockExpr;
602#if GTSAM_ENABLE_BOOST_SERIALIZATION
604 friend class boost::serialization::access;
605 template <
class ARCHIVE>
606 void serialize(ARCHIVE& ar,
const unsigned int ) {
611 matrix_.triangularView<Eigen::Lower>() =
612 matrix_.triangularView<Eigen::Upper>().transpose();
613 ar& BOOST_SERIALIZATION_NVP(matrix_);
614 ar& BOOST_SERIALIZATION_NVP(variableColOffsets_);
615 ar& BOOST_SERIALIZATION_NVP(blockStart_);
A thin wrapper around std::vector that uses a custom allocator.
Typedefs for easier changing of types.
typedef and functions to augment Eigen's MatrixXd
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
FastVector is a type alias to a std::vector with a custom memory allocator.
Definition FastVector.h:33
Global functions in a separate testing namespace.
Definition chartTesting.h:28
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition types.h:49
void split(const G &g, const PredecessorMap< KEY > &tree, G &Ab1, G &Ab2)
Split the graph into two parts: one corresponds to the given spanning tree, and the other corresponds...
Definition graph-inl.h:245
bool choleskyPartial(Matrix &ABC, size_t nFrontal, size_t topleft)
Partial Cholesky computes a factor [R S such that [R' 0 [R S = [A B 0 L] S' I] 0 L] B' C].
Definition cholesky.cpp:114
Half-open range of physical upper-triangular block columns.
Definition SymmetricBlockMatrix.h:48
bool operator()(DenseIndex column) const
Return whether this range owns a physical block column.
Definition SymmetricBlockMatrix.h:53
bool owns(DenseIndex I, DenseIndex J) const
Return whether this range owns the stored block for logical indices I,J.
Definition SymmetricBlockMatrix.h:58
This class stores a dense matrix and allows it to be accessed as a collection of blocks.
Definition SymmetricBlockMatrix.h:80
Block full()
Get the full matrix as a block.
Definition SymmetricBlockMatrix.h:556
DenseIndex blockStart_
Changes apparent matrix view, see main class comment.
Definition SymmetricBlockMatrix.h:92
void updateFixedOffDiagonalBlockAt(DenseIndex rowOffset, DenseIndex colOffset, const XprType &xpr)
Increment a fixed-size upper block using cached scalar offsets.
Definition SymmetricBlockMatrix.h:360
void setZeroColumns(DenseIndex beginCol, DenseIndex endCol)
Set the block columns between beginCol (inclusive) and endCol (exclusive) to zero.
Definition SymmetricBlockMatrix.h:462
void setDiagonalBlock(DenseIndex I, const XprType &xpr)
Set a diagonal block.
Definition SymmetricBlockMatrix.h:251
DenseIndex nActualBlocks() const
Number of actual blocks in the full matrix.
Definition SymmetricBlockMatrix.h:526
Vector diagonal(DenseIndex J) const
Get the diagonal of the J'th diagonal block.
Definition SymmetricBlockMatrix.h:202
Matrix matrix_
The full matrix.
Definition SymmetricBlockMatrix.h:87
void setAllZero()
Set entire matrix zero.
Definition SymmetricBlockMatrix.h:458
void addToDiagonalBlock(DenseIndex I, const Vector &deltaDiag)
Add a vector to the diagonal entries of block I.
Definition SymmetricBlockMatrix.h:310
DenseIndex cols() const
Column size.
Definition SymmetricBlockMatrix.h:159
DenseIndex getDim(DenseIndex block) const
Number of dimensions for variable on this diagonal block.
Definition SymmetricBlockMatrix.h:165
void setZero()
Set the entire active matrix zero.
Definition SymmetricBlockMatrix.h:455
void setOffDiagonalBlock(DenseIndex I, DenseIndex J, const XprType &xpr)
Set an off-diagonal block.
Definition SymmetricBlockMatrix.h:259
void setFullMatrix(const XprType &xpr)
Set the entire active matrix.
Definition SymmetricBlockMatrix.h:449
Eigen::SelfAdjointView< Block, Eigen::Upper > selfadjointView()
Get self adjoint view.
Definition SymmetricBlockMatrix.h:437
constBlock aboveDiagonalBlock(DenseIndex I, DenseIndex J) const
Get block above the diagonal (I, J).
Definition SymmetricBlockMatrix.h:205
void addUpperTriangular(const SymmetricBlockMatrix &other)
Add the upper-triangular part of another symmetric block matrix.
Definition SymmetricBlockMatrix.h:427
constBlock block_(DenseIndex iBlock, DenseIndex jBlock, DenseIndex blockRows=1, DenseIndex blockCols=1) const
Get an arbitrary block from the matrix. Indices are in block units.
Definition SymmetricBlockMatrix.h:537
void updateFixedDiagonalBlockAt(DenseIndex scalarOffset, const XprType &xpr)
Increment a fixed-size diagonal block using its cached scalar offset.
Definition SymmetricBlockMatrix.h:299
constBlock full() const
Get the full matrix as a block.
Definition SymmetricBlockMatrix.h:553
DenseIndex rows() const
Row size.
Definition SymmetricBlockMatrix.h:153
Block aboveDiagonalRange(DenseIndex i_startBlock, DenseIndex i_endBlock, DenseIndex j_startBlock, DenseIndex j_endBlock)
Get a range [i,j) from the matrix. Indices are in block units.
Definition SymmetricBlockMatrix.h:236
Eigen::TriangularView< constBlock, Eigen::Upper > triangularView(DenseIndex I, DenseIndex J) const
Return the square sub-matrix that contains blocks(i:j, i:j) as a triangular view.
Definition SymmetricBlockMatrix.h:219
Eigen::SelfAdjointView< constBlock, Eigen::Upper > diagonalBlock(DenseIndex J) const
Return the J'th diagonal block as a self adjoint view.
Definition SymmetricBlockMatrix.h:196
SymmetricBlockMatrix(const CONTAINER &dimensions, const Matrix &matrix, bool appendOneDimension=false)
Construct from a container of the sizes of each vertical block and a pre-prepared matrix.
Definition SymmetricBlockMatrix.h:121
DenseIndex blockStart() const
Retrieve the first logical block, i.e.
Definition SymmetricBlockMatrix.h:497
Matrix block(DenseIndex I, DenseIndex J) const
Get a copy of a block (anywhere in the matrix).
Definition SymmetricBlockMatrix.cpp:65
Eigen::SelfAdjointView< constBlock, Eigen::Upper > selfadjointView() const
Get self adjoint view.
Definition SymmetricBlockMatrix.h:442
void updateOffDiagonalBlock(DenseIndex I, DenseIndex J, const XprType &xpr)
Update an off diagonal block.
Definition SymmetricBlockMatrix.h:325
SymmetricBlockMatrix(const CONTAINER &dimensions, bool appendOneDimension=false)
Construct from a container of the sizes of each block.
Definition SymmetricBlockMatrix.h:100
void updateDiagonalBlockAt(DenseIndex scalarOffset, const XprType &xpr)
Increment a diagonal block using its cached scalar offset.
Definition SymmetricBlockMatrix.h:286
DenseIndex blockScalarOffset(DenseIndex block) const
Return the scalar offset of a block in the underlying matrix.
Definition SymmetricBlockMatrix.h:170
SymmetricBlockMatrix(ITERATOR firstBlockDim, ITERATOR lastBlockDim, bool appendOneDimension=false)
Construct from iterator over the sizes of each vertical block.
Definition SymmetricBlockMatrix.h:110
DenseIndex & blockStart()
Retrieve or modify the first logical block, i.e.
Definition SymmetricBlockMatrix.h:492
constBlock aboveDiagonalRange(DenseIndex i_startBlock, DenseIndex i_endBlock, DenseIndex j_startBlock, DenseIndex j_endBlock) const
Get a range [i,j) from the matrix. Indices are in block units.
Definition SymmetricBlockMatrix.h:226
SymmetricBlockMatrix()
Construct from an empty matrix (asserts that the matrix is empty).
Definition SymmetricBlockMatrix.cpp:31
Eigen::SelfAdjointView< Block, Eigen::Upper > diagonalBlock(DenseIndex J)
Return the J'th diagonal block as a self adjoint view.
Definition SymmetricBlockMatrix.h:191
Block block_(DenseIndex iBlock, DenseIndex jBlock, DenseIndex blockRows=1, DenseIndex blockCols=1)
Get an arbitrary block from the matrix. Indices are in block units.
Definition SymmetricBlockMatrix.h:545
void updateDiagonalBlock(DenseIndex I, const XprType &xpr)
Increment the diagonal block by the values in xpr.
Definition SymmetricBlockMatrix.h:271
DenseIndex nOffsets() const
Number of offsets in the full matrix.
Definition SymmetricBlockMatrix.h:523
std::array< DenseIndex, 4 > calcIndices(DenseIndex iBlock, DenseIndex jBlock, DenseIndex blockRows, DenseIndex blockCols) const
Compute the indices into the underlying matrix for a given block.
Definition SymmetricBlockMatrix.h:559
DenseIndex nBlocks() const
Block count.
Definition SymmetricBlockMatrix.h:162
FastVector< DenseIndex > variableColOffsets_
the starting columns of each block (0-based)
Definition SymmetricBlockMatrix.h:89
constBlock blockView(DenseIndex I, DenseIndex J) const
Get a block view (anywhere in the matrix) without allocating a copy.
Definition SymmetricBlockMatrix.h:186
Eigen::SelfAdjointView< constBlock, Eigen::Upper > selfadjointView(DenseIndex I, DenseIndex J) const
Return the square sub-matrix that contains blocks(i:j, i:j).
Definition SymmetricBlockMatrix.h:211
void addScaledIdentity(DenseIndex I, double lambda)
Add lambda * I to the diagonal block I.
Definition SymmetricBlockMatrix.h:317
DenseIndex offset(DenseIndex block) const
Get an offset for a block index (in the active view).
Definition SymmetricBlockMatrix.h:529
void updateOffDiagonalBlockAt(DenseIndex rowOffset, DenseIndex colOffset, const XprType &xpr)
Increment an upper off-diagonal block using cached scalar offsets.
Definition SymmetricBlockMatrix.h:348
Indicate Cholesky factorization failure.
Definition ThreadsafeException.h:116
This class stores a dense matrix and allows it to be accessed as a collection of vertical blocks.
Definition VerticalBlockMatrix.h:47