首页 > 解决方案 > C# 中的 Lambert W 函数

问题描述

计算乘积对数函数的函数,也称为 Lambert W 函数。

通过使用它

double lam = LambertW(1);

标签: c#

解决方案


    public static double LambertW(double x)
    {
        // LambertW is not defined in this section
        if (x < -Math.Exp(-1))
            throw new Exception("The LambertW-function is not defined for " + x + ".");

        // computes the first branch for real values only

        // amount of iterations (empirically found)
        int amountOfIterations = Math.Max(4, (int)Math.Ceiling(Math.Log10(x) / 3));

        // initial guess is based on 0 < ln(a) < 3
        double w = 3 * Math.Log(x + 1) / 4;

        // Halley's method via eqn (5.9) in Corless et al (1996)
        for (int i = 0; i < amountOfIterations; i++)
            w = w - (w * Math.Exp(w) - x) / (Math.Exp(w) * (w + 1) - (w + 2) * (w * Math.Exp(w) - x) / (2 * w + 2));

        return w;
    }

推荐阅读