13#ifndef REAL_TIME_TRANSPORT_BLOCK_MATRICES_BLOCK_DIAGONAL_H
14#define REAL_TIME_TRANSPORT_BLOCK_MATRICES_BLOCK_DIAGONAL_H
18#include <SciCore/Definitions.h>
21#include "../RealTimeTransport_export.h"
24namespace RealTimeTransport
64 template <
typename DenseMatrixT>
67 fromDense(matrix, blockDimensions);
163 template <
typename DenseMatrixT>
164 void fromDense(
const DenseMatrixT& matrix,
const std::vector<
int>& blockDimensions)
166#ifdef REAL_TIME_TRANSPORT_DEBUG
167 if (isBlockDiagonal(matrix, blockDimensions) ==
false)
169 throw Error(
"Matrix is not block diagonal as required");
173 size_t numBlocks = blockDimensions.size();
174 _blocks.resize(numBlocks);
177 for (
int i = 0; i <
static_cast<
int>(numBlocks); ++i)
179 _blocks[i] = extractDenseBlock(matrix, i, i, blockDimensions);
186 void setZero(
const std::vector<
int>& blockDimensions);
203 template <
class Archive>
204 void serialize(Archive& archive)
210 std::vector<MatrixType> _blocks;
224template <
typename ScalarT,
typename T>
228 const Eigen::Matrix<T, Eigen::Dynamic, 1>& x,
229 Eigen::Matrix<T, Eigen::Dynamic, 1>& result)
231#ifdef REAL_TIME_TRANSPORT_DEBUG
232 if (A.totalCols() != x.size() || A.totalRows() != result.size())
234 throw Error(
"Vector size does not match the total size of the block diagonal matrix.");
241 const auto& block = A(i);
242 result.segment(currentRow, block.rows()) += alpha * block * x.segment(currentRow, block.rows());
244 currentRow += block.rows();
249template <
typename ScalarT,
typename T>
250Eigen::Matrix<T, Eigen::Dynamic, 1> product(
252 const BlockDiagonalMatrix& A,
253 const Eigen::Matrix<T, Eigen::Dynamic, 1>& x)
255 using ReturnType = Eigen::Matrix<T, Eigen::Dynamic, 1>;
257 ReturnType returnValue = ReturnType::Zero(x.size());
258 addProduct(alpha, A, x, returnValue);
263Eigen::Matrix<T, Eigen::Dynamic, 1> operator*(
264 const BlockDiagonalMatrix& A,
265 const Eigen::Matrix<T, Eigen::Dynamic, 1>& x)
267 return product(1.0, A, x);
271template <
typename ScalarT,
typename T>
274 const Eigen::Matrix<T, 1, Eigen::Dynamic>& x,
276 Eigen::Matrix<T, 1, Eigen::Dynamic>& result)
278#ifdef REAL_TIME_TRANSPORT_DEBUG
279 if (A.totalRows() != x.size() || A.totalCols() != result.size())
281 throw Error(
"Vector size does not match the total size of the block diagonal matrix.");
288 const auto& block = A(i);
289 result.segment(currentCol, block.cols()) += alpha * x.segment(currentCol, block.cols()) * block;
291 currentCol += block.cols();
296template <
typename ScalarT,
typename T>
297Eigen::Matrix<T, 1, Eigen::Dynamic> product(
299 const Eigen::Matrix<T, 1, Eigen::Dynamic>& x,
300 const BlockDiagonalMatrix& A)
302 using ReturnType = Eigen::Matrix<T, 1, Eigen::Dynamic>;
304 ReturnType returnValue = ReturnType::Zero(x.size());
305 addProduct(alpha, x, A, returnValue);
310Eigen::Matrix<T, 1, Eigen::Dynamic> operator*(
311 const Eigen::Matrix<T, 1, Eigen::Dynamic>& x,
312 const BlockDiagonalMatrix& A)
315 return product(1.0, x, A);
#define REALTIMETRANSPORT_EXPORT
Definition RealTimeTransport_export.h:15
Represents a block diagonal matrix.
Definition BlockDiagonalMatrix.h:33
void setZero(const std::vector< int > &blockDimensions)
Sets the matrix to zero with block dimensions blockDimensions.
MatrixType & operator()(int i)
Returns the matrix block with index i.
void fromDense(const DenseMatrixT &matrix, const std::vector< int > &blockDimensions)
Constructs a block diagonal matrix from a dense matrix where the dimensions of the blocks are given b...
Definition BlockDiagonalMatrix.h:164
std::vector< int > blockDimensions() const
Returns a vector containing the dimensions of each block.
int totalRows() const noexcept
Returns the total number of rows.
const MatrixType & operator()(int i) const
Returns the matrix block with index i.
BlockDiagonalMatrix & operator*=(const BlockDiagonalMatrix &rhs)
Multiplies the block diagonal matrix rhs from the right to object.
int totalCols() const noexcept
Returns the total number of columns.
bool operator!=(const BlockDiagonalMatrix &other) const
Inequality comparison operator.
BlockDiagonalMatrix() noexcept
Constructs an empty block diagonal matrix.
Scalar & element(int row, int colum)
Returns the matrix element at a given row and column.
void fromBlocks(std::vector< MatrixType > &&newBlocks)
Creates a new block diagonal matrix from given blocks.
BlockDiagonalMatrix(const BlockDiagonalMatrix &other)
Copy constructor.
bool operator==(const BlockDiagonalMatrix &other) const
Equality comparison operator.
BlockDiagonalMatrix & operator+=(const BlockDiagonalMatrix &rhs)
Adds the block diagonal matrix rhs to the object.
const Scalar & element(int row, int column) const
Returns the matrix element at a given row and column.
BlockDiagonalMatrix & operator*=(SciCore::Real scalar)
Multiplies the object by the a scalar.
int numBlocks() const noexcept
Multiplies the number of blocks.
BlockDiagonalMatrix & operator=(const BlockDiagonalMatrix &other)
Copy assignment operator.
BlockDiagonalMatrix(std::vector< MatrixType > &&blocks) noexcept
Constructs a block diagonal matrix with given blocks.
BlockDiagonalMatrix & operator=(BlockDiagonalMatrix &&other) noexcept
Move assignment operator.
BlockDiagonalMatrix & operator-=(const BlockDiagonalMatrix &rhs)
Subtracts the block diagonal matrix rhs from the object.
static BlockDiagonalMatrix Zero(const std::vector< int > &blockDimensions)
Returns a matrix with given blockDimensions that is zero everywhere.
BlockDiagonalMatrix(const DenseMatrixT &matrix, const std::vector< int > &blockDimensions)
Constructs a block diagonal matrix from a dense matrix where the dimensions of the blocks are given b...
Definition BlockDiagonalMatrix.h:65
MatrixType toDense() const
Returns a dense matrix representation.
static BlockDiagonalMatrix Identity(const std::vector< int > &blockDimensions)
Returns a matrix with given blockDimensions equal to the identity matrix.
BlockDiagonalMatrix(BlockDiagonalMatrix &&other) noexcept
Move constructor.