root/bdm/stat/libFN.h @ 22

Revision 22, 3.7 kB (checked in by smidl, 16 years ago)

upravy Kalmana

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