root/library/bdm/math/functions.h @ 1130

Revision 1130, 5.8 kB (checked in by smidl, 14 years ago)

Basic outline of universal LQG controller

  • Property svn:eol-style set to native
Line 
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
18namespace bdm {
19
20//! class representing function \f$f(x) = a\f$, here \c rv is empty
21class constfn : public fnc {
22    //! value of the function
23    vec val;
24
25public:
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$
39class 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;
47public :
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};
72UIREGISTER(linfn);
73
74
75//! Class representing function \f$ f(x) = [x' 1] Q [x' 1]' \f$ with Q in choleski decomposition
76class quadraticfn: public fnc {
77public :
78    //! Identification of \f$x\f$
79    RV rv;
80    //! Matrix Q
81    chmat Q;
82
83   
84    vec eval ( const vec &cond ) {
85        bdm_assert_debug ( cond.length() == Q.cols(), "quadraticfc::eval Wrong cond." );
86        return vec_1( Q.qform(concat(cond,1.0)) );
87    }
88
89    void from_setting(const Setting &set) {
90//        UI::get(Q,set,"Q",UI::compulsory);
91    }
92    void validate() {
93        dimy = 1;
94        dimc = Q.cols()-1;
95    }
96
97};
98UIREGISTER(quadraticfn);
99
100/*!
101\brief Class representing a differentiable function of two variables \f$f(x,u)\f$.
102
103Function of two variables.
104
105TODO:
1061) Technically, it could have a common parent (e.g. \c fnc ) with other functions. For now, we keep it as it is.
1072) It could be generalized into multivariate form, (which was original meaning of \c fnc ).
108*/
109class diffbifn: public fnc {
110protected:
111    //! Indentifier of the first rv.
112    RV rvx;
113    //! Indentifier of the second rv.
114    RV rvu;
115    //! cache for rvx.count()
116    int dimx;
117    //! cache for rvu.count()
118    int dimu;
119public:
120    //! Evaluates \f$f(x0,u0)\f$ (VS: Do we really need common eval? )
121    vec eval ( const vec &cond ) {
122        bdm_assert_debug ( cond.length() == ( dimx + dimu ), "linfn::eval Wrong cond." );
123        if ( dimu > 0 ) {
124            return eval ( cond ( 0, dimx - 1 ), cond ( dimx, dimx + dimu - 1 ) );//-1 = end (in matlab)
125        } else {
126            return eval ( cond ( 0, dimx - 1 ), vec ( 0 ) );//-1 = end (in matlab)
127        }
128
129    }
130
131    //! Evaluates \f$f(x0,u0)\f$
132    virtual vec eval ( const vec &x0, const vec &u0 ) {
133        return zeros ( dimy );
134    };
135    //! 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.
136    virtual void dfdx_cond ( const vec &x0, const vec &u0, mat &A , bool full = true ) {};
137    //! 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.
138    virtual void dfdu_cond ( const vec &x0, const vec &u0, mat &A, bool full = true ) {};
139    //!Default constructor (dimy is not set!)
140    diffbifn () : fnc() {};
141    //! access function
142    int _dimx() const {
143        return dimx;
144    }
145    //! access function
146    int _dimu() const {
147        return dimu;
148    }
149};
150
151//! Class representing function \f$f(x,u) = Ax+Bu\f$
152//TODO can be generalized into multilinear form!
153class bilinfn: public diffbifn {
154    mat A;
155    mat B;
156public :
157    //!\name Constructors
158    //!@{
159
160    bilinfn () : diffbifn (), A(), B() { }
161
162    bilinfn ( const mat &A0, const mat &B0 ) {
163        set_parameters ( A0, B0 );
164    }
165
166    //! Alternative initialization
167    void set_parameters ( const mat &A0, const mat &B0 ) {
168        bdm_assert ( A0.rows() == B0.rows(), "bilinfn matrices must have the same number of rows" );
169        A = A0;
170        B = B0;
171        dimy = A.rows();
172        dimx = A.cols();
173        dimu = B.cols();
174    }
175    //!@}
176
177    //!\name Mathematical operations
178    //!@{
179    inline vec eval ( const  vec &x0, const vec &u0 ) {
180        bdm_assert_debug ( x0.length() == dimx, "bilinfn::eval Wrong xcond." );
181        bdm_assert_debug ( u0.length() == dimu, "bilinfn::eval Wrong ucond." );
182        return A*x0 + B*u0;
183    }
184
185    void dfdx_cond ( const vec &x0, const vec &u0, mat &F, bool full ) {
186        bdm_assert_debug ( ( F.cols() == A.cols() ) && ( F.rows() == A.rows() ), "Allocated F is not compatible." );
187        if ( full ) F = A;      //else : nothing has changed no need to regenerate
188    }
189
190    void dfdu_cond ( const vec &x0, const vec &u0, mat &F,  bool full = true ) {
191        bdm_assert_debug ( ( F.cols() == B.cols() ) && ( F.rows() == B.rows() ), "Allocated F is not compatible." );
192        if ( full ) F = B;      //else : nothing has changed no need to regenerate
193    }
194    //!@}
195};
196
197} //namespace
198#endif // FN_H
Note: See TracBrowser for help on using the browser.