algebra.h
Go to the documentation of this file.
1 /********************************************************************************
2  * Neural Network Framework. *
3  * Copyright (C) 2005-2011 Gianluca Massera <emmegian@yahoo.it> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the Free Software *
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
18  ********************************************************************************/
19 
20 #ifndef ALGEBRA_H
21 #define ALGEBRA_H
22 
23 #ifndef PI_GRECO
24  #define PI_GRECO 3.14159265358979323846f
25 #endif
26 #include "vectors.h"
27 #include "matrices.h"
28 #include <cmath>
29 
38 namespace farsa {
39 
44 inline FARSA_NNFW_TEMPLATE DoubleVector& exp( DoubleVector& data ) {
45  for( unsigned int i=0; i<data.size(); i++ ) {
46  data[i] = std::exp(data[i]);
47  }
48  return data;
49 };
53 inline FARSA_NNFW_TEMPLATE unsigned int maxIndex( const DoubleVector& src ) {
54  unsigned int mi = 0;
55  for( unsigned int i=0; i<src.size(); i++ ) {
56  if ( src[mi] < src[i] ) {
57  mi = i;
58  }
59  }
60  return mi;
61 };
65 inline FARSA_NNFW_TEMPLATE DoubleVector& inv( DoubleVector& data ) {
66  for( unsigned int i=0; i<data.size(); i++ ) {
67  data[i] = 1.0 / data[i];
68  }
69  return data;
70 };
74 inline FARSA_NNFW_TEMPLATE double sum( const DoubleVector& data ) {
75  double s = 0.0;
76  for( unsigned int i=0; i<data.size(); i++ ) {
77  s += data[i];
78  }
79  return s;
80 };
84 inline FARSA_NNFW_TEMPLATE DoubleVector& square( DoubleVector& data ) {
85  for( unsigned int i=0; i<data.size(); i++ ) {
86  data[i] *= data[i];
87  }
88  return data;
89 };
93 inline FARSA_NNFW_TEMPLATE double mean( const DoubleVector& src ) {
94  return sum( src ) / ( src.size() );
95 };
99 inline FARSA_NNFW_TEMPLATE double mse( const DoubleVector& target, const DoubleVector& actual ) {
100  //--- will throw IncompatibleVectors
101  DoubleVector min = target - actual;
102  return mean( square( min ) );
103 };
107 inline FARSA_NNFW_TEMPLATE const DoubleVector operator-( const double& a, const DoubleVector& x ) {
108  // it create a temporary DoubleVector
109  DoubleVector y(x.size(), true, 0);
110  for( unsigned int i=0; i<y.size(); i++ ) {
111  y[i] = a - x[i];
112  }
113  return y;
114 };
118 inline FARSA_NNFW_TEMPLATE const DoubleVector operator-( const DoubleVector& x, const double& a ) {
119  // it create a temporary DoubleVector
120  DoubleVector y(x.size(), true, 0);
121  for( unsigned int i=0; i<y.size(); i++ ) {
122  y[i] = x[i] - a;
123  }
124  return y;
125 };
129 inline FARSA_NNFW_TEMPLATE const DoubleVector operator+( const double& a, const DoubleVector& x ) {
130  // it create a temporary DoubleVector
131  DoubleVector y(x.size(), true, 0);
132  for( unsigned int i=0; i<y.size(); i++ ) {
133  y[i] = a + x[i];
134  }
135  return y;
136 };
140 inline FARSA_NNFW_TEMPLATE const DoubleVector operator+( const DoubleVector& x, const double& a ) {
141  // it create a temporary DoubleVector
142  DoubleVector y(x.size(), true, 0);
143  for( unsigned int i=0; i<y.size(); i++ ) {
144  y[i] = x[i] + a;
145  }
146  return y;
147 };
151 inline FARSA_NNFW_TEMPLATE const DoubleVector operator*( const double& a, const DoubleVector& x ) {
152  // it create a temporary DoubleVector
153  DoubleVector y(x.size(), true, 0);
154  for( unsigned int i=0; i<y.size(); i++ ) {
155  y[i] = a * x[i];
156  }
157  return y;
158 };
162 inline FARSA_NNFW_TEMPLATE const DoubleVector operator*( const DoubleVector& x, const double& a ) {
163  // it create a temporary DoubleVector
164  DoubleVector y(x.size(), true, 0);
165  for( unsigned int i=0; i<y.size(); i++ ) {
166  y[i] = x[i] * a;
167  }
168  return y;
169 };
173 inline FARSA_NNFW_TEMPLATE const DoubleVector operator/( const double& a, const DoubleVector& x ) {
174  // it create a temporary DoubleVector
175  DoubleVector y(x.size(), true, 0);
176  for( unsigned int i=0; i<y.size(); i++ ) {
177  y[i] = a / x[i];
178  }
179  return y;
180 };
184 inline FARSA_NNFW_TEMPLATE const DoubleVector operator/( const DoubleVector& x, const double& a ) {
185  // it create a temporary DoubleVector
186  DoubleVector y(x.size(), true, 0);
187  for( unsigned int i=0; i<y.size(); i++ ) {
188  y[i] = x[i] / a;
189  }
190  return y;
191 };
195 inline FARSA_NNFW_TEMPLATE DoubleVector& subtract( DoubleVector& r, const DoubleVector& x, const DoubleVector& y ) {
196 #ifdef FARSA_DEBUG
197  if ( r.size() != y.size() || y.size() != x.size() ) {
198  throw IncompatibleVectors("Incompatibles DoubleVectors in subtract operation (dimensions must be equals");
199  }
200 #endif
201  for( unsigned int i=0; i<r.size(); i++ ) {
202  r[i] = x[i]-y[i];
203  }
204  return r;
205 };
209 inline FARSA_NNFW_TEMPLATE DoubleVector& subtract( DoubleVector& r, double a, const DoubleVector& x ) {
210 #ifdef FARSA_DEBUG
211  if ( r.size() != x.size() ) {
212  throw IncompatibleVectors("Incompatibles DoubleVectors in subtract operation (dimensions must be equals");
213  }
214 #endif
215  for( unsigned int i=0; i<r.size(); i++ ) {
216  r[i] = a-x[i];
217  }
218  return r;
219 };
223 inline FARSA_NNFW_TEMPLATE DoubleVector& mul( DoubleVector& r, double a, const DoubleVector& x ) {
224 #ifdef FARSA_DEBUG
225  if ( r.size() != x.size() ) {
226  throw IncompatibleVectors("Incompatibles DoubleVectors in multiplication operation (dimensions must be equals");
227  }
228 #endif
229  for( unsigned int i=0; i<r.size(); i++ ) {
230  r[i] = a*x[i];
231  }
232  return r;
233 }
234 
244 inline FARSA_NNFW_TEMPLATE DoubleVector& deltarule( DoubleVector& r, double rate, const DoubleVector& x, const DoubleVector& y ) {
245 #ifdef FARSA_DEBUG
246  if ( r.size() != y.size() || y.size() != x.size() ) {
247  throw IncompatibleVectors("Incompatibles DoubleVectors in deltarule operation (dimensions must be equals");
248  }
249 #endif
250  for( unsigned int i=0; i<r.size(); i++ ) {
251  r[i] += rate*x[i]*y[i];
252  }
253  return r;
254 };
255 
263 inline FARSA_NNFW_TEMPLATE DoubleVector& mul( DoubleVector& y, const DoubleVector& x, const DoubleMatrix& m ) {
264 #ifdef FARSA_DEBUG
265  if ( y.size() != m.cols() || x.size() != m.rows() ) {
266  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");
267  }
268 #endif
269  for ( unsigned int j = 0; j<m.rows(); j++ ) {
270  for ( unsigned int i = 0; i<m.cols(); i++ ) {
271  y[i] = x[j] * m[j][i];
272  }
273  }
274  return y;
275 };
283 inline FARSA_NNFW_TEMPLATE DoubleVector& amul( DoubleVector& y, const DoubleVector& x, const DoubleMatrix& m ) {
284 #ifdef FARSA_DEBUG
285  if ( y.size() != m.cols() || x.size() != m.rows() ) {
286  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");
287  }
288 #endif
289  for ( unsigned int j = 0; j<m.rows(); j++ ) {
290  for ( unsigned int i = 0; i<m.cols(); i++ ) {
291  y[i] += x[j] * m[j][i];
292  }
293  }
294  return y;
295 };
296 
304 inline FARSA_NNFW_TEMPLATE DoubleVector& mul( DoubleVector& y, const DoubleMatrix& m, const DoubleVector& x ) {
305 #ifdef FARSA_DEBUG
306  if ( y.size() != m.rows() || x.size() != m.cols() ) {
307  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");
308  }
309 #endif
310  for ( unsigned int j = 0; j<m.rows(); j++ ) {
311  for ( unsigned int i = 0; i<m.cols(); i++ ) {
312  y[j] = m[j][i] * x[i];
313  }
314  }
315  return y;
316 };
324 inline FARSA_NNFW_TEMPLATE DoubleVector& amul( DoubleVector& y, const DoubleMatrix& m, const DoubleVector& x ) {
325 #ifdef FARSA_DEBUG
326  if ( y.size() != m.rows() || x.size() != m.cols() ) {
327  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");
328  }
329 #endif
330  for ( unsigned int j = 0; j<m.rows(); j++ ) {
331  for ( unsigned int i = 0; i<m.cols(); i++ ) {
332  y[j] += m[j][i] * x[i];
333  }
334  }
335  return y;
336 };
345 inline FARSA_NNFW_TEMPLATE DoubleMatrix& deltarule( DoubleMatrix& m, double rate, const DoubleVector& x, const DoubleVector& y ) {
346 #ifdef FARSA_DEBUG
347  if ( x.size() != m.rows() || y.size() != m.cols() ) {
348  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");
349  }
350 #endif
351  for ( unsigned int r=0; r<m.rows(); r++ ) {
352  for ( unsigned int c=0; c<m.cols(); c++ ) {
353  m[r][c] += rate * x[r] * y[c];
354  }
355  }
356  return m;
357 }
358 
359 }
360 
361 #endif