| 1 | /****************************************************************************/ |
| 2 | /* This file is part of FreeFEM. */ |
| 3 | /* */ |
| 4 | /* FreeFEM is free software: you can redistribute it and/or modify */ |
| 5 | /* it under the terms of the GNU Lesser General Public License as */ |
| 6 | /* published by the Free Software Foundation, either version 3 of */ |
| 7 | /* the License, or (at your option) any later version. */ |
| 8 | /* */ |
| 9 | /* FreeFEM is distributed in the hope that it will be useful, */ |
| 10 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 11 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 12 | /* GNU Lesser General Public License for more details. */ |
| 13 | /* */ |
| 14 | /* You should have received a copy of the GNU Lesser General Public License */ |
| 15 | /* along with FreeFEM. If not, see <http://www.gnu.org/licenses/>. */ |
| 16 | /****************************************************************************/ |
| 17 | // SUMMARY : ... |
| 18 | // LICENSE : LGPLv3 |
| 19 | // ORG : LJLL Universite Pierre et Marie Curie, Paris, FRANCE |
| 20 | // AUTHORS : ... |
| 21 | // E-MAIL : ... |
| 22 | |
| 23 | #ifndef NRJ_HH |
| 24 | #define NRJ_HH |
| 25 | |
| 26 | #include "defs.hpp" |
| 27 | |
| 28 | // Warning : |
| 29 | // - no copy operator |
| 30 | // - no = operator |
| 31 | |
| 32 | template< class Param, class Vect, class Mat, class Real > |
| 33 | class tNRJ { |
| 34 | protected: |
| 35 | int nappel_val; |
| 36 | int nappel_grad; |
| 37 | int nappel_hess; |
| 38 | int nparam; |
| 39 | |
| 40 | Real val; |
| 41 | Vect *grad; |
| 42 | Mat *hess; |
| 43 | |
| 44 | public: |
| 45 | // Warning: |
| 46 | // In the tNRJ constructor, you must initialize grad and/or hess if you use it |
| 47 | // That is not done automatically as it depends on type |
| 48 | tNRJ(int); |
| 49 | virtual ~tNRJ( ); |
| 50 | |
| 51 | // To precise for each function of tNRJ |
| 52 | virtual Real Val(const Param &) = 0; |
| 53 | |
| 54 | Real getVal(const Param &); |
| 55 | |
| 56 | // To precise if needed, else return null |
| 57 | virtual Vect *Gradient(const Param &); |
| 58 | |
| 59 | Vect *getGradient(const Param &); |
| 60 | |
| 61 | // To precise if needed, else return null |
| 62 | virtual Mat *Hessian(const Param &); |
| 63 | |
| 64 | Mat *getHessian(const Param &); |
| 65 | |
| 66 | int Appel_Val( ) const { return nappel_val; }; |
| 67 | int Appel_Grad( ) const { return nappel_grad; }; |
| 68 | int Appel_Hess( ) const { return nappel_hess; }; |
| 69 | }; |
| 70 | |
| 71 | template< class Param, class Vect, class Mat, class Real > |
| 72 | tNRJ< Param, Vect, Mat, Real >::~tNRJ( ) { |
| 73 | if (grad != NULL) delete grad; |
| 74 | if (hess != NULL) delete hess; |
| 75 | } |
| 76 | |
| 77 | template< class Param, class Vect, class Mat, class Real > |
| 78 | tNRJ< Param, Vect, Mat, Real >::tNRJ(int n) : val(0.) { |
| 79 | nparam = n; |
| 80 | nappel_val = 0; |
| 81 | nappel_grad = 0; |
| 82 | nappel_hess = 0; |
| 83 | grad = NULL; |
| 84 | hess = NULL; |
| 85 | } |
| 86 | |
| 87 | template< class Param, class Vect, class Mat, class Real > |
| 88 | Real tNRJ< Param, Vect, Mat, Real >::getVal(const Param &p) { |
| 89 | nappel_val++; |
| 90 | return Val(p); |
| 91 | } |
| 92 | |
| 93 | template< class Param, class Vect, class Mat, class Real > |
| 94 | Vect *tNRJ< Param, Vect, Mat, Real >::getGradient(const Param &p) { |
| 95 | nappel_grad++; |
| 96 | return Gradient(p); |
| 97 | } |
| 98 | |
| 99 | template< class Param, class Vect, class Mat, class Real > |
| 100 | Vect *tNRJ< Param, Vect, Mat, Real >::Gradient(const Param &) { |
| 101 | return NULL; |
| 102 | } |
| 103 | |
| 104 | template< class Param, class Vect, class Mat, class Real > |
| 105 | Mat *tNRJ< Param, Vect, Mat, Real >::getHessian(const Param &p) { |
| 106 | nappel_hess++; |
| 107 | return Hessian(p); |
| 108 | } |
| 109 | |
| 110 | template< class Param, class Vect, class Mat, class Real > |
| 111 | Mat *tNRJ< Param, Vect, Mat, Real >::Hessian(const Param &) { |
| 112 | return NULL; |
| 113 | } |
| 114 | |
| 115 | #endif |