- Main Page
- Related Pages
- Classes
- Files

- File List
- File Members

# functions.h

    00001 //
    00002 // C++ Interface: itpp_ext
    00003 //
    00004 // Description:
    00005 //
    00006 //
    00007 // Author: smidl <smidl@utia.cas.cz>, (C) 2008
    00008 //
    00009 // Copyright: See COPYING file that comes with this distribution
    00010 //
    00011 //
    00012 #ifndef FN_H
    00013 #define FN_H
    00014
    00015 #include "../bdmerror.h"
    00016 #include "../base/bdmbase.h"
    00017
    00018 namespace bdm {
    00019
    00021 class constfn : public fnc {
    00023         vec val;
    00024
    00025 public:
    00026         //vec eval() {return val;};
    00028         vec eval ( const vec &cond ) {
    00029                 return val;
    00030         };
    00032         constfn ( const vec &val0 ) : fnc(), val ( val0 ) {
    00033                 dimy = val.length();
    00034         };
    00035 };
    00036
    00038 class linfn: public fnc {
    00040         RV rv;
    00042         mat A;
    00044         vec B;
    00045 public :
    00046         vec eval ( const vec &cond ) {
    00047                 bdm_assert_debug ( cond.length() == A.cols(), "linfn::eval Wrong cond." );
    00048                 return A * cond + B;
    00049         }
    00050
    00051 //              linfn evalsome ( ivec &rvind );
    00053         linfn ( ) : fnc(), A ( ), B () { };
    00055         void set_parameters ( const mat &A0 , const vec &B0 ) {
    00056                 A = A0;
    00057                 B = B0;
    00058                 dimy = A.rows();
    00059         };
    00060 };
    00061
    00062
    00072 class diffbifn: public fnc {
    00073 protected:
    00075         RV rvx;
    00077         RV rvu;
    00079         int dimx;
    00081         int dimu;
    00082 public:
    00084         vec eval ( const vec &cond ) {
    00085                 bdm_assert_debug ( cond.length() == ( dimx + dimu ), "linfn::eval Wrong cond." );
    00086                 return eval ( cond ( 0, dimx - 1 ), cond ( dimx, dimx + dimu - 1 ) );//-1 = end (in matlab)
    00087         }
    00088
    00090         virtual vec eval ( const vec &x0, const vec &u0 ) {
    00091                 return zeros ( dimy );
    00092         };
    00094         virtual void dfdx_cond ( const vec &x0, const vec &u0, mat &A , bool full = true ) {};
    00096         virtual void dfdu_cond ( const vec &x0, const vec &u0, mat &A, bool full = true ) {};
    00098         diffbifn () : fnc() {};
    00100         int _dimx() const {
    00101                 return dimx;
    00102         }
    00104         int _dimu() const {
    00105                 return dimu;
    00106         }
    00107 };
    00108
    00110 //TODO can be generalized into multilinear form!
    00111 class bilinfn: public diffbifn {
    00112         mat A;
    00113         mat B;
    00114 public :
    00117
    00118         bilinfn () : diffbifn (), A(), B() { }
    00119
    00120         bilinfn ( const mat &A0, const mat &B0 ) {
    00121                 set_parameters ( A0, B0 );
    00122         }
    00123
    00125         void set_parameters ( const mat &A0, const mat &B0 ) {
    00126                 bdm_assert_debug ( A0.rows() == B0.rows(), "bilinfn matrices must have the same number of rows" );
    00127                 A = A0;
    00128                 B = B0;
    00129                 dimy = A.rows();
    00130                 dimx = A.cols();
    00131                 dimu = B.cols();
    00132         }
    00134
    00137         inline vec eval ( const  vec &x0, const vec &u0 ) {
    00138                 bdm_assert_debug ( x0.length() == dimx, "bilinfn::eval Wrong xcond." );
    00139                 bdm_assert_debug ( u0.length() == dimu, "bilinfn::eval Wrong ucond." );
    00140                 return A*x0 + B*u0;
    00141         }
    00142
    00143         void dfdx_cond ( const vec &x0, const vec &u0, mat &F, bool full ) {
    00144                 bdm_assert_debug ( ( F.cols() == A.cols() ) && ( F.rows() == A.rows() ), "Allocated F is not compatible." );
    00145                 if ( full ) F = A;      //else : nothing has changed no need to regenerate
    00146         }
    00147
    00148         void dfdu_cond ( const vec &x0, const vec &u0, mat &F,  bool full = true ) {
    00149                 bdm_assert_debug ( ( F.cols() == B.cols() ) && ( F.rows() == B.rows() ), "Allocated F is not compatible." );
    00150                 if ( full ) F = B;      //else : nothing has changed no need to regenerate
    00151         }
    00153 };
    00154
    00155 } //namespace
    00156 #endif // FN_H