44 #ifndef ROL_OBJECTIVE_DEF_H 45 #define ROL_OBJECTIVE_DEF_H 57 Teuchos::RCP<Vector<Real> > xd = d.
clone();
60 return (this->value(*xd,ftol) - this->value(x,ftol)) / tol;
66 Real deriv = 0.0, h = 0.0, xi = 0.0;
70 deriv = this->dirDeriv(x,*x.
basis(i),h);
78 if ( v.
norm() == 0. ) {
84 Real h = std::max(1.0,x.
norm()/v.
norm())*tol;
88 Teuchos::RCP<Vector<Real> > g = hv.
clone();
89 this->gradient(*g,x,gtol);
92 Teuchos::RCP<Vector<Real> > xnew = x.
clone();
99 this->gradient(hv,*xnew,gtol);
109 template <
class Real>
113 const bool printToStream,
114 std::ostream & outStream,
118 std::vector<Real> steps(numSteps);
119 for(
int i=0;i<numSteps;++i) {
120 steps[i] = pow(10,-i);
123 return checkGradient(x,g,d,steps,printToStream,outStream,order);
129 template <
class Real>
133 const std::vector<Real> &steps,
134 const bool printToStream,
135 std::ostream & outStream,
138 TEUCHOS_TEST_FOR_EXCEPTION( order<1 || order>4, std::invalid_argument,
139 "Error: finite difference order must be 1,2,3, or 4" );
146 int numSteps = steps.size();
148 std::vector<Real> tmp(numVals);
149 std::vector<std::vector<Real> > gCheck(numSteps, tmp);
152 Teuchos::oblackholestream oldFormatState;
153 oldFormatState.copyfmt(outStream);
157 Real val = this->value(x,tol);
160 Teuchos::RCP<Vector<Real> > gtmp = g.
clone();
161 this->gradient(*gtmp, x, tol);
162 Real dtg = d.
dot(gtmp->dual());
165 Teuchos::RCP<Vector<Real> > xnew = x.
clone();
167 for (
int i=0; i<numSteps; i++) {
177 gCheck[i][2] =
weights[order-1][0] * val;
179 for(
int j=0; j<order; ++j) {
181 xnew->axpy(eta*
shifts[order-1][j], d);
184 if(
weights[order-1][j+1] != 0 ) {
186 gCheck[i][2] +=
weights[order-1][j+1] * this->value(*xnew,tol);
192 gCheck[i][3] = std::abs(gCheck[i][2] - gCheck[i][1]);
196 outStream << std::right
197 << std::setw(20) <<
"Step size" 198 << std::setw(20) <<
"grad'*dir" 199 << std::setw(20) <<
"FD approx" 200 << std::setw(20) <<
"abs error" 203 outStream << std::scientific << std::setprecision(11) << std::right
204 << std::setw(20) << gCheck[i][0]
205 << std::setw(20) << gCheck[i][1]
206 << std::setw(20) << gCheck[i][2]
207 << std::setw(20) << gCheck[i][3]
214 outStream.copyfmt(oldFormatState);
226 template <
class Real>
230 const bool printToStream,
231 std::ostream & outStream,
234 std::vector<Real> steps(numSteps);
235 for(
int i=0;i<numSteps;++i) {
236 steps[i] = pow(10,-i);
239 return checkHessVec(x,hv,v,steps,printToStream,outStream,order);
244 template <
class Real>
248 const std::vector<Real> &steps,
249 const bool printToStream,
250 std::ostream & outStream,
253 TEUCHOS_TEST_FOR_EXCEPTION( order<1 || order>4, std::invalid_argument,
254 "Error: finite difference order must be 1,2,3, or 4" );
262 int numSteps = steps.size();
264 std::vector<Real> tmp(numVals);
265 std::vector<std::vector<Real> > hvCheck(numSteps, tmp);
268 Teuchos::oblackholestream oldFormatState;
269 oldFormatState.copyfmt(outStream);
272 Teuchos::RCP<Vector<Real> > g = hv.
clone();
274 this->gradient(*g, x, tol);
277 Teuchos::RCP<Vector<Real> > Hv = hv.
clone();
278 this->hessVec(*Hv, v, x, tol);
279 Real normHv = Hv->norm();
282 Teuchos::RCP<Vector<Real> > gdif = hv.
clone();
283 Teuchos::RCP<Vector<Real> > gnew = hv.
clone();
284 Teuchos::RCP<Vector<Real> > xnew = x.
clone();
286 for (
int i=0; i<numSteps; i++) {
294 gdif->scale(
weights[order-1][0]);
296 for(
int j=0; j<order; ++j) {
299 xnew->axpy(eta*
shifts[order-1][j], v);
302 if(
weights[order-1][j+1] != 0 ) {
304 this->gradient(*gnew, *xnew, tol);
305 gdif->axpy(
weights[order-1][j+1],*gnew);
310 gdif->scale(1.0/eta);
314 hvCheck[i][1] = normHv;
315 hvCheck[i][2] = gdif->norm();
316 gdif->axpy(-1.0, *Hv);
317 hvCheck[i][3] = gdif->norm();
321 outStream << std::right
322 << std::setw(20) <<
"Step size" 323 << std::setw(20) <<
"norm(Hess*vec)" 324 << std::setw(20) <<
"norm(FD approx)" 325 << std::setw(20) <<
"norm(abs error)" 328 outStream << std::scientific << std::setprecision(11) << std::right
329 << std::setw(20) << hvCheck[i][0]
330 << std::setw(20) << hvCheck[i][1]
331 << std::setw(20) << hvCheck[i][2]
332 << std::setw(20) << hvCheck[i][3]
339 outStream.copyfmt(oldFormatState);
351 const bool printToStream,
352 std::ostream & outStream ) {
357 Teuchos::RCP<Vector<Real> > h = hv.
clone();
358 this->hessVec(*h, v, x, tol);
359 Real wHv = w.
dot(h->dual());
361 this->hessVec(*h, w, x, tol);
362 Real vHw = v.
dot(h->dual());
364 std::vector<Real> hsymCheck(3, 0);
368 hsymCheck[2] = std::abs(vHw-wHv);
371 Teuchos::oblackholestream oldFormatState;
372 oldFormatState.copyfmt(outStream);
375 outStream << std::right
376 << std::setw(20) <<
"<w, H(x)v>" 377 << std::setw(20) <<
"<v, H(x)w>" 378 << std::setw(20) <<
"abs error" 380 outStream << std::scientific << std::setprecision(11) << std::right
381 << std::setw(20) << hsymCheck[0]
382 << std::setw(20) << hsymCheck[1]
383 << std::setw(20) << hsymCheck[2]
388 outStream.copyfmt(oldFormatState);
virtual void scale(const Real alpha)=0
Compute where .
virtual int dimension() const
Return dimension of the vector space.
const double weights[4][5]
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
virtual Real dirDeriv(const Vector< Real > &x, const Vector< Real > &d, Real &tol)
Compute directional derivative.
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
virtual Teuchos::RCP< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual void zero()
Set to zero vector.
Defines the linear algebra or vector space interface.
virtual Real dot(const Vector &x) const =0
Compute where .
virtual std::vector< std::vector< Real > > checkGradient(const Vector< Real > &x, const Vector< Real > &d, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
Finite-difference gradient check.
virtual void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
virtual std::vector< std::vector< Real > > checkHessVec(const Vector< Real > &x, const Vector< Real > &v, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
Finite-difference Hessian-applied-to-vector check.
virtual Teuchos::RCP< Vector > basis(const int i) const
Return i-th basis vector.
virtual Real norm() const =0
Returns where .
virtual std::vector< Real > checkHessSym(const Vector< Real > &x, const Vector< Real > &v, const Vector< Real > &w, const bool printToStream=true, std::ostream &outStream=std::cout)
Hessian symmetry check.
static const double ROL_EPSILON
Platform-dependent machine epsilon.