示例#1
0
        /// <summary>
        /// Use the root finder to solve Q(a,x) == q, for x
        /// </summary>
        /// <param name="a"></param>
        /// <param name="q"></param>
        /// <param name="guess"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public static double SolveGivenAQ(double a, double q, double guess, double min, double max)
        {
            Func <double, ValueTuple <double, double, double> > halleyF = (double x) => {
                // Calculate Q(x) - q and the first two derivatives

                double f  = Math2.GammaQ(a, x) - q;
                double f1 = -Math2.GammaPDerivative(a, x);
                double f2 = -f1 * (1.0 + (1.0 - a) / x);
                return(f, f1, f2);
            };


            const double relTolerance = RootFinder.DefaultRelTolerance;
            const double absTolerance = 0;
            RootResults  rr           = RootFinder.Halley(halleyF, guess, min, max, relTolerance, absTolerance, Policies.MaxRootIterations);

            if (rr == null)
            {
                Policies.ReportRootNotFoundError("Invalid parameter in root solver");
                return(double.NaN);
            }
#if EXTRA_DEBUG
            Debug.WriteLine("Halley iterations: {0}", rr.Iterations);
#endif
            if (!rr.Success)
            {
                Policies.ReportRootNotFoundError("Root not found after {0} iterations", rr.Iterations);
                return(double.NaN);
            }

            return(rr.SolutionX);
        }