00001
00002 #ifndef FUNCTIONS_H
00003 #define FUNCTIONS_H
00004
00005
00006 #include "vec.h"
00007
00008 namespace FD {
00009
00010 static float *calc_tansig_table()
00011 {
00012 float *table = new float [2001];
00013 for (int i=0;i<2001;i++)
00014 {
00015 float xx = .01*i - 10;
00016 table[i] = 2/(1+exp(-2*xx)) - 1;
00017 }
00018
00019 return table;
00020 }
00021
00022 static float *tansig_table = calc_tansig_table();
00023
00024 static float *calc_sigmoid_table()
00025 {
00026 float *table = new float [2001];
00027 for (int i=0;i<2001;i++)
00028 {
00029 float xx = .01*i - 10;
00030 table[i] = 1/(1+exp(-xx));
00031 }
00032
00033 return table;
00034 }
00035
00036 static float *sigmoid_table = calc_sigmoid_table();
00037
00038 inline void sigmoid(float *x, float *y, int len)
00039 {
00040 for (int i=0;i<len;i++)
00041 {
00042 float xx=*x++;
00043
00044 if (xx>9.9)
00045 xx=9.9;
00046 else if (xx<-9.9)
00047 xx=-9.9;
00048
00049 float n = xx*100.0+1000.0;
00050 int n1 = int(n);
00051 float f = n - n1;
00052 *y++ = (1-f)*sigmoid_table[n1] + f*sigmoid_table[n1+1];
00053
00054 }
00055 }
00056
00057 inline void deriv_sigmoid(float *x, float *y, int len)
00058 {
00059
00060
00061
00062
00063
00064
00065
00066 float *end = x+len;
00067 while (x<end-3)
00068 {
00069 *y++ = *x * (1-*x);
00070 x++;
00071 *y++ = *x * (1-*x);
00072 x++;
00073 *y++ = *x * (1-*x);
00074 x++;
00075 *y++ = *x * (1-*x);
00076 x++;
00077 }
00078 while (x<end)
00079 {
00080 *y++ = *x * (1-*x);
00081 x++;
00082 }
00083
00084
00085 }
00086
00087 inline void tansig(float *x, float *y, int len)
00088 {
00089 for (int i=0;i<len;i++)
00090 {
00091 float xx=*x++;
00092
00093 if (xx>9.9)
00094 xx=9.9;
00095 else if (xx<-9.9)
00096 xx=-9.9;
00097
00098 float n = xx*100.0+1000.0;
00099 int n1 = int(n);
00100 float f = n - n1;
00101 *y++ = (1-f)*tansig_table[n1] + f*tansig_table[n1+1];
00102 }
00103 }
00104
00105 inline void deriv_tansig(float *x, float *y, int len)
00106 {
00107
00108
00109
00110
00111
00112
00113
00114 float *end = x+len;
00115 while (x<end-3)
00116 {
00117 *y++ = (1- (*x)*(*x));
00118 x++;
00119 *y++ = (1- (*x)*(*x));
00120 x++;
00121 *y++ = (1- (*x)*(*x));
00122 x++;
00123 *y++ = (1- (*x)*(*x));
00124 x++;
00125 }
00126 while (x<end)
00127 {
00128 *y++ = (1- (*x)*(*x));
00129 x++;
00130 }
00131
00132 }
00133
00134 inline void lin(float *x, float *y, int len)
00135 {
00136 for (int i=0;i<len;i++)
00137 *y++ = *x++;
00138 }
00139
00140 inline void deriv_lin(float *x, float *y, int len)
00141 {
00142 for (int i=0;i<len;i++)
00143 *y++ = 1;
00144 }
00145 }
00146 #endif