include/realmat.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 realmat.h directly; Instead, You have to include types.h"
00023 // --- follow define avoid to get a lot of understandable error !
00024 #define REALMAT_H
00025 #endif
00026 
00027 #ifndef REALMAT_H
00028 #define REALMAT_H
00029 
00034 namespace nnfw {
00035 
00041 class NNFW_API RealMat : public MatrixData<Real, RealVec> {
00042 public:
00045 
00048     RealMat( u_int rows, u_int cols );
00049 
00054     RealMat( RealVec& src, u_int rstart, u_int rend, u_int rows, u_int cols );
00055 
00058     ~RealMat();
00059 
00061 
00063 
00065     RealMat& operator-() {
00066         -rawdata();
00067         return (*this);
00068     };
00069 
00071     RealMat& transpose() {
00072         Real tmp;
00073         RealMat& self = *this;
00074         for( u_int i=0; i<rows(); i++ ) {
00075             for( u_int j=i+1; j<cols(); j++ ) {
00076                 tmp = self[i][j];
00077                 self[i][j] = self[j][i];
00078                 self[j][i] = tmp;
00079             }
00080         }
00081         return (*this);
00082     };
00083 
00085     
00088 
00090     RealMat& operator+=(const RealMat& r ) {
00091 #ifdef NNFW_DEBUG
00092         if( rows() != r.rows() || cols() != r.cols() ) {
00093             nError() << "Different dimension";
00094             return (*this);
00095         }
00096 #endif
00097         rawdata() += r.rawdata();
00098         return (*this);
00099     };
00100     
00102     RealMat& operator+=(const Real& r ) {
00103         rawdata() += r;
00104         return (*this);
00105     };
00106     
00108     RealMat& operator-=(const RealMat& r ) {
00109 #ifdef NNFW_DEBUG
00110         if( rows() != r.rows() || cols() != r.cols() ) {
00111             nError() << "Different dimension";
00112             return (*this);
00113         }
00114 #endif
00115         rawdata() -= r.rawdata();
00116         return (*this);
00117     };
00118     
00120     RealMat& operator-=(const Real& r ) {
00121         rawdata() -= r;
00122         return (*this);
00123     };
00124     
00126     RealMat& operator*=(const RealMat& r ) {
00127 #ifdef NNFW_DEBUG
00128         if( rows() != r.rows() || cols() != r.cols() ) {
00129             nError() << "Different dimension";
00130             return (*this);
00131         }
00132 #endif
00133         rawdata() *= r.rawdata();
00134         return (*this);
00135     };
00136     
00138     RealMat& operator*=(const Real& r ) {
00139         rawdata() *= r;
00140         return (*this);
00141     };
00142     
00144     RealMat& operator/=(const RealMat& r ) {
00145 #ifdef NNFW_DEBUG
00146         if( rows() != r.rows() || cols() != r.cols() ) {
00147             nError() << "Different dimension";
00148             return (*this);
00149         }
00150 #endif
00151         rawdata() /= r.rawdata();
00152         return (*this);
00153     };
00154     
00156     RealMat& operator/=(const Real& r ) {
00157         rawdata() /= r;
00158         return (*this);
00159     };
00160 
00162     
00165 
00172     static RealVec& mul( RealVec& y, const RealVec& x, const RealMat& m );
00173 
00180     static RealVec& mul( RealVec& y, const RealMat& m, const RealVec& x );
00181 
00188     RealMat& deltarule( Real rate, const RealVec& x, const RealVec& y );
00189 
00191 
00193 
00195     RealMat& cover( const MatrixData<bool>& mask ) {
00196         RealMat& self = *this;
00197         for( u_int r=0; r<rows(); r++ ) {
00198             for( u_int c=0; c<cols(); c++ ) {
00199                 if ( !mask[r][c] ) {
00200                     self[r][c] = 0.0;
00201                 }
00202             }
00203         }
00204         return (*this);
00205     };
00206 
00208 
00210 
00212     RealMat& exp();
00213 
00215     RealMat& scale( const Real v ) {
00216         for( u_int i=0; i<size(); i++ ) {
00217             rawdata()[i] *= v;
00218         }
00219         return (*this);
00220     };
00221 
00223     RealMat& inv();
00224 
00226     Real norm() {
00227         return rawdata().norm();
00228     };
00230     void normalize() {
00231         rawdata().normalize();
00232     };
00233 
00235 
00236     //--- for accessing from C interface implementation
00237     friend Real* getRawData( RealMat& );
00238 
00239 };
00240 
00241 }
00242 
00243 #endif
00244 
BerliOS Developer Logo Valid XHTML 1.0 Transitional Valid CSS!