10 SimpleModel(
int numPts) :
muq::Modeling::
ModPiece({numPts,2},{numPts}) {};
15 Eigen::VectorXd
const& x = inputs.at(0).get();
16 Eigen::VectorXd
const& c = inputs.at(1).get();
18 Eigen::VectorXd y = c(0)*x + c(1)*Eigen::VectorXd::Ones(x.size());
28 Eigen::VectorXd
const& x = inputs.at(0).get();
29 Eigen::VectorXd
const& c = inputs.at(1).get();
33 jacobian = c(0)*Eigen::VectorXd::Identity(x.size(), x.size());
45 Eigen::VectorXd
const& sens)
override
47 Eigen::VectorXd
const& x = inputs.at(0).get();
48 Eigen::VectorXd
const& c = inputs.at(1).get();
65 Eigen::VectorXd
const& vec)
override
67 Eigen::VectorXd
const& x = inputs.at(0).get();
68 Eigen::VectorXd
const& c = inputs.at(1).get();
76 jacobianAction = vec(0)*x + vec(1)*Eigen::VectorXd::Ones(x.size());
84 Eigen::VectorXd
const& sens,
85 Eigen::VectorXd
const& vec)
override
87 Eigen::VectorXd
const& x = inputs.at(0).get();
88 Eigen::VectorXd
const& c = inputs.at(1).get();
91 if((inWrt1==0)&&(inWrt2==1)){
95 }
else if((inWrt2==0)&&(inWrt1==1)){
101 }
else if((inWrt1==0)&&(inWrt2==2)){
105 }
else if((inWrt1==1)&&(inWrt2==2)){
121 unsigned int numPts = 10;
122 Eigen::VectorXd x = Eigen::VectorXd::Random(numPts);
124 Eigen::VectorXd c(2);
127 auto mod = std::make_shared<SimpleModel>(numPts);
129 Eigen::VectorXd y = mod->Evaluate(x,c).at(0);
131 std::cout <<
"c = " << c.transpose() << std::endl;
132 std::cout <<
"x = " << x.transpose() << std::endl;
133 std::cout <<
"y = " << y.transpose() << std::endl;
137 Eigen::VectorXd grad, gradFd, vec;
138 Eigen::VectorXd hessAct, hessActFd;
139 Eigen::VectorXd sens = Eigen::VectorXd::Random(mod->outputSizes(0));
142 grad = mod->Gradient(0,wrt1, x,c,sens);
143 gradFd = mod->GradientByFD(0,wrt1,std::vector<Eigen::VectorXd>{x,c},sens);
144 std::cout <<
"Gradient Comparison:\n" << grad.transpose() <<
"\nvs\n" << gradFd.transpose() << std::endl << std::endl;
146 vec = Eigen::VectorXd::Random(mod->inputSizes(wrt2));
147 hessAct = mod->ApplyHessian(0, wrt1, wrt2, std::vector<Eigen::VectorXd>{x,c}, sens, vec);
148 hessActFd = mod->ApplyHessianByFD(0,wrt1,wrt2,std::vector<Eigen::VectorXd>{x,c},sens,vec);
150 std::cout <<
"Hessian Comparison:\n" << hessAct.transpose() <<
"\nvs\n" << hessActFd.transpose() << std::endl << std::endl;
153 grad = mod->Gradient(0,wrt1, x,c,sens);
154 gradFd = mod->GradientByFD(0,wrt1,std::vector<Eigen::VectorXd>{x,c},sens);
155 std::cout <<
"Gradient Comparison:\n" << grad.transpose() <<
"\nvs\n" << gradFd.transpose() << std::endl << std::endl;
157 vec = Eigen::VectorXd::Random(mod->inputSizes(wrt2));
158 hessAct = mod->ApplyHessian(0, wrt1, wrt2, std::vector<Eigen::VectorXd>{x,c}, sens, vec);
159 hessActFd = mod->ApplyHessianByFD(0,wrt1,wrt2,std::vector<Eigen::VectorXd>{x,c},sens,vec);
161 std::cout <<
"Hessian Comparison:\n" << hessAct.transpose() <<
"\nvs\n" << hessActFd.transpose() << std::endl << std::endl;
166 grad = mod->Gradient(0,wrt1, x,c,sens);
167 gradFd = mod->GradientByFD(0,wrt1,std::vector<Eigen::VectorXd>{x,c},sens);
168 std::cout <<
"Gradient Comparison:\n" << grad.transpose() <<
"\nvs\n" << gradFd.transpose() << std::endl << std::endl;
170 vec = Eigen::VectorXd::Random(mod->inputSizes(wrt2));
171 hessAct = mod->ApplyHessian(0, wrt1, wrt2, std::vector<Eigen::VectorXd>{x,c}, sens, vec);
172 hessActFd = mod->ApplyHessianByFD(0,wrt1,wrt2,std::vector<Eigen::VectorXd>{x,c},sens,vec);
174 std::cout <<
"Hessian Comparison:\n" << hessAct.transpose() <<
"\nvs\n" << hessActFd.transpose() << std::endl << std::endl;
178 vec = Eigen::VectorXd::Random(mod->outputSizes(0));
179 hessAct = mod->ApplyHessian(0, wrt1, wrt2, std::vector<Eigen::VectorXd>{x,c}, sens, vec);
180 hessActFd = mod->ApplyHessianByFD(0,wrt1,wrt2,std::vector<Eigen::VectorXd>{x,c},sens,vec);
182 std::cout <<
"Hessian Comparison:\n" << hessAct.transpose() <<
"\nvs\n" << hessActFd.transpose() << std::endl << std::endl;
Provides an abstract interface for defining vector-valued model components.
virtual void GradientImpl(unsigned int const outputDimWrt, unsigned int const inputDimWrt, ref_vector< Eigen::VectorXd > const &input, Eigen::VectorXd const &sensitivity)
virtual void ApplyJacobianImpl(unsigned int const outputDimWrt, unsigned int const inputDimWrt, ref_vector< Eigen::VectorXd > const &input, Eigen::VectorXd const &vec)
const Eigen::VectorXi inputSizes
std::vector< Eigen::VectorXd > outputs
ModPiece(std::vector< int > const &inputSizes, std::vector< int > const &outputSizes)
virtual void EvaluateImpl(ref_vector< boost::any > const &inputs) override
User-implemented function that determines the behavior of this muq::Modeling::WorkPiece.
virtual void JacobianImpl(unsigned int const outputDimWrt, unsigned int const inputDimWrt, ref_vector< Eigen::VectorXd > const &input)
const Eigen::VectorXi outputSizes
Eigen::VectorXd hessAction
Eigen::VectorXd jacobianAction
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)
std::vector< std::reference_wrapper< const T > > ref_vector
A vector of references to something ...