#include #include #include #define FROM -10.0 #define TO 10.0 #define STEP 0.01 typedef struct NEURON { double output, old_output; int N; double *weights; struct NEURON **input_neuron; double (*outputf)(); } neuron; double sigmoid( double x ) { return 1.0 / ( 1.0 + exp ( -x ) ); } int initialize_neuron( neuron *a, int N ) { int i; a->output = 0.0; a->old_output = 0.0; a->N = N; a->weights = (double *)malloc(sizeof(double) * N); if ( a->weights == NULL ) { fprintf(stderr, "### malloc() failed.\n"); exit (EXIT_FAILURE); } for ( i=0; iN; i++ ) { a->weights[i] = 0.0; } a->input_neuron = (neuron **)malloc(sizeof (neuron *) * N); if ( a->input_neuron == NULL ) { fprintf(stderr, "### malloc() failed.\n"); exit (EXIT_FAILURE); } a->outputf = sigmoid; return EXIT_SUCCESS; } void calc_output ( neuron *a ) { int i; double wrk = 0.0; for ( i=0; iN; i++ ) { wrk += a->weights[i] * a->input_neuron[i]->output; } a->output = a->outputf( wrk ); } int main(void) { neuron output, input; initialize_neuron( &output, 1 ); initialize_neuron( &input, 0 ); output.input_neuron[0] = &input; output.weights[0] = 1.0; for (input.output=FROM; input.output<=TO;input.output += STEP){ calc_output( &output ); fprintf(stdout, "%5.3f %7.3f\n", input.output, output.output); } return 0; }