AIXI.Utils.Log1P C# (CSharp) Метод

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

public static Log1P ( double x ) : double
x double
Результат double
        public static double Log1P(double x)
        {
            //Author: John D Cook, licence: public domain
            // http://www.johndcook.com/blog/csharp_log_one_plus_x/
            if (x <= -1.0)
            {
                string msg = String.Format("Invalid input argument: {0}", x);
                throw new ArgumentOutOfRangeException(msg);
            }

            if (Math.Abs(x) > 1e-4)
            {
                // x is large enough that the obvious evaluation is OK
                return Math.Log(1.0 + x);
            }

            // Use Taylor approx. log(1 + x) = x - x^2/2 with error roughly x^3/3
            // Since |x| < 10^-4, |x|^3 < 10^-12, relative error less than 10^-8

            return (-0.5*x + 1.0)*x;
        }

Usage Example

        public void UpdateLogProbability(int index)
        {
            if (this.IsLeaf(index))
            {
                this.Nodes[index].LogProbability = this.Nodes[index].LogKt;
            }
            else
            {
                double logChildProbability = 0;
                int    lchild = this.Nodes[index].Child1;
                if (lchild != -1)
                {
                    logChildProbability += this.Nodes[lchild].LogProbability;
                }
                int rchild = this.Nodes[index].Child0;
                if (rchild != -1)
                {
                    logChildProbability += this.Nodes[rchild].LogProbability;
                }

                //for better numerical results
                double a = Math.Max(this.Nodes[index].LogKt, logChildProbability);
                double b = Math.Min(this.Nodes[index].LogKt, logChildProbability);

                this.Nodes[index].LogProbability = Math.Log(0.5) + a + Utils.Log1P(Math.Exp(b - a));
                //todo: is it fast enough to compute Math.Log(0.5) every time. Joel has it cached.
            }
        }
All Usage Examples Of AIXI.Utils::Log1P