15 #ifndef ROL_HELPERFUNCTIONS_HPP
16 #define ROL_HELPERFUNCTIONS_HPP
22 #include "Teuchos_SerialDenseMatrix.hpp"
23 #include "Teuchos_SerialDenseVector.hpp"
24 #include "Teuchos_LAPACK.hpp"
31 Real tol = std::sqrt(ROL_EPSILON<Real>());
34 Teuchos::SerialDenseMatrix<int, Real> H(dim, dim);
36 ROL::Ptr<Vector<Real> > e = x.
clone();
37 ROL::Ptr<Vector<Real> > h = x.
dual().
clone();
39 for (
int i=0; i<
dim; i++) {
42 for (
int j=0; j<
dim; j++) {
45 H(j,i) = e->apply(*h);
57 Real tol = std::sqrt(ROL_EPSILON<Real>());
60 Teuchos::SerialDenseMatrix<int, Real> H(dim, dim);
62 ROL::Ptr<Vector<Real> > ei = x.
clone();
63 ROL::Ptr<Vector<Real> > ej = x.
dual().
clone();
64 ROL::Ptr<Vector<Real> > h = x.
dual().
clone();
66 for (
int i=0; i<
dim; i++) {
69 for (
int j=0; j<
dim; j++) {
84 Teuchos::SerialDenseMatrix<int, Real> M(dim, dim);
86 ROL::Ptr<Vector<Real> > ei = x.
clone();
87 ROL::Ptr<Vector<Real> > ej = x.
clone();
89 for (
int i=0; i<
dim; i++) {
91 for (
int j=0; j<
dim; j++) {
93 M(j,i) = ej->dot(*ei);
102 std::vector<std::vector<Real> >
computeEigenvalues(
const Teuchos::SerialDenseMatrix<int, Real> & mat) {
104 Teuchos::LAPACK<int, Real> lapack;
106 Teuchos::SerialDenseMatrix<int, Real> mymat(Teuchos::Copy, mat);
111 int n = mat.numRows();
113 std::vector<Real> real(n, 0);
114 std::vector<Real> imag(n, 0);
115 std::vector<std::vector<Real> > eigenvals;
125 std::vector<Real> work(lwork, 0);
129 lapack.GEEV(jobvl, jobvr, n, &mymat(0,0), n, &real[0], &imag[0], vl, ldvl, vr, ldvr, &work[0], lwork, &info);
131 eigenvals.push_back(real);
132 eigenvals.push_back(imag);
141 const Teuchos::SerialDenseMatrix<int, Real> & B) {
143 Teuchos::LAPACK<int, Real> lapack;
145 Teuchos::SerialDenseMatrix<int, Real> myA(Teuchos::Copy, A);
146 Teuchos::SerialDenseMatrix<int, Real> myB(Teuchos::Copy, B);
153 std::vector<Real> real(n, 0);
154 std::vector<Real> imag(n, 0);
155 std::vector<Real> beta(n, 0);
156 std::vector<std::vector<Real> > eigenvals;
166 std::vector<Real> work(lwork, 0);
170 lapack.GGEV(jobvl, jobvr, n, &myA(0,0), n, &myB(0,0), n, &real[0], &imag[0], &beta[0],
171 vl, ldvl, vr, ldvr, &work[0], lwork, &info);
173 for (
int i=0; i<n; i++) {
178 eigenvals.push_back(real);
179 eigenvals.push_back(imag);
187 Teuchos::SerialDenseMatrix<int, Real>
computeInverse(
const Teuchos::SerialDenseMatrix<int, Real> & mat) {
189 Teuchos::LAPACK<int, Real> lapack;
191 Teuchos::SerialDenseMatrix<int, Real> mymat(Teuchos::Copy, mat);
193 int n = mat.numRows();
195 std::vector<int> ipiv(n, 0);
199 std::vector<Real> work(lwork, 0);
203 lapack.GETRF(n, n, &mymat(0,0), n, &ipiv[0], &info);
204 lapack.GETRI(n, &mymat(0,0), n, &ipiv[0], &work[0], lwork, &info);
215 ROL::Ptr<Objective<Real> >
obj_;
216 ROL::Ptr<BoundConstraint<Real> >
con_;
230 bool useSecantPrecond =
false,
231 bool useSecantHessVec =
false,
235 obj_ = ROL::makePtrFromRef(obj);
236 con_ = ROL::makePtrFromRef(con);
241 obj_->update(x,flag,iter);
242 con_->update(x,flag,iter);
246 return obj_->value(x,tol);
250 obj_->gradient(g,x,tol);
254 return obj_->dirDeriv(x,d,tol);
262 obj_->hessVec( Hv, v, x, tol );
271 obj_->invHessVec(Hv,v,x,tol);
280 obj_->precond( Mv, v, x, tol );
297 if (
con_->isActivated() ) {
337 if (
con_->isActivated() ) {
378 if (
con_->isActivated() ) {
418 if (
con_->isActivated() ) {
459 if (
con_->isActivated() ) {
499 if (
con_->isActivated() ) {
548 return con_->isFeasible(v);
552 return con_->isActivated();
556 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)