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

 

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.rhkb.cn/news/111522.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

LNMP架构之搭建Discuz论坛

LNMP 一、编译安装Nginx1&#xff09;前置准备2&#xff09;开始编译安装3&#xff09;添加到系统服务&#xff08;systemd启动&#xff09; 二、编译安装MySQL服务1&#xff09;前置准备2&#xff09;编译安装3&#xff09;编辑配置文件4&#xff09;更改mysql安装目录和配置文…

VR全景展示:打造三维、立体化的VR房产参观方式

我们从近期的新闻中可以了解到&#xff0c;房地产行业正在经历挑战和压力&#xff0c;因为房地产销售市场的持续低迷&#xff0c;导致很多公司出现了债务危机。线下销售模式效果不佳&#xff0c;很多房企开始转战线上销售&#xff0c;VR全景展示方案为房地产销售带来了全新的体…

下面是实践百度飞桨上面的pm2.5分类项目_logistic regression相关

part1:数据的引入&#xff0c;和前一个linear regression基本是一样 part2:数据解析——也就是数据的“规格化” 首先&#xff0c;打算用dataMat[]和labelMat[]数据存储feature和label&#xff0c;并且文件变量fr 然后&#xff0c;是这个for line in fr.readlines()循环&#…

华为OD机试 - MELON的难题 - 动态规划(Java 2023 B卷 100分)

目录 一、题目描述二、输入描述三、输出描述四、动态规划五、解题思路六、Java算法源码七、效果展示1、输入2、输出3、说明华为OD机试 2023B卷题库疯狂收录中,刷题点这里 一、题目描述 MELON有一堆精美的雨花石(数量为n,重量各异),准备送给S和W。MELON希望送给俩人的雨花石…

【javaweb】学习日记Day6 - Mysql 数据库 DDL DML

之前学习过的SQL语句笔记总结戳这里→【数据库原理与应用 - 第六章】T-SQL 在SQL Server的使用_Roye_ack的博客-CSDN博客 目录 一、概述 1、如何安装及配置路径Mysql&#xff1f; 2、SQL分类 二、DDL 数据定义 1、数据库操作 2、IDEA内置数据库使用 &#xff08;1&…

微服务--服务介绍

1.2.2 常见微服务架构 1. dubbo: zookeeper dubbo SpringMVC/SpringBoot 配套 通信方式: rpc 注册中心: zookeeper/redis 配置中心: diamond 2.SpringCloud: 全家桶轻松入第三方组件(Netflix) 配套 通信方式: http restful 注册中心: eruka /consul 配置中心: config 断 路器…

系统架构设计师-计算机系统基础知识(2)

目录 一、存储管理 1、页式存储 2、段式存储 3、段页式存储 二、磁盘管理 1、先来先服务FCFS 2、最短寻道时间优先SSTF 三、文件系统 1、文件基本概念 2、文件的类型&#xff1a; 3、索引文件结构 4、位示图 一、存储管理 1、页式存储 将程序与内存划分为同样大小的块&…

java错误处理百科

一、业务开发缺陷 ① 工期紧、逻辑复杂&#xff0c;开发人员会更多地考虑主流程逻辑的正确实现&#xff0c;忽略非主流程逻辑&#xff0c;或保障、补偿、一致性逻辑的实现&#xff1b; ② 往往缺乏详细的设计、监控和容量规划的闭环&#xff0c;结果就是随着业务发展出现各种各…

SpringAOP详解(下)

proxyFactory代理对象创建方式和代理对象调用方法过程&#xff1a; springaop创建动态代理对象和代理对象调用方法过程&#xff1a; 一、TargetSource的使用 Lazy注解&#xff0c;当加在属性上时&#xff0c;会产生一个代理对象赋值给这个属性&#xff0c;产生代理对象的代码为…

一台服务器上部署 Redis 伪集群

哈喽大家好&#xff0c;我是咸鱼 今天这篇文章介绍如何在一台服务器&#xff08;以 CentOS 7.9 为例&#xff09;上通过 redis-trib.rb 工具搭建 Redis cluster &#xff08;三主三从&#xff09; redis-trib.rb 是一个基于 Ruby 编写的脚本&#xff0c;其功能涵盖了创建、管…

c++ opencv将彩色图像按连通域区分

要将彩色图像按连通域区分&#xff0c;您可以使用 OpenCV 中的 cv::connectedComponents 函数。 下面是一个简单的示例代码&#xff0c;说明如何使用 cv::connectedComponents 函数来检测并标记图像中的连通域&#xff1a; #include <opencv2/opencv.hpp> #include <…

uniapp微信小程序使用stomp.js实现STOMP传输协议的实时聊天

简介&#xff1a; 原生微信小程序中使用 本来使用websocket&#xff0c;后端同事使用了stomp协议&#xff0c;导致前端也需要对应修改。 如何使用 1.yarn add stompjs 2.版本 “stompjs”: “^2.3.3” 3.在static/js中新建stomp.js和websocket.js&#xff0c;然后在需要使用…

kafka消息系统实战

kafka是什么&#xff1f; 是一种高吞吐量的、分布式、发布、订阅、消息系统 1.导入maven坐标 <dependency><groupId>org.apache.kafka</groupId><artifactId>kafka-clients</artifactId><version>2.4.1</version></dependency&…

MyBatis-Plus 总结

MyBatis-Plus简介 官网&#xff1a;https://baomidou.com/ GitHub&#xff1a;https://github.com/baomidou/mybatis-plus Gitee&#xff1a;https://gitee.com/baomidou/mybatis-plus 简介 MyBatis-Plus &#xff08;简称 MP&#xff09;是一个 MyBatis的增强工具&#x…

(十九)大数据实战——Flume数据采集框架安装部署

前言 本节内容我们主要介绍一下大数据数据采集框架flume的安装部署&#xff0c;Flume 是一款流行的开源分布式系统&#xff0c;用于高效地采集、汇总和传输大规模数据。它主要用于处理大量产生的日志数据和事件流。Flume 支持从各种数据源&#xff08;如日志文件、消息队列、数…

危险的套娃:攻击者在 PDF 文件中隐藏恶意Word 文档

据BleepingComputer消息&#xff0c;日本计算机紧急响应小组 (JPCERT) 日前分享了在2023 年 7 月检测到的利用PDF文档的新型攻击——PDF MalDoc攻击&#xff0c;能将恶意 Word 文件嵌入 PDF 来绕过安全检测。 JPCERT采样了一种多格式文件&#xff0c;能被大多数扫描引擎和工具识…

机器学习-神经网络(西瓜书)

神经网络 5.1 神经元模型 在生物神经网络中&#xff0c;神经元之间相互连接&#xff0c;当一个神经元受到的外界刺激足够大时&#xff0c;就会产生兴奋&#xff08;称为"激活"&#xff09;&#xff0c;并将剩余的"刺激"向相邻的神经元传导。 神经元模型…

Spring Boot框架以及它的优势

文章目录 介绍1. **简化配置**2. **快速启动**3. **自动配置**4. **集成第三方库和框架**5. **微服务支持**6. **内嵌式数据库支持**7. **健康监控和管理**8. **可插拔的开发工具**9. **丰富的社区和生态系统**10. **良好的测试支持&#xff1a;** 核心特性**1. 依赖注入&#…

2023蓝帽杯初赛ctf部分题目

Web LovePHP 打开网站环境&#xff0c;发现显示出源码 来可以看到php版本是7.4.33 简单分析了下&#xff0c;主要是道反序列化的题其中发现get传入的参数里有_号是非法字符&#xff0c;如果直接传值传入my_secret.flag&#xff0c;会被php处理掉 绕过 _ 的方法 对于__可以…

设计模式-组合模式

核心思想 组合模式可以使用一棵树来表示组合模式使得用户可以使用一致的方法操作单个对象和组合对象组合模式又叫部分整体模式&#xff0c;将对象组合成树形结构以表示“部分-整体”的层次结构&#xff0c;可以更好的实现管理操作&#xff0c;部分-整体对象的操作基本一样&…