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,
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);
295 obj_->hessVec( Hv, v, x, tol );
304 obj_->invHessVec(Hv,v,x,tol);
313 obj_->precond( Mv, v, x, tol );
330 if (
con_->isActivated() ) {
370 if (
con_->isActivated() ) {
411 if (
con_->isActivated() ) {
451 if (
con_->isActivated() ) {
492 if (
con_->isActivated() ) {
532 if (
con_->isActivated() ) {
581 return con_->isFeasible(v);
585 return con_->isActivated();
589 con_->computeProjectedStep(v,x);
Provides the interface to evaluate objective functions.
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
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.
virtual int dimension() const
Return dimension of the vector space.
ROL::Ptr< ROL::Vector< Real > > primalV_
virtual ROL::Ptr< Vector > basis(const int i) const
Return i-th basis vector.
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)
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 ...
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)