MUQ  0.4.3
LinearOperator.cpp
Go to the documentation of this file.
2 
3 
4 using namespace muq::Modeling;
5 
6 LinearOperator::LinearOperator(int rowsIn, int colsIn, int numInputCols) : muq::Modeling::ModPiece(colsIn*numInputCols*Eigen::VectorXi::Ones(1),
7  rowsIn*numInputCols*Eigen::VectorXi::Ones(1)),
8  ncols(colsIn),
9  nrows(rowsIn)
10 {
11 };
12 
13 void LinearOperator::Apply(Eigen::Ref<const Eigen::MatrixXd> const& x, Eigen::Ref<Eigen::MatrixXd> y)
14 {
15  assert(y.cols()==x.cols());
16  y = Apply(x);
17 };
18 
19 
20 void LinearOperator::ApplyTranspose(Eigen::Ref<const Eigen::MatrixXd> const& x, Eigen::Ref<Eigen::MatrixXd> y)
21 {
22  assert(y.cols()==x.cols());
23  y = ApplyTranspose(x);
24 };
25 
26 Eigen::MatrixXd LinearOperator::GetMatrix()
27 {
28 
29  Eigen::MatrixXd output(nrows, ncols);
30  Eigen::MatrixXd rhs = Eigen::MatrixXd::Identity(ncols, ncols);
31 
32  for(int i=0; i<ncols; ++i)
33  output.col(i) = Apply(rhs.col(i));
34 
35  return output;
36 }
37 
39 {
40  outputs.resize(1);
41  outputs.at(0) = Apply(input.at(0).get()).col(0);
42 }
43 
44 void LinearOperator::GradientImpl(unsigned int const outputDimWrt,
45  unsigned int const inputDimWrt,
47  Eigen::VectorXd const& sensitivity)
48 {
49  gradient = ApplyTranspose(sensitivity);
50 }
51 
52 void LinearOperator::JacobianImpl(unsigned int const outputDimWrt,
53  unsigned int const inputDimWrt,
55 {
56  jacobian = GetMatrix();
57 }
58 
59 void LinearOperator::ApplyJacobianImpl(unsigned int const outputDimWrt,
60  unsigned int const inputDimWrt,
62  Eigen::VectorXd const& vec)
63 {
64  jacobianAction = Apply(vec);
65 }
66 
67 void LinearOperator::ApplyHessianImpl(unsigned int const outWrt,
68  unsigned int inWrt1,
69  unsigned int inWrt2,
71  Eigen::VectorXd const& sens,
72  Eigen::VectorXd const& vec)
73 {
74 
75  // If inWrt1==inWrt2, then the Hessian is zero
76  if(inWrt1==inWrt2){
77  hessAction = Eigen::VectorXd::Zero(cols());
78  }else{
79 
80  /* Since there is only one input, inWrt1!=inWrt2 will only be possible if inWrt2
81  is wrt to the sensitivity. Since the gradient is A^Ts, the Jacobian of this
82  wrt to the sensitivity is A^T.
83  */
85  }
86 }
LinearOperator(int rowsIn, int colsIn, int numInputCols=1)
virtual Eigen::MatrixXd Apply(Eigen::Ref< const Eigen::MatrixXd > const &x)=0
virtual Eigen::MatrixXd ApplyTranspose(Eigen::Ref< const Eigen::MatrixXd > const &x)=0
Provides an abstract interface for defining vector-valued model components.
Definition: ModPiece.h:148
Eigen::MatrixXd jacobian
Definition: ModPiece.h:506
virtual void GradientImpl(unsigned int const outputDimWrt, unsigned int const inputDimWrt, ref_vector< Eigen::VectorXd > const &input, Eigen::VectorXd const &sensitivity)
Definition: ModPiece.cpp:237
virtual void ApplyJacobianImpl(unsigned int const outputDimWrt, unsigned int const inputDimWrt, ref_vector< Eigen::VectorXd > const &input, Eigen::VectorXd const &vec)
Definition: ModPiece.cpp:262
std::vector< Eigen::VectorXd > outputs
Definition: ModPiece.h:503
virtual void EvaluateImpl(ref_vector< boost::any > const &inputs) override
User-implemented function that determines the behavior of this muq::Modeling::WorkPiece.
Definition: ModPiece.cpp:179
Eigen::VectorXd gradient
Definition: ModPiece.h:504
virtual void JacobianImpl(unsigned int const outputDimWrt, unsigned int const inputDimWrt, ref_vector< Eigen::VectorXd > const &input)
Definition: ModPiece.cpp:245
Eigen::VectorXd hessAction
Definition: ModPiece.h:507
Eigen::VectorXd jacobianAction
Definition: ModPiece.h:505
virtual void ApplyHessianImpl(unsigned int const outWrt, unsigned int const inWrt1, unsigned int const inWrt2, ref_vector< Eigen::VectorXd > const &input, Eigen::VectorXd const &sens, Eigen::VectorXd const &vec)
Definition: ModPiece.cpp:436
std::vector< std::reference_wrapper< const T > > ref_vector
A vector of references to something ...
Definition: WorkPiece.h:37