MUQ  0.4.3
BlockDiagonalOperator.cpp
Go to the documentation of this file.
2 
3 
4 using namespace muq::Modeling;
5 
6 
7 BlockDiagonalOperator::BlockDiagonalOperator(std::vector<std::shared_ptr<LinearOperator>> const& blocksIn) : LinearOperator(SumRows(blocksIn), SumCols(blocksIn)), blocks(blocksIn){};
8 
9 
10 Eigen::MatrixXd BlockDiagonalOperator::Apply(Eigen::Ref<const Eigen::MatrixXd> const& x)
11 {
12 
13  assert(x.rows() == ncols);
14 
15  int currRow = 0;
16  int currCol = 0;
17 
18  Eigen::MatrixXd output(nrows,x.cols());
19 
20  for(int i=0; i<blocks.size(); ++i)
21  {
22  output.block(currRow, 0, blocks.at(i)->rows(), x.cols()) = blocks.at(i)->Apply( x.block(currCol, 0, blocks.at(i)->cols(), x.cols()) );
23  currRow += blocks.at(i)->rows();
24  currCol += blocks.at(i)->cols();
25  }
26 
27  return output;
28 }
29 
30 Eigen::MatrixXd BlockDiagonalOperator::ApplyTranspose(Eigen::Ref<const Eigen::MatrixXd> const& x)
31 {
32 
33  assert(x.rows() == nrows);
34 
35  int currRow = 0;
36  int currCol = 0;
37 
38  Eigen::MatrixXd output(ncols,x.cols());
39 
40  for(int i=0; i<blocks.size(); ++i)
41  {
42  output.block(currRow, 0, blocks.at(i)->cols(), x.cols()) = blocks.at(i)->ApplyTranspose( x.block(currCol, 0, blocks.at(i)->rows(), x.cols()) );
43  currRow += blocks.at(i)->cols();
44  currCol += blocks.at(i)->rows();
45  }
46 
47  return output;
48 }
49 
51 {
52 
53  Eigen::MatrixXd output = Eigen::MatrixXd::Zero(nrows,ncols);
54  int currRow = 0;
55  int currCol = 0;
56 
57  for(int i=0; i<blocks.size(); ++i)
58  {
59  output.block(currRow, currCol, blocks.at(i)->rows(), blocks.at(i)->cols()) = blocks.at(i)->GetMatrix();
60  currRow += blocks.at(i)->rows();
61  currCol += blocks.at(i)->cols();
62  }
63 
64  return output;
65 }
66 
67 
68 int BlockDiagonalOperator::SumRows(std::vector<std::shared_ptr<LinearOperator>> const& blocksIn)
69 {
70  int sum = 0;
71  for(auto& block : blocksIn)
72  sum += block->rows();
73  return sum;
74 }
75 
76 int BlockDiagonalOperator::SumCols(std::vector<std::shared_ptr<LinearOperator>> const& blocksIn)
77 {
78  int sum = 0;
79  for(auto& block : blocksIn)
80  sum += block->cols();
81  return sum;
82 }
virtual Eigen::MatrixXd GetMatrix() override
static int SumRows(std::vector< std::shared_ptr< LinearOperator >> const &blocksIn)
BlockDiagonalOperator(std::vector< std::shared_ptr< LinearOperator >> const &blocksIn)
std::vector< std::shared_ptr< LinearOperator > > blocks
virtual Eigen::MatrixXd Apply(Eigen::Ref< const Eigen::MatrixXd > const &x) override
static int SumCols(std::vector< std::shared_ptr< LinearOperator >> const &blocksIn)
virtual Eigen::MatrixXd ApplyTranspose(Eigen::Ref< const Eigen::MatrixXd > const &x) override
Generic linear operator base class.