C#,数值计算——高斯权重(GaussianWeights)的计算方法与源程序

news2024/11/16 18:06:03

 

1 文本格式

using System;

namespace Legalsoft.Truffer
{
    public class GaussianWeights
    {
        private static double[] x = {
            0.1488743389816312, 0.4333953941292472, 0.6794095682990244,
            0.8650633666889845, 0.9739065285171717
        };
        private static double[] w = {
            0.2955242247147529, 0.2692667193099963, 0.2190863625159821,
            0.1494513491505806, 0.0666713443086881
        };

        public GaussianWeights()
        {
        }


        /*
        public static double qgaus(UniVarRealValueFun f1, double a, double b)
        {
            //func = f1;
            return qgaus(a,b);
        }
        */
        /// <summary>
        /// Returns the integral of the function or functor func between a and b, by
        /// ten-point Gauss-Legendre integration: the function is evaluated exactly
        /// ten times at interior points in the range of integration.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static double qgaus(UniVarRealValueFun func, double a, double b)
        {
            double xm = 0.5 * (b + a);
            double xr = 0.5 * (b - a);
            double s = 0;
            for (int j = 0; j < 5; j++)
            {
                double dx = xr * x[j];
                s += w[j] * (func.funk(xm + dx) + func.funk(xm - dx));
            }
            return s *= xr;
        }

        /// <summary>
        /// Given the lower and upper limits of integration x1 and x2, this routine
        /// returns arrays x[0..n - 1] and w[0..n - 1] of length n, containing the
        /// abscissas and weights of the Gauss-Legendre n-point quadrature formula.
        /// </summary>
        /// <param name="x1"></param>
        /// <param name="x2"></param>
        /// <param name="x"></param>
        /// <param name="w"></param>
        public static void gauleg(double x1, double x2, double[] x, double[] w)
        {
            const double EPS = 1.0e-14;

            double z1;
            double pp;
            int n = x.Length;
            int m = (n + 1) / 2;
            double xm = 0.5 * (x2 + x1);
            double xl = 0.5 * (x2 - x1);
            for (int i = 0; i < m; i++)
            {
                double z = Math.Cos(3.141592654 * (i + 0.75) / (n + 0.5));
                do
                {
                    double p1 = 1.0;
                    double p2 = 0.0;
                    for (int j = 0; j < n; j++)
                    {
                        double p3 = p2;
                        p2 = p1;
                        p1 = ((2.0 * j + 1.0) * z * p2 - j * p3) / (j + 1);
                    }
                    pp = n * (z * p1 - p2) / (z * z - 1.0);
                    z1 = z;
                    z = z1 - p1 / pp;
                } while (Math.Abs(z - z1) > EPS);
                x[i] = xm - xl * z;
                x[n - 1 - i] = xm + xl * z;
                w[i] = 2.0 * xl / ((1.0 - z * z) * pp * pp);
                w[n - 1 - i] = w[i];
            }
        }

        /// <summary>
        /// Given alf, the parameter a of the Laguerre polynomials, this routine
        /// returns arrays x[0..n - 1] and w[0..n - 1] containing the abscissas and weights
        /// of the n-point Gauss-Laguerre quadrature formula.The smallest abscissa is
        /// returned in x[0], the largest in x[n - 1].
        /// </summary>
        /// <param name="x"></param>
        /// <param name="w"></param>
        /// <param name="alf"></param>
        /// <exception cref="Exception"></exception>
        public static void gaulag(double[] x, double[] w, double alf)
        {
            const int MAXIT = 10;
            const double EPS = 1.0e-14;

            double p2 = 0.0;
            double pp = 0.0;
            double z = 0.0;
            int n = x.Length;
            for (int i = 0; i < n; i++)
            {
                if (i == 0)
                {
                    z = (1.0 + alf) * (3.0 + 0.92 * alf) / (1.0 + 2.4 * n + 1.8 * alf);
                }
                else if (i == 1)
                {
                    z += (15.0 + 6.25 * alf) / (1.0 + 0.9 * alf + 2.5 * n);
                }
                else
                {
                    int ai = i - 1;
                    z += ((1.0 + 2.55 * ai) / (1.9 * ai) + 1.26 * ai * alf / (1.0 + 3.5 * ai)) * (z - x[i - 2]) / (1.0 + 0.3 * alf);
                }
                int its = 0;
                for (; its < MAXIT; its++)
                {
                    double p1 = 1.0;
                    p2 = 0.0;
                    for (int j = 0; j < n; j++)
                    {
                        double p3 = p2;
                        p2 = p1;
                        p1 = ((2 * j + 1 + alf - z) * p2 - (j + alf) * p3) / (j + 1);
                    }
                    pp = (n * p1 - (n + alf) * p2) / z;
                    double z1 = z;
                    z = z1 - p1 / pp;
                    if (Math.Abs(z - z1) <= EPS)
                    {
                        break;
                    }
                }
                if (its >= MAXIT)
                {
                    throw new Exception("too many iterations in gaulag");
                }
                x[i] = z;
                w[i] = -Math.Exp(Globals.gammln(alf + n) - Globals.gammln((double)n)) / (pp * n * p2);
            }
        }

        /// <summary>
        /// This routine returns arrays x[0..n - 1] and w[0..n - 1] containing the
        /// abscissas and weights of the n-point Gauss-Hermite quadrature formula.
        /// The largest abscissa is returned in x[0], the most negative in x[n - 1].
        /// </summary>
        /// <param name="x"></param>
        /// <param name="w"></param>
        /// <exception cref="Exception"></exception>
        public static void gauher(double[] x, double[] w)
        {
            const double EPS = 1.0e-14;
            const double PIM4 = 0.7511255444649425;
            const int MAXIT = 10;
            double p2 = 0.0;
            double pp = 0.0;
            double z = 0.0;
            int n = x.Length;
            int m = (n + 1) / 2;
            for (int i = 0; i < m; i++)
            {
                if (i == 0)
                {
                    z = Math.Sqrt((double)(2 * n + 1)) - 1.85575 * Math.Pow((double)(2 * n + 1), -0.16667);
                }
                else if (i == 1)
                {
                    z -= 1.14 * Math.Pow((double)n, 0.426) / z;
                }
                else if (i == 2)
                {
                    z = 1.86 * z - 0.86 * x[0];
                }
                else if (i == 3)
                {
                    z = 1.91 * z - 0.91 * x[1];
                }
                else
                {
                    z = 2.0 * z - x[i - 2];
                }

                int its = 0;
                for (; its < MAXIT; its++)
                {
                    double p1 = PIM4;
                    p2 = 0.0;
                    for (int j = 0; j < n; j++)
                    {
                        double p3 = p2;
                        p2 = p1;
                        p1 = z * Math.Sqrt(2.0 / (j + 1)) * p2 - Math.Sqrt((double)j / (j + 1)) * p3;
                    }
                    pp = Math.Sqrt((double)(2 * n)) * p2;
                    double z1 = z;
                    z = z1 - p1 / pp;
                    if (Math.Abs(z - z1) <= EPS)
                    {
                        break;
                    }
                }
                if (its >= MAXIT)
                {
                    throw new Exception("too many iterations in gauher");
                }
                x[i] = z;
                x[n - 1 - i] = -z;
                w[i] = 2.0 / (pp * pp);
                w[n - 1 - i] = w[i];
            }
        }

        /// <summary>
        /// Given alf and bet, the parameters a and b of the Jacobi polynomials, this
        /// routine returns arrays x[0..n - 1] and w[0..n - 1] containing the abscissas and
        /// weights of the n-point Gauss-Jacobi quadrature formula.The largest
        /// abscissa is returned in x[0], the smallest in x[n - 1].
        /// </summary>
        /// <param name="x"></param>
        /// <param name="w"></param>
        /// <param name="alf"></param>
        /// <param name="bet"></param>
        /// <exception cref="Exception"></exception>
        public static void gaujac(double[] x, double[] w, double alf, double bet)
        {
            const int MAXIT = 10;
            const double EPS = 1.0e-14;
            double p2 = 0.0;
            double pp = 0.0;
            double temp = 0.0;
            double z = 0.0;
            int n = x.Length;
            for (int i = 0; i < n; i++)
            {
                if (i == 0)
                {
                    double an = alf / n;
                    double bn = bet / n;
                    double r1 = (1.0 + alf) * (2.78 / (4.0 + n * n) + 0.768 * an / n);
                    double r2 = 1.0 + 1.48 * an + 0.96 * bn + 0.452 * an * an + 0.83 * an * bn;
                    z = 1.0 - r1 / r2;
                }
                else if (i == 1)
                {
                    double r1 = (4.1 + alf) / ((1.0 + alf) * (1.0 + 0.156 * alf));
                    double r2 = 1.0 + 0.06 * (n - 8.0) * (1.0 + 0.12 * alf) / n;
                    double r3 = 1.0 + 0.012 * bet * (1.0 + 0.25 * Math.Abs(alf)) / n;
                    z -= (1.0 - z) * r1 * r2 * r3;
                }
                else if (i == 2)
                {
                    double r1 = (1.67 + 0.28 * alf) / (1.0 + 0.37 * alf);
                    double r2 = 1.0 + 0.22 * (n - 8.0) / n;
                    double r3 = 1.0 + 8.0 * bet / ((6.28 + bet) * n * n);
                    z -= (x[0] - z) * r1 * r2 * r3;
                }
                else if (i == n - 2)
                {
                    double r1 = (1.0 + 0.235 * bet) / (0.766 + 0.119 * bet);
                    double r2 = 1.0 / (1.0 + 0.639 * (n - 4.0) / (1.0 + 0.71 * (n - 4.0)));
                    double r3 = 1.0 / (1.0 + 20.0 * alf / ((7.5 + alf) * n * n));
                    z += (z - x[n - 4]) * r1 * r2 * r3;
                }
                else if (i == n - 1)
                {
                    double r1 = (1.0 + 0.37 * bet) / (1.67 + 0.28 * bet);
                    double r2 = 1.0 / (1.0 + 0.22 * (n - 8.0) / n);
                    double r3 = 1.0 / (1.0 + 8.0 * alf / ((6.28 + alf) * n * n));
                    z += (z - x[n - 3]) * r1 * r2 * r3;
                }
                else
                {
                    z = 3.0 * x[i - 1] - 3.0 * x[i - 2] + x[i - 3];
                }
                double alfbet = alf + bet;
                int its = 1;
                for (; its <= MAXIT; its++)
                {
                    temp = 2.0 + alfbet;
                    double p1 = (alf - bet + temp * z) / 2.0;
                    p2 = 1.0;
                    for (int j = 2; j <= n; j++)
                    {
                        double p3 = p2;
                        p2 = p1;
                        temp = 2 * j + alfbet;
                        double a = 2 * j * (j + alfbet) * (temp - 2.0);
                        double b = (temp - 1.0) * (alf * alf - bet * bet + temp * (temp - 2.0) * z);
                        double c = 2.0 * (j - 1 + alf) * (j - 1 + bet) * temp;
                        p1 = (b * p2 - c * p3) / a;
                    }
                    pp = (n * (alf - bet - temp * z) * p1 + 2.0 * (n + alf) * (n + bet) * p2) / (temp * (1.0 - z * z));
                    double z1 = z;
                    z = z1 - p1 / pp;
                    if (Math.Abs(z - z1) <= EPS)
                    {
                        break;
                    }
                }
                if (its > MAXIT)
                {
                    throw new Exception("too many iterations in gaujac");
                }
                x[i] = z;
                w[i] = Math.Exp(Globals.gammln(alf + n) + Globals.gammln(bet + n) - Globals.gammln(n + 1.0) - Globals.gammln(n + alfbet + 1.0)) * temp * Math.Pow(2.0, alfbet) / (pp * p2);
            }
        }

        /// <summary>
        /// Computes the abscissas and weights for a Gaussian quadrature formula from
        /// the Jacobi matrix.On input, a[0..n - 1] and b[0..n - 1] are the coefficients
        /// of the recurrence relation for the set of monic orthogonal polynomials. The
        /// quantity u0= S(a, b)W(x) dx is input as amu0.The abscissas x[0..n - 1] are
        /// returned in descending order, with the corresponding weights in w[0..n - 1].
        /// The arrays a and b are modified.Execution can be speeded up by modifying
        /// tqli and eigsrt to compute only the zeroth component of each eigenvector.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <param name="amu0"></param>
        /// <param name="x"></param>
        /// <param name="w"></param>
        public static void gaucof(double[] a, double[] b, double amu0, double[] x, double[] w)
        {
            int n = a.Length;
            for (int i = 0; i < n; i++)
            {
                if (i != 0)
                {
                    b[i] = Math.Sqrt(b[i]);
                }
            }
            Symmeig sym = new Symmeig(a, b);
            for (int i = 0; i < n; i++)
            {
                x[i] = sym.d[i];
                w[i] = amu0 * sym.z[0, i] * sym.z[0, i];
            }
        }

        /// <summary>
        /// Computes the abscissas and weights for a Gauss-Radau quadrature formula.On
        /// input, a[0..n - 1] and b[0..n - 1] are the coefficients of the recurrence
        /// relation for the set of monic orthogo- nal polynomials corresponding to the
        /// weight function. (b[0] is not referenced.) The quantity u0=S(a, b)W(x)dx is
        /// input as amu0.x1 is input as either endpoint of the interval.The
        /// abscissas x[0..n - 1] are returned in descending order, with the
        /// corresponding weights in w[0..n - 1]. The arrays a and b are modified.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <param name="amu0"></param>
        /// <param name="x1"></param>
        /// <param name="x"></param>
        /// <param name="w"></param>
        public static void radau(double[] a, double[] b, double amu0, double x1, double[] x, double[] w)
        {
            int n = a.Length;
            if (n == 1)
            {
                x[0] = x1;
                w[0] = amu0;
            }
            else
            {
                double p = x1 - a[0];
                double pm1 = 1.0;
                double p1 = p;
                for (int i = 1; i < n - 1; i++)
                {
                    p = (x1 - a[i]) * p1 - b[i] * pm1;
                    pm1 = p1;
                    p1 = p;
                }
                a[n - 1] = x1 - b[n - 1] * pm1 / p;
                gaucof(a,  b, amu0,  x,  w);
            }
        }

        /// <summary>
        /// Computes the abscissas and weights for a Gauss-Lobatto quadrature formula.
        /// On input, the vectors a[0..n - 1] and b[0..n - 1] are the coefficients of the
        /// recurrence relation for the set of monic orthogonal polynomials
        /// corresponding to the weight function. (b[0] is not referenced.) The
        /// quantity u0=S(a, b)W(x)dx is input as amu0.x1 amd xn are input as the
        /// endpoints of the interval.The abscissas x[0..n - 1] are returned in
        /// descending order, with the corresponding weights in w[0..n - 1]. The arrays a
        /// and b are modified.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <param name="amu0"></param>
        /// <param name="x1"></param>
        /// <param name="xn"></param>
        /// <param name="x"></param>
        /// <param name="w"></param>
        /// <exception cref="Exception"></exception>
        public static void lobatto(double[] a, double[] b, double amu0, double x1, double xn, double[] x, double[] w)
        {
            int n = a.Length;
            if (n <= 1)
            {
                throw new Exception("n must be bigger than 1 in lobatto");
            }
            double pl = x1 - a[0];
            double pr = xn - a[0];
            double pm1l = 1.0;
            double pm1r = 1.0;
            double p1l = pl;
            double p1r = pr;
            for (int i = 1; i < n - 1; i++)
            {
                pl = (x1 - a[i]) * p1l - b[i] * pm1l;
                pr = (xn - a[i]) * p1r - b[i] * pm1r;
                pm1l = p1l;
                pm1r = p1r;
                p1l = pl;
                p1r = pr;
            }
            double det = pl * pm1r - pr * pm1l;
            a[n - 1] = (x1 * pl * pm1r - xn * pr * pm1l) / det;
            b[n - 1] = (xn - x1) * pl * pr / det;
            gaucof(a,  b, amu0,  x,  w);
        }

    }
}
 

2 代码格式

using System;

namespace Legalsoft.Truffer
{
    public class GaussianWeights
    {
        private static double[] x = {
            0.1488743389816312, 0.4333953941292472, 0.6794095682990244,
            0.8650633666889845, 0.9739065285171717
        };
        private static double[] w = {
            0.2955242247147529, 0.2692667193099963, 0.2190863625159821,
            0.1494513491505806, 0.0666713443086881
        };

        public GaussianWeights()
        {
        }


        /*
        public static double qgaus(UniVarRealValueFun f1, double a, double b)
        {
            //func = f1;
            return qgaus(a,b);
        }
        */
        /// <summary>
        /// Returns the integral of the function or functor func between a and b, by
        /// ten-point Gauss-Legendre integration: the function is evaluated exactly
        /// ten times at interior points in the range of integration.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static double qgaus(UniVarRealValueFun func, double a, double b)
        {
            double xm = 0.5 * (b + a);
            double xr = 0.5 * (b - a);
            double s = 0;
            for (int j = 0; j < 5; j++)
            {
                double dx = xr * x[j];
                s += w[j] * (func.funk(xm + dx) + func.funk(xm - dx));
            }
            return s *= xr;
        }

        /// <summary>
        /// Given the lower and upper limits of integration x1 and x2, this routine
        /// returns arrays x[0..n - 1] and w[0..n - 1] of length n, containing the
        /// abscissas and weights of the Gauss-Legendre n-point quadrature formula.
        /// </summary>
        /// <param name="x1"></param>
        /// <param name="x2"></param>
        /// <param name="x"></param>
        /// <param name="w"></param>
        public static void gauleg(double x1, double x2, double[] x, double[] w)
        {
            const double EPS = 1.0e-14;

            double z1;
            double pp;
            int n = x.Length;
            int m = (n + 1) / 2;
            double xm = 0.5 * (x2 + x1);
            double xl = 0.5 * (x2 - x1);
            for (int i = 0; i < m; i++)
            {
                double z = Math.Cos(3.141592654 * (i + 0.75) / (n + 0.5));
                do
                {
                    double p1 = 1.0;
                    double p2 = 0.0;
                    for (int j = 0; j < n; j++)
                    {
                        double p3 = p2;
                        p2 = p1;
                        p1 = ((2.0 * j + 1.0) * z * p2 - j * p3) / (j + 1);
                    }
                    pp = n * (z * p1 - p2) / (z * z - 1.0);
                    z1 = z;
                    z = z1 - p1 / pp;
                } while (Math.Abs(z - z1) > EPS);
                x[i] = xm - xl * z;
                x[n - 1 - i] = xm + xl * z;
                w[i] = 2.0 * xl / ((1.0 - z * z) * pp * pp);
                w[n - 1 - i] = w[i];
            }
        }

        /// <summary>
        /// Given alf, the parameter a of the Laguerre polynomials, this routine
        /// returns arrays x[0..n - 1] and w[0..n - 1] containing the abscissas and weights
        /// of the n-point Gauss-Laguerre quadrature formula.The smallest abscissa is
        /// returned in x[0], the largest in x[n - 1].
        /// </summary>
        /// <param name="x"></param>
        /// <param name="w"></param>
        /// <param name="alf"></param>
        /// <exception cref="Exception"></exception>
        public static void gaulag(double[] x, double[] w, double alf)
        {
            const int MAXIT = 10;
            const double EPS = 1.0e-14;

            double p2 = 0.0;
            double pp = 0.0;
            double z = 0.0;
            int n = x.Length;
            for (int i = 0; i < n; i++)
            {
                if (i == 0)
                {
                    z = (1.0 + alf) * (3.0 + 0.92 * alf) / (1.0 + 2.4 * n + 1.8 * alf);
                }
                else if (i == 1)
                {
                    z += (15.0 + 6.25 * alf) / (1.0 + 0.9 * alf + 2.5 * n);
                }
                else
                {
                    int ai = i - 1;
                    z += ((1.0 + 2.55 * ai) / (1.9 * ai) + 1.26 * ai * alf / (1.0 + 3.5 * ai)) * (z - x[i - 2]) / (1.0 + 0.3 * alf);
                }
                int its = 0;
                for (; its < MAXIT; its++)
                {
                    double p1 = 1.0;
                    p2 = 0.0;
                    for (int j = 0; j < n; j++)
                    {
                        double p3 = p2;
                        p2 = p1;
                        p1 = ((2 * j + 1 + alf - z) * p2 - (j + alf) * p3) / (j + 1);
                    }
                    pp = (n * p1 - (n + alf) * p2) / z;
                    double z1 = z;
                    z = z1 - p1 / pp;
                    if (Math.Abs(z - z1) <= EPS)
                    {
                        break;
                    }
                }
                if (its >= MAXIT)
                {
                    throw new Exception("too many iterations in gaulag");
                }
                x[i] = z;
                w[i] = -Math.Exp(Globals.gammln(alf + n) - Globals.gammln((double)n)) / (pp * n * p2);
            }
        }

        /// <summary>
        /// This routine returns arrays x[0..n - 1] and w[0..n - 1] containing the
        /// abscissas and weights of the n-point Gauss-Hermite quadrature formula.
        /// The largest abscissa is returned in x[0], the most negative in x[n - 1].
        /// </summary>
        /// <param name="x"></param>
        /// <param name="w"></param>
        /// <exception cref="Exception"></exception>
        public static void gauher(double[] x, double[] w)
        {
            const double EPS = 1.0e-14;
            const double PIM4 = 0.7511255444649425;
            const int MAXIT = 10;
            double p2 = 0.0;
            double pp = 0.0;
            double z = 0.0;
            int n = x.Length;
            int m = (n + 1) / 2;
            for (int i = 0; i < m; i++)
            {
                if (i == 0)
                {
                    z = Math.Sqrt((double)(2 * n + 1)) - 1.85575 * Math.Pow((double)(2 * n + 1), -0.16667);
                }
                else if (i == 1)
                {
                    z -= 1.14 * Math.Pow((double)n, 0.426) / z;
                }
                else if (i == 2)
                {
                    z = 1.86 * z - 0.86 * x[0];
                }
                else if (i == 3)
                {
                    z = 1.91 * z - 0.91 * x[1];
                }
                else
                {
                    z = 2.0 * z - x[i - 2];
                }

                int its = 0;
                for (; its < MAXIT; its++)
                {
                    double p1 = PIM4;
                    p2 = 0.0;
                    for (int j = 0; j < n; j++)
                    {
                        double p3 = p2;
                        p2 = p1;
                        p1 = z * Math.Sqrt(2.0 / (j + 1)) * p2 - Math.Sqrt((double)j / (j + 1)) * p3;
                    }
                    pp = Math.Sqrt((double)(2 * n)) * p2;
                    double z1 = z;
                    z = z1 - p1 / pp;
                    if (Math.Abs(z - z1) <= EPS)
                    {
                        break;
                    }
                }
                if (its >= MAXIT)
                {
                    throw new Exception("too many iterations in gauher");
                }
                x[i] = z;
                x[n - 1 - i] = -z;
                w[i] = 2.0 / (pp * pp);
                w[n - 1 - i] = w[i];
            }
        }

        /// <summary>
        /// Given alf and bet, the parameters a and b of the Jacobi polynomials, this
        /// routine returns arrays x[0..n - 1] and w[0..n - 1] containing the abscissas and
        /// weights of the n-point Gauss-Jacobi quadrature formula.The largest
        /// abscissa is returned in x[0], the smallest in x[n - 1].
        /// </summary>
        /// <param name="x"></param>
        /// <param name="w"></param>
        /// <param name="alf"></param>
        /// <param name="bet"></param>
        /// <exception cref="Exception"></exception>
        public static void gaujac(double[] x, double[] w, double alf, double bet)
        {
            const int MAXIT = 10;
            const double EPS = 1.0e-14;
            double p2 = 0.0;
            double pp = 0.0;
            double temp = 0.0;
            double z = 0.0;
            int n = x.Length;
            for (int i = 0; i < n; i++)
            {
                if (i == 0)
                {
                    double an = alf / n;
                    double bn = bet / n;
                    double r1 = (1.0 + alf) * (2.78 / (4.0 + n * n) + 0.768 * an / n);
                    double r2 = 1.0 + 1.48 * an + 0.96 * bn + 0.452 * an * an + 0.83 * an * bn;
                    z = 1.0 - r1 / r2;
                }
                else if (i == 1)
                {
                    double r1 = (4.1 + alf) / ((1.0 + alf) * (1.0 + 0.156 * alf));
                    double r2 = 1.0 + 0.06 * (n - 8.0) * (1.0 + 0.12 * alf) / n;
                    double r3 = 1.0 + 0.012 * bet * (1.0 + 0.25 * Math.Abs(alf)) / n;
                    z -= (1.0 - z) * r1 * r2 * r3;
                }
                else if (i == 2)
                {
                    double r1 = (1.67 + 0.28 * alf) / (1.0 + 0.37 * alf);
                    double r2 = 1.0 + 0.22 * (n - 8.0) / n;
                    double r3 = 1.0 + 8.0 * bet / ((6.28 + bet) * n * n);
                    z -= (x[0] - z) * r1 * r2 * r3;
                }
                else if (i == n - 2)
                {
                    double r1 = (1.0 + 0.235 * bet) / (0.766 + 0.119 * bet);
                    double r2 = 1.0 / (1.0 + 0.639 * (n - 4.0) / (1.0 + 0.71 * (n - 4.0)));
                    double r3 = 1.0 / (1.0 + 20.0 * alf / ((7.5 + alf) * n * n));
                    z += (z - x[n - 4]) * r1 * r2 * r3;
                }
                else if (i == n - 1)
                {
                    double r1 = (1.0 + 0.37 * bet) / (1.67 + 0.28 * bet);
                    double r2 = 1.0 / (1.0 + 0.22 * (n - 8.0) / n);
                    double r3 = 1.0 / (1.0 + 8.0 * alf / ((6.28 + alf) * n * n));
                    z += (z - x[n - 3]) * r1 * r2 * r3;
                }
                else
                {
                    z = 3.0 * x[i - 1] - 3.0 * x[i - 2] + x[i - 3];
                }
                double alfbet = alf + bet;
                int its = 1;
                for (; its <= MAXIT; its++)
                {
                    temp = 2.0 + alfbet;
                    double p1 = (alf - bet + temp * z) / 2.0;
                    p2 = 1.0;
                    for (int j = 2; j <= n; j++)
                    {
                        double p3 = p2;
                        p2 = p1;
                        temp = 2 * j + alfbet;
                        double a = 2 * j * (j + alfbet) * (temp - 2.0);
                        double b = (temp - 1.0) * (alf * alf - bet * bet + temp * (temp - 2.0) * z);
                        double c = 2.0 * (j - 1 + alf) * (j - 1 + bet) * temp;
                        p1 = (b * p2 - c * p3) / a;
                    }
                    pp = (n * (alf - bet - temp * z) * p1 + 2.0 * (n + alf) * (n + bet) * p2) / (temp * (1.0 - z * z));
                    double z1 = z;
                    z = z1 - p1 / pp;
                    if (Math.Abs(z - z1) <= EPS)
                    {
                        break;
                    }
                }
                if (its > MAXIT)
                {
                    throw new Exception("too many iterations in gaujac");
                }
                x[i] = z;
                w[i] = Math.Exp(Globals.gammln(alf + n) + Globals.gammln(bet + n) - Globals.gammln(n + 1.0) - Globals.gammln(n + alfbet + 1.0)) * temp * Math.Pow(2.0, alfbet) / (pp * p2);
            }
        }

        /// <summary>
        /// Computes the abscissas and weights for a Gaussian quadrature formula from
        /// the Jacobi matrix.On input, a[0..n - 1] and b[0..n - 1] are the coefficients
        /// of the recurrence relation for the set of monic orthogonal polynomials. The
        /// quantity u0= S(a, b)W(x) dx is input as amu0.The abscissas x[0..n - 1] are
        /// returned in descending order, with the corresponding weights in w[0..n - 1].
        /// The arrays a and b are modified.Execution can be speeded up by modifying
        /// tqli and eigsrt to compute only the zeroth component of each eigenvector.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <param name="amu0"></param>
        /// <param name="x"></param>
        /// <param name="w"></param>
        public static void gaucof(double[] a, double[] b, double amu0, double[] x, double[] w)
        {
            int n = a.Length;
            for (int i = 0; i < n; i++)
            {
                if (i != 0)
                {
                    b[i] = Math.Sqrt(b[i]);
                }
            }
            Symmeig sym = new Symmeig(a, b);
            for (int i = 0; i < n; i++)
            {
                x[i] = sym.d[i];
                w[i] = amu0 * sym.z[0, i] * sym.z[0, i];
            }
        }

        /// <summary>
        /// Computes the abscissas and weights for a Gauss-Radau quadrature formula.On
        /// input, a[0..n - 1] and b[0..n - 1] are the coefficients of the recurrence
        /// relation for the set of monic orthogo- nal polynomials corresponding to the
        /// weight function. (b[0] is not referenced.) The quantity u0=S(a, b)W(x)dx is
        /// input as amu0.x1 is input as either endpoint of the interval.The
        /// abscissas x[0..n - 1] are returned in descending order, with the
        /// corresponding weights in w[0..n - 1]. The arrays a and b are modified.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <param name="amu0"></param>
        /// <param name="x1"></param>
        /// <param name="x"></param>
        /// <param name="w"></param>
        public static void radau(double[] a, double[] b, double amu0, double x1, double[] x, double[] w)
        {
            int n = a.Length;
            if (n == 1)
            {
                x[0] = x1;
                w[0] = amu0;
            }
            else
            {
                double p = x1 - a[0];
                double pm1 = 1.0;
                double p1 = p;
                for (int i = 1; i < n - 1; i++)
                {
                    p = (x1 - a[i]) * p1 - b[i] * pm1;
                    pm1 = p1;
                    p1 = p;
                }
                a[n - 1] = x1 - b[n - 1] * pm1 / p;
                gaucof(a,  b, amu0,  x,  w);
            }
        }

        /// <summary>
        /// Computes the abscissas and weights for a Gauss-Lobatto quadrature formula.
        /// On input, the vectors a[0..n - 1] and b[0..n - 1] are the coefficients of the
        /// recurrence relation for the set of monic orthogonal polynomials
        /// corresponding to the weight function. (b[0] is not referenced.) The
        /// quantity u0=S(a, b)W(x)dx is input as amu0.x1 amd xn are input as the
        /// endpoints of the interval.The abscissas x[0..n - 1] are returned in
        /// descending order, with the corresponding weights in w[0..n - 1]. The arrays a
        /// and b are modified.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <param name="amu0"></param>
        /// <param name="x1"></param>
        /// <param name="xn"></param>
        /// <param name="x"></param>
        /// <param name="w"></param>
        /// <exception cref="Exception"></exception>
        public static void lobatto(double[] a, double[] b, double amu0, double x1, double xn, double[] x, double[] w)
        {
            int n = a.Length;
            if (n <= 1)
            {
                throw new Exception("n must be bigger than 1 in lobatto");
            }
            double pl = x1 - a[0];
            double pr = xn - a[0];
            double pm1l = 1.0;
            double pm1r = 1.0;
            double p1l = pl;
            double p1r = pr;
            for (int i = 1; i < n - 1; i++)
            {
                pl = (x1 - a[i]) * p1l - b[i] * pm1l;
                pr = (xn - a[i]) * p1r - b[i] * pm1r;
                pm1l = p1l;
                pm1r = p1r;
                p1l = pl;
                p1r = pr;
            }
            double det = pl * pm1r - pr * pm1l;
            a[n - 1] = (x1 * pl * pm1r - xn * pr * pm1l) / det;
            b[n - 1] = (xn - x1) * pl * pr / det;
            gaucof(a,  b, amu0,  x,  w);
        }

    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/947425.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

豆瓣《乡村振兴战略下传统村落文化旅游设计》中国建筑出版传媒许少辉八一新书

豆瓣《乡村振兴战略下传统村落文化旅游设计》中国建筑出版传媒许少辉八一新书

八一参考文献:[八一新书]许少辉.乡村振兴战略下传统村落文化旅游设计[M]北京:中国建筑出版传媒,2022.

八一参考文献&#xff1a;&#xff3b;八一新书&#xff3d;许少辉&#xff0e;乡村振兴战略下传统村落文化旅游设计&#xff3b;&#xff2d;&#xff3d;北京&#xff1a;中国建筑出版传媒&#xff0c;&#xff12;&#xff10;&#xff12;&#xff12;&#xff0e;

MySQL数据库——多表查询(2)-内连接、外连接

目录 内连接 查询语法 内连接演示 外连接 查询语法 外连接演示 内连接 内连接查询的是两张表交集的部分&#xff0c;返回A表和B表交集部分的数据。内连接分为两种形式&#xff1a;隐式内连接和显式内连接。 查询语法 隐式内连接 SELECT 字段列表 FROM 表1,表2 WHERE 条…

redis应用 9: Scan

在平时线上 Redis 维护工作中&#xff0c;有时候需要从 Redis 实例成千上万的 key 中找出特定前缀的 key 列表来手动处理数据&#xff0c;可能是修改它的值&#xff0c;也可能是删除 key。这里就有一个问题&#xff0c;如何从海量的 key 中找出满足特定前缀的 key 列表来&#…

python unitest自动化框架

以下举一个最简单的unitest实例&#xff0c;包含备注&#xff0c;自己拉取代码运行一次就知道原理了 import unittest import osclass TestSample(unittest.TestCase):classmethoddef setUpClass(cls) -> None:print(整个测试类只执行一次)def setUp(self) -> None:prin…

【uniapp 配置启动页面隐私弹窗】

为什么需要配置 原因 根据工业和信息化部关于开展APP侵害用户权益专项整治要求&#xff0c;App提交到应用市场必须满足以下条件&#xff1a; 1.应用启动运行时需弹出隐私政策协议&#xff0c;说明应用采集用户数据 2.应用不能强制要求用户授予权限&#xff0c;即不能“不给权…

ClickHouse进阶(五):副本与分片-1-

进入正文前&#xff0c;感谢宝子们订阅专题、点赞、评论、收藏&#xff01;关注IT贫道&#xff0c;获取高质量博客内容&#xff01; &#x1f3e1;个人主页&#xff1a;含各种IT体系技术,IT贫道_Apache Doris,大数据OLAP体系技术栈,Kerberos安全认证-CSDN博客 &#x1f4cc;订阅…

电脑上的视频如何导入苹果手机?

AirDroid支持Windows、macOS、android、iOS相互传输文件、视频、图片等。 想要从电脑传输文件到iPhone也很简单&#xff0c;在电脑和iPhone都安装AirDroid&#xff0c;连接同一网络&#xff0c;然后登录同一个帐号就可以了。可绑定的iPhone数量不限&#xff0c;只要都登录同一…

《向量数据库指南》——向量数据库Milvus Cloud的成本和收益

谈及成本问题&#xff0c;我认为向量数据库的成本主要包括两个方面。首先&#xff0c;是将数据进行向量化所需的成本&#xff0c;涉及神经网络模型的训练和向量化等方面。其次&#xff0c;是向量数据库本身的成本。 今年向量数据库和 AIGC 之所以能够如此受欢迎&#xff0c;主要…

redis 应用 4: HyperLogLog

我们先思考一个常见的业务问题&#xff1a;如果你负责开发维护一个大型的网站&#xff0c;有一天老板找产品经理要网站每个网页每天的 UV 数据&#xff0c;然后让你来开发这个统计模块&#xff0c;你会如何实现&#xff1f; img 如果统计 PV 那非常好办&#xff0c;给每个网页一…

2023年天府杯——C 题:码头停靠问题代码分析

问题 1&#xff1a;如何确定每个码头的使用顺序和时间分配&#xff0c;以最小化船 只的等待和延迟时间&#xff1f; 这段代码是用来生成船只到达时间表&#xff0c;并且根据船只类型和码头类型进行分配和时间分配&#xff0c;最后将结果保存为Excel表格。 具体分块分析如下&am…

自动化测试(四):pytest结合allure生成测试报告

Allure 报告框架的名称 allure&#xff1a; noun [ U ] 诱惑;魅力;吸引力 文章目录 1. allure下载2. pytest框架使用allure3. 生成allure报告 1. allure下载 下载前需要先安装JDK&#xff0c;这里可以参考自动化测试(二)。 Allure下载路径&#xff1a;https://github.com/allu…

删除您和Bard的对话记录

如果您想删除您和Bard的对话记录&#xff0c;可以通过以下步骤操作&#xff1a; 前往Bard网站或应用程序。https://myactivity.google.com/product/bard?otzr1点击左下角的“删除”下拉框。 您可以选择删除所有对话记录、过去一小时或过去一天的对话记录、特定时间段的对话记…

yolov5和yolov7部署的研究

1.结论 onnx推理比torch快3倍, openvino比onnx快一丢丢。 | yolov7.pt 转 onnx python export.py --weights best_31.pt --grid --end2end --simplify --topk-all 10 --iou-thres 0.65 --conf-thres 0.65 --img-size 320 320 --max-wh 200可以看到yolov7的 onnx是包括nms…

Mybatis参数(parameterType)

在此之前&#xff0c;我们已经介绍了Mybatis的一些基本用法&#xff0c;包括了Mybatis查询数据、结果映射&#xff08;resultMap&#xff09;等。本篇我们主要介绍Mybatis在查询数据时如何传递参数。 一、准备工作 这里我们直接使用脚本初始化数据库中的数据 -- 如果数据库不…

Spring boot使用Kafka Java反序列化漏洞 CVE-2023-34040

文章目录 0.前言漏洞spring-kafka 介绍 1.参考文档2.基础介绍3.解决方案3.1. 升级版本3.2. 替代方案 4.Spring kafka 使用教程代码示例 0.前言 背景&#xff1a;公司项目扫描到 Spring-Kafka上使用通配符模式匹配进行的安全绕过漏洞 CVE-2023-20873 漏洞 中等风险 | 2023年8月…

android 输入法demo

背景&#xff1a; 一个简单的android输入法demo&#xff0c;支持输入png、gif&#xff0c;jpeg、webp等格式。 此示例演示如何编写一个应用程序&#xff0c;该应用程序接受使用 Commit Content API 从键盘发送的丰富内容&#xff08;例如图像&#xff09;。 用户通常希望通过表…

thingsboard 双向rpc,tb服务端下发指令,设备端接收指令并回复指令

背景 最近有朋友问,在使用thingsboard的rpc组件时,第一次进来总是报错,如下图,request timeout 这是因为当你打开这个页面时,该组件会发送一个getvalue的rpc来获取设备的当前数值,如果设备端没有收到,或者没有回应就会报这个错误。 所以为了有来有回,就必须实现设备端…

机器人制作开源方案 | 桌面级机械臂--运动控制

1. 调整总线舵机的模式 实现思路&#xff1a; 机械臂包括转台、大臂、小臂三部分&#xff0c;先设置好总线舵机每个ID的工作模式。下图是计划给舵机的各部分设置的ID号&#xff1a; 接下来为各部分设置相应的舵机模式&#xff08;见下表&#xff09;&#xff0c;并在程序里进行…

动态规划-路径问题

不同路径&#xff08;medium&#xff09; 题目链接: 62. 不同路径 题目描述: 一个机器人位于一个 m x n 网格的左上角 &#xff08;起始点在下图中标记为 “Start” &#xff09;。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角&#xff08;在下图中标记为…