[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 { |
---|
| 22 | //! value of the function |
---|
| 23 | vec val; |
---|
[19] | 24 | |
---|
[477] | 25 | public: |
---|
| 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(); |
---|
[800] | 34 | dimc = 0; |
---|
[477] | 35 | }; |
---|
[19] | 36 | }; |
---|
| 37 | |
---|
[85] | 38 | //! Class representing function \f$f(x) = Ax+B\f$ |
---|
[477] | 39 | class linfn: public fnc { |
---|
| 40 | //! Identification of \f$x\f$ |
---|
| 41 | RV rv; |
---|
| 42 | //! Matrix A |
---|
| 43 | mat A; |
---|
| 44 | //! vector B |
---|
| 45 | vec B; |
---|
| 46 | public : |
---|
| 47 | vec eval ( const vec &cond ) { |
---|
[565] | 48 | bdm_assert_debug ( cond.length() == A.cols(), "linfn::eval Wrong cond." ); |
---|
| 49 | return A * cond + B; |
---|
| 50 | } |
---|
[19] | 51 | |
---|
[33] | 52 | // linfn evalsome ( ivec &rvind ); |
---|
[477] | 53 | //!default constructor |
---|
| 54 | linfn ( ) : fnc(), A ( ), B () { }; |
---|
| 55 | //! Set values of \c A and \c B |
---|
| 56 | void set_parameters ( const mat &A0 , const vec &B0 ) { |
---|
| 57 | A = A0; |
---|
| 58 | B = B0; |
---|
[800] | 59 | }; |
---|
| 60 | void from_setting(const Setting &set){ |
---|
| 61 | UI::get(A,set,"A",UI::compulsory); |
---|
| 62 | UI::get(B,set,"B",UI::compulsory); |
---|
| 63 | } |
---|
| 64 | void validate(){ |
---|
[477] | 65 | dimy = A.rows(); |
---|
[800] | 66 | dimc = A.cols(); |
---|
| 67 | } |
---|
| 68 | |
---|
[19] | 69 | }; |
---|
[800] | 70 | UIREGISTER(linfn); |
---|
[19] | 71 | |
---|
[22] | 72 | |
---|
| 73 | /*! |
---|
[85] | 74 | \brief Class representing a differentiable function of two variables \f$f(x,u)\f$. |
---|
[22] | 75 | |
---|
| 76 | Function of two variables. |
---|
| 77 | |
---|
| 78 | TODO: |
---|
| 79 | 1) Technically, it could have a common parent (e.g. \c fnc ) with other functions. For now, we keep it as it is. |
---|
| 80 | 2) It could be generalized into multivariate form, (which was original meaning of \c fnc ). |
---|
| 81 | */ |
---|
[477] | 82 | class diffbifn: public fnc { |
---|
| 83 | protected: |
---|
| 84 | //! Indentifier of the first rv. |
---|
| 85 | RV rvx; |
---|
| 86 | //! Indentifier of the second rv. |
---|
| 87 | RV rvu; |
---|
| 88 | //! cache for rvx.count() |
---|
| 89 | int dimx; |
---|
| 90 | //! cache for rvu.count() |
---|
| 91 | int dimu; |
---|
| 92 | public: |
---|
| 93 | //! Evaluates \f$f(x0,u0)\f$ (VS: Do we really need common eval? ) |
---|
| 94 | vec eval ( const vec &cond ) { |
---|
[565] | 95 | bdm_assert_debug ( cond.length() == ( dimx + dimu ), "linfn::eval Wrong cond." ); |
---|
[737] | 96 | if ( dimu > 0 ) { |
---|
[665] | 97 | return eval ( cond ( 0, dimx - 1 ), cond ( dimx, dimx + dimu - 1 ) );//-1 = end (in matlab) |
---|
| 98 | } else { |
---|
[737] | 99 | return eval ( cond ( 0, dimx - 1 ), vec ( 0 ) );//-1 = end (in matlab) |
---|
[665] | 100 | } |
---|
[737] | 101 | |
---|
[565] | 102 | } |
---|
[22] | 103 | |
---|
[477] | 104 | //! Evaluates \f$f(x0,u0)\f$ |
---|
| 105 | virtual vec eval ( const vec &x0, const vec &u0 ) { |
---|
| 106 | return zeros ( dimy ); |
---|
| 107 | }; |
---|
| 108 | //! 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. |
---|
| 109 | virtual void dfdx_cond ( const vec &x0, const vec &u0, mat &A , bool full = true ) {}; |
---|
| 110 | //! 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. |
---|
| 111 | virtual void dfdu_cond ( const vec &x0, const vec &u0, mat &A, bool full = true ) {}; |
---|
| 112 | //!Default constructor (dimy is not set!) |
---|
| 113 | diffbifn () : fnc() {}; |
---|
| 114 | //! access function |
---|
| 115 | int _dimx() const { |
---|
| 116 | return dimx; |
---|
| 117 | } |
---|
| 118 | //! access function |
---|
| 119 | int _dimu() const { |
---|
| 120 | return dimu; |
---|
| 121 | } |
---|
[22] | 122 | }; |
---|
| 123 | |
---|
[85] | 124 | //! Class representing function \f$f(x,u) = Ax+Bu\f$ |
---|
[22] | 125 | //TODO can be generalized into multilinear form! |
---|
[477] | 126 | class bilinfn: public diffbifn { |
---|
| 127 | mat A; |
---|
| 128 | mat B; |
---|
| 129 | public : |
---|
| 130 | //!\name Constructors |
---|
| 131 | //!@{ |
---|
[292] | 132 | |
---|
[565] | 133 | bilinfn () : diffbifn (), A(), B() { } |
---|
| 134 | |
---|
| 135 | bilinfn ( const mat &A0, const mat &B0 ) { |
---|
[477] | 136 | set_parameters ( A0, B0 ); |
---|
[565] | 137 | } |
---|
| 138 | |
---|
| 139 | //! Alternative initialization |
---|
| 140 | void set_parameters ( const mat &A0, const mat &B0 ) { |
---|
[620] | 141 | bdm_assert ( A0.rows() == B0.rows(), "bilinfn matrices must have the same number of rows" ); |
---|
[477] | 142 | A = A0; |
---|
| 143 | B = B0; |
---|
| 144 | dimy = A.rows(); |
---|
| 145 | dimx = A.cols(); |
---|
| 146 | dimu = B.cols(); |
---|
| 147 | } |
---|
| 148 | //!@} |
---|
| 149 | |
---|
| 150 | //!\name Mathematical operations |
---|
| 151 | //!@{ |
---|
| 152 | inline vec eval ( const vec &x0, const vec &u0 ) { |
---|
[565] | 153 | bdm_assert_debug ( x0.length() == dimx, "bilinfn::eval Wrong xcond." ); |
---|
| 154 | bdm_assert_debug ( u0.length() == dimu, "bilinfn::eval Wrong ucond." ); |
---|
[477] | 155 | return A*x0 + B*u0; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | void dfdx_cond ( const vec &x0, const vec &u0, mat &F, bool full ) { |
---|
[565] | 159 | bdm_assert_debug ( ( F.cols() == A.cols() ) && ( F.rows() == A.rows() ), "Allocated F is not compatible." ); |
---|
[477] | 160 | if ( full ) F = A; //else : nothing has changed no need to regenerate |
---|
| 161 | } |
---|
[565] | 162 | |
---|
[477] | 163 | void dfdu_cond ( const vec &x0, const vec &u0, mat &F, bool full = true ) { |
---|
[565] | 164 | bdm_assert_debug ( ( F.cols() == B.cols() ) && ( F.rows() == B.rows() ), "Allocated F is not compatible." ); |
---|
[477] | 165 | if ( full ) F = B; //else : nothing has changed no need to regenerate |
---|
| 166 | } |
---|
| 167 | //!@} |
---|
[19] | 168 | }; |
---|
[33] | 169 | |
---|
[254] | 170 | } //namespace |
---|
[33] | 171 | #endif // FN_H |
---|