| 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 | // Param class, inherited from KN |
| 24 | // This is the class of parameters against which we optimize |
| 25 | // We can possibly add constraints |
| 26 | |
| 27 | #ifndef PARAM_HH |
| 28 | #define PARAM_HH |
| 29 | |
| 30 | #include <iostream> |
| 31 | using namespace std; |
| 32 | |
| 33 | #include "defs.hpp" |
| 34 | //#include "mvvtp.h" |
| 35 | //#include "mvblas.h" |
| 36 | |
| 37 | // We nees a Vect type for update function |
| 38 | template< class Real > |
| 39 | class Param : public KN< Real > { |
| 40 | private: |
| 41 | int ndim; |
| 42 | // These bound can be used in the update method of LineSearch.hpp |
| 43 | // This is a way to implement a projected gradient |
| 44 | KN< Real >* maxParam; |
| 45 | KN< Real >* minParam; |
| 46 | |
| 47 | public: |
| 48 | Param( ); |
| 49 | // construct a continuous model with size n |
| 50 | explicit Param(int n); |
| 51 | // construct a continuous model space with boundary and initial values |
| 52 | Param(const KN< Real >& maxp, const KN< Real >& minp, const KN< Real >& initmod); |
| 53 | // construct a continuous model space with boundary |
| 54 | Param(const KN< Real >& maxp, const KN< Real >& minp); |
| 55 | // construct a continuous model space with initial values |
| 56 | explicit Param(const KN< Real >& initmod); |
| 57 | // copy operator |
| 58 | Param(const Param< Real >& p); |
| 59 | virtual ~Param( ); |
| 60 | |
| 61 | Param< Real >& operator=(const Param< Real >&); |
| 62 | |
| 63 | KN< Real >* modMax( ) const; |
| 64 | KN< Real >* modMin( ) const; |
| 65 | void setModMax(const KN< Real >& v); |
| 66 | void setModMin(const KN< Real >& v); |
| 67 | |
| 68 | // cf. Stroustrup page 612-613 for operator << |
| 69 | virtual ostream& toto(ostream&) const; |
| 70 | |
| 71 | private: // no copy |
| 72 | }; |
| 73 | |
| 74 | // Constructors |
| 75 | template< class Real > |
| 76 | Param< Real >::Param(void) : KN< Real >( ) { |
| 77 | // cerr << "Param default constructor" << endl; |
| 78 | maxParam = NULL; |
| 79 | minParam = NULL; |
| 80 | } |
| 81 | |
| 82 | template< class Real > |
| 83 | Param< Real >::Param(int n) : KN< Real >(n) { |
| 84 | // cerr << "Param constructor 0" << endl; |
| 85 | maxParam = NULL; |
| 86 | minParam = NULL; |
| 87 | } |
| 88 | |
| 89 | template< class Real > |
| 90 | Param< Real >::Param(const KN< Real >& maxp, const KN< Real >& minp, const KN< Real >& initmod) |
| 91 | : KN< Real >(initmod) { |
| 92 | int ndim = initmod.size( ); |
| 93 | |
| 94 | // cerr << "Param constructor 1" << endl; |
| 95 | |
| 96 | maxParam = new KN< Real >(ndim); |
| 97 | minParam = new KN< Real >(ndim); |
| 98 | maxParam[0] = maxp; |
| 99 | minParam[0] = minp; |
| 100 | } |
| 101 | |
| 102 | template< class Real > |
| 103 | Param< Real >::Param(const KN< Real >& maxp, const KN< Real >& minp) |
| 104 | : KN< Real >(Min(minp.size( ), maxp.size( ))) { |
| 105 | int ndim = Min(minp.size( ), maxp.size( )); |
| 106 | |
| 107 | // cerr << "Param constructor 2" << endl; |
| 108 | |
| 109 | maxParam = new KN< Real >(ndim); |
| 110 | minParam = new KN< Real >(ndim); |
| 111 | maxParam[0] = maxp; |
| 112 | minParam[0] = minp; |
| 113 | } |
| 114 | |
| 115 | template< class Real > |
| 116 | Param< Real >::Param(const KN< Real >& initmod) : KN< Real >(initmod) { |
| 117 | // cerr << "Param constructor 3" << endl; |
| 118 | |
| 119 | maxParam = NULL; |
| 120 | minParam = NULL; |
| 121 | } |
| 122 | |
| 123 | // copy operator |
| 124 | template< class Real > |
| 125 | Param< Real >::Param(const Param& p) : KN< Real >(p) { |
| 126 | // cerr << "Param copy operator" << endl; |
| 127 | |
| 128 | if ((p.maxParam) == NULL) |
| 129 | maxParam = NULL; |
| 130 | else { |
| 131 | maxParam = new KN< Real >(ndim); |
| 132 | *maxParam = *(p.maxParam); |
| 133 | } |
| 134 | |
| 135 | if ((p.minParam) == NULL) |
| 136 | minParam = NULL; |
| 137 | else { |
| 138 | minParam = new KN< Real >(ndim); |
| 139 | *minParam = *(p.minParam); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | template< class Real > |
| 144 | Param< Real >& Param< Real >::operator=(const Param< Real >& p) { |
| 145 | // cerr << "Param = operator" << endl; |
| 146 | // modif FH 042005 for gcc4.0 |
| 147 | KN< Real >& a1 = *this; |
| 148 | const KN< Real >& a2 = p; |
| 149 | a1 = a2; // KN copy operator |
| 150 | |
| 151 | int ndim = p.size( ); |
| 152 | |
| 153 | if ((p.maxParam) == NULL) |
| 154 | maxParam = NULL; |
| 155 | else { |
| 156 | if (maxParam) delete maxParam; |
| 157 | maxParam = new KN< Real >(ndim); |
| 158 | *maxParam = *(p.maxParam); |
| 159 | } |
| 160 | |
| 161 | if ((p.minParam) == NULL) |
| 162 | minParam = NULL; |
| 163 | else { |
| 164 | if (minParam) delete minParam; |
| 165 | minParam = new KN< Real >(ndim); |
| 166 | *minParam = *(p.minParam); |
| 167 | } |
| 168 | |
| 169 | return (*this); |
| 170 | } |
| 171 | |
| 172 | template< class Real > |
| 173 | Param< Real >::~Param( ) { |
| 174 | // cerr << "Param destructor" << endl; |
| 175 | |
| 176 | if (maxParam != NULL) delete maxParam; |
| 177 | if (minParam != NULL) delete minParam; |
| 178 | } |
| 179 | |
| 180 | template< class Real > |
| 181 | KN< Real >* Param< Real >::modMax( ) const { |
| 182 | return maxParam; |
| 183 | } |
| 184 | |
| 185 | template< class Real > |
| 186 | KN< Real >* Param< Real >::modMin( ) const { |
| 187 | return minParam; |
| 188 | } |
| 189 | |
| 190 | template< class Real > |
| 191 | void Param< Real >::setModMax(const KN< Real >& v) { |
| 192 | if (maxParam != NULL) delete maxParam; |
| 193 | maxParam = new KN< Real >(v); |
| 194 | } |
| 195 | |
| 196 | template< class Real > |
| 197 | void Param< Real >::setModMin(const KN< Real >& v) { |
| 198 | if (minParam != NULL) delete minParam; |
| 199 | minParam = new KN< Real >(v); |
| 200 | } |
| 201 | |
| 202 | template< class Real > |
| 203 | ostream& Param< Real >::toto(ostream& os) const { |
| 204 | for (long i = 0; i < (*this).size( ); i++) os << (*this)[i] << " "; |
| 205 | os << endl; |
| 206 | |
| 207 | return os; |
| 208 | } |
| 209 | |
| 210 | template< class Real > |
| 211 | ostream& operator<<(ostream& os, const Param< Real >& d) { |
| 212 | return d.toto(os); |
| 213 | } |
| 214 | |
| 215 | #endif |