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

Revision 665, 4.5 kB (checked in by smidl, 15 years ago)

Compilation and minor extensions

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