MathPHP\Functions\Special::gamma PHP Метод

gamma() публичный статический Метод

For postive integers: Γ(n) = (n - 1)! For half integers: _ (2n)! Γ(½ + n) = √π ------- 4ⁿ n! For real numbers: use Lanczos approximation
public static gamma ( number $n ) : number
$n number
Результат 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)) < 1.0E-5) && $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)) < 1.0E-5) && $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($π);
            if ($x == 0) {
                return $√π;
            }
            $⟮2n−1⟯‼︎ = Combinatorics::doubleFactorial(2 * $x - 1);
            /**
             * Put it all together
             *  _  (2n-1)!!
             * √π ---------
             *       2ⁿ
             */
            return $√π * ($⟮2n−1⟯‼︎ / 2 ** $x);
        }
        // Generic real number case
        return self::gammaLanczos($n);
    }

Usage Example

Пример #1
0
 /**
  * Probability density function
  *
  *     / ν + 1 \
  *  Γ |  -----  |
  *     \   2   /    /    x² \ ⁻⁽ᵛ⁺¹⁾/²
  *  -------------  | 1 + --  |
  *   __    / ν \    \    ν  /
  *  √νπ Γ |  -  |
  *         \ 2 /
  *
  *
  * @param number $x percentile
  * @param int    $ν degrees of freedom > 0
  */
 public static function PDF($x, int $ν)
 {
     Support::checkLimits(self::LIMITS, ['x' => $x, 'ν' => $ν]);
     $π = \M_PI;
     // Numerator
     $Γ⟮⟮ν+1⟯∕2⟯ = Special::gamma(($ν + 1) / 2);
     $⟮1+x²∕ν⟯ = 1 + $x ** 2 / $ν;
     $−⟮ν+1⟯∕2 = -($ν + 1) / 2;
     // Denominator
     $√⟮νπ⟯ = sqrt($ν * $π);
     $Γ⟮ν∕2⟯ = Special::gamma($ν / 2);
     return $Γ⟮⟮ν+1⟯∕2⟯ * $⟮1+x²∕ν⟯ ** $−⟮ν+1⟯∕2 / ($√⟮νπ⟯ * $Γ⟮ν∕2⟯);
 }
All Usage Examples Of MathPHP\Functions\Special::gamma