nnfw/include/algebra.h
Go to the documentation of this file.
00001 /******************************************************************************** 00002 * Neural Network Framework. * 00003 * Copyright (C) 2005-2011 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 #ifndef ALGEBRA_H 00021 #define ALGEBRA_H 00022 00023 #ifndef PI_GRECO 00024 #define PI_GRECO 3.14159265358979323846f 00025 #endif 00026 #include "vectors.h" 00027 #include "matrices.h" 00028 #include <cmath> 00029 00038 namespace farsa { 00039 00044 inline FARSA_NNFW_TEMPLATE DoubleVector& exp( DoubleVector& data ) { 00045 for( unsigned int i=0; i<data.size(); i++ ) { 00046 data[i] = std::exp(data[i]); 00047 } 00048 return data; 00049 }; 00053 inline FARSA_NNFW_TEMPLATE unsigned int maxIndex( const DoubleVector& src ) { 00054 unsigned int mi = 0; 00055 for( unsigned int i=0; i<src.size(); i++ ) { 00056 if ( src[mi] < src[i] ) { 00057 mi = i; 00058 } 00059 } 00060 return mi; 00061 }; 00065 inline FARSA_NNFW_TEMPLATE DoubleVector& inv( DoubleVector& data ) { 00066 for( unsigned int i=0; i<data.size(); i++ ) { 00067 data[i] = 1.0 / data[i]; 00068 } 00069 return data; 00070 }; 00074 inline FARSA_NNFW_TEMPLATE double sum( const DoubleVector& data ) { 00075 double s = 0.0; 00076 for( unsigned int i=0; i<data.size(); i++ ) { 00077 s += data[i]; 00078 } 00079 return s; 00080 }; 00084 inline FARSA_NNFW_TEMPLATE DoubleVector& square( DoubleVector& data ) { 00085 for( unsigned int i=0; i<data.size(); i++ ) { 00086 data[i] *= data[i]; 00087 } 00088 return data; 00089 }; 00093 inline FARSA_NNFW_TEMPLATE double mean( const DoubleVector& src ) { 00094 return sum( src ) / ( src.size() ); 00095 }; 00099 inline FARSA_NNFW_TEMPLATE double mse( const DoubleVector& target, const DoubleVector& actual ) { 00100 //--- will throw IncompatibleVectors 00101 DoubleVector min = target - actual; 00102 return mean( square( min ) ); 00103 }; 00107 inline FARSA_NNFW_TEMPLATE const DoubleVector operator-( const double& a, const DoubleVector& x ) { 00108 // it create a temporary DoubleVector 00109 DoubleVector y(x.size(), true, 0); 00110 for( unsigned int i=0; i<y.size(); i++ ) { 00111 y[i] = a - x[i]; 00112 } 00113 return y; 00114 }; 00118 inline FARSA_NNFW_TEMPLATE const DoubleVector operator-( const DoubleVector& x, const double& a ) { 00119 // it create a temporary DoubleVector 00120 DoubleVector y(x.size(), true, 0); 00121 for( unsigned int i=0; i<y.size(); i++ ) { 00122 y[i] = x[i] - a; 00123 } 00124 return y; 00125 }; 00129 inline FARSA_NNFW_TEMPLATE const DoubleVector operator+( const double& a, const DoubleVector& x ) { 00130 // it create a temporary DoubleVector 00131 DoubleVector y(x.size(), true, 0); 00132 for( unsigned int i=0; i<y.size(); i++ ) { 00133 y[i] = a + x[i]; 00134 } 00135 return y; 00136 }; 00140 inline FARSA_NNFW_TEMPLATE const DoubleVector operator+( const DoubleVector& x, const double& a ) { 00141 // it create a temporary DoubleVector 00142 DoubleVector y(x.size(), true, 0); 00143 for( unsigned int i=0; i<y.size(); i++ ) { 00144 y[i] = x[i] + a; 00145 } 00146 return y; 00147 }; 00151 inline FARSA_NNFW_TEMPLATE const DoubleVector operator*( const double& a, const DoubleVector& x ) { 00152 // it create a temporary DoubleVector 00153 DoubleVector y(x.size(), true, 0); 00154 for( unsigned int i=0; i<y.size(); i++ ) { 00155 y[i] = a * x[i]; 00156 } 00157 return y; 00158 }; 00162 inline FARSA_NNFW_TEMPLATE const DoubleVector operator*( const DoubleVector& x, const double& a ) { 00163 // it create a temporary DoubleVector 00164 DoubleVector y(x.size(), true, 0); 00165 for( unsigned int i=0; i<y.size(); i++ ) { 00166 y[i] = x[i] * a; 00167 } 00168 return y; 00169 }; 00173 inline FARSA_NNFW_TEMPLATE const DoubleVector operator/( const double& a, const DoubleVector& x ) { 00174 // it create a temporary DoubleVector 00175 DoubleVector y(x.size(), true, 0); 00176 for( unsigned int i=0; i<y.size(); i++ ) { 00177 y[i] = a / x[i]; 00178 } 00179 return y; 00180 }; 00184 inline FARSA_NNFW_TEMPLATE const DoubleVector operator/( const DoubleVector& x, const double& a ) { 00185 // it create a temporary DoubleVector 00186 DoubleVector y(x.size(), true, 0); 00187 for( unsigned int i=0; i<y.size(); i++ ) { 00188 y[i] = x[i] / a; 00189 } 00190 return y; 00191 }; 00195 inline FARSA_NNFW_TEMPLATE DoubleVector& subtract( DoubleVector& r, const DoubleVector& x, const DoubleVector& y ) { 00196 #ifdef FARSA_DEBUG 00197 if ( r.size() != y.size() || y.size() != x.size() ) { 00198 throw IncompatibleVectors("Incompatibles DoubleVectors in subtract operation (dimensions must be equals"); 00199 } 00200 #endif 00201 for( unsigned int i=0; i<r.size(); i++ ) { 00202 r[i] = x[i]-y[i]; 00203 } 00204 return r; 00205 }; 00209 inline FARSA_NNFW_TEMPLATE DoubleVector& subtract( DoubleVector& r, double a, const DoubleVector& x ) { 00210 #ifdef FARSA_DEBUG 00211 if ( r.size() != x.size() ) { 00212 throw IncompatibleVectors("Incompatibles DoubleVectors in subtract operation (dimensions must be equals"); 00213 } 00214 #endif 00215 for( unsigned int i=0; i<r.size(); i++ ) { 00216 r[i] = a-x[i]; 00217 } 00218 return r; 00219 }; 00223 inline FARSA_NNFW_TEMPLATE DoubleVector& mul( DoubleVector& r, double a, const DoubleVector& x ) { 00224 #ifdef FARSA_DEBUG 00225 if ( r.size() != x.size() ) { 00226 throw IncompatibleVectors("Incompatibles DoubleVectors in multiplication operation (dimensions must be equals"); 00227 } 00228 #endif 00229 for( unsigned int i=0; i<r.size(); i++ ) { 00230 r[i] = a*x[i]; 00231 } 00232 return r; 00233 } 00234 00244 inline FARSA_NNFW_TEMPLATE DoubleVector& deltarule( DoubleVector& r, double rate, const DoubleVector& x, const DoubleVector& y ) { 00245 #ifdef FARSA_DEBUG 00246 if ( r.size() != y.size() || y.size() != x.size() ) { 00247 throw IncompatibleVectors("Incompatibles DoubleVectors in deltarule operation (dimensions must be equals"); 00248 } 00249 #endif 00250 for( unsigned int i=0; i<r.size(); i++ ) { 00251 r[i] += rate*x[i]*y[i]; 00252 } 00253 return r; 00254 }; 00255 00263 inline FARSA_NNFW_TEMPLATE DoubleVector& mul( DoubleVector& y, const DoubleVector& x, const DoubleMatrix& m ) { 00264 #ifdef FARSA_DEBUG 00265 if ( y.size() != m.cols() || x.size() != m.rows() ) { 00266 throw IncompatibleMatrices("Incompatible DoubleVector and DoubleMatrix in mul operation (the number of rows must be equal to the size of x vector and the size of result vector must be equal to the number of columns of matrix"); 00267 } 00268 #endif 00269 for ( unsigned int j = 0; j<m.rows(); j++ ) { 00270 for ( unsigned int i = 0; i<m.cols(); i++ ) { 00271 y[i] = x[j] * m[j][i]; 00272 } 00273 } 00274 return y; 00275 }; 00283 inline FARSA_NNFW_TEMPLATE DoubleVector& amul( DoubleVector& y, const DoubleVector& x, const DoubleMatrix& m ) { 00284 #ifdef FARSA_DEBUG 00285 if ( y.size() != m.cols() || x.size() != m.rows() ) { 00286 throw IncompatibleMatrices("Incompatible DoubleVector and DoubleMatrix in mul operation (the number of rows must be equal to the size of x vector and the size of result vector must be equal to the number of columns of matrix"); 00287 } 00288 #endif 00289 for ( unsigned int j = 0; j<m.rows(); j++ ) { 00290 for ( unsigned int i = 0; i<m.cols(); i++ ) { 00291 y[i] += x[j] * m[j][i]; 00292 } 00293 } 00294 return y; 00295 }; 00296 00304 inline FARSA_NNFW_TEMPLATE DoubleVector& mul( DoubleVector& y, const DoubleMatrix& m, const DoubleVector& x ) { 00305 #ifdef FARSA_DEBUG 00306 if ( y.size() != m.rows() || x.size() != m.cols() ) { 00307 throw IncompatibleMatrices("Incompatible DoubleVector and DoubleMatrix in mul operation (the number of columns must be equal to the size of x vector and the size of result vector must be equal to the number of rows of matrix"); 00308 } 00309 #endif 00310 for ( unsigned int j = 0; j<m.rows(); j++ ) { 00311 for ( unsigned int i = 0; i<m.cols(); i++ ) { 00312 y[j] = m[j][i] * x[i]; 00313 } 00314 } 00315 return y; 00316 }; 00324 inline FARSA_NNFW_TEMPLATE DoubleVector& amul( DoubleVector& y, const DoubleMatrix& m, const DoubleVector& x ) { 00325 #ifdef FARSA_DEBUG 00326 if ( y.size() != m.rows() || x.size() != m.cols() ) { 00327 throw IncompatibleMatrices("Incompatible DoubleVector and DoubleMatrix in mul operation (the number of columns must be equal to the size of x vector and the size of result vector must be equal to the number of rows of matrix"); 00328 } 00329 #endif 00330 for ( unsigned int j = 0; j<m.rows(); j++ ) { 00331 for ( unsigned int i = 0; i<m.cols(); i++ ) { 00332 y[j] += m[j][i] * x[i]; 00333 } 00334 } 00335 return y; 00336 }; 00345 inline FARSA_NNFW_TEMPLATE DoubleMatrix& deltarule( DoubleMatrix& m, double rate, const DoubleVector& x, const DoubleVector& y ) { 00346 #ifdef FARSA_DEBUG 00347 if ( x.size() != m.rows() || y.size() != m.cols() ) { 00348 throw IncompatibleVectors("Incompatible DoubleVectors and DoubleMatrix in deltarule operation (the number of rows must be equal to the size of x vector and the number of columns must be equal to the size of y vector"); 00349 } 00350 #endif 00351 for ( unsigned int r=0; r<m.rows(); r++ ) { 00352 for ( unsigned int c=0; c<m.cols(); c++ ) { 00353 m[r][c] += rate * x[r] * y[c]; 00354 } 00355 } 00356 return m; 00357 } 00358 00359 } 00360 00361 #endif