/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * Geophysical Computational Tools & Library (GCTL)
 *
 * Copyright (c) 2023  Yi Zhang (yizhang-geo@zju.edu.cn)
 *
 * GCTL is distributed under a dual licensing scheme. You can redistribute 
 * it and/or modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation, either version 2 
 * of the License, or (at your option) any later version. You should have 
 * received a copy of the GNU Lesser General Public License along with this 
 * program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * If the terms and conditions of the LGPL v.2. would prevent you from using 
 * the GCTL, please consider the option to obtain a commercial license for a 
 * fee. These licenses are offered by the GCTL's original author. As a rule, 
 * licenses are provided "as-is", unlimited in time for a one time fee. Please 
 * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget 
 * to include some description of your company and the realm of its activities. 
 * Also add information on how to contact you by electronic and paper mail.
 ******************************************************/

#include "multinary.h"

#define SQRT2PI 2.5066282746310002416

gctl::multinary::multinary(){}

gctl::multinary::multinary(double mini, double maxi, double std, unsigned int num)
{
    init(mini, maxi, std, num);
}

gctl::multinary::multinary(const array<double> &xs, const array<double> &stds)
{
    init(xs, stds);
}
    
gctl::multinary::~multinary(){}

void gctl::multinary::init(double mini, double maxi, double std, unsigned int num)
{
    pnum_ = num - 1;
    of_ = mini;

    double dm = (maxi - of_)/(num - 1);

    std_.resize(pnum_, std);
    mp_.resize(pnum_);
    ys_.resize(pnum_);
    for (size_t i = 0; i < pnum_; i++)
    {
        mp_[i] = dm*(i+1);
    }

    ys_[0] = mp_[0];
    for (size_t i = 1; i < pnum_; i++)
    {
        ys_[i] = mp_[i] - mp_[i-1];
    }
    return;
}

void gctl::multinary::init(const array<double> &xs, const array<double> &stds)
{
    if (xs.size() - stds.size() != 1) throw std::runtime_error("[gctl::multinary] Invalid parameters' size.");

    pnum_ = xs.size() - 1;
    of_ = xs.front();

    std_ = stds;
    mp_.resize(pnum_);
    ys_.resize(pnum_);

    for (size_t i = 0; i < pnum_; i++)
    {
        mp_[i] = xs[i+1] - of_;
    }

    ys_[0] = mp_[0];
    for (size_t i = 1; i < pnum_; i++)
    {
        ys_[i] = mp_[i] - mp_[i-1];
    }
    return;
}

double gctl::multinary::val(double x) const
{
    //return GCTL_ZERO*(x - of_) + 0.5*Pm_(x - of_) + of_;
    return 0.5*Pm_(x - of_) + of_;
}

double gctl::multinary::der(double x) const
{
    //return GCTL_ZERO + Pm_grad_(x - of_);
    return Pm_grad_(x - of_);
}

double gctl::multinary::Pm_(double x) const
{
    double pm = 0;
    for (size_t i = 0; i < pnum_; i++)
    {
        pm += ys_[i]*(1.0 + erf((x + 0.5*ys_[i] - mp_[i])/(M_SQRT2*std_[i])));
    }
    return pm;
}

double gctl::multinary::Pm_grad_(double x) const
{
    double gm = 0;
    for (size_t i = 0; i < pnum_; i++)
    {
        gm += exp(-1.0*(x + 0.5*ys_[i] - mp_[i])*(x + 0.5*ys_[i] - mp_[i])/(2.0*std_[i]*std_[i]))/(SQRT2PI*std_[i]);
    }
    return gm;
}