Exemplo n.º 1
0
 public static void QK21Rule(QuadFunction f, double a, double b, out double result, out double abserr, out double resabs, out double resasc)
 {
     double[] fv1 = new double[11];
     double[] fv2 = new double[11];
     QuadKronrod.IntegrationQK(11, xgk, wg, wgk, fv1, fv2, f, a, b, out result, out abserr, out resabs, out resasc);
 }
Exemplo n.º 2
0
 public static void QK21Rule(QuadFunction f, double a, double b, out double result, out double abserr, out double resabs, out double resasc)
 {
     double[] fv1 = new double[11];
     double[] fv2 = new double[11];
     QuadKronrod.IntegrationQK(11, xgk, wg, wgk, fv1, fv2, f, a, b, out result, out abserr, out resabs, out resasc);
 }
Exemplo n.º 3
0
        public static void IntegrationQK(int n, double[] xgk, double[] wg, double[] wgk, double[] fv1, double[] fv2,
                                         QuadFunction f, double a, double b, out double result, out double abserr, out double resabs, out double resasc)
        {
            double center          = 0.5 * (a + b);
            double half_length     = 0.5 * (b - a);
            double abs_half_length = Math.Abs(half_length);
            double f_center        = f(center);

            double result_gauss   = 0;
            double result_kronrod = f_center * wgk[n - 1];

            double result_abs = Math.Abs(result_kronrod);
            double result_asc = 0;
            double mean = 0, err = 0;

            if (n % 2 == 0)
            {
                result_gauss = f_center * wg[n / 2 - 1];
            }

            for (int j = 0; j < (n - 1) / 2; j++)
            {
                int    jtw      = j * 2 + 1;
                double abscissa = half_length * xgk[jtw];
                double fval1    = f(center - abscissa);
                double fval2    = f(center + abscissa);
                double fsum     = fval1 + fval2;

                fv1[jtw]        = fval1;
                fv2[jtw]        = fval2;
                result_gauss   += wg[j] * fsum;
                result_kronrod += wgk[jtw] * fsum;
                result_abs     += wgk[jtw] * (Math.Abs(fval1) + Math.Abs(fval2));
            }

            for (int j = 0; j < n / 2; j++)
            {
                int    jtwm1    = j * 2;
                double abscissa = half_length * xgk[jtwm1];
                double fval1    = f(center - abscissa);
                double fval2    = f(center + abscissa);

                fv1[jtwm1]      = fval1;
                fv2[jtwm1]      = fval2;
                result_kronrod += wgk[jtwm1] * (fval1 + fval2);
                result_abs     += wgk[jtwm1] * (Math.Abs(fval1) + Math.Abs(fval2));
            }

            mean       = result_kronrod * 0.5;
            result_asc = wgk[n - 1] * Math.Abs(f_center - mean);

            for (int j = 0; j < n - 1; j++)
            {
                result_asc += wgk[j] * (Math.Abs(fv1[j] - mean) + Math.Abs(fv2[j] - mean));
            }

            /* scale by the width of the integration region */

            err = (result_kronrod - result_gauss) * half_length;

            result_kronrod *= half_length;
            result_abs     *= abs_half_length;
            result_asc     *= abs_half_length;

            result = result_kronrod;
            resabs = result_abs;
            resasc = result_asc;
            abserr = Error.RescaleError(err, result_abs, result_asc);
        }
        public static void IntegrationQK(int n, double[] xgk, double[] wg, double[] wgk, double[] fv1, double[] fv2,
            QuadFunction f, double a, double b, out double result, out double abserr, out double resabs, out double resasc)
        {
            double center = 0.5 * (a + b);
            double half_length = 0.5 * (b - a);
            double abs_half_length = Math.Abs(half_length);
            double f_center = f(center);

            double result_gauss = 0;
            double result_kronrod = f_center * wgk[n - 1];

            double result_abs = Math.Abs(result_kronrod);
            double result_asc = 0;
            double mean = 0, err = 0;

            if (n % 2 == 0) {
                result_gauss = f_center * wg[n / 2 - 1];
            }

            for (int j = 0; j < (n - 1) / 2; j++) {
                int jtw = j * 2 + 1;
                double abscissa = half_length * xgk[jtw];
                double fval1 = f(center - abscissa);
                double fval2 = f(center + abscissa);
                double fsum = fval1 + fval2;

                fv1[jtw] = fval1;
                fv2[jtw] = fval2;
                result_gauss += wg[j] * fsum;
                result_kronrod += wgk[jtw] * fsum;
                result_abs += wgk[jtw] * (Math.Abs(fval1) + Math.Abs(fval2));
            }

            for (int j = 0; j < n / 2; j++) {
                int jtwm1 = j * 2;
                double abscissa = half_length * xgk[jtwm1];
                double fval1 = f(center - abscissa);
                double fval2 = f(center + abscissa);

                fv1[jtwm1] = fval1;
                fv2[jtwm1] = fval2;
                result_kronrod += wgk[jtwm1] * (fval1 + fval2);
                result_abs += wgk[jtwm1] * (Math.Abs(fval1) + Math.Abs(fval2));
            }

            mean = result_kronrod * 0.5;
            result_asc = wgk[n - 1] * Math.Abs(f_center - mean);

            for (int j = 0; j < n - 1; j++) {
                result_asc += wgk[j] * (Math.Abs(fv1[j] - mean) + Math.Abs(fv2[j] - mean));
            }

            /* scale by the width of the integration region */

            err = (result_kronrod - result_gauss) * half_length;

            result_kronrod *= half_length;
            result_abs *= abs_half_length;
            result_asc *= abs_half_length;

            result = result_kronrod;
            resabs = result_abs;
            resasc = result_asc;
            abserr = Error.RescaleError(err, result_abs, result_asc);
        }
Exemplo n.º 5
0
        static void multirunQuadratic(int runs)
        {
            List <string> edata = new List <string> ();
            List <string> ddata = new List <string> ();
            int           seed  = Int32.Parse(Prompt("What seed do you want? (32 bit int only)"));

            for (int z = 0; z < runs; z++)
            {
                string[]  total     = File.ReadAllLines("AllData.txt");
                string[]  fakes     = File.ReadAllLines("FakeData.txt");
                string[]  reals     = File.ReadAllLines("RealData.txt");
                string[]  corrected = new string[total.Length];
                int[]     indexes   = new int[fakes.Length];
                Stopwatch eWatch    = new Stopwatch();

                eWatch.Start();
                // Console.Write(indexes[1]);
                Random       rand = new Random(seed);
                QuadFunction fun  = new QuadFunction(rand.Next(1000), rand.Next(1000), rand.Next(1000));
                // Conso/le.WriteLine("Fakes: " + fakes.Length + "\nIndexes:" + indexes.Length);
                for (int i = 0; i < fakes.Length; i++)
                {
                    int index = SafeIndex(indexes, Math.Abs(fun.Evaluate(i * seed) % total.Length), total.Length);
                    // Console.WriteLine(index);
                    corrected[index] = fakes[i];
                    // Console.WriteLine(i);
                    indexes[i] = index;
                }
                int r = 0;
                for (int i = 0; i < total.Length; i++)
                {
                    if (r >= reals.Length)
                    {
                        Console.Write("ERROR\n");
                        break;
                    }
                    if (!indexes.Contains(i))
                    {
                        corrected[i] = reals[r];
                        r++;
                    }
                }
                eWatch.Stop();
                // File.AppendAllText ("Info.txt", "\nTime for Quad Encryption: " + eWatch.ElapsedMilliseconds + "ms");
                bool dupsFound = false;

                for (int i = 0; i < total.Length; i++)
                {
                    if (corrected[i] == null)
                    {
                        dupsFound = true;
                        break;
                    }
                    if (corrected[i].Trim().Equals(""))
                    {
                        dupsFound = true;
                        break;
                    }
                }
                if (dupsFound)
                {
                    Console.WriteLine("FOUND DUPLICATES");
                }
                Stopwatch dWatch = new Stopwatch();
                dWatch.Start();
                string[] eReals = new string[reals.Length];
                for (int i = 0; i < fakes.Length; i++)
                {
                    int index = SafeIndex(indexes, Math.Abs(fun.Evaluate(i * seed) % total.Length), total.Length);
                    corrected[index] = "";
                }
                int rr = 0;

                for (int i = 0; i < total.Length; i++)
                {
                    if (rr >= eReals.Length)
                    {
                        break;
                    }
                    if (corrected[i] != null && !corrected[i].Trim().Equals(""))
                    {
                        eReals[rr] = corrected[i];
                        rr++;
                    }
                }
                dWatch.Stop();
                ddata.Add((z + 1) + "," + dWatch.ElapsedMilliseconds);
                edata.Add((z + 1) + "," + eWatch.ElapsedMilliseconds);
            }
            File.WriteAllLines("QuadEncryptionRun.csv", edata.ToArray());
            AverageTaker("QuadEncryptionRun.csv");
            File.WriteAllLines("QuadDecryptionRun.csv", ddata.ToArray());
            AverageTaker("QuadDecryptionRun.csv");
        }
        public static void Integrate(QuadFunction f, double a, double b, double epsabs, double epsrel, int limit, out double result, out double abserr, IntegrationRule q)
        {
            double area, errsum;
            double result0, abserr0, resabs0, resasc0;
            double tolerance;
            int iteration = 0;
            int roundoff_type1 = 0, roundoff_type2 = 0, error_type = 0;

            double round_off;

            /* Initialize results */
            IntegrationWorkspace workspace = new IntegrationWorkspace(limit, a, b);

            result = 0;
            abserr = 0;

            if (epsabs <= 0 && (epsrel < 50 * QuadConst.dbl_eps || epsrel < 0.5e-28)) {
                throw new ArgumentException("Tolerance cannot be achieved with given epsabs and epsrel");
            }

            /* perform the first integration */

            q(f, a, b, out result0, out abserr0, out resabs0, out resasc0);

            workspace.SetInitialResult(result0, abserr0);

            /* Test on accuracy */

            tolerance = Math.Max(epsabs, epsrel * Math.Abs(result0));

            /* need IEEE rounding here to match original quadpack behavior */

            round_off = 50 * QuadConst.dbl_eps * resabs0;

            if (abserr0 <= round_off && abserr0 > tolerance) {
                result = result0;
                abserr = abserr0;

                throw new InvalidOperationException("cannot reach tolerance because of roundoff error on first attempt");
            }
            else if ((abserr0 <= tolerance && abserr0 != resasc0) || abserr0 == 0.0) {
                result = result0;
                abserr = abserr0;

                //return GSL_SUCCESS;
                return;
            }
            else if (limit == 1) {
                result = result0;
                abserr = abserr0;

                throw new InvalidOperationException("a maximum of one iteration was insufficient");
            }

            area = result0;
            errsum = abserr0;

            iteration = 1;

            do {
                double a1, b1, a2, b2;
                double a_i, b_i, r_i, e_i;
                double area1 = 0, area2 = 0, area12 = 0;
                double error1 = 0, error2 = 0, error12 = 0;
                double resasc1, resasc2;
                double resabs1, resabs2;

                /* Bisect the subinterval with the largest error estimate */

                workspace.Retrieve(out a_i, out b_i, out r_i, out e_i);

                a1 = a_i;
                b1 = 0.5 * (a_i + b_i);
                a2 = b1;
                b2 = b_i;

                q(f, a1, b1, out area1, out error1, out resabs1, out resasc1);
                q(f, a2, b2, out area2, out error2, out resabs2, out resasc2);

                area12 = area1 + area2;
                error12 = error1 + error2;

                errsum += (error12 - e_i);
                area += area12 - r_i;

                if (resasc1 != error1 && resasc2 != error2) {
                    double delta = r_i - area12;

                    if (Math.Abs(delta) <= 1.0e-5 * Math.Abs(area12) && error12 >= 0.99 * e_i) {
                        roundoff_type1++;
                    }
                    if (iteration >= 10 && error12 > e_i) {
                        roundoff_type2++;
                    }
                }

                tolerance = Math.Max(epsabs, epsrel * Math.Abs(area));

                if (errsum > tolerance) {
                    if (roundoff_type1 >= 6 || roundoff_type2 >= 20) {
                        error_type = 2;   /* round off error */
                    }

                    /* set error flag in the case of bad integrand behaviour at
                         a point of the integration range */

                    if (workspace.SubintervalTooSmall(a1, a2, b2)) {
                        error_type = 3;
                    }
                }

                workspace.Update(a1, b1, area1, error1, a2, b2, area2, error2);

                workspace.Retrieve(out a_i, out b_i, out r_i, out e_i);

                iteration++;

            }
            while (iteration < limit && (error_type == 0) && errsum > tolerance);

            result = workspace.SumResults();
            abserr = errsum;

            if (errsum <= tolerance) {
                //return GSL_SUCCESS;
            }
            else if (error_type == 2) {
                throw new InvalidOperationException("roundoff error prevents tolerance from being achieved");
                /*GSL_ERROR ("roundoff error prevents tolerance from being achieved",
                                     GSL_EROUND);*/
            }
            else if (error_type == 3) {
                throw new InvalidOperationException("bad integrand found in the integration interval");
                /*GSL_ERROR ("bad integrand behavior found in the integration interval",
                                     GSL_ESING);*/
            }
            else if (iteration == limit) {
                //GSL_ERROR ("maximum number of subdivisions reached", GSL_EMAXITER);
                throw new InvalidOperationException("maximum number of subdivisions reached");
            }
            else {
                //GSL_ERROR ("could not integrate function", GSL_EFAILED);
                throw new InvalidOperationException("could not integrate function");
            }
        }
Exemplo n.º 7
0
        public static void Integrate(QuadFunction f, double a, double b, double epsabs, double epsrel, int limit, out double result, out double abserr, IntegrationRule q)
        {
            double area, errsum;
            double result0, abserr0, resabs0, resasc0;
            double tolerance;
            int    iteration = 0;
            int    roundoff_type1 = 0, roundoff_type2 = 0, error_type = 0;

            double round_off;

            /* Initialize results */
            IntegrationWorkspace workspace = new IntegrationWorkspace(limit, a, b);

            result = 0;
            abserr = 0;

            if (epsabs <= 0 && (epsrel < 50 * QuadConst.dbl_eps || epsrel < 0.5e-28))
            {
                throw new ArgumentException("Tolerance cannot be achieved with given epsabs and epsrel");
            }

            /* perform the first integration */

            q(f, a, b, out result0, out abserr0, out resabs0, out resasc0);

            workspace.SetInitialResult(result0, abserr0);

            /* Test on accuracy */

            tolerance = Math.Max(epsabs, epsrel * Math.Abs(result0));

            /* need IEEE rounding here to match original quadpack behavior */

            round_off = 50 * QuadConst.dbl_eps * resabs0;

            if (abserr0 <= round_off && abserr0 > tolerance)
            {
                result = result0;
                abserr = abserr0;

                throw new InvalidOperationException("cannot reach tolerance because of roundoff error on first attempt");
            }
            else if ((abserr0 <= tolerance && abserr0 != resasc0) || abserr0 == 0.0)
            {
                result = result0;
                abserr = abserr0;

                //return GSL_SUCCESS;
                return;
            }
            else if (limit == 1)
            {
                result = result0;
                abserr = abserr0;

                throw new InvalidOperationException("a maximum of one iteration was insufficient");
            }

            area   = result0;
            errsum = abserr0;

            iteration = 1;

            do
            {
                double a1, b1, a2, b2;
                double a_i, b_i, r_i, e_i;
                double area1 = 0, area2 = 0, area12 = 0;
                double error1 = 0, error2 = 0, error12 = 0;
                double resasc1, resasc2;
                double resabs1, resabs2;

                /* Bisect the subinterval with the largest error estimate */

                workspace.Retrieve(out a_i, out b_i, out r_i, out e_i);

                a1 = a_i;
                b1 = 0.5 * (a_i + b_i);
                a2 = b1;
                b2 = b_i;

                q(f, a1, b1, out area1, out error1, out resabs1, out resasc1);
                q(f, a2, b2, out area2, out error2, out resabs2, out resasc2);

                area12  = area1 + area2;
                error12 = error1 + error2;

                errsum += (error12 - e_i);
                area   += area12 - r_i;

                if (resasc1 != error1 && resasc2 != error2)
                {
                    double delta = r_i - area12;

                    if (Math.Abs(delta) <= 1.0e-5 * Math.Abs(area12) && error12 >= 0.99 * e_i)
                    {
                        roundoff_type1++;
                    }
                    if (iteration >= 10 && error12 > e_i)
                    {
                        roundoff_type2++;
                    }
                }

                tolerance = Math.Max(epsabs, epsrel * Math.Abs(area));

                if (errsum > tolerance)
                {
                    if (roundoff_type1 >= 6 || roundoff_type2 >= 20)
                    {
                        error_type = 2;                           /* round off error */
                    }

                    /* set error flag in the case of bad integrand behaviour at
                     *       a point of the integration range */

                    if (workspace.SubintervalTooSmall(a1, a2, b2))
                    {
                        error_type = 3;
                    }
                }

                workspace.Update(a1, b1, area1, error1, a2, b2, area2, error2);

                workspace.Retrieve(out a_i, out b_i, out r_i, out e_i);

                iteration++;
            }while (iteration < limit && (error_type == 0) && errsum > tolerance);

            result = workspace.SumResults();
            abserr = errsum;

            if (errsum <= tolerance)
            {
                //return GSL_SUCCESS;
            }
            else if (error_type == 2)
            {
                throw new InvalidOperationException("roundoff error prevents tolerance from being achieved");

                /*GSL_ERROR ("roundoff error prevents tolerance from being achieved",
                 *                                       GSL_EROUND);*/
            }
            else if (error_type == 3)
            {
                throw new InvalidOperationException("bad integrand found in the integration interval");

                /*GSL_ERROR ("bad integrand behavior found in the integration interval",
                 *                                       GSL_ESING);*/
            }
            else if (iteration == limit)
            {
                //GSL_ERROR ("maximum number of subdivisions reached", GSL_EMAXITER);
                throw new InvalidOperationException("maximum number of subdivisions reached");
            }
            else
            {
                //GSL_ERROR ("could not integrate function", GSL_EFAILED);
                throw new InvalidOperationException("could not integrate function");
            }
        }