Skip to content

Commit b658c33

Browse files
committed
Sparse matrix support in block-diagonal LDLT
1 parent 8d4d929 commit b658c33

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

include/albatross/src/linalg/block_diagonal.hpp

+40
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ struct BlockDiagonalLDLT {
3232
Eigen::Matrix<_Scalar, _Rows, _Cols>
3333
sqrt_solve(const Eigen::Matrix<_Scalar, _Rows, _Cols> &rhs) const;
3434

35+
template <class _Scalar, int _Options, typename _StorageIndex>
36+
Eigen::Matrix<_Scalar, Eigen::Dynamic, Eigen::Dynamic>
37+
solve(const Eigen::SparseMatrix<_Scalar, _Options, _StorageIndex> &rhs) const;
38+
39+
template <class _Scalar, int _Options, typename _StorageIndex>
40+
Eigen::Matrix<_Scalar, Eigen::Dynamic, Eigen::Dynamic> sqrt_solve(
41+
const Eigen::SparseMatrix<_Scalar, _Options, _StorageIndex> &rhs) const;
42+
3543
template <class _Scalar, int _Rows, int _Cols>
3644
Eigen::Matrix<_Scalar, _Rows, _Cols>
3745
solve(const Eigen::Matrix<_Scalar, _Rows, _Cols> &rhs,
@@ -102,6 +110,38 @@ inline Eigen::Matrix<_Scalar, _Rows, _Cols> BlockDiagonalLDLT::sqrt_solve(
102110
return output;
103111
}
104112

113+
template <class _Scalar, int _Options, typename _StorageIndex>
114+
inline Eigen::Matrix<_Scalar, Eigen::Dynamic, Eigen::Dynamic>
115+
BlockDiagonalLDLT::solve(
116+
const Eigen::SparseMatrix<_Scalar, _Options, _StorageIndex> &rhs) const {
117+
ALBATROSS_ASSERT(cols() == rhs.rows());
118+
Eigen::Index i = 0;
119+
Eigen::Matrix<_Scalar, Eigen::Dynamic, Eigen::Dynamic> output(rows(),
120+
rhs.cols());
121+
for (const auto &b : blocks) {
122+
const auto rhs_chunk = rhs.block(i, 0, b.rows(), rhs.cols());
123+
output.block(i, 0, b.rows(), rhs.cols()) = b.solve(rhs_chunk);
124+
i += b.rows();
125+
}
126+
return output;
127+
}
128+
129+
template <class _Scalar, int _Options, typename _StorageIndex>
130+
inline Eigen::Matrix<_Scalar, Eigen::Dynamic, Eigen::Dynamic>
131+
BlockDiagonalLDLT::sqrt_solve(
132+
const Eigen::SparseMatrix<_Scalar, _Options, _StorageIndex> &rhs) const {
133+
ALBATROSS_ASSERT(cols() == rhs.rows());
134+
Eigen::Index i = 0;
135+
Eigen::Matrix<_Scalar, Eigen::Dynamic, Eigen::Dynamic> output(rows(),
136+
rhs.cols());
137+
for (const auto &b : blocks) {
138+
const auto rhs_chunk = rhs.block(i, 0, b.rows(), rhs.cols());
139+
output.block(i, 0, b.rows(), rhs.cols()) = b.sqrt_solve(rhs_chunk);
140+
i += b.rows();
141+
}
142+
return output;
143+
}
144+
105145
inline std::map<size_t, Eigen::Index>
106146
BlockDiagonalLDLT::block_to_row_map() const {
107147
Eigen::Index row = 0;

0 commit comments

Comments
 (0)