1 | // |
---|
2 | // C++ Interface: itpp_ext |
---|
3 | // |
---|
4 | // Description: |
---|
5 | // |
---|
6 | // |
---|
7 | // Author: smidl <smidl@utia.cas.cz>, (C) 2008 |
---|
8 | // |
---|
9 | // Copyright: See COPYING file that comes with this distribution |
---|
10 | // |
---|
11 | // |
---|
12 | #ifndef FN_H |
---|
13 | #define FN_H |
---|
14 | |
---|
15 | #include "libBM.h" |
---|
16 | |
---|
17 | namespace bdm{ |
---|
18 | |
---|
19 | //! class representing function \f$f(x) = a\f$, here \c rv is empty |
---|
20 | class constfn : public fnc |
---|
21 | { |
---|
22 | //! value of the function |
---|
23 | vec val; |
---|
24 | |
---|
25 | public: |
---|
26 | //vec eval() {return val;}; |
---|
27 | //! inherited |
---|
28 | vec eval ( const vec &cond ) {return val;}; |
---|
29 | //!Default constructor |
---|
30 | constfn ( const vec &val0 ) :fnc(), val ( val0 ) {dimy=val.length();}; |
---|
31 | }; |
---|
32 | |
---|
33 | //! Class representing function \f$f(x) = Ax+B\f$ |
---|
34 | class linfn: public fnc |
---|
35 | { |
---|
36 | //! Identification of \f$x\f$ |
---|
37 | RV rv; |
---|
38 | //! Matrix A |
---|
39 | mat A; |
---|
40 | //! vector B |
---|
41 | vec B; |
---|
42 | public : |
---|
43 | vec eval (const vec &cond ) {it_assert_debug ( cond.length() ==A.cols(), "linfn::eval Wrong cond." );return A*cond+B;}; |
---|
44 | |
---|
45 | // linfn evalsome ( ivec &rvind ); |
---|
46 | //!default constructor |
---|
47 | linfn ( ) : fnc(), A ( ),B () { }; |
---|
48 | //! Set values of \c A and \c B |
---|
49 | void set_parameters ( const mat &A0 , const vec &B0 ) {A=A0; B=B0; dimy=A.rows();}; |
---|
50 | }; |
---|
51 | |
---|
52 | |
---|
53 | /*! |
---|
54 | \brief Class representing a differentiable function of two variables \f$f(x,u)\f$. |
---|
55 | |
---|
56 | Function of two variables. |
---|
57 | |
---|
58 | TODO: |
---|
59 | 1) Technically, it could have a common parent (e.g. \c fnc ) with other functions. For now, we keep it as it is. |
---|
60 | 2) It could be generalized into multivariate form, (which was original meaning of \c fnc ). |
---|
61 | */ |
---|
62 | class diffbifn: public fnc |
---|
63 | { |
---|
64 | protected: |
---|
65 | //! Indentifier of the first rv. |
---|
66 | RV rvx; |
---|
67 | //! Indentifier of the second rv. |
---|
68 | RV rvu; |
---|
69 | //! cache for rvx.count() |
---|
70 | int dimx; |
---|
71 | //! cache for rvu.count() |
---|
72 | int dimu; |
---|
73 | public: |
---|
74 | //! Evaluates \f$f(x0,u0)\f$ (VS: Do we really need common eval? ) |
---|
75 | vec eval ( const vec &cond ) |
---|
76 | { |
---|
77 | it_assert_debug ( cond.length() == ( dimx+dimu ), "linfn::eval Wrong cond." ); |
---|
78 | return eval ( cond ( 0,dimx-1 ),cond ( dimx,dimx+dimu ) );//-1 = end (in matlab) |
---|
79 | }; |
---|
80 | |
---|
81 | //! Evaluates \f$f(x0,u0)\f$ |
---|
82 | virtual vec eval ( const vec &x0, const vec &u0 ) {return zeros ( dimy );}; |
---|
83 | //! 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. |
---|
84 | virtual void dfdx_cond ( const vec &x0, const vec &u0, mat &A , bool full=true ) {}; |
---|
85 | //! 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. |
---|
86 | virtual void dfdu_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) {}; |
---|
87 | //!Default constructor (dimy is not set!) |
---|
88 | diffbifn () : fnc() {}; |
---|
89 | //! access function |
---|
90 | int _dimx() const{return dimx;} |
---|
91 | //! access function |
---|
92 | int _dimu() const{return dimu;} |
---|
93 | }; |
---|
94 | |
---|
95 | //! Class representing function \f$f(x,u) = Ax+Bu\f$ |
---|
96 | //TODO can be generalized into multilinear form! |
---|
97 | class bilinfn: public diffbifn |
---|
98 | { |
---|
99 | mat A; |
---|
100 | mat B; |
---|
101 | public : |
---|
102 | //!\name Constructors |
---|
103 | //!@{ |
---|
104 | |
---|
105 | bilinfn () : diffbifn () ,A() ,B() {}; |
---|
106 | bilinfn (const mat A0, const mat B0) {set_parameters(A0,B0);}; |
---|
107 | //! Alternative constructor |
---|
108 | void set_parameters(const mat A0, const mat B0){ |
---|
109 | it_assert_debug(A0.rows()==B0.rows(),""); |
---|
110 | A=A0;B=B0; |
---|
111 | dimy=A.rows(); |
---|
112 | dimx=A.cols(); |
---|
113 | dimu=B.cols(); |
---|
114 | } |
---|
115 | //!@} |
---|
116 | |
---|
117 | //!\name Mathematical operations |
---|
118 | //!@{ |
---|
119 | inline vec eval ( const vec &x0, const vec &u0 ) |
---|
120 | { |
---|
121 | it_assert_debug ( x0.length() ==dimx, "linfn::eval Wrong xcond." ); |
---|
122 | it_assert_debug ( u0.length() ==dimu, "linfn::eval Wrong ucond." ); |
---|
123 | return A*x0+B*u0; |
---|
124 | } |
---|
125 | |
---|
126 | void dfdx_cond ( const vec &x0, const vec &u0, mat &F, bool full ) |
---|
127 | { |
---|
128 | it_assert_debug ( ( F.cols() ==A.cols() ) & ( F.rows() ==A.rows() ),"Allocated F is not compatible." ); |
---|
129 | if ( full ) F=A; //else : nothing has changed no need to regenerate |
---|
130 | } |
---|
131 | //! |
---|
132 | void dfdu_cond ( const vec &x0, const vec &u0, mat &F, bool full=true ) |
---|
133 | { |
---|
134 | it_assert_debug ( ( F.cols() ==B.cols() ) & ( F.rows() ==B.rows() ),"Allocated F is not compatible." ); |
---|
135 | if ( full ) F=B; //else : nothing has changed no need to regenerate |
---|
136 | } |
---|
137 | //!@} |
---|
138 | }; |
---|
139 | |
---|
140 | } //namespace |
---|
141 | #endif // FN_H |
---|