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 <itpp/itbase.h> |
---|
16 | #include "libBM.h" |
---|
17 | |
---|
18 | using namespace itpp; |
---|
19 | |
---|
20 | //! class representing function $f(x) = a$, here rv is empty |
---|
21 | class constfn : public fnc |
---|
22 | { |
---|
23 | //! value of the function |
---|
24 | vec val; |
---|
25 | |
---|
26 | public: |
---|
27 | //vec eval() {return val;}; |
---|
28 | //! inherited |
---|
29 | vec eval ( const vec &cond ) {return val;}; |
---|
30 | //!Default constructor |
---|
31 | constfn ( const vec &val0 ) :val ( val0 ) {}; |
---|
32 | }; |
---|
33 | |
---|
34 | //! Class representing function $f(x) = Ax+B$ |
---|
35 | class linfn: public fnc |
---|
36 | { |
---|
37 | //! Identification of $x$ |
---|
38 | RV rv; |
---|
39 | //! Matrix A |
---|
40 | mat A; |
---|
41 | //! Matrix B |
---|
42 | vec B; |
---|
43 | public : |
---|
44 | vec eval (const vec &cond ) {it_assert_debug ( cond.length() ==rv.count(), "linfn::eval Wrong cond." );return A*cond+B;}; |
---|
45 | |
---|
46 | // linfn evalsome ( ivec &rvind ); |
---|
47 | //!default constructor |
---|
48 | linfn ( const RV &rv0 ) :rv ( rv0 ),A ( eye ( rv0.count() ) ),B ( zeros ( rv0.count() ) ) { }; |
---|
49 | //! Set values of \c A and \c B |
---|
50 | void set_parameters ( const mat &A0 , const vec &B0 ) {A=A0; B=B0;}; |
---|
51 | }; |
---|
52 | |
---|
53 | |
---|
54 | /*! |
---|
55 | \brief Class representing a differentiable function of two variables $f(x,u)$. |
---|
56 | |
---|
57 | Function of two variables. |
---|
58 | |
---|
59 | TODO: |
---|
60 | 1) Technically, it could have a common parent (e.g. \c fnc ) with other functions. For now, we keep it as it is. |
---|
61 | 2) It could be generalized into multivariate form, (which was original meaning of \c fnc ). |
---|
62 | */ |
---|
63 | class diffbifn: public fnc |
---|
64 | { |
---|
65 | protected: |
---|
66 | //! Indentifier of the first rv. |
---|
67 | RV rvx; |
---|
68 | //! Indentifier of the second rv. |
---|
69 | RV rvu; |
---|
70 | //! cache for rvx.count() |
---|
71 | int dimx; |
---|
72 | //! cache for rvu.count() |
---|
73 | int dimu; |
---|
74 | public: |
---|
75 | //! Evaluates $f(x0,u0)$ (VS: Do we really need common eval? ) |
---|
76 | vec eval ( const vec &cond ) |
---|
77 | { |
---|
78 | it_assert_debug ( cond.length() == ( dimx+dimu ), "linfn::eval Wrong cond." ); |
---|
79 | return eval ( cond ( 0,dimx-1 ),cond ( dimx,dimx+dimu ) );//-1 = end (in matlab) |
---|
80 | }; |
---|
81 | |
---|
82 | //! Evaluates $f(x0,u0)$ |
---|
83 | virtual vec eval ( const vec &x0, const vec &u0 ) {return zeros ( dimy );}; |
---|
84 | //! 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 $x$, @param u0 numeric value of $u$ @param A a place where the result will be stored. |
---|
85 | virtual void dfdx_cond ( const vec &x0, const vec &u0, mat &A , bool full=true ) {}; |
---|
86 | //! 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 $x$, @param u0 numeric value of $u$ @param A a place where the result will be stored. |
---|
87 | virtual void dfdu_cond ( const vec &x0, const vec &u0, mat &A, bool full=true ) {}; |
---|
88 | //!Default constructor (dimy is not set!) |
---|
89 | diffbifn ( const RV rvx0, const RV rvu0 ) : rvx ( rvx0 ),rvu ( rvu0 ) {dimx=rvx.count();dimu=rvu.count();}; |
---|
90 | //! access function |
---|
91 | int _dimx() const{return dimx;} |
---|
92 | //! access function |
---|
93 | int _dimu() const{return dimu;} |
---|
94 | }; |
---|
95 | |
---|
96 | //! Class representing function $f(x,u) = Ax+Bu$ |
---|
97 | //TODO can be generalized into multilinear form! |
---|
98 | class bilinfn: public diffbifn |
---|
99 | { |
---|
100 | mat A; |
---|
101 | mat B; |
---|
102 | public : |
---|
103 | vec eval ( const vec &x0, const vec &u0 ); |
---|
104 | |
---|
105 | //! Default constructor |
---|
106 | bilinfn ( const RV &rvx0, const RV &rvu0 ) : diffbifn ( rvx0,rvu0 ) ,A ( eye ( dimx ) ),B ( zeros ( dimx,dimu ) ) {}; |
---|
107 | //! Alternative constructor |
---|
108 | bilinfn ( const RV &rvx0, const RV &rvu0, const mat &A0, const mat &B0 ); |
---|
109 | //! |
---|
110 | void dfdx_cond ( const vec &x0, const vec &u0, mat &F, bool full ) |
---|
111 | { |
---|
112 | it_assert_debug ( ( F.cols() ==A.cols() ) & ( F.rows() ==A.rows() ),"Allocated F is not compatible." ); |
---|
113 | if ( full ) F=A; //else : nothing has changed no need to regenerate |
---|
114 | } |
---|
115 | //! |
---|
116 | void dfdu_cond ( const vec &x0, const vec &u0, mat &F, bool full=true ) |
---|
117 | { |
---|
118 | it_assert_debug ( ( F.cols() ==B.cols() ) & ( F.rows() ==B.rows() ),"Allocated F is not compatible." ); |
---|
119 | if ( full ) F=B; //else : nothing has changed no need to regenerate |
---|
120 | } |
---|
121 | }; |
---|
122 | |
---|
123 | #endif // FN_H |
---|