49 #ifndef ROL_HELPERFUNCTIONS_HPP 50 #define ROL_HELPERFUNCTIONS_HPP 56 #include "Teuchos_SerialDenseMatrix.hpp" 57 #include "Teuchos_SerialDenseVector.hpp" 58 #include "Teuchos_LAPACK.hpp" 65 Real tol = std::sqrt(ROL_EPSILON<Real>());
68 Teuchos::SerialDenseMatrix<int, Real> H(dim, dim);
70 ROL::Ptr<Vector<Real> > e = x.
clone();
71 ROL::Ptr<Vector<Real> > h = x.
dual().
clone();
73 for (
int i=0; i<dim; i++) {
76 for (
int j=0; j<dim; j++) {
78 H(j,i) = e->dot(h->dual());
90 Real tol = std::sqrt(ROL_EPSILON<Real>());
93 Teuchos::SerialDenseMatrix<int, Real> H(dim, dim);
95 ROL::Ptr<Vector<Real> > ei = x.
clone();
96 ROL::Ptr<Vector<Real> > ej = x.
dual().
clone();
97 ROL::Ptr<Vector<Real> > h = x.
dual().
clone();
99 for (
int i=0; i<dim; i++) {
102 for (
int j=0; j<dim; j++) {
104 H(j,i) = ej->dot(*h);
117 Teuchos::SerialDenseMatrix<int, Real> M(dim, dim);
119 ROL::Ptr<Vector<Real> > ei = x.
clone();
120 ROL::Ptr<Vector<Real> > ej = x.
clone();
122 for (
int i=0; i<dim; i++) {
124 for (
int j=0; j<dim; j++) {
126 M(j,i) = ej->dot(*ei);
135 std::vector<std::vector<Real> >
computeEigenvalues(
const Teuchos::SerialDenseMatrix<int, Real> & mat) {
137 Teuchos::LAPACK<int, Real> lapack;
139 Teuchos::SerialDenseMatrix<int, Real> mymat(Teuchos::Copy, mat);
144 int n = mat.numRows();
146 std::vector<Real> real(n, 0);
147 std::vector<Real> imag(n, 0);
148 std::vector<std::vector<Real> > eigenvals;
158 std::vector<Real> work(lwork, 0);
162 lapack.GEEV(jobvl, jobvr, n, &mymat(0,0), n, &real[0], &imag[0], vl, ldvl, vr, ldvr, &work[0], lwork, &info);
164 eigenvals.push_back(real);
165 eigenvals.push_back(imag);
174 const Teuchos::SerialDenseMatrix<int, Real> & B) {
176 Teuchos::LAPACK<int, Real> lapack;
178 Teuchos::SerialDenseMatrix<int, Real> myA(Teuchos::Copy, A);
179 Teuchos::SerialDenseMatrix<int, Real> myB(Teuchos::Copy, B);
186 std::vector<Real> real(n, 0);
187 std::vector<Real> imag(n, 0);
188 std::vector<Real> beta(n, 0);
189 std::vector<std::vector<Real> > eigenvals;
199 std::vector<Real> work(lwork, 0);
203 lapack.GGEV(jobvl, jobvr, n, &myA(0,0), n, &myB(0,0), n, &real[0], &imag[0], &beta[0],
204 vl, ldvl, vr, ldvr, &work[0], lwork, &info);
206 for (
int i=0; i<n; i++) {
211 eigenvals.push_back(real);
212 eigenvals.push_back(imag);
220 Teuchos::SerialDenseMatrix<int, Real>
computeInverse(
const Teuchos::SerialDenseMatrix<int, Real> & mat) {
222 Teuchos::LAPACK<int, Real> lapack;
224 Teuchos::SerialDenseMatrix<int, Real> mymat(Teuchos::Copy, mat);
226 int n = mat.numRows();
228 std::vector<int> ipiv(n, 0);
232 std::vector<Real> work(lwork, 0);
236 lapack.GETRF(n, n, &mymat(0,0), n, &ipiv[0], &info);
237 lapack.GETRI(n, &mymat(0,0), n, &ipiv[0], &work[0], lwork, &info);
248 ROL::Ptr<Objective<Real> >
obj_;
249 ROL::Ptr<BoundConstraint<Real> >
con_;
263 bool useSecantPrecond =
false,
264 bool useSecantHessVec =
false,
266 : isInitialized_(false), useSecantPrecond_(useSecantPrecond),
267 useSecantHessVec_(useSecantHessVec), eps_(eps) {
268 obj_ = ROL::makePtrFromRef(obj);
269 con_ = ROL::makePtrFromRef(con);
274 obj_->update(x,flag,iter);
275 con_->update(x,flag,iter);
279 return obj_->value(x,tol);
283 obj_->gradient(g,x,tol);
287 return obj_->dirDeriv(x,d,tol);
291 if ( useSecantHessVec_ ) {
292 secant_->applyB( Hv, v );
295 obj_->hessVec( Hv, v, x, tol );
300 if ( useSecantHessVec_ ) {
301 secant_->applyH(Hv,v);
304 obj_->invHessVec(Hv,v,x,tol);
309 if ( useSecantPrecond_ ) {
310 secant_->applyH( Mv, v );
313 obj_->precond( Mv, v, x, tol );
330 if ( con_->isActivated() ) {
331 if (!isInitialized_) {
332 primalV_ = x.
clone();
333 dualV_ = x.
dual().clone();
334 isInitialized_ =
true;
339 con_->pruneActive(*primalV_,d,p,eps_);
343 con_->pruneActive(Hv,d,p,eps_);
347 con_->pruneInactive(*primalV_,d,p,eps_);
348 dualV_->set(primalV_->dual());
349 con_->pruneInactive(*dualV_,d,p,eps_);
370 if ( con_->isActivated() ) {
371 if (!isInitialized_) {
372 primalV_ = x.
clone();
373 dualV_ = x.
dual().clone();
374 isInitialized_ =
true;
379 con_->pruneActive(*primalV_,p,eps_);
383 con_->pruneActive(Hv,p,eps_);
387 con_->pruneInactive(*primalV_,p,eps_);
388 dualV_->set(primalV_->dual());
389 con_->pruneInactive(*dualV_,p,eps_);
411 if ( con_->isActivated() ) {
412 if (!isInitialized_) {
413 primalV_ = x.
clone();
414 dualV_ = x.
dual().clone();
415 isInitialized_ =
true;
420 con_->pruneActive(*dualV_,d,p,eps_);
424 con_->pruneActive(Hv,d,p,eps_);
428 con_->pruneInactive(*dualV_,d,p,eps_);
429 primalV_->set(dualV_->dual());
430 con_->pruneInactive(*primalV_,d,p,eps_);
451 if ( con_->isActivated() ) {
452 if (!isInitialized_) {
453 primalV_ = x.
clone();
454 dualV_ = x.
dual().clone();
455 isInitialized_ =
true;
460 con_->pruneActive(*dualV_,p,eps_);
464 con_->pruneActive(Hv,p,eps_);
468 con_->pruneInactive(*dualV_,p,eps_);
469 primalV_->set(dualV_->dual());
470 con_->pruneInactive(*primalV_,p,eps_);
492 if ( con_->isActivated() ) {
493 if (!isInitialized_) {
494 primalV_ = x.
clone();
495 dualV_ = x.
dual().clone();
496 isInitialized_ =
true;
501 con_->pruneActive(*dualV_,d,p,eps_);
505 con_->pruneActive(Mv,d,p,eps_);
509 con_->pruneInactive(*dualV_,d,p,eps_);
510 primalV_->set(dualV_->dual());
511 con_->pruneInactive(*primalV_,d,p,eps_);
532 if ( con_->isActivated() ) {
533 if (!isInitialized_) {
534 primalV_ = x.
clone();
535 dualV_ = x.
dual().clone();
536 isInitialized_ =
true;
541 con_->pruneActive(*dualV_,p,eps_);
545 con_->pruneActive(Mv,p,eps_);
549 con_->pruneInactive(*dualV_,p,eps_);
550 primalV_->set(dualV_->dual());
551 con_->pruneInactive(*primalV_,p,eps_);
565 con_->pruneActive(v,g,x,eps_);
569 con_->pruneActive(v,x,eps_);
573 con_->pruneInactive(v,g,x,eps_);
577 con_->pruneInactive(v,x,eps_);
581 return con_->isFeasible(v);
585 return con_->isActivated();
589 con_->computeProjectedStep(v,x);
Provides the interface to evaluate objective functions.
Real value(const Vector< Real > &x, Real &tol)
Compute value.
bool isConActivated(void)
ROL::Ptr< Secant< Real > > secant_
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
ROL::Ptr< ROL::Vector< Real > > primalV_
virtual void plus(const Vector &x)=0
Compute , where .
void reducedHessVec(Vector< Real > &Hv, const Vector< Real > &v, const Vector< Real > &p, const Vector< Real > &d, const Vector< Real > &x, Real &tol)
Apply the reduced Hessian to a vector, v. The reduced Hessian first removes elements of v correspondi...
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Real dirDeriv(const Vector< Real > &x, const Vector< Real > &d, Real &tol)
Compute directional derivative.
void pruneInactive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x)
Teuchos::SerialDenseMatrix< int, Real > computeScaledDenseHessian(Objective< Real > &obj, const Vector< Real > &x)
ProjectedObjective(Objective< Real > &obj, BoundConstraint< Real > &con, ROL::Ptr< Secant< Real > > &secant, bool useSecantPrecond=false, bool useSecantHessVec=false, Real eps=0.0)
ROL::Ptr< Objective< Real > > obj_
bool isFeasible(const Vector< Real > &v)
virtual ROL::Ptr< Vector > basis(const int i) const
Return i-th basis vector.
void pruneActive(Vector< Real > &v, const Vector< Real > &g, const Vector< Real > &x)
void reducedPrecond(Vector< Real > &Mv, const Vector< Real > &v, const Vector< Real > &p, const Vector< Real > &x, Real &tol)
Apply the reduced preconditioner to a vector, v. The reduced preconditioner first removes elements of...
Defines the linear algebra or vector space interface.
void hessVec(Vector< Real > &Hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Teuchos::SerialDenseMatrix< int, Real > computeDenseHessian(Objective< Real > &obj, const Vector< Real > &x)
std::vector< std::vector< Real > > computeGenEigenvalues(const Teuchos::SerialDenseMatrix< int, Real > &A, const Teuchos::SerialDenseMatrix< int, Real > &B)
void reducedInvHessVec(Vector< Real > &Hv, const Vector< Real > &v, const Vector< Real > &p, const Vector< Real > &x, Real &tol)
Apply the reduced inverse Hessian to a vector, v. The reduced inverse Hessian first removes elements ...
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
virtual int dimension() const
Return dimension of the vector space.
Provides interface for and implements limited-memory secant operators.
ROL::Ptr< ROL::Vector< Real > > dualV_
std::vector< std::vector< Real > > computeEigenvalues(const Teuchos::SerialDenseMatrix< int, Real > &mat)
void reducedInvHessVec(Vector< Real > &Hv, const Vector< Real > &v, const Vector< Real > &p, const Vector< Real > &d, const Vector< Real > &x, Real &tol)
Apply the reduced inverse Hessian to a vector, v. The reduced inverse Hessian first removes elements ...
void pruneInactive(Vector< Real > &v, const Vector< Real > &x)
void reducedHessVec(Vector< Real > &Hv, const Vector< Real > &v, const Vector< Real > &p, const Vector< Real > &x, Real &tol)
Apply the reduced Hessian to a vector, v. The reduced Hessian first removes elements of v correspondi...
Provides the interface to apply upper and lower bound constraints.
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update objective function.
Teuchos::SerialDenseMatrix< int, Real > computeDotMatrix(const Vector< Real > &x)
ROL::Ptr< BoundConstraint< Real > > con_
void computeProjectedStep(Vector< Real > &v, const Vector< Real > &x)
void reducedPrecond(Vector< Real > &Mv, const Vector< Real > &v, const Vector< Real > &p, const Vector< Real > &d, const Vector< Real > &x, Real &tol)
Apply the reduced preconditioner to a vector, v. The reduced preconditioner first removes elements of...
void project(Vector< Real > &x)
void invHessVec(Vector< Real > &Hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply inverse Hessian approximation to vector.
Teuchos::SerialDenseMatrix< int, Real > computeInverse(const Teuchos::SerialDenseMatrix< int, Real > &mat)
void precond(Vector< Real > &Mv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply preconditioner to vector.
void pruneActive(Vector< Real > &v, const Vector< Real > &x)