MUQ  0.4.3
LinearOperator.h
Go to the documentation of this file.
1 #ifndef LINEAROPERATOR_H
2 #define LINEAROPERATOR_H
3 
5 
6 #include <Eigen/Core>
7 
8 #include <memory>
9 #include <iostream>
10 #include <exception>
11 
13 
14 namespace muq
15 {
16 namespace Modeling
17 {
18 
19 
20 class LinearOperatorTypeException: public std::exception
21 {
22 public:
23  LinearOperatorTypeException(std::string const& type){
24  msg = "Tried creating a linear operator from an unsupported type " + type + ". Make sure all necessary headers are included and a child of LinearOperator exists for this type.";
25  };
26 
27  virtual const char* what() const throw()
28  {
29  return msg.c_str();
30  }
31 
32  std::string msg;
33 };
34 
35 class LinearOperator;
36 
37 template<typename MatrixType>
39 {
40  static std::shared_ptr<LinearOperator> Create(MatrixType const& A)
41  {
43 
44  return std::shared_ptr<LinearOperator>();
45 
46  };
47 };
48 
72 public:
73 
74  LinearOperator(int rowsIn, int colsIn, int numInputCols=1);
75 
76  virtual ~LinearOperator(){};
77 
79  virtual Eigen::MatrixXd Apply(Eigen::Ref<const Eigen::MatrixXd> const& x) = 0;
80 
82  virtual Eigen::MatrixXd ApplyTranspose(Eigen::Ref<const Eigen::MatrixXd> const& x) = 0;
83 
85  virtual void Apply(Eigen::Ref<const Eigen::MatrixXd> const& x, Eigen::Ref<Eigen::MatrixXd> y);
86 
88  virtual void ApplyTranspose(Eigen::Ref<const Eigen::MatrixXd> const& x, Eigen::Ref<Eigen::MatrixXd> y);
89 
91  int rows() const { return nrows; }
92 
94  int cols() const { return ncols; }
95 
97  virtual Eigen::MatrixXd GetMatrix();
98 
99  template<typename OtherType>
100  static std::shared_ptr<LinearOperator> Create(OtherType const& A)
101  {
103  }
104 
105 protected:
106 
107  const int ncols;
108  const int nrows;
109 
110  // ModPiece overrides
111  virtual void EvaluateImpl(muq::Modeling::ref_vector<Eigen::VectorXd> const& input) override;
112 
113  virtual void GradientImpl(unsigned int const outputDimWrt,
114  unsigned int const inputDimWrt,
116  Eigen::VectorXd const& sensitivity) override;
117 
118  virtual void JacobianImpl(unsigned int const outputDimWrt,
119  unsigned int const inputDimWrt,
120  muq::Modeling::ref_vector<Eigen::VectorXd> const& input) override;
121 
122  virtual void ApplyJacobianImpl(unsigned int const outputDimWrt,
123  unsigned int const inputDimWrt,
125  Eigen::VectorXd const& vec) override;
126 
127  virtual void ApplyHessianImpl(unsigned int const outWrt,
128  unsigned int inWrt1,
129  unsigned int inWrt2,
131  Eigen::VectorXd const& sens,
132  Eigen::VectorXd const& vec) override;
133 };
134 
135 
136 
137 
138 } // namespace Modeling
139 } // namespace MUQ
140 
141 #endif
virtual const char * what() const
LinearOperatorTypeException(std::string const &type)
Generic linear operator base class.
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
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
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
virtual void JacobianImpl(unsigned int const outputDimWrt, unsigned int const inputDimWrt, ref_vector< Eigen::VectorXd > const &input)
Definition: ModPiece.cpp:245
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
std::string demangle(const char *name)
Definition: Demangler.cpp:6
static std::shared_ptr< LinearOperator > Create(MatrixType const &A)