//
// Created by fluffymoo on 11.6.18.
//


#include "NeuronLogistic.h"

NeuronLogistic_d2::NeuronLogistic_d2( double * b ) {
    this->bias = b;
}

double NeuronLogistic_d2::activate( double x ) {
    //(e^(b + x) (e^b - e^x))/(e^b + e^x)^3

    double b = 0.0;
    if( this->bias ){
        b = *this->bias;
    }
    double ex = std::pow(E, x);
    double eb = std::pow(E, b);

    return (eb*ex*(eb - ex))/((eb + ex)*(eb + ex)*(eb + ex));
}

double NeuronLogistic_d2::activation_function_eval_derivative_bias( double x ) {
    //-(e^(b + x) (-4 e^(b + x) + e^(2 b) + e^(2 x)))/(e^b + e^x)^4

    double b = 0.0;
    if( this->bias ){
        b = *this->bias;
    }
    double eb = std::pow(E, b);
    double ex = std::pow(E, x);

    return  -(eb*ex*(-4*eb*ex + eb*eb +ex*ex))/((eb + ex)*(eb + ex)*(eb + ex)*(eb + ex));
}

double NeuronLogistic_d2::activation_function_eval_derivative( double x ) {
    //(e^(b + x) (-4 e^(b + x) + e^(2 b) + e^(2 x)))/(e^b + e^x)^4
    return -this->activation_function_eval_derivative_bias( x );
}

NeuronLogistic* NeuronLogistic_d2::get_derivative() {
    //TODO maybe not the best way
    return nullptr;
}

NeuronLogistic_d1::NeuronLogistic_d1( double * b ) {
    this->bias = b;
}


double NeuronLogistic_d1::activate( double x ) {
    //e^(b - x)/(e^(b - x) + 1)^2
    double b = 0.0;
    if( this->bias ){
        b = *this->bias;
    }
    double ex = std::pow(E, x);
    double eb = std::pow(E, b);
    double d = (eb/ex);

    return d/((d + 1)*(d + 1));
}

double NeuronLogistic_d1::activation_function_eval_derivative_bias( double x ) {
    //(e^(b + x) (e^x - e^b))/(e^b + e^x)^3
    double b = 0.0;
    if( this->bias ){
        b = *this->bias;
    }

    double ex = std::pow(E, x);
    double eb = std::pow(E, b);

    return (eb*ex* (ex - eb))/((eb + ex)*(eb + ex)*(eb + ex));
}

double NeuronLogistic_d1::activation_function_eval_derivative( double x ) {
    //(e^(b + x) (e^b - e^x))/(e^b + e^x)^3
    return -this->activation_function_eval_derivative_bias( x );
}

NeuronLogistic* NeuronLogistic_d1::get_derivative( ) {
    //(e^(b + x) (e^b - e^x))/(e^b + e^x)^3
    NeuronLogistic_d2* output = nullptr;

    output = new NeuronLogistic_d2( this->bias );

    return output;
}

NeuronLogistic::NeuronLogistic( double * b ) {
    this->bias = b;
}

double NeuronLogistic::activate( double x ) {
    //(1 + e^(-x + b))^(-1)

    double b = 0.0;
    if( this->bias ){
        b = *this->bias;
    }
    double ex = std::pow(E, b - x);
    return std::pow(1.0 + ex, -1);
}

double NeuronLogistic::activation_function_eval_derivative_bias( double x ) {
    //-e^(b - x)/(e^(b - x) + 1)^2
    double b = 0.0;
    if( this->bias ){
        b = *this->bias;
    }
    double ex = std::pow(E, b - x);

    return -ex/((ex + 1)*(ex + 1));
}


double NeuronLogistic::activation_function_eval_derivative( double x ) {
    //e^(b - x)/(e^(b - x) + 1)^2
    return -this->activation_function_eval_derivative_bias( x );

}

NeuronLogistic* NeuronLogistic::get_derivative( ) {

    NeuronLogistic_d1 *output = nullptr;
    output = new NeuronLogistic_d1( this->bias );

    return output;

}