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 "../bdmerror.h" |
---|
16 | #include "../base/bdmbase.h" |
---|
17 | |
---|
18 | namespace bdm { |
---|
19 | |
---|
20 | //! class representing function \f$f(x) = a\f$, here \c rv is empty |
---|
21 | class constfn : public fnc { |
---|
22 | //! value of the function |
---|
23 | vec val; |
---|
24 | |
---|
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 | dimc = 0; |
---|
35 | }; |
---|
36 | }; |
---|
37 | |
---|
38 | //! Class representing function \f$f(x) = Ax+B\f$ |
---|
39 | class linfn: public fnc { |
---|
40 | public: |
---|
41 | //! Identification of \f$x\f$ |
---|
42 | RV rv; |
---|
43 | //! Matrix A |
---|
44 | mat A; |
---|
45 | //! vector B |
---|
46 | vec B; |
---|
47 | public : |
---|
48 | vec eval ( const vec &cond ) { |
---|
49 | bdm_assert_debug ( cond.length() == A.cols(), "linfn::eval Wrong cond." ); |
---|
50 | return A * cond + B; |
---|
51 | } |
---|
52 | |
---|
53 | // linfn evalsome ( ivec &rvind ); |
---|
54 | //!default constructor |
---|
55 | linfn ( ) : fnc(), A ( ), B () { }; |
---|
56 | linfn ( const mat &A0, const vec &B0 ) : fnc(), A (A0 ), B (B0) { }; |
---|
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 | |
---|
71 | }; |
---|
72 | UIREGISTER(linfn); |
---|
73 | |
---|
74 | |
---|
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 | //!default constructor |
---|
84 | quadraticfn ( ) : fnc(), Q ( ) { }; |
---|
85 | quadraticfn ( const mat &Q0) : fnc(), Q (Q0 ) { }; |
---|
86 | |
---|
87 | vec eval ( const vec &cond ) { |
---|
88 | bdm_assert_debug ( cond.length() == Q.cols(), "quadraticfc::eval Wrong cond." ); |
---|
89 | return vec_1( Q.qform(concat(cond,1.0)) ); |
---|
90 | } |
---|
91 | |
---|
92 | void from_setting(const Setting &set) { |
---|
93 | //UI::get(Q,set,"Q",UI::compulsory); |
---|
94 | } |
---|
95 | void validate() { |
---|
96 | dimy = 1; |
---|
97 | dimc = Q.cols()-1; |
---|
98 | } |
---|
99 | |
---|
100 | }; |
---|
101 | UIREGISTER(quadraticfn); |
---|
102 | |
---|
103 | /*! |
---|
104 | \brief Class representing a differentiable function of two variables \f$f(x,u)\f$. |
---|
105 | |
---|
106 | Function of two variables. |
---|
107 | |
---|
108 | TODO: |
---|
109 | 1) Technically, it could have a common parent (e.g. \c fnc ) with other functions. For now, we keep it as it is. |
---|
110 | 2) It could be generalized into multivariate form, (which was original meaning of \c fnc ). |
---|
111 | */ |
---|
112 | class diffbifn: public fnc { |
---|
113 | protected: |
---|
114 | //! Indentifier of the first rv. |
---|
115 | RV rvx; |
---|
116 | //! Indentifier of the second rv. |
---|
117 | RV rvu; |
---|
118 | //! cache for rvx.count() |
---|
119 | int dimx; |
---|
120 | //! cache for rvu.count() |
---|
121 | int dimu; |
---|
122 | public: |
---|
123 | //! Evaluates \f$f(x0,u0)\f$ (VS: Do we really need common eval? ) |
---|
124 | vec eval ( const vec &cond ) { |
---|
125 | bdm_assert_debug ( cond.length() == ( dimx + dimu ), "linfn::eval Wrong cond." ); |
---|
126 | if ( dimu > 0 ) { |
---|
127 | return eval ( cond ( 0, dimx - 1 ), cond ( dimx, dimx + dimu - 1 ) );//-1 = end (in matlab) |
---|
128 | } else { |
---|
129 | return eval ( cond ( 0, dimx - 1 ), vec ( 0 ) );//-1 = end (in matlab) |
---|
130 | } |
---|
131 | |
---|
132 | } |
---|
133 | |
---|
134 | //! Evaluates \f$f(x0,u0)\f$ |
---|
135 | virtual vec eval ( const vec &x0, const vec &u0 ) { |
---|
136 | return zeros ( dimy ); |
---|
137 | }; |
---|
138 | //! 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. |
---|
139 | virtual void dfdx_cond ( const vec &x0, const vec &u0, mat &A , bool full = true ) {}; |
---|
140 | //! 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. |
---|
141 | virtual void dfdu_cond ( const vec &x0, const vec &u0, mat &A, bool full = true ) {}; |
---|
142 | //!Default constructor (dimy is not set!) |
---|
143 | diffbifn () : fnc() {}; |
---|
144 | //! access function |
---|
145 | int _dimx() const { |
---|
146 | return dimx; |
---|
147 | } |
---|
148 | //! access function |
---|
149 | int _dimu() const { |
---|
150 | return dimu; |
---|
151 | } |
---|
152 | void validate(){dimc=dimx+dimu;}; |
---|
153 | }; |
---|
154 | |
---|
155 | //! Class representing function \f$f(x,u) = Ax+Bu\f$ |
---|
156 | //TODO can be generalized into multilinear form! |
---|
157 | class bilinfn: public diffbifn { |
---|
158 | mat A; |
---|
159 | mat B; |
---|
160 | public : |
---|
161 | //!\name Constructors |
---|
162 | //!@{ |
---|
163 | |
---|
164 | bilinfn () : diffbifn (), A(), B() { } |
---|
165 | |
---|
166 | bilinfn ( const mat &A0, const mat &B0 ) { |
---|
167 | set_parameters ( A0, B0 ); |
---|
168 | } |
---|
169 | |
---|
170 | //! Alternative initialization |
---|
171 | void set_parameters ( const mat &A0, const mat &B0 ) { |
---|
172 | bdm_assert ( A0.rows() == B0.rows(), "bilinfn matrices must have the same number of rows" ); |
---|
173 | A = A0; |
---|
174 | B = B0; |
---|
175 | dimy = A.rows(); |
---|
176 | dimx = A.cols(); |
---|
177 | dimu = B.cols(); |
---|
178 | } |
---|
179 | //!@} |
---|
180 | |
---|
181 | //!\name Mathematical operations |
---|
182 | //!@{ |
---|
183 | inline vec eval ( const vec &x0, const vec &u0 ) { |
---|
184 | bdm_assert_debug ( x0.length() == dimx, "bilinfn::eval Wrong xcond." ); |
---|
185 | bdm_assert_debug ( u0.length() == dimu, "bilinfn::eval Wrong ucond." ); |
---|
186 | return dimu>0 ? A*x0 + B*u0 : A*x0; |
---|
187 | } |
---|
188 | |
---|
189 | void dfdx_cond ( const vec &x0, const vec &u0, mat &F, bool full ) { |
---|
190 | bdm_assert_debug ( ( F.cols() == A.cols() ) && ( F.rows() == A.rows() ), "Allocated F is not compatible." ); |
---|
191 | if ( full ) F = A; //else : nothing has changed no need to regenerate |
---|
192 | } |
---|
193 | |
---|
194 | void dfdu_cond ( const vec &x0, const vec &u0, mat &F, bool full = true ) { |
---|
195 | bdm_assert_debug ( ( F.cols() == B.cols() ) && ( F.rows() == B.rows() ), "Allocated F is not compatible." ); |
---|
196 | if ( full ) F = B; //else : nothing has changed no need to regenerate |
---|
197 | } |
---|
198 | void from_setting(const Setting &set) { |
---|
199 | diffbifn::from_setting(set); |
---|
200 | UI::get(A,set,"A",UI::compulsory); |
---|
201 | UI::get(B,set,"B",UI::compulsory); |
---|
202 | } |
---|
203 | void validate() { |
---|
204 | dimy = A.rows(); |
---|
205 | dimx = A.cols(); |
---|
206 | dimu = B.cols(); |
---|
207 | dimc =dimx+dimu; |
---|
208 | } |
---|
209 | //!@} |
---|
210 | }; |
---|
211 | UIREGISTER(bilinfn); |
---|
212 | |
---|
213 | } //namespace |
---|
214 | #endif // FN_H |
---|