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