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
RevLine 
[11]1//
2// C++ Interface: itpp_ext
3//
[19]4// Description:
[11]5//
6//
7// Author: smidl <smidl@utia.cas.cz>, (C) 2008
8//
9// Copyright: See COPYING file that comes with this distribution
10//
11//
[33]12#ifndef FN_H
13#define FN_H
14
[565]15#include "../bdmerror.h"
[384]16#include "../base/bdmbase.h"
[11]17
[477]18namespace bdm {
[11]19
[85]20//! class representing function \f$f(x) = a\f$, here \c rv is empty
[477]21class constfn : public fnc {
[1064]22    //! value of the function
23    vec val;
[19]24
[477]25public:
[1064]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    };
[19]36};
37
[85]38//! Class representing function \f$f(x) = Ax+B\f$
[477]39class linfn: public fnc {
[1064]40    //! Identification of \f$x\f$
41    RV rv;
42    //! Matrix A
43    mat A;
44    //! vector B
45    vec B;
[477]46public :
[1064]47    vec eval ( const vec &cond ) {
48        bdm_assert_debug ( cond.length() == A.cols(), "linfn::eval Wrong cond." );
49        return A * cond + B;
50    }
[19]51
[33]52//              linfn evalsome ( ivec &rvind );
[1064]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
[19]69};
[800]70UIREGISTER(linfn);
[19]71
[22]72
73/*!
[85]74\brief Class representing a differentiable function of two variables \f$f(x,u)\f$.
[22]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*/
[477]82class diffbifn: public fnc {
83protected:
[1064]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;
[477]92public:
[1064]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        }
[737]101
[1064]102    }
[22]103
[1064]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    }
[22]122};
123
[85]124//! Class representing function \f$f(x,u) = Ax+Bu\f$
[22]125//TODO can be generalized into multilinear form!
[477]126class bilinfn: public diffbifn {
[1064]127    mat A;
128    mat B;
[477]129public :
[1064]130    //!\name Constructors
131    //!@{
[292]132
[1064]133    bilinfn () : diffbifn (), A(), B() { }
[565]134
[1064]135    bilinfn ( const mat &A0, const mat &B0 ) {
136        set_parameters ( A0, B0 );
137    }
[565]138
[1064]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    //!@}
[477]149
[1064]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    }
[477]157
[1064]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    }
[565]162
[1064]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    //!@}
[19]168};
[33]169
[254]170} //namespace
[33]171#endif // FN_H
Note: See TracBrowser for help on using the browser.