Exemple #1
0
        /// <summary>
        /// Perform one iteration of searching for a root with Newton-Raphson method
        /// </summary>
        /// <param name="f"></param>
        /// <param name="df"></param>
        /// <param name="value"></param>
        /// <param name="precision"></param>
        /// <returns></returns>
        private static ComplexNumber NewtonIter(FastExpression f, FastExpression df, ComplexNumber value, int precision)
        {
            ComplexNumber prev          = value;
            int           minCheckIters = (int)Math.Sqrt(precision);

            for (int i = 0; i < precision; i++)
            {
                if (i == precision - 1)
                {
                    prev = value.Copy();
                }
                try // TODO: remove try catch in for
                {
                    value -= (f.Substitute(value) / df.Substitute(value)) as ComplexNumber;
                }
                catch (MathSException)
                {
                    throw new MathSException("Two or more variables in SolveNt is forbidden");
                }
                if (i > minCheckIters && prev == value)
                {
                    return(value);
                }
            }
            if (Number.Abs(prev - value) > MathS.Settings.PrecisionErrorCommon)
            {
                return(RealNumber.NaN());
            }
            else
            {
                return(value);
            }
        }
Exemple #2
0
        /// <summary>
        /// Perform one iteration of searching for a root with Newton-Raphson method
        /// </summary>
        /// <param name="f"></param>
        /// <param name="df"></param>
        /// <param name="value"></param>
        /// <param name="precision"></param>
        /// <returns></returns>
        private static ComplexNumber NewtonIter(FastExpression f, FastExpression df, Complex value, int precision)
        {
            Complex prev = value;

            Complex ChooseGood()
            {
                if (Complex.Abs(prev - value) > (double)MathS.Settings.PrecisionErrorCommon.Value)
                {
                    return(double.NaN);
                }
                else
                {
                    return(value);
                }
            }

            int minCheckIters = (int)Math.Sqrt(precision);

            for (int i = 0; i < precision; i++)
            {
                if (i == precision - 1)
                {
                    prev = value; //.Copy();
                }
                try               // TODO: remove try catch in for
                {
                    var dfv = df.Substitute(value);
                    if (dfv == 0)
                    {
                        return(ChooseGood());
                    }
                    value -= f.Substitute(value) / dfv;
                }
                catch (OverflowException)
                {
                    return(ChooseGood());
                }
                if (i > minCheckIters && prev == value)
                {
                    return(value);
                }
            }
            return(ChooseGood());
        }