[11] | 1 | // |
---|
| 2 | // C++ Interface: itpp_ext |
---|
| 3 | // |
---|
[19] | 4 | // Description: |
---|
[11] | 5 | // |
---|
| 6 | // |
---|
| 7 | // Author: smidl <smidl@utia.cas.cz>, (C) 2008 |
---|
| 8 | // |
---|
| 9 | // Copyright: See COPYING file that comes with this distribution |
---|
| 10 | // |
---|
| 11 | // |
---|
[33] | 12 | #ifndef FN_H |
---|
| 13 | #define FN_H |
---|
| 14 | |
---|
[565] | 15 | #include "../bdmerror.h" |
---|
[384] | 16 | #include "../base/bdmbase.h" |
---|
[11] | 17 | |
---|
[477] | 18 | namespace bdm { |
---|
[11] | 19 | |
---|
[85] | 20 | //! class representing function \f$f(x) = a\f$, here \c rv is empty |
---|
[477] | 21 | class constfn : public fnc { |
---|
[1064] | 22 | //! value of the function |
---|
| 23 | vec val; |
---|
[19] | 24 | |
---|
[477] | 25 | public: |
---|
[1064] | 26 | //vec eval() {return val;}; |
---|
| 27 | //! inherited |
---|
| 28 | vec eval ( const vec &cond ) { |
---|
| 29 | return val; |
---|
| 30 | }; |
---|
| 31 | //!Default constructor |
---|
| 32 | constfn ( const vec &val0 ) : fnc(), val ( val0 ) { |
---|
| 33 | dimy = val.length(); |
---|
| 34 | dimc = 0; |
---|
| 35 | }; |
---|
[19] | 36 | }; |
---|
| 37 | |
---|
[85] | 38 | //! Class representing function \f$f(x) = Ax+B\f$ |
---|
[477] | 39 | class linfn: public fnc { |
---|
[1130] | 40 | public: |
---|
[1064] | 41 | //! Identification of \f$x\f$ |
---|
| 42 | RV rv; |
---|
| 43 | //! Matrix A |
---|
| 44 | mat A; |
---|
| 45 | //! vector B |
---|
| 46 | vec B; |
---|
[477] | 47 | public : |
---|
[1064] | 48 | vec eval ( const vec &cond ) { |
---|
| 49 | bdm_assert_debug ( cond.length() == A.cols(), "linfn::eval Wrong cond." ); |
---|
| 50 | return A * cond + B; |
---|
| 51 | } |
---|
[19] | 52 | |
---|
[33] | 53 | // linfn evalsome ( ivec &rvind ); |
---|
[1064] | 54 | //!default constructor |
---|
| 55 | linfn ( ) : fnc(), A ( ), B () { }; |
---|
[1130] | 56 | linfn ( const mat &A0, const vec &B0 ) : fnc(), A (A0 ), B (B0) { }; |
---|
[1064] | 57 | //! Set values of \c A and \c B |
---|
| 58 | void set_parameters ( const mat &A0 , const vec &B0 ) { |
---|
| 59 | A = A0; |
---|
| 60 | B = B0; |
---|
| 61 | }; |
---|
| 62 | void from_setting(const Setting &set) { |
---|
| 63 | UI::get(A,set,"A",UI::compulsory); |
---|
| 64 | UI::get(B,set,"B",UI::compulsory); |
---|
| 65 | } |
---|
| 66 | void validate() { |
---|
| 67 | dimy = A.rows(); |
---|
| 68 | dimc = A.cols(); |
---|
| 69 | } |
---|
| 70 | |
---|
[19] | 71 | }; |
---|
[800] | 72 | UIREGISTER(linfn); |
---|
[19] | 73 | |
---|
[22] | 74 | |
---|
[1130] | 75 | //! Class representing function \f$ f(x) = [x' 1] Q [x' 1]' \f$ with Q in choleski decomposition |
---|
| 76 | class quadraticfn: public fnc { |
---|
| 77 | public : |
---|
| 78 | //! Identification of \f$x\f$ |
---|
| 79 | RV rv; |
---|
| 80 | //! Matrix Q |
---|
| 81 | chmat Q; |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | vec eval ( const vec &cond ) { |
---|
| 85 | bdm_assert_debug ( cond.length() == Q.cols(), "quadraticfc::eval Wrong cond." ); |
---|
| 86 | return vec_1( Q.qform(concat(cond,1.0)) ); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | void from_setting(const Setting &set) { |
---|
| 90 | // UI::get(Q,set,"Q",UI::compulsory); |
---|
| 91 | } |
---|
| 92 | void validate() { |
---|
| 93 | dimy = 1; |
---|
| 94 | dimc = Q.cols()-1; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | }; |
---|
| 98 | UIREGISTER(quadraticfn); |
---|
| 99 | |
---|
[22] | 100 | /*! |
---|
[85] | 101 | \brief Class representing a differentiable function of two variables \f$f(x,u)\f$. |
---|
[22] | 102 | |
---|
| 103 | Function of two variables. |
---|
| 104 | |
---|
| 105 | TODO: |
---|
| 106 | 1) Technically, it could have a common parent (e.g. \c fnc ) with other functions. For now, we keep it as it is. |
---|
| 107 | 2) It could be generalized into multivariate form, (which was original meaning of \c fnc ). |
---|
| 108 | */ |
---|
[477] | 109 | class diffbifn: public fnc { |
---|
| 110 | protected: |
---|
[1064] | 111 | //! Indentifier of the first rv. |
---|
| 112 | RV rvx; |
---|
| 113 | //! Indentifier of the second rv. |
---|
| 114 | RV rvu; |
---|
| 115 | //! cache for rvx.count() |
---|
| 116 | int dimx; |
---|
| 117 | //! cache for rvu.count() |
---|
| 118 | int dimu; |
---|
[477] | 119 | public: |
---|
[1064] | 120 | //! Evaluates \f$f(x0,u0)\f$ (VS: Do we really need common eval? ) |
---|
| 121 | vec eval ( const vec &cond ) { |
---|
| 122 | bdm_assert_debug ( cond.length() == ( dimx + dimu ), "linfn::eval Wrong cond." ); |
---|
| 123 | if ( dimu > 0 ) { |
---|
| 124 | return eval ( cond ( 0, dimx - 1 ), cond ( dimx, dimx + dimu - 1 ) );//-1 = end (in matlab) |
---|
| 125 | } else { |
---|
| 126 | return eval ( cond ( 0, dimx - 1 ), vec ( 0 ) );//-1 = end (in matlab) |
---|
| 127 | } |
---|
[737] | 128 | |
---|
[1064] | 129 | } |
---|
[22] | 130 | |
---|
[1064] | 131 | //! Evaluates \f$f(x0,u0)\f$ |
---|
| 132 | virtual vec eval ( const vec &x0, const vec &u0 ) { |
---|
| 133 | return zeros ( dimy ); |
---|
| 134 | }; |
---|
| 135 | //! Evaluates \f$A=\frac{d}{dx}f(x,u)|_{x0,u0}\f$ and writes result into \c A . @param full denotes that even unchanged entries are to be rewritten. When, false only the changed elements are computed. @param x0 numeric value of \f$x\f$, @param u0 numeric value of \f$u\f$ @param A a place where the result will be stored. |
---|
| 136 | virtual void dfdx_cond ( const vec &x0, const vec &u0, mat &A , bool full = true ) {}; |
---|
| 137 | //! Evaluates \f$A=\frac{d}{du}f(x,u)|_{x0,u0}\f$ and writes result into \c A . @param full denotes that even unchanged entries are to be rewritten. When, false only the changed elements are computed. @param x0 numeric value of \f$x\f$, @param u0 numeric value of \f$u\f$ @param A a place where the result will be stored. |
---|
| 138 | virtual void dfdu_cond ( const vec &x0, const vec &u0, mat &A, bool full = true ) {}; |
---|
| 139 | //!Default constructor (dimy is not set!) |
---|
| 140 | diffbifn () : fnc() {}; |
---|
| 141 | //! access function |
---|
| 142 | int _dimx() const { |
---|
| 143 | return dimx; |
---|
| 144 | } |
---|
| 145 | //! access function |
---|
| 146 | int _dimu() const { |
---|
| 147 | return dimu; |
---|
| 148 | } |
---|
[22] | 149 | }; |
---|
| 150 | |
---|
[85] | 151 | //! Class representing function \f$f(x,u) = Ax+Bu\f$ |
---|
[22] | 152 | //TODO can be generalized into multilinear form! |
---|
[477] | 153 | class bilinfn: public diffbifn { |
---|
[1064] | 154 | mat A; |
---|
| 155 | mat B; |
---|
[477] | 156 | public : |
---|
[1064] | 157 | //!\name Constructors |
---|
| 158 | //!@{ |
---|
[292] | 159 | |
---|
[1064] | 160 | bilinfn () : diffbifn (), A(), B() { } |
---|
[565] | 161 | |
---|
[1064] | 162 | bilinfn ( const mat &A0, const mat &B0 ) { |
---|
| 163 | set_parameters ( A0, B0 ); |
---|
| 164 | } |
---|
[565] | 165 | |
---|
[1064] | 166 | //! Alternative initialization |
---|
| 167 | void set_parameters ( const mat &A0, const mat &B0 ) { |
---|
| 168 | bdm_assert ( A0.rows() == B0.rows(), "bilinfn matrices must have the same number of rows" ); |
---|
| 169 | A = A0; |
---|
| 170 | B = B0; |
---|
| 171 | dimy = A.rows(); |
---|
| 172 | dimx = A.cols(); |
---|
| 173 | dimu = B.cols(); |
---|
| 174 | } |
---|
| 175 | //!@} |
---|
[477] | 176 | |
---|
[1064] | 177 | //!\name Mathematical operations |
---|
| 178 | //!@{ |
---|
| 179 | inline vec eval ( const vec &x0, const vec &u0 ) { |
---|
| 180 | bdm_assert_debug ( x0.length() == dimx, "bilinfn::eval Wrong xcond." ); |
---|
| 181 | bdm_assert_debug ( u0.length() == dimu, "bilinfn::eval Wrong ucond." ); |
---|
| 182 | return A*x0 + B*u0; |
---|
| 183 | } |
---|
[477] | 184 | |
---|
[1064] | 185 | void dfdx_cond ( const vec &x0, const vec &u0, mat &F, bool full ) { |
---|
| 186 | bdm_assert_debug ( ( F.cols() == A.cols() ) && ( F.rows() == A.rows() ), "Allocated F is not compatible." ); |
---|
| 187 | if ( full ) F = A; //else : nothing has changed no need to regenerate |
---|
| 188 | } |
---|
[565] | 189 | |
---|
[1064] | 190 | void dfdu_cond ( const vec &x0, const vec &u0, mat &F, bool full = true ) { |
---|
| 191 | bdm_assert_debug ( ( F.cols() == B.cols() ) && ( F.rows() == B.rows() ), "Allocated F is not compatible." ); |
---|
| 192 | if ( full ) F = B; //else : nothing has changed no need to regenerate |
---|
| 193 | } |
---|
| 194 | //!@} |
---|
[19] | 195 | }; |
---|
[33] | 196 | |
---|
[254] | 197 | } //namespace |
---|
[33] | 198 | #endif // FN_H |
---|