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

Revision 620, 4.4 kB (checked in by smidl, 15 years ago)

replace assert_debug by assert in classes interacting with users (from_setting, set_parameters, validate)

  • 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                return eval ( cond ( 0, dimx - 1 ), cond ( dimx, dimx + dimu - 1 ) );//-1 = end (in matlab)
87        }
88
89        //! Evaluates \f$f(x0,u0)\f$
90        virtual vec eval ( const vec &x0, const vec &u0 ) {
91                return zeros ( dimy );
92        };
93        //! 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.
94        virtual void dfdx_cond ( const vec &x0, const vec &u0, mat &A , bool full = true ) {};
95        //! 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.
96        virtual void dfdu_cond ( const vec &x0, const vec &u0, mat &A, bool full = true ) {};
97        //!Default constructor (dimy is not set!)
98        diffbifn () : fnc() {};
99        //! access function
100        int _dimx() const {
101                return dimx;
102        }
103        //! access function
104        int _dimu() const {
105                return dimu;
106        }
107};
108
109//! Class representing function \f$f(x,u) = Ax+Bu\f$
110//TODO can be generalized into multilinear form!
111class bilinfn: public diffbifn {
112        mat A;
113        mat B;
114public :
115        //!\name Constructors
116        //!@{
117
118        bilinfn () : diffbifn (), A(), B() { }
119
120        bilinfn ( const mat &A0, const mat &B0 ) {
121                set_parameters ( A0, B0 );
122        }
123
124        //! Alternative initialization
125        void set_parameters ( const mat &A0, const mat &B0 ) {
126                bdm_assert ( A0.rows() == B0.rows(), "bilinfn matrices must have the same number of rows" );
127                A = A0;
128                B = B0;
129                dimy = A.rows();
130                dimx = A.cols();
131                dimu = B.cols();
132        }
133        //!@}
134
135        //!\name Mathematical operations
136        //!@{
137        inline vec eval ( const  vec &x0, const vec &u0 ) {
138                bdm_assert_debug ( x0.length() == dimx, "bilinfn::eval Wrong xcond." );
139                bdm_assert_debug ( u0.length() == dimu, "bilinfn::eval Wrong ucond." );
140                return A*x0 + B*u0;
141        }
142
143        void dfdx_cond ( const vec &x0, const vec &u0, mat &F, bool full ) {
144                bdm_assert_debug ( ( F.cols() == A.cols() ) && ( F.rows() == A.rows() ), "Allocated F is not compatible." );
145                if ( full ) F = A;      //else : nothing has changed no need to regenerate
146        }
147
148        void dfdu_cond ( const vec &x0, const vec &u0, mat &F,  bool full = true ) {
149                bdm_assert_debug ( ( F.cols() == B.cols() ) && ( F.rows() == B.rows() ), "Allocated F is not compatible." );
150                if ( full ) F = B;      //else : nothing has changed no need to regenerate
151        }
152        //!@}
153};
154
155} //namespace
156#endif // FN_H
Note: See TracBrowser for help on using the browser.