/* * File : funcs.c * * Andrew H. Fagg * 5/10/91 * * Useful library routines for work in NSL. * Includes : * Random weight generators. * Useful functions on vectors and matrices * */ #include "nsl_include.h" //#include //#include #include "alib.h" #define nl << "\n" #define sp << " " #ifndef NSL_PC extern double drand48(); #endif /* * float random_value() * * Return = a random number between [-1.0, 1.0]. * */ float random_value() { return(((float)(rand() % 30001)/ 15000.0) - 1.0); } /* * float random() * * Return = a random number between [0.0, 1.0). * */ float randomv() { return((float)(rand() % 30000)/ 30000.0); } nsl_vector& random(nsl_vector& vec) { int i; int j; i = vec.get_xn(); for(j = 0; j < i; ++j) #ifndef NSL_PC vec.elem(j) = (float) drand48(); #else vec.elem(j) = (float) rand(); #endif return vec; } nsl_matrix& random(nsl_matrix& mat) { int i; int j; int xn = mat.get_xn(); int yn = mat.get_yn(); for(i = 0; i < xn; ++i) for(j = 0; j < yn; ++j) #ifndef NSL_PC mat.elem(i,j) = (float) drand48(); #else mat.elem(i,j) = (float) rand(); #endif return mat; } /* * nsl_vector square(nsl_vector& a) * * Square each element in a vector. * */ nsl_vector square(nsl_vector& a) { int i; int j; i = a.get_xn(); nsl_vector out(i); for(j = 0; j < i; ++j) { out.elem(j) = a.elem(j) * a.elem(j); } return(out); } /* * nsl_vector sqrt(nsl_vector& a) * * Take the square-root of each element in a vector. * */ nsl_vector sqrt(nsl_vector& a) { int i; int j; i = a.get_xn(); nsl_vector out(i); for(j = 0; j < i; ++j) { out.elem(j) = sqrt(a.elem(j)); } return(out); } /* * nsl_vector abs(nsl_vector& a) * * Take the absolute value of each element of a vector. */ nsl_vector abs(nsl_vector& a) { int i; int len; len = a.get_xn(); nsl_vector out(len); for(i = 0; i < len; ++i) out.elem(i) = fabs(a.elem(i)); return(out); } /* * nsl_vector apply_func(nsl_vector& a, double (*func)(double)) * * Apply a function to each element of a vector. Returns * a new vector. */ nsl_vector apply_func(nsl_vector& a, double (*func)(double)) { int i; int len; len = a.get_xn(); nsl_vector out(len); for(i = 0; i < len; ++i) out.elem(i) = (func)(a.elem(i)); return(out); } /* * nsl_matrix apply_func(nsl_matrix& a, double (*func)(double)) * * Apply a function to each element of a matrix. Returns a * new matrix. */ nsl_matrix apply_func(nsl_matrix& a, double (*func)(double)) { int i, j; int len1; int len2; len1 = a.get_xn(); len2 = a.get_yn(); nsl_matrix out(len1, len2); for(i = 0; i < len1; ++i) for(j = 0; j < len2; ++j) out.elem(i,j) = (func)(a.elem(i,j)); return(out); } /* * nsl_vector sigmoid(nsl_vector& vec) * * In place, apply the sigmoid function to each element of . * * Return = a pointer to the modified vector. * */ nsl_vector sigmoid(nsl_vector& vec) { int i; int xn; xn = vec.get_xn(); nsl_vector out(xn); for(i = 0; i < xn; ++i) { out.elem(i) = (1.0 / (1.0 + exp(-vec.elem(i)))); } return out; } /* * void randomize(nsl_vector& vec) * * Set the values of to random numbers in the range [-1.0, 1.0]. * */ void randomize(nsl_vector& vec) { int i; int init; int limit; init = vec.get_x0(); limit = vec.get_x1(); for(i = init; i <= limit; ++i) { vec.elem(i) = random_value(); } } /* * overload void randomize(nsl_matrix& mat) * * Set the values of to random numbers in the range [-1.0, 1.0]. * */ void randomize(nsl_matrix& mat) { int i, j; int x0, x1; int y0, y1; x0 = mat.get_x0(); x1 = mat.get_x1(); y0 = mat.get_y0(); y1 = mat.get_y1(); for(i = x0; i <= x1; ++i) for(j = y0; j <= y1; ++j) { mat.elem(i,j) = random_value(); } }