<commit_before><?php
namespace Math\Functions;

use Math\Probability\Combinatorics;
use Math\Statistics\RandomVariable;
use Math\Functions\Map\Single;

class Special
{
    /**
     * Sign function (signum function) - sgn
     * Extracts the sign of a real number.
     * https://en.wikipedia.org/wiki/Sign_function
     *
     *          { -1 if x < 0
     * sgn(x) = {  0 if x = 0
     *          {  1 if x > 0
     *
     * @param number $x
     *
     * @return int
     */
    public static function signum(float $x): int
    {
        if ($x == 0) {
            return 0;
        }
        return $x < 0 ? -1 : 1;
    }

    /**
     * Sign function (signum function) - sgn
     * Convenience wrapper for signum function.
     */
    public static function sgn(float $x): int
    {
        return self::signum($x);
    }

    /**
     * Gamma function
     * https://en.wikipedia.org/wiki/Gamma_function
     * https://en.wikipedia.org/wiki/Particular_values_of_the_Gamma_function
     *
     * For postive integers:
     *  Γ(n) = (n - 1)!
     *
     * For half integers:
     *
     *             _   (2n)!
     * Γ(½ + n) = √π  -------
     *                 4ⁿ n!
     *
     * For real numbers: use Lanczos approximation
     *
     * @param number $n
     *
     * @return number
     */
    public static function gamma($n)
    {
        // Basic integer/factorial cases
        if ($n == 0) {
            return \INF;
        }
        // Negative integer, or negative int as a float (Ex: from beta(-0.1, -0.9) since it will call Γ(x + y))
        if ((is_int($n) || is_numeric($n) && abs($n - round($n)) < 0.00001) && $n < 0) {
            return -\INF;
        }
        // Positive integer, or postive int as a float (Ex: from beta(0.1, 0.9) since it will call Γ(x + y))
        if ((is_int($n) || is_numeric($n) && abs($n - round($n)) < 0.00001) && $n > 0) {
            return Combinatorics::factorial(round($n) - 1);
        }

        // Half integer cases (determine if int + 0.5)
        if ((round($n * 2) / 2 / $n) == 1) {
            // Compute parts of equation
            $π     = \M_PI;
            $x     = round($n - 0.5, 0);
            $√π    = sqrt($π);
            $⟮2n⟯！  = Combinatorics::factorial(2 * $x);
            $⟮4ⁿn！⟯ = 4**$x * Combinatorics::factorial($x);

            /**
             * Put it all together
             *  _  (2n)!
             * √π -------
             *     4ⁿ n!
             */
            return $√π * ($⟮2n⟯！ / $⟮4ⁿn！⟯);
        }

        // Generic real number case
        return self::gammaLanczos($n);<commit_msg>Extending the range of gamma<commit_after><?php
namespace Math\Functions;

use Math\Probability\Combinatorics;
use Math\Statistics\RandomVariable;
use Math\Functions\Map\Single;

class Special
{
    /**
     * Sign function (signum function) - sgn
     * Extracts the sign of a real number.
     * https://en.wikipedia.org/wiki/Sign_function
     *
     *          { -1 if x < 0
     * sgn(x) = {  0 if x = 0
     *          {  1 if x > 0
     *
     * @param number $x
     *
     * @return int
     */
    public static function signum(float $x): int
    {
        if ($x == 0) {
            return 0;
        }
        return $x < 0 ? -1 : 1;
    }

    /**
     * Sign function (signum function) - sgn
     * Convenience wrapper for signum function.
     */
    public static function sgn(float $x): int
    {
        return self::signum($x);
    }

    /**
     * Gamma function
     * https://en.wikipedia.org/wiki/Gamma_function
     * https://en.wikipedia.org/wiki/Particular_values_of_the_Gamma_function
     *
     * For postive integers:
     *  Γ(n) = (n - 1)!
     *
     * For half integers:
     *
     *             _   (2n)!
     * Γ(½ + n) = √π  -------
     *                 4ⁿ n!
     *
     * For real numbers: use Lanczos approximation
     *
     * @param number $n
     *
     * @return number
     */
    public static function gamma($n)
    {
        // Basic integer/factorial cases
        if ($n == 0) {
            return \INF;
        }
        // Negative integer, or negative int as a float (Ex: from beta(-0.1, -0.9) since it will call Γ(x + y))
        if ((is_int($n) || is_numeric($n) && abs($n - round($n)) < 0.00001) && $n < 0) {
            return -\INF;
        }
        // Positive integer, or postive int as a float (Ex: from beta(0.1, 0.9) since it will call Γ(x + y))
        if ((is_int($n) || is_numeric($n) && abs($n - round($n)) < 0.00001) && $n > 0) {
            return Combinatorics::factorial(round($n) - 1);
        }

        // Half integer cases (determine if int + 0.5)
        if ((round($n * 2) / 2 / $n) == 1) {
            // Compute parts of equation
            $π     = \M_PI;
            $x     = round($n - 0.5, 0);
            $√π    = sqrt($π);
            $⟮2n−1⟯！！  = Combinatorics::doubleFactorial(2 * $x - 1);

            /**
             * Put it all together
             *  _  (2n-1)!!
             * √π ---------
             *       2ⁿ
             */
            return $√π * ($⟮2n⟯！ / 2**$x);
        }

        // Generic real number case
        return self::gammaLanczos($n);
{'fraction_non_alphanumeric': 0.15217391304347827, 'fraction_numerical': 0.021156832298136644, 'mean_word_length': 1.4068192433442317, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 7, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5686891', 'n_tokens_mistral': 1850, 'n_tokens_neox': 1742, 'n_words': 448}