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

Revision 1064, 5.1 kB (checked in by mido, 14 years ago)

astyle applied all over the library

  • 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    //! Identification of \f$x\f$
41    RV rv;
42    //! Matrix A
43    mat A;
44    //! vector B
45    vec B;
46public :
47    vec eval ( const vec &cond ) {
48        bdm_assert_debug ( cond.length() == A.cols(), "linfn::eval Wrong cond." );
49        return A * cond + B;
50    }
51
52//              linfn evalsome ( ivec &rvind );
53    //!default constructor
54    linfn ( ) : fnc(), A ( ), B () { };
55    //! Set values of \c A and \c B
56    void set_parameters ( const mat &A0 , const vec &B0 ) {
57        A = A0;
58        B = B0;
59    };
60    void from_setting(const Setting &set) {
61        UI::get(A,set,"A",UI::compulsory);
62        UI::get(B,set,"B",UI::compulsory);
63    }
64    void validate() {
65        dimy = A.rows();
66        dimc = A.cols();
67    }
68
69};
70UIREGISTER(linfn);
71
72
73/*!
74\brief Class representing a differentiable function of two variables \f$f(x,u)\f$.
75
76Function of two variables.
77
78TODO:
791) Technically, it could have a common parent (e.g. \c fnc ) with other functions. For now, we keep it as it is.
802) It could be generalized into multivariate form, (which was original meaning of \c fnc ).
81*/
82class diffbifn: public fnc {
83protected:
84    //! Indentifier of the first rv.
85    RV rvx;
86    //! Indentifier of the second rv.
87    RV rvu;
88    //! cache for rvx.count()
89    int dimx;
90    //! cache for rvu.count()
91    int dimu;
92public:
93    //! Evaluates \f$f(x0,u0)\f$ (VS: Do we really need common eval? )
94    vec eval ( const vec &cond ) {
95        bdm_assert_debug ( cond.length() == ( dimx + dimu ), "linfn::eval Wrong cond." );
96        if ( dimu > 0 ) {
97            return eval ( cond ( 0, dimx - 1 ), cond ( dimx, dimx + dimu - 1 ) );//-1 = end (in matlab)
98        } else {
99            return eval ( cond ( 0, dimx - 1 ), vec ( 0 ) );//-1 = end (in matlab)
100        }
101
102    }
103
104    //! Evaluates \f$f(x0,u0)\f$
105    virtual vec eval ( const vec &x0, const vec &u0 ) {
106        return zeros ( dimy );
107    };
108    //! 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.
109    virtual void dfdx_cond ( const vec &x0, const vec &u0, mat &A , bool full = true ) {};
110    //! 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.
111    virtual void dfdu_cond ( const vec &x0, const vec &u0, mat &A, bool full = true ) {};
112    //!Default constructor (dimy is not set!)
113    diffbifn () : fnc() {};
114    //! access function
115    int _dimx() const {
116        return dimx;
117    }
118    //! access function
119    int _dimu() const {
120        return dimu;
121    }
122};
123
124//! Class representing function \f$f(x,u) = Ax+Bu\f$
125//TODO can be generalized into multilinear form!
126class bilinfn: public diffbifn {
127    mat A;
128    mat B;
129public :
130    //!\name Constructors
131    //!@{
132
133    bilinfn () : diffbifn (), A(), B() { }
134
135    bilinfn ( const mat &A0, const mat &B0 ) {
136        set_parameters ( A0, B0 );
137    }
138
139    //! Alternative initialization
140    void set_parameters ( const mat &A0, const mat &B0 ) {
141        bdm_assert ( A0.rows() == B0.rows(), "bilinfn matrices must have the same number of rows" );
142        A = A0;
143        B = B0;
144        dimy = A.rows();
145        dimx = A.cols();
146        dimu = B.cols();
147    }
148    //!@}
149
150    //!\name Mathematical operations
151    //!@{
152    inline vec eval ( const  vec &x0, const vec &u0 ) {
153        bdm_assert_debug ( x0.length() == dimx, "bilinfn::eval Wrong xcond." );
154        bdm_assert_debug ( u0.length() == dimu, "bilinfn::eval Wrong ucond." );
155        return A*x0 + B*u0;
156    }
157
158    void dfdx_cond ( const vec &x0, const vec &u0, mat &F, bool full ) {
159        bdm_assert_debug ( ( F.cols() == A.cols() ) && ( F.rows() == A.rows() ), "Allocated F is not compatible." );
160        if ( full ) F = A;      //else : nothing has changed no need to regenerate
161    }
162
163    void dfdu_cond ( const vec &x0, const vec &u0, mat &F,  bool full = true ) {
164        bdm_assert_debug ( ( F.cols() == B.cols() ) && ( F.rows() == B.rows() ), "Allocated F is not compatible." );
165        if ( full ) F = B;      //else : nothing has changed no need to regenerate
166    }
167    //!@}
168};
169
170} //namespace
171#endif // FN_H
Note: See TracBrowser for help on using the browser.