#include #include #include #define FROM 0.0 #define TO 100.0 #define TAU 0.001 typedef struct NEURON { double output, old_output; int Ninp; double *inp_wgt; struct NEURON **inp_neuron; } neuron; void initialize_neuron(neuron *a, int N) { int i; a->output = 0.0; a->Ninp = N; a->inp_wgt = (double *)malloc(sizeof(double) * (N)); if ( a->inp_wgt == NULL ) { fprintf(stderr,"### malloc() faild.\n"); exit (EXIT_FAILURE); } for (i=0; i< a->Ninp; i++) { a->inp_wgt[i] = 0.0; } a->inp_neuron = (neuron **)malloc(sizeof(neuron *) * (N)); if ( a->inp_neuron == NULL ) { fprintf(stderr,"### malloc() faild.\n"); exit (EXIT_FAILURE); } } double output_f(double value) { return value; } void calc_neuron(neuron *a) { double wrk; int i; wrk = 0.0; for(i=0; i< a->Ninp; i++){ wrk += a->inp_wgt[i] * a->inp_neuron[i]->old_output; } a->output += output_f(wrk) * TAU; } void update_neuron(neuron *a) { a->old_output = a->output; } int main_loop( neuron *a, neuron *b) { double t = FROM; while ( t <= TO ){ printf("%6.3f %7.4f %7.4f\n", t, a->output, b->output); calc_neuron(a); calc_neuron(b); update_neuron(a); update_neuron(b); t += TAU; } return 0; } int main(int argc, char **argv) { neuron x, y; if ( argc != 3 ) { printf("### Usage: %s \n", argv[0]); exit (EXIT_FAILURE); } initialize_neuron(&x, 2); initialize_neuron(&y, 2); x.output = x.old_output = atof(argv[1]); y.output = y.old_output = atof(argv[2]); x.inp_neuron[0] = &x; x.inp_neuron[1] = &y; y.inp_neuron[0] = &y; y.inp_neuron[1] = &x; x.inp_wgt[0] = 0.0; /* w_{xx} */ x.inp_wgt[1] = 1.0; /* w_{xy} */ y.inp_wgt[1] = -1.0; /* w_{yx} */ y.inp_wgt[0] = 0.0; /* w_{yy} */ return main_loop(&x, &y); }