//#include "stdafx.h"
#include "neuron.h"




neuron::neuron(int typ,int Nplus1,int Nminus1,int ind, double epsilon, double mom){

     //   double rmax=RAND_MAX;
   //     double wx;
        this->epsilon=epsilon;
        this->epsilon2=0.01;
        this->lambda=1.0;
        this->typ=typ;
        this->Nplus1=Nplus1;
        this->Nminus1=Nminus1;
        this->nplus1 = new neuron *[Nplus1];
        this->nminus1= new neuron *[Nminus1];
        this->w=new double[Nminus1];
        this->index=ind;
        this->moment=mom;
      
       
}
neuron::~neuron(){
        delete w;
        delete nminus1;
        delete nplus1;

}

void neuron::adaptuj_vahy1(double d){
        int i;
        double u;
         dEdYj=Y-d;
        this->wbias+=(-1)*epsilon*(dEdYj)*(lambda)*1;
        for(i=0;i<Nminus1;i++){
              u= (this->nminus1[i])->Y;
              deltaW=(-1)*epsilon*(dEdYj)*(lambda)*Y*(1-Y)*u;


        }

}

void neuron::adaptuj_vahy3(double *x){
        int i;
        double u;
        dEdYj=0;
        for(i=0;i<Nplus1;i++){  // vypocet chyby pre vnutorne neurony
                double c=(this->nplus1[i])->dEdYj;
                double cc=(this->nplus1[i])->w[index];
                dEdYj+=((this->nplus1[i])->dEdYj)*((this->nplus1[i])->lambda)*((this->nplus1[i])->Y)*(1- (this->nplus1[i])->Y)*((this->nplus1[i])->w[index]);

        }
		this->wbias+=(-1)*epsilon*(dEdYj)*(lambda)*1;
        for(i=0;i<Nminus1;i++){
                u= x[i];
                deltaW=(-1)*epsilon*(dEdYj)*(lambda)*Y*(1-Y)*u ;//+ moment*(this->deltaodlw[i]);
                w[i]+=deltaW ;
                w[i]-=epsilon*(dEdYj)*(lambda)*Y*(1-Y)*u;
                
        }
}
double neuron::myexp(double x){
        if(x>50.0)
                return(exp(35.0));
        if(x<-50.0)
                return(exp(-35.0));
        return(exp(x));
}
void neuron::eval1(double *x){
        int i;
         this->ksi=0;
         this->ksi+= 1*(this->wbias);
         for(i=0;i<Nminus1;i++){

                        this->ksi+=x[i]*w[i];
         }
         Y=1.0/(1+myexp((-1)*(this->lambda)*(this->ksi)));

 }

void neuron::eval2(void){
        int i;
         this->ksi=0;
         this->ksi+= 1*(this->wbias);
         for(i=0;i<Nminus1;i++){
                if(((this->nminus1)[i])!= NULL){
                        this->ksi+=(((this->nminus1)[i])->Y)*w[i];
                }
         }
                  Y=1.0/(1+myexp((-1)*(this->lambda)*(this->ksi)));


 }