include/realvec.h

Go to the documentation of this file.
00001 /********************************************************************************
00002  *  Neural Network Framework.                                                   *
00003  *  Copyright (C) 2005-2008 Gianluca Massera <emmegian@yahoo.it>                *
00004  *                                                                              *
00005  *  This program is free software; you can redistribute it and/or modify        *
00006  *  it under the terms of the GNU General Public License as published by        *
00007  *  the Free Software Foundation; either version 2 of the License, or           *
00008  *  (at your option) any later version.                                         *
00009  *                                                                              *
00010  *  This program is distributed in the hope that it will be useful,             *
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00013  *  GNU General Public License for more details.                                *
00014  *                                                                              *
00015  *  You should have received a copy of the GNU General Public License           *
00016  *  along with this program; if not, write to the Free Software                 *
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00018  ********************************************************************************/
00019 
00020 // --- You can't include it directly
00021 #ifndef TYPES_INCLUDES
00022 #error "You can't include realvec.h directly; Instead, You have to include types.h"
00023 // --- follow define avoid to get a lot of understandable error !
00024 #define REALVEC_H
00025 #endif
00026 
00027 #ifndef REALVEC_H
00028 #define REALVEC_H
00029 
00034 namespace nnfw {
00035 
00041 class NNFW_API RealVec : public VectorData<Real> {
00042 public:
00045 
00048     RealVec( u_int size );
00049     
00052     RealVec( u_int size, Real value );
00053 
00056     RealVec();
00057 
00060     RealVec( RealVec& src, u_int idStart, u_int idEnd );
00061 
00064     RealVec( const Real* r, u_int dim );
00065 
00068     RealVec( const RealVec& orig );
00069 
00072     RealVec& operator=( const RealVec& src ) {
00073         RealVec& self = *this;
00074         self.resize( src.size() );
00075         self.assign( src );
00076         return self;
00077     };
00078 
00080 
00082 
00085     RealVec& operator<<( const Real& value ) {
00086         append( value );
00087         return (*this);
00088     };
00089 
00091 
00093 
00095     RealVec& operator-() {
00096         for( u_int i=0; i<vsize; i++ ) {
00097             data[i] = -data[i];
00098         }
00099         return (*this);
00100     };
00101 
00103 
00105 
00107     RealVec& operator+=(const RealVec& r ) {
00108 #ifdef NNFW_DEBUG
00109         if( vsize != r.vsize ) {
00110             nError() << "Different numbers of element" ;
00111             return (*this);
00112         }
00113 #endif
00114         for( u_int i=0; i<vsize; i++ ) {
00115             data[i] += r.data[i];
00116         }
00117         return (*this);
00118     };
00120     RealVec& operator+=(const Real& r ) {
00121         for( u_int i=0; i<vsize; i++ ) {
00122             data[i] += r;
00123         }
00124         return (*this);
00125     };
00127     RealVec& operator-=(const RealVec& r ) {
00128 #ifdef NNFW_DEBUG
00129         if( vsize != r.vsize ) {
00130             nError() << "Different numbers of element" ;
00131             return *this;
00132         }
00133 #endif
00134         for( u_int i=0; i<vsize; i++ ) {
00135             data[i] -= r.data[i];
00136         }
00137         return (*this);
00138     };
00140     RealVec& operator-=(const Real& r ) {
00141         for( u_int i=0; i<vsize; i++ ) {
00142             data[i] -= r;
00143         }
00144         return (*this);
00145     };
00147     RealVec& operator*=(const RealVec& r ) {
00148 #ifdef NNFW_DEBUG
00149         if( vsize != r.vsize ) {
00150             nError() << "Different numbers of element" ;
00151             return (*this);
00152         }
00153 #endif
00154         for( u_int i=0; i<vsize; i++ ) {
00155             data[i] *= r.data[i];
00156         }
00157         return (*this);
00158     };
00160     RealVec& operator*=(const Real& r ) {
00161         for( u_int i=0; i<vsize; i++ ) {
00162             data[i] *= r;
00163         }
00164         return (*this);
00165     };
00167     RealVec& operator/=(const RealVec& r ) {
00168 #ifdef NNFW_DEBUG
00169         if( vsize != r.vsize ) {
00170             nError() << "Different numbers of element" ;
00171             return (*this);
00172         }
00173 #endif
00174         for( u_int i=0; i<vsize; i++ ) {
00175             data[i] /= r.data[i];
00176         }
00177         return (*this);
00178     };
00180     RealVec& operator/=(const Real& r ) {
00181         for( u_int i=0; i<vsize; i++ ) {
00182             data[i] /= r;
00183         }
00184         return (*this);
00185     };
00186 
00188     Real dot( const RealVec& r ) {
00189 #ifdef NNFW_DEBUG
00190         if( vsize != r.vsize ) {
00191             nError() << "Different numbers of element" ;
00192             return 0.0;
00193         }
00194 #endif
00195         Real ret = 0.0;
00196         for( u_int i=0; i<vsize; i++ ) {
00197             ret += data[i] * r.data[i];
00198         }
00199         return ret;
00200     };
00201 
00203 
00205 
00208     RealVec& exp();
00209 
00212     RealVec& scale( const Real v ) {
00213         for( u_int i=0; i<vsize; i++ ) {
00214             data[i] *= v;
00215         }
00216         return (*this);
00217     };
00218 
00221     RealVec& inv();
00222 
00225     Real norm();
00226 
00229     RealVec& square() {
00230         for( u_int i=0; i<vsize; i++ ) {
00231             data[i] *= data[i];
00232         }
00233         return (*this);
00234     };
00235     
00238     Real sum() {
00239         Real s = 0.0;
00240         for( u_int i=0; i<vsize; i++ ) {
00241             s += data[i];
00242         }
00243         return s;
00244     };
00245 
00248     RealVec& normalize() {
00249         Real n = norm();
00250         if ( n==0.0 ) return (*this);
00251         for( u_int i=0; i<vsize; i++ ) {
00252             data[i] /= n;
00253         }
00254         return (*this);
00255     };
00256 
00260     RealVec& neg() {
00261         for( u_int i=0; i<vsize; i++ ) {
00262             data[i] = !data[i];
00263         }
00264         return (*this);
00265     };
00266 
00269     void assign_xminusy( const RealVec& x, const RealVec& y ) {
00270         for( u_int i=0; i<vsize; i++ ) {
00271             data[i] = x[i]-y[i];
00272         }
00273     };
00274 
00277     void assign_minusx( const RealVec& x ) {
00278         for( u_int i=0; i<vsize; i++ ) {
00279             data[i] = -x[i];
00280         }
00281     };
00282 
00285     void assign_aminusx( Real a, const RealVec& x ) {
00286         for( u_int i=0; i<vsize; i++ ) {
00287             data[i] = a-x[i];
00288         }
00289     };
00290 
00293     void assign_amulx( const Real a, const RealVec& x ) {
00294         for( u_int i=0; i<vsize; i++ ) {
00295             data[i] = a*x[i];
00296         }
00297     };
00298 
00301     void assign_adivx( const Real a, const RealVec& x ) {
00302         for( u_int i=0; i<vsize; i++ ) {
00303             data[i] = a/x[i];
00304         }
00305     };
00306         
00309     Real mean() {
00310         return this->sum() / vsize;
00311     };
00312 
00314     int maxIndex() {
00315         int mi = 0;
00316         for( u_int i=1; i<vsize; i++ ) {
00317             if ( data[i] > data[mi] ) mi=i;
00318         }
00319         return mi;
00320     }
00321 
00323     Real maxValue() {
00324         return data[maxIndex()];
00325     }
00326 
00328     int minIndex() {
00329         int mi = 0;
00330         for( u_int i=1; i<vsize; i++ ) {
00331             if ( data[i] < data[mi] ) mi=i;
00332         }
00333         return mi;
00334     }
00335 
00337     Real minValue() {
00338         return data[minIndex()];
00339     }
00340 
00343     RealVec& step( Real threshold ) {
00344         for ( u_int i = 0; i<vsize; i++ ) {
00345             ( data[i] > threshold ) ? data[i] = 1.0f : data[i] = 0.0f;
00346         }
00347         return (*this);
00348     }
00349 
00352     static void createAllBinaries( RealVec* vector, unsigned long int pats, u_int dims );
00353 
00356     static Real mse( const RealVec& target, const RealVec& actual );
00357 
00364     RealVec& deltarule( Real rate, const RealVec& x, const RealVec& y ) {
00365         for( u_int i=0; i<vsize; i++ ) {
00366             data[i] += rate*x[i]*y[i];
00367         }
00368         return (*this);
00369     };
00370 
00372 
00374 
00383     static RealMat& outprod( RealMat& m, const RealVec& x, const RealVec& y );
00384 
00386     
00387 
00388 protected:
00389 
00390     friend class RealMat;
00392     Real* rawdata() const {
00393         return VectorData<Real>::rawdata();
00394     };
00395 
00396     //--- for accessing from C interface implementation
00397     friend Real* getRawData( RealVec& );
00398 
00399 };
00400 
00401 }
00402 
00403 #endif
00404 
BerliOS Developer Logo Valid XHTML 1.0 Transitional Valid CSS!