/************************************************************************* Copyright Rice University, 2011. All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, provided that the above copyright notice(s) and this permission notice appear in all copies of the Software and that both the above copyright notice(s) and this permission notice appear in supporting documentation. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. Except as contained in this notice, the name of a copyright holder shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization of the copyright holder. **************************************************************************/ #ifndef __RVL_RNOP #define __RVL_RNOP #include "op.hh" #include "rnmat.hh" /** Builds an op (nonlinear function on R^n, or some part of it) out of two functions: int f(int m, int n, scalar const * in, scalar * out); int df(int m, int n, scalar const * in, scalar * jac); n is the number of input components, m the number of outputs - so f defines a function from R^n to R^m. The dimensions appear as arguments to enable sanity-checking, as usual. df has n inputs and m*n outputs, namely the jacobian matrix in column major storage.x */ namespace RVL { /** FO class encapsulating eval of f */ template class CFunction: public BinaryLocalFunctionObject { public: void operator()(LocalDataContainer & y, LocalDataContainer const & x) { int err=0; if (err=f(y.getSize(), x.getSize(), x.getData(), y.getData())) { RVLException e; e<<"Error: CFunction::operator() - err="< class CJacobian: public UnaryLocalFunctionObjectConstEval { private: GenMat & a; CJacobian(); CJacobian(CJacobian const &); public: CJacobian(GenMat & _a): a(_a) {} void operator()(LocalDataContainer const & x) { int err=0; if (err=df(a.getNRows(), a.getNCols(), x.getData(), a.getData())) { RVLException e; e<<"Error: CJacobian::operator() - err="< class OpWithGenMatDeriv: public Operator { public: virtual GenMat const & getGenMat() const = 0; }; /** Local Operator class based on two C-style functions, one computing the function value, the other computing the Jacobian matrix. */ template class GenOp: public OpWithGenMatDeriv { private: // domain and range RnSpace const & dom; RnSpace const & rng; // GenMat matrix LinearOp object and intialization flag, // modifiable post-construction mutable GenMat deriv; mutable bool init; // common code for intializing the Jacobian (GenMat object), // hoisted out. Note that initialization will be done only once, // per the design of Operator. void buildJacobian(Vector const & x) const { try { if (!init) { CJacobian j(deriv); x.eval(j); init=true; } } catch (RVLException & e) { e<<"\ncalled from GenOp::buildJacobian\n"; throw e; } } GenOp(); protected: /** \f$y = F(x)\f$ */ virtual void apply(const Vector & x, Vector & y) const { try { CFunction ff; y.eval(ff,x); // why not buildJacobian(x); } catch (RVLException & e) { e<<"\ncalled from GenOp::apply\n"; throw e; } } /** \f$dy = DF(x)dx\f$ */ virtual void applyDeriv(const Vector & x, const Vector & dx, Vector & dy) const { try { buildJacobian(x); deriv.applyOp(dx,dy); } catch (RVLException & e) { e<<"\ncalled from GenOp::applyDeriv\n"; throw e; } } /** \f$dx = DF(x)^*dy\f$ */ virtual void applyAdjDeriv(const Vector & x, const Vector & dy, Vector & dx) const { try { buildJacobian(x); deriv.applyAdjOp(dy,dx); } catch (RVLException & e) { e<<"\ncalled from GenOp::applyAdjDeriv\n"; throw e; } } virtual Operator * clone() const { return new GenOp(dom,rng); } public: /** main constructor: inputs are domain and range RnSpaces */ GenOp(RnSpace const & _dom, RnSpace const & _rng) : dom(_dom), rng(_rng), deriv(dom,rng), init(false) {} /** copy constructor - note how important this is, as it's used to define the clone method, which is what the OperatorEvaluation class uses to create new internal copies of the Operator. */ GenOp(GenOp const & a) : dom(a.dom), rng(a.rng), deriv(dom,rng), init(false) {} Space const & getDomain() const { return dom; } Space const & getRange() const { return rng; } /** public access to the internal GenMat - but only if it has been initialized */ GenMat const & getGenMat() const { if (init) return deriv; RVLException e; e<<"Error: GenOp::getGenMat\n"; e<<"Jacobian matrix not initialized - must evaluate somewhere first!\n"; throw e; } ostream & write(ostream & str) const { str<<"GenOp instance\n"; str<<"domain:\n"; dom.write(str); str<<"range:\n"; rng.write(str); return str; } }; } #endif