Exemple #1
0
        internal virtual int cfs(double x)
        {
            if (x < 0)
            {
                return(cfs(-x));
            }

            int a0 = ( int )Math.floor(x);

            if (x == ( double )a0)
            {
                return(a0);
            }

            int a1 = ( int )Math.floor(1.0 / (x - a0));

            int z = a0 * a1 + 1;

            if (Math.abs(( double )z / ( double )a1 - x) < 1.0e-6)
            {
                return(z);
            }

            return(0);
        }
Exemple #2
0
        public override double Norm()
        {
            double r;

            if (Math.abs(Re) > Math.abs(Im))
            {
                r = Im / Re;
                r = Math.abs(Re) * Math.Sqrt(1 + r * r);
            }
            else if (Im != 0)
            {
                r = Re / Im;
                r = Math.abs(Im) * Math.Sqrt(1 + r * r);
            }
            else
            {
                r = 0.0;
            }

            return(r);
        }
Exemple #3
0
        public override int Eval(Stack st)
        {
            int narg = GetNarg(st);

            if (narg != 4)
            {
                throw new ParseException("Usage: ROMBERG (exp,var,ll,ul)");
            }

            var exp = GetAlgebraic(st);
            var v   = GetVariable(st);
            var ll  = GetAlgebraic(st);
            var ul  = GetAlgebraic(st);

            LambdaAlgebraic xc = new ExpandConstants();

            exp = xc.SymEval(exp);
            ll  = xc.SymEval(ll);
            ul  = xc.SymEval(ul);

            if (!(ll is Symbol) || !(ul is Symbol))
            {
                throw new ParseException("Usage: ROMBERG (exp,var,ll,ul)");
            }

            var rombergtol = 1.0e-4;
            int rombergit  = 11;

            var a1 = Session.Proc.Store.GetNum("rombergit");

            if (a1 != null)
            {
                rombergit = a1.ToInt();
            }

            a1 = Session.Proc.Store.GetNum("rombergtol");

            if (a1 != null)
            {
                rombergtol = a1.ToComplex().Re;
            }

            var a = (( Symbol )ll).ToComplex().Re;
            var b = (( Symbol )ul).ToComplex().Re;

            var I = Matrix.CreateRectangularArray <double>(rombergit, rombergit);
            int i = 0, n = 1;

            var t = trapez(exp, v, n, a, b);

            if (!(t is Symbol))
            {
                throw new ParseException("Expression must evaluate to number");
            }

            I[0][0] = (( Symbol )t).ToComplex().Re;

            double epsa = 1.1 * rombergtol;

            while (epsa > rombergtol && i < rombergit - 1)
            {
                i++;
                n *= 2;
                t  = trapez(exp, v, n, a, b);

                I[0][i] = (( Symbol )t).ToComplex().Re;

                double f = 1.0;

                for (int k = 1; k <= i; k++)
                {
                    f      *= 4;
                    I[k][i] = I[k - 1][i] + (I[k - 1][i] - I[k - 1][i - 1]) / (f - 1.0);
                }

                epsa = Math.abs((I[i][i] - I[i - 1][i - 1]) / I[i][i]);
            }

            st.Push(new Complex(I[i][i]));

            return(0);
        }
Exemple #4
0
        protected internal static Symbol readNumber(StringBuilder s)
        {
            int kmax = 0;

            while (kmax < s.Length && oneof(s[kmax], "0123456789.eE+-"))
            {
                kmax++;
            }

            var sub = s.ToString().Substring(0, kmax);

            var cultureInfo = new CultureInfo("")
            {
                NumberFormat = { NumberDecimalSeparator = "." }
            };

            for (int k = kmax; k > 0; k--)
            {
                try
                {
                    var ts = sub.Substring(0, k);

                    var x = double.Parse(ts, cultureInfo);

                    if (ts.EndsWith(".", StringComparison.Ordinal) && s.Length > k && (s[k] == '^' || s[k] == '/'))
                    {
                        continue;
                    }

                    bool imag = false;

                    if (s.Length > k && (s[k] == 'i' || s[k] == 'j'))
                    {
                        imag = true;
                        k++;
                    }

                    s.Remove(0, k);

                    if (Math.abs(x) > 1e15)
                    {
                        try
                        {
                            var bi = new BigInteger(ts, 10);

                            return(imag ? ( Symbol )(new Number(bi) * Symbol.IONE) : new Number(bi));
                        }
                        catch (Exception)
                        {
                        }
                    }

                    return(imag ? new Complex(0, x) : new Complex(x));
                }
                catch (Exception)
                {
                }
            }

            throw new ParseException("Internal Error.");
        }