/*
     Description: 正态分布相关函数

     Copyright : All right reserved by ICT

     Author : Zhang Zhonghai
     Date : 2025/12/26
*/
#pragma once

#include <math.h>

struct NormalDistribution {
    /**
     * Default inverse cumulative probability accuracy.
     * @since 2.1
     */
    static constexpr double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9;
    /** &radic;(2) */
    static constexpr double SQRT2 = 1.414213562373095;

    static constexpr double PI = 105414357.0 / 33554432.0 + 1.984187159361080883e-9;

    /** Mean of this distribution. */
    double mean;
    /** Standard deviation of this distribution. */

    double standardDeviation;
    /** The value of {@code log(sd) + 0.5*log(2*pi)} stored for faster computation. */

    double logStandardDeviationPlusHalfLog2Pi;
    /** Inverse cumulative probability accuracy. */

    double solverAbsoluteAccuracy;

    NormalDistribution(double mean, double sd) {
        this->mean = mean;
        standardDeviation = sd;
        logStandardDeviationPlusHalfLog2Pi = std::log(sd) + 0.5 * std::log(2 * PI);
        solverAbsoluteAccuracy = DEFAULT_INVERSE_ABSOLUTE_ACCURACY;
    }

    double logDensity(double x) const {
        const double x0 = x - mean;
        const double x1 = x0 / standardDeviation;
        return -0.5 * x1 * x1 - logStandardDeviationPlusHalfLog2Pi;
    }
};