MUQ  0.4.3
CwiseUnaryOperator.h
Go to the documentation of this file.
1 #ifndef CWISEUNARYOPERATOR_H
2 #define CWISEUNARYOPERATOR_H
3 
4 #include <Eigen/Core>
5 #include <stan/math/fwd/scal.hpp>
6 
10 
11 namespace muq{
12  namespace Modeling{
13 
14  template<double (*T1)(double),
15  stan::math::fvar<double> (*T2)(stan::math::fvar<double> const&),
16  stan::math::fvar<stan::math::fvar<double>> (*T3)(stan::math::fvar<stan::math::fvar<double>> const&)>
17  class CwiseUnaryOperator : public ModPiece{
18 
19  public:
20  CwiseUnaryOperator(unsigned int dim) : ModPiece(dim*Eigen::VectorXi::Ones(1), dim*Eigen::VectorXi::Ones(1)){};
21 
22  protected:
23 
24  virtual std::string CreateName() const override
25  {
26  int status;
27  std::stringstream ss;
28 
29  std::string className = muq::Utilities::demangle(typeid(*this).name());
30  className = muq::Utilities::StringUtilities::Split(className, '<').at(1);
31  className = muq::Utilities::StringUtilities::Split(className, ',').at(0);
32  className.erase(0,2);
33  className.erase(className.size()-1,className.size());
34 
35  // Clean up the class name to remove long templates
36  ss << "CwiseUnaryOperator_" << className << "_" << id;
37 
38  return ss.str();
39  }
40 
41 
42  private:
43 
44 
45  virtual void EvaluateImpl(ref_vector<Eigen::VectorXd> const& in) override
46  {
47  outputs.resize(1);
48  outputs.at(0).resize(inputSizes(0));
49  for(int i=0; i<inputSizes(0); ++i)
50  outputs.at(0)(i) = T1(in.at(0).get()(i));
51  };
52 
53  virtual void JacobianImpl(unsigned int outWrt,
54  unsigned int inWrt,
55  ref_vector<Eigen::VectorXd> const& in) override
56  {
57  jacobian = Eigen::MatrixXd::Zero(inputSizes(0), inputSizes(0));
58  for(int i=0; i<inputSizes(0); ++i){
59  stan::math::fvar<double> x(in.at(0).get()(i), 1.0);
60  jacobian(i,i) = T2(x).tangent();
61  }
62  };
63 
64  virtual void GradientImpl(unsigned int outWrt,
65  unsigned int inWrt,
67  Eigen::VectorXd const& sens) override
68  {
69  gradient.resize(inputSizes(0));
70  for(int i=0; i<inputSizes(0); ++i){
71  stan::math::fvar<double> x(in.at(0).get()(i), 1.0);
72  gradient(i) = sens(i)*T2(x).tangent();
73  }
74  };
75 
76  virtual void ApplyJacobianImpl(unsigned int outWrt,
77  unsigned int inWrt,
79  Eigen::VectorXd const& vec) override
80  {
81  jacobianAction.resize(inputSizes(0));
82  for(int i=0; i<inputSizes(0); ++i){
83  stan::math::fvar<double> x(in.at(0).get()(i), 1.0);
84  jacobianAction(i) = vec(i)*T2(x).tangent();
85  }
86  };
87 
88  virtual void ApplyHessianImpl(unsigned int outWrt,
89  unsigned int inWrt1,
90  unsigned int inWrt2,
92  Eigen::VectorXd const& sens,
93  Eigen::VectorXd const& vec) override
94  {
95  hessAction.resize(inputSizes(0));
96 
97  // If this is with respect to the sensitivity input
98  if(inWrt2==1){
99 
100  for(int i=0; i<inputSizes(0); ++i){
101  stan::math::fvar<double> x(in.at(0).get()(i), 1.0);
102  hessAction(i) = vec(i)*T2(x).tangent();
103  }
104 
105  }else{
106  for(int i=0; i<inputSizes(0); ++i){
107  stan::math::fvar<stan::math::fvar<double>> x(stan::math::fvar<double>(in.at(0).get()(i), 1.0),1.0);
108  hessAction(i) = sens(i)*vec(i)*T3(x).tangent().tangent();
109  }
110  }
111  };
112 
113 
114  };
115 
116  // Define specific operators
152 
153  }
154 }
155 
156 #endif
virtual void GradientImpl(unsigned int outWrt, unsigned int inWrt, ref_vector< Eigen::VectorXd > const &in, Eigen::VectorXd const &sens) override
virtual std::string CreateName() const override
virtual void JacobianImpl(unsigned int outWrt, unsigned int inWrt, ref_vector< Eigen::VectorXd > const &in) override
virtual void EvaluateImpl(ref_vector< Eigen::VectorXd > const &in) override
virtual void ApplyHessianImpl(unsigned int outWrt, unsigned int inWrt1, unsigned int inWrt2, ref_vector< Eigen::VectorXd > const &in, Eigen::VectorXd const &sens, Eigen::VectorXd const &vec) override
virtual void ApplyJacobianImpl(unsigned int outWrt, unsigned int inWrt, ref_vector< Eigen::VectorXd > const &in, Eigen::VectorXd const &vec) override
Provides an abstract interface for defining vector-valued model components.
Definition: ModPiece.h:148
CwiseUnaryOperator< std::sin, stan::math::sin, stan::math::sin > SinOperator
CwiseUnaryOperator< std::erfc, stan::math::erfc, stan::math::erfc > ErfcOperator
CwiseUnaryOperator< stan::math::inv_Phi, stan::math::inv_Phi, stan::math::inv_Phi > InvPhiOperator
CwiseUnaryOperator< stan::math::Phi, stan::math::Phi, stan::math::Phi > PhiOperator
CwiseUnaryOperator< std::tan, stan::math::tan, stan::math::tan > TanOperator
CwiseUnaryOperator< std::ceil, stan::math::ceil, stan::math::ceil > CeilOperator
CwiseUnaryOperator< std::tgamma, stan::math::tgamma, stan::math::tgamma > TgammaOperator
CwiseUnaryOperator< std::sinh, stan::math::sinh, stan::math::sinh > SinhOperator
CwiseUnaryOperator< std::exp, stan::math::exp, stan::math::exp > ExpOperator
CwiseUnaryOperator< std::acos, stan::math::acos, stan::math::acos > AcosOperator
CwiseUnaryOperator< std::floor, stan::math::floor, stan::math::floor > FloorOperator
CwiseUnaryOperator< std::tanh, stan::math::tanh, stan::math::tanh > TanhOperator
CwiseUnaryOperator< std::atan, stan::math::atan, stan::math::atan > AtanOperator
CwiseUnaryOperator< stan::math::inv, stan::math::inv, stan::math::inv > InvOperator
CwiseUnaryOperator< std::erf, stan::math::erf, stan::math::erf > ErfOperator
CwiseUnaryOperator< std::cosh, stan::math::cosh, stan::math::cosh > CoshOperator
CwiseUnaryOperator< std::cbrt, stan::math::cbrt, stan::math::cbrt > CbrtOperator
CwiseUnaryOperator< stan::math::lgamma, stan::math::lgamma, stan::math::lgamma > LogGammaOperator
CwiseUnaryOperator< std::cos, stan::math::cos, stan::math::cos > CosOperator
CwiseUnaryOperator< std::log10, stan::math::log10, stan::math::log10 > Log10Operator
CwiseUnaryOperator< std::atanh, stan::math::atanh, stan::math::atanh > AtanhOperator
CwiseUnaryOperator< stan::math::log_inv_logit, stan::math::log_inv_logit, stan::math::log_inv_logit > LogInvLogitOperator
CwiseUnaryOperator< stan::math::square, stan::math::square, stan::math::square > SquareOperator
CwiseUnaryOperator< stan::math::trigamma, stan::math::trigamma, stan::math::trigamma > TrigammaOperator
CwiseUnaryOperator< std::abs, stan::math::abs, stan::math::abs > AbsOperator
CwiseUnaryOperator< std::asin, stan::math::asin, stan::math::asin > AsinOperator
CwiseUnaryOperator< stan::math::digamma, stan::math::digamma, stan::math::digamma > DigammaOperator
CwiseUnaryOperator< stan::math::inv_logit, stan::math::inv_logit, stan::math::inv_logit > InvLogitOperator
CwiseUnaryOperator< std::round, stan::math::round, stan::math::round > RoundOperator
CwiseUnaryOperator< stan::math::logit, stan::math::logit, stan::math::logit > LogitOperator
CwiseUnaryOperator< std::sqrt, stan::math::sqrt, stan::math::sqrt > SqrtOperator
CwiseUnaryOperator< stan::math::inv_square, stan::math::inv_square, stan::math::inv_square > InvSquareOperator
std::vector< std::reference_wrapper< const T > > ref_vector
A vector of references to something ...
Definition: WorkPiece.h:37
CwiseUnaryOperator< std::log2, stan::math::log2, stan::math::log2 > Log2Operator
CwiseUnaryOperator< stan::math::inv_sqrt, stan::math::inv_sqrt, stan::math::inv_sqrt > InvSqrtOperator
CwiseUnaryOperator< std::log, stan::math::log, stan::math::log > LogOperator
std::vector< std::string > Split(std::string str, char delim=',')
Split a string into parts based on a particular character delimiter. Also Strips whitespace from part...
std::string demangle(const char *name)
Definition: Demangler.cpp:6