Example #1
0
    public static int Main(string[] args)
    {
        autogk.autogkstate state = new autogk.autogkstate();
        double             v     = 0;

        autogk.autogkreport rep = new autogk.autogkreport();


        //
        // f(x) = x*sin(x), integrated at [-pi, pi].
        // Exact answer is 2*pi
        //
        autogk.autogksmooth(-Math.PI, +Math.PI, ref state);
        while (autogk.autogkiteration(ref state))
        {
            state.f = state.x * Math.Sin(state.x);
        }
        autogk.autogkresults(ref state, ref v, ref rep);
        System.Console.Write("integral(x*sin(x),-pi,+pi) = ");
        System.Console.Write("{0,0:F2}", v);
        System.Console.WriteLine();
        System.Console.Write("Exact answer is ");
        System.Console.Write("{0,0:F2}", 2 * Math.PI);
        System.Console.WriteLine();
        return(0);
    }
    public static int Main(string[] args)
    {
        autogk.autogkstate state = new autogk.autogkstate();
        double v = 0;
        autogk.autogkreport rep = new autogk.autogkreport();

        
        //
        // f(x) = x*sin(x), integrated at [-pi, pi].
        // Exact answer is 2*pi
        //
        autogk.autogksmooth(-Math.PI, +Math.PI, ref state);
        while( autogk.autogkiteration(ref state) )
        {
            state.f = state.x*Math.Sin(state.x);
        }
        autogk.autogkresults(ref state, ref v, ref rep);
        System.Console.Write("integral(x*sin(x),-pi,+pi) = ");
        System.Console.Write("{0,0:F2}",v);
        System.Console.WriteLine();
        System.Console.Write("Exact answer is ");
        System.Console.Write("{0,0:F2}",2*Math.PI);
        System.Console.WriteLine();
        return 0;
    }
Example #3
0
    public static int Main(string[] args)
    {
        autogk.autogkstate state = new autogk.autogkstate();
        double             v     = 0;

        autogk.autogkreport rep = new autogk.autogkreport();
        double a     = 0;
        double b     = 0;
        double alpha = 0;


        //
        // f1(x) = (1+x)*(x-a)^alpha, alpha=-0.3
        // Exact answer is (B-A)^(Alpha+2)/(Alpha+2) + (1+A)*(B-A)^(Alpha+1)/(Alpha+1)
        //
        // This code demonstrates use of the State.XMinusA (State.BMinusX) field.
        //
        // If we try to use State.X instead of State.XMinusA,
        // we will end up dividing by zero! (in 64-bit precision)
        //
        a     = 1.0;
        b     = 5.0;
        alpha = -0.9;
        autogk.autogksingular(a, b, alpha, 0.0, ref state);
        while (autogk.autogkiteration(ref state))
        {
            state.f = Math.Pow(state.xminusa, alpha) * (1 + state.x);
        }
        autogk.autogkresults(ref state, ref v, ref rep);
        System.Console.Write("integral((1+x)*(x-a)^alpha) on [");
        System.Console.Write("{0,0:F1}", a);
        System.Console.Write("; ");
        System.Console.Write("{0,0:F1}", b);
        System.Console.Write("] = ");
        System.Console.Write("{0,0:F2}", v);
        System.Console.WriteLine();
        System.Console.Write("Exact answer is ");
        System.Console.Write("{0,0:F2}", Math.Pow(b - a, alpha + 2) / (alpha + 2) + (1 + a) * Math.Pow(b - a, alpha + 1) / (alpha + 1));
        System.Console.WriteLine();
        return(0);
    }
        /*************************************************************************
        Test
        *************************************************************************/
        public static bool testautogk(bool silent)
        {
            bool result = new bool();
            double a = 0;
            double b = 0;
            autogk.autogkstate state = new autogk.autogkstate();
            autogk.autogkreport rep = new autogk.autogkreport();
            double v = 0;
            double exact = 0;
            double eabs = 0;
            double alpha = 0;
            int pkind = 0;
            double errtol = 0;
            bool simpleerrors = new bool();
            bool sngenderrors = new bool();
            bool waserrors = new bool();

            simpleerrors = false;
            sngenderrors = false;
            waserrors = false;
            errtol = 10000*math.machineepsilon;
            
            //
            // Simple test: integral(exp(x),+-1,+-2), no maximum width requirements
            //
            a = (2*math.randominteger(2)-1)*1.0;
            b = (2*math.randominteger(2)-1)*2.0;
            autogk.autogksmooth(a, b, state);
            while( autogk.autogkiteration(state) )
            {
                state.f = Math.Exp(state.x);
            }
            autogk.autogkresults(state, ref v, rep);
            exact = Math.Exp(b)-Math.Exp(a);
            eabs = Math.Abs(Math.Exp(b)-Math.Exp(a));
            if( rep.terminationtype<=0 )
            {
                simpleerrors = true;
            }
            else
            {
                simpleerrors = simpleerrors | (double)(Math.Abs(exact-v))>(double)(errtol*eabs);
            }
            
            //
            // Simple test: integral(exp(x),+-1,+-2), XWidth=0.1
            //
            a = (2*math.randominteger(2)-1)*1.0;
            b = (2*math.randominteger(2)-1)*2.0;
            autogk.autogksmoothw(a, b, 0.1, state);
            while( autogk.autogkiteration(state) )
            {
                state.f = Math.Exp(state.x);
            }
            autogk.autogkresults(state, ref v, rep);
            exact = Math.Exp(b)-Math.Exp(a);
            eabs = Math.Abs(Math.Exp(b)-Math.Exp(a));
            if( rep.terminationtype<=0 )
            {
                simpleerrors = true;
            }
            else
            {
                simpleerrors = simpleerrors | (double)(Math.Abs(exact-v))>(double)(errtol*eabs);
            }
            
            //
            // Simple test: integral(cos(100*x),0,2*pi), no maximum width requirements
            //
            a = 0;
            b = 2*Math.PI;
            autogk.autogksmooth(a, b, state);
            while( autogk.autogkiteration(state) )
            {
                state.f = Math.Cos(100*state.x);
            }
            autogk.autogkresults(state, ref v, rep);
            exact = 0;
            eabs = 4;
            if( rep.terminationtype<=0 )
            {
                simpleerrors = true;
            }
            else
            {
                simpleerrors = simpleerrors | (double)(Math.Abs(exact-v))>(double)(errtol*eabs);
            }
            
            //
            // Simple test: integral(cos(100*x),0,2*pi), XWidth=0.3
            //
            a = 0;
            b = 2*Math.PI;
            autogk.autogksmoothw(a, b, 0.3, state);
            while( autogk.autogkiteration(state) )
            {
                state.f = Math.Cos(100*state.x);
            }
            autogk.autogkresults(state, ref v, rep);
            exact = 0;
            eabs = 4;
            if( rep.terminationtype<=0 )
            {
                simpleerrors = true;
            }
            else
            {
                simpleerrors = simpleerrors | (double)(Math.Abs(exact-v))>(double)(errtol*eabs);
            }
            
            //
            // singular problem on [a,b] = [0.1, 0.5]
            //     f2(x) = (1+x)*(b-x)^alpha, -1 < alpha < 1
            //
            for(pkind=0; pkind<=6; pkind++)
            {
                a = 0.1;
                b = 0.5;
                if( pkind==0 )
                {
                    alpha = -0.9;
                }
                if( pkind==1 )
                {
                    alpha = -0.5;
                }
                if( pkind==2 )
                {
                    alpha = -0.1;
                }
                if( pkind==3 )
                {
                    alpha = 0.0;
                }
                if( pkind==4 )
                {
                    alpha = 0.1;
                }
                if( pkind==5 )
                {
                    alpha = 0.5;
                }
                if( pkind==6 )
                {
                    alpha = 0.9;
                }
                
                //
                // f1(x) = (1+x)*(x-a)^alpha, -1 < alpha < 1
                // 1. use singular integrator for [a,b]
                // 2. use singular integrator for [b,a]
                //
                exact = Math.Pow(b-a, alpha+2)/(alpha+2)+(1+a)*Math.Pow(b-a, alpha+1)/(alpha+1);
                eabs = Math.Abs(exact);
                autogk.autogksingular(a, b, alpha, 0.0, state);
                while( autogk.autogkiteration(state) )
                {
                    if( (double)(state.xminusa)<(double)(0.01) )
                    {
                        state.f = Math.Pow(state.xminusa, alpha)*(1+state.x);
                    }
                    else
                    {
                        state.f = Math.Pow(state.x-a, alpha)*(1+state.x);
                    }
                }
                autogk.autogkresults(state, ref v, rep);
                if( rep.terminationtype<=0 )
                {
                    sngenderrors = true;
                }
                else
                {
                    sngenderrors = sngenderrors | (double)(Math.Abs(v-exact))>(double)(errtol*eabs);
                }
                autogk.autogksingular(b, a, 0.0, alpha, state);
                while( autogk.autogkiteration(state) )
                {
                    if( (double)(state.bminusx)>(double)(-0.01) )
                    {
                        state.f = Math.Pow(-state.bminusx, alpha)*(1+state.x);
                    }
                    else
                    {
                        state.f = Math.Pow(state.x-a, alpha)*(1+state.x);
                    }
                }
                autogk.autogkresults(state, ref v, rep);
                if( rep.terminationtype<=0 )
                {
                    sngenderrors = true;
                }
                else
                {
                    sngenderrors = sngenderrors | (double)(Math.Abs(-v-exact))>(double)(errtol*eabs);
                }
                
                //
                // f1(x) = (1+x)*(b-x)^alpha, -1 < alpha < 1
                // 1. use singular integrator for [a,b]
                // 2. use singular integrator for [b,a]
                //
                exact = (1+b)*Math.Pow(b-a, alpha+1)/(alpha+1)-Math.Pow(b-a, alpha+2)/(alpha+2);
                eabs = Math.Abs(exact);
                autogk.autogksingular(a, b, 0.0, alpha, state);
                while( autogk.autogkiteration(state) )
                {
                    if( (double)(state.bminusx)<(double)(0.01) )
                    {
                        state.f = Math.Pow(state.bminusx, alpha)*(1+state.x);
                    }
                    else
                    {
                        state.f = Math.Pow(b-state.x, alpha)*(1+state.x);
                    }
                }
                autogk.autogkresults(state, ref v, rep);
                if( rep.terminationtype<=0 )
                {
                    sngenderrors = true;
                }
                else
                {
                    sngenderrors = sngenderrors | (double)(Math.Abs(v-exact))>(double)(errtol*eabs);
                }
                autogk.autogksingular(b, a, alpha, 0.0, state);
                while( autogk.autogkiteration(state) )
                {
                    if( (double)(state.xminusa)>(double)(-0.01) )
                    {
                        state.f = Math.Pow(-state.xminusa, alpha)*(1+state.x);
                    }
                    else
                    {
                        state.f = Math.Pow(b-state.x, alpha)*(1+state.x);
                    }
                }
                autogk.autogkresults(state, ref v, rep);
                if( rep.terminationtype<=0 )
                {
                    sngenderrors = true;
                }
                else
                {
                    sngenderrors = sngenderrors | (double)(Math.Abs(-v-exact))>(double)(errtol*eabs);
                }
            }
            
            //
            // end
            //
            waserrors = simpleerrors | sngenderrors;
            if( !silent )
            {
                System.Console.Write("TESTING AUTOGK");
                System.Console.WriteLine();
                System.Console.Write("INTEGRATION WITH GIVEN ACCURACY:          ");
                if( simpleerrors | sngenderrors )
                {
                    System.Console.Write("FAILED");
                    System.Console.WriteLine();
                }
                else
                {
                    System.Console.Write("OK");
                    System.Console.WriteLine();
                }
                System.Console.Write("* SIMPLE PROBLEMS:                        ");
                if( simpleerrors )
                {
                    System.Console.Write("FAILED");
                    System.Console.WriteLine();
                }
                else
                {
                    System.Console.Write("OK");
                    System.Console.WriteLine();
                }
                System.Console.Write("* SINGULAR PROBLEMS (ENDS OF INTERVAL):   ");
                if( sngenderrors )
                {
                    System.Console.Write("FAILED");
                    System.Console.WriteLine();
                }
                else
                {
                    System.Console.Write("OK");
                    System.Console.WriteLine();
                }
                if( waserrors )
                {
                    System.Console.Write("TEST FAILED");
                    System.Console.WriteLine();
                }
                else
                {
                    System.Console.Write("TEST PASSED");
                    System.Console.WriteLine();
                }
                System.Console.WriteLine();
                System.Console.WriteLine();
            }
            result = !waserrors;
            return result;
        }
Example #5
0
 public autogkreport(autogk.autogkreport obj)
 {
     _innerobj = obj;
 }
Example #6
0
 public autogkreport()
 {
     _innerobj = new autogk.autogkreport();
 }
Example #7
0
        /*************************************************************************
        This function  calculates  arc length, i.e. length of  curve  between  t=a
        and t=b.

        INPUT PARAMETERS:
            P   -   parametric spline interpolant
            A,B -   parameter values corresponding to arc ends:
                    * B>A will result in positive length returned
                    * B<A will result in negative length returned

        RESULT:
            length of arc starting at T=A and ending at T=B.


          -- ALGLIB PROJECT --
             Copyright 30.05.2010 by Bochkanov Sergey
        *************************************************************************/
        public static double pspline3arclength(pspline3interpolant p,
            double a,
            double b)
        {
            double result = 0;
            autogk.autogkstate state = new autogk.autogkstate();
            autogk.autogkreport rep = new autogk.autogkreport();
            double sx = 0;
            double dsx = 0;
            double d2sx = 0;
            double sy = 0;
            double dsy = 0;
            double d2sy = 0;
            double sz = 0;
            double dsz = 0;
            double d2sz = 0;

            autogk.autogksmooth(a, b, state);
            while( autogk.autogkiteration(state) )
            {
                spline1d.spline1ddiff(p.x, state.x, ref sx, ref dsx, ref d2sx);
                spline1d.spline1ddiff(p.y, state.x, ref sy, ref dsy, ref d2sy);
                spline1d.spline1ddiff(p.z, state.x, ref sz, ref dsz, ref d2sz);
                state.f = apserv.safepythag3(dsx, dsy, dsz);
            }
            autogk.autogkresults(state, ref result, rep);
            alglib.ap.assert(rep.terminationtype>0, "PSpline3ArcLength: internal error!");
            return result;
        }