示例#1
0
    private static void bernstein_vandermonde_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    BERNSTEIN_VANDERMONDE_TEST tests BERNSTEIN_VANDERMONDE.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    03 December 2015
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        double[] a;
        int      n;

        Console.WriteLine("");
        Console.WriteLine("BERNSTEIN_VANDERMONDE_TEST");
        Console.WriteLine("  BERNSTEIN_VANDERMONDE returns an NxN matrix whose (I,J) entry");
        Console.WriteLine("  is the value of the J-th Bernstein polynomial of degree N-1");
        Console.WriteLine("  evaluated at the I-th equally spaced point in [0,1].");

        n = 8;
        a = BernsteinPolynomial.bernstein_vandermonde(n);
        typeMethods.r8mat_print(n, n, a, "  Bernstein Vandermonde ( 8 ):");
    }
示例#2
0
    private static void bernstein_matrix_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    BERNSTEIN_MATRIX_TEST tests BERNSTEIN_MATRIX.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    26 January 2016
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        Console.WriteLine("");
        Console.WriteLine("BERNSTEIN_MATRIX_TEST");
        Console.WriteLine("  BERNSTEIN_MATRIX returns a matrix A which transforms a");
        Console.WriteLine("  polynomial coefficient vector from the power basis to");
        Console.WriteLine("  the Bernstein basis.");

        int n = 5;

        double[] a = BernsteinPolynomial.bernstein_matrix(n);
        typeMethods.r8mat_print(n, n, a, "  Bernstein matrix A of order 5:");
    }
示例#3
0
        public void BernsteinPolynomialCalculation_ShouldWork(int i, int n, double u, double expected)
        {
            // Arrange

            // Act
            var actual = BernsteinPolynomial.Calculate(i, n, u);

            // Assert
            Assert.AreEqual(expected, actual, 0.0001);
        }
    public static void bernstein_poly_test( )

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    BERNSTEIN_POLY_TEST tests BERNSTEIN_POLY.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    27 February 2015
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        double b = 0;

        double[] bvec = new double[11];
        int      k    = 0;
        int      n    = 0;
        double   x    = 0;

        Console.WriteLine("");
        Console.WriteLine("BERNSTEIN_POLY_TEST:");
        Console.WriteLine("  BERNSTEIN_POLY evaluates the Bernstein polynomials.");
        Console.WriteLine("");
        Console.WriteLine("   N   K   X   Exact   B(N,K)(X)");
        Console.WriteLine("");

        int n_data = 0;

        for ( ; ;)
        {
            Burkardt.Values.Bernstein.bernstein_poly_values(ref n_data, ref n, ref k, ref x, ref b);

            if (n_data == 0)
            {
                break;
            }

            BernsteinPolynomial.bernstein_poly(n, x, ref bvec);

            Console.WriteLine("  " + n.ToString(CultureInfo.InvariantCulture).PadLeft(4)
                              + "  " + k.ToString(CultureInfo.InvariantCulture).PadLeft(4)
                              + "  " + x.ToString(CultureInfo.InvariantCulture).PadLeft(7)
                              + "  " + b.ToString(CultureInfo.InvariantCulture).PadLeft(14)
                              + "  " + bvec[k].ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");
        }
    }
示例#5
0
    private static void bernstein_poly_01_matrix_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    BERNSTEIN_POLY_01_MATRIX_TEST tests BERNSTEIN_POLY_01_MATRIX.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    27 January 2016
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        Console.WriteLine("");
        Console.WriteLine("BERNSTEIN_POLY_01_MATRIX_TEST");
        Console.WriteLine("  BERNSTEIN_POLY_01_MATRIX is given M data values X,");
        Console.WriteLine("  and a degree N, and returns an Mx(N+1) matrix B such that");
        Console.WriteLine("  B(i,j) is the j-th Bernstein polynomial evaluated at the.");
        Console.WriteLine("  i-th data value.");

        int m = 5;

        double[] x = typeMethods.r8vec_linspace_new(m, 0.0, 1.0);
        int      n = 1;

        double[] b = BernsteinPolynomial.bernstein_poly_01_matrix(m, n, x);
        typeMethods.r8mat_print(m, n + 1, b, "  B(5,1+1):");

        m = 5;
        x = typeMethods.r8vec_linspace_new(m, 0.0, 1.0);
        n = 4;
        b = BernsteinPolynomial.bernstein_poly_01_matrix(m, n, x);
        typeMethods.r8mat_print(m, n + 1, b, "  B(5,4+1):");

        m = 10;
        x = typeMethods.r8vec_linspace_new(m, 0.0, 1.0);
        n = 4;
        b = BernsteinPolynomial.bernstein_poly_01_matrix(m, n, x);
        typeMethods.r8mat_print(m, n + 1, b, "  B(10,4+1):");

        m = 3;
        x = typeMethods.r8vec_linspace_new(m, 0.0, 1.0);
        n = 5;
        b = BernsteinPolynomial.bernstein_poly_01_matrix(m, n, x);
        typeMethods.r8mat_print(m, n + 1, b, "  B(3,5+1):");
    }
示例#6
0
    private static void bernstein_poly_01_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    BERNSTEIN_POLY_01_TEST tests BERNSTEIN_POLY_01.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    29 July 2011
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        double b = 0;
        int    k = 0;
        int    n = 0;
        double x = 0;

        Console.WriteLine("");
        Console.WriteLine("BERNSTEIN_POLY_01_TEST:");
        Console.WriteLine("  BERNSTEIN_POLY_01 evaluates the Bernstein polynomials");
        Console.WriteLine("  based on the interval [0,1].");
        Console.WriteLine("");
        Console.WriteLine("     N     K     X       Exact         BP01(N,K)(X)");
        Console.WriteLine("");

        int n_data = 0;

        while (true)
        {
            Burkardt.Values.Bernstein.bernstein_poly_01_values(ref n_data, ref n, ref k, ref x, ref b);

            if (n_data == 0)
            {
                break;
            }

            double[] bvec = BernsteinPolynomial.bernstein_poly_01(n, x);

            Console.WriteLine("  " + n.ToString().PadLeft(4)
                              + "  " + k.ToString().PadLeft(4)
                              + "  " + x.ToString().PadLeft(7)
                              + "  " + b.ToString().PadLeft(14)
                              + "  " + bvec[k].ToString().PadLeft(14) + "");
        }
    }
示例#7
0
        private double[] CalculateUFactors(double u)
        {
            double[] uFactors = new double[Degree + 1];

            for (int i = 0; i < uFactors.Length; i++)
            {
                uFactors[i] = BernsteinPolynomial.Calculate(i, Degree, u);
            }

            return(uFactors);
        }
    public static void bpab_test( )

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    BPAB_TEST tests BPAB.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    02 June 2007
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int N = 10;

        double[] bern = new double[N + 1];
        int      i;

        Console.WriteLine("");
        Console.WriteLine("BPAB_TEST");
        Console.WriteLine("  BPAB evaluates Bernstein polynomials.");
        Console.WriteLine("");

        const double x = 0.3;
        const double a = 0.0;
        const double b = 1.0;

        BernsteinPolynomial.bpab(N, x, a, b, ref bern);

        Console.WriteLine("  The Bernstein polynomials of degree " + N + "");
        Console.WriteLine("  based on the interval from " + a + "");
        Console.WriteLine("  to " + b + "");
        Console.WriteLine("  evaluated at X = " + x + "");
        Console.WriteLine("");

        for (i = 0; i <= N; i++)
        {
            Console.WriteLine("  " + i.ToString(CultureInfo.InvariantCulture).PadLeft(4)
                              + "  " + bern[i].ToString(CultureInfo.InvariantCulture).PadLeft(14) + "");
        }
    }
示例#9
0
        public Point3D Evaluate(double u, double v)
        {
            var subpoints = new List <Point3D>();

            for (var i = 0; i < 4; ++i)
            {
                var subpoint = BernsteinPolynomial.Evaluate3DPolynomial(
                    ArrayHelpers.GetRow(ControlPoints, i), u
                    );

                subpoints.Add(subpoint);
            }

            return(BernsteinPolynomial.Evaluate3DPolynomial(subpoints, v));
        }
示例#10
0
    private static void bernstein_poly_01_test2()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    BERNSTEIN_POLY_01_TEST2 tests BERNSTEIN_POLY_01.
    //
    //  Discussion:
    //
    //    Here we test the Partition-of-Unity property.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    27 January 2016
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        Console.WriteLine("");
        Console.WriteLine("BERNSTEIN_POLY_01_TEST2:");
        Console.WriteLine("  BERNSTEIN_POLY_01 evaluates the Bernstein polynomials");
        Console.WriteLine("  based on the interval [0,1].");
        Console.WriteLine("");
        Console.WriteLine("  Here we test the partition of unity property.");
        Console.WriteLine("");
        Console.WriteLine("     N     X          Sum ( 0 <= K <= N ) BP01(N,K)(X)");
        Console.WriteLine("");

        int seed = 123456789;

        for (int n = 0; n <= 10; n++)
        {
            double x = UniformRNG.r8_uniform_01(ref seed);

            double[] bvec = BernsteinPolynomial.bernstein_poly_01(n, x);

            Console.WriteLine("  " + n.ToString().PadLeft(4)
                              + "  " + x.ToString().PadLeft(7)
                              + "  " + typeMethods.r8vec_sum(n + 1, bvec).ToString().PadLeft(14) + "");
        }
    }
示例#11
0
        public Vector3D Derivative(
            double u, double v,
            DerivativeParameter parameter
            )
        {
            double firstParameter, secondParameter;
            Func <int, Point3D[]> generator;

            switch (parameter)
            {
            case DerivativeParameter.U:
                generator       = (i) => ArrayHelpers.GetColumn(ControlPoints, i);
                firstParameter  = v;
                secondParameter = u;
                break;

            case DerivativeParameter.V:
                generator       = (i) => ArrayHelpers.GetRow(ControlPoints, i);
                firstParameter  = u;
                secondParameter = v;
                break;

            default:
                throw new ArgumentOutOfRangeException(
                          nameof(parameter),
                          parameter,
                          null
                          );
            }

            var subpoints = new List <Point3D>();

            for (var i = 0; i < 4; ++i)
            {
                subpoints.Add(BernsteinPolynomial.Evaluate3DPolynomial(
                                  generator(i),
                                  firstParameter
                                  ));
            }

            var derivative = BernsteinPolynomial.CalculateDerivative(subpoints);

            return((Vector3D)BernsteinPolynomial.Evaluate3DPolynomial(
                       derivative,
                       secondParameter
                       ));
        }
示例#12
0
    private static void bernstein_matrix_inverse_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    BERNSTEIN_MATRIX_INVERSE_TEST tests BERNSTEIN_MATRIX_INVERSE.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    26 January 2016
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        Console.WriteLine("");
        Console.WriteLine("BERNSTEIN_MATRIX_INVERSE_TEST");
        Console.WriteLine("  BERNSTEIN_MATRIX_INVERSE computes the inverse of the");
        Console.WriteLine("  Bernstein matrix A.");
        Console.WriteLine("");
        Console.WriteLine("     N     ||A||       ||inv(A)|| ||I-A*inv(A)||");
        Console.WriteLine("");

        for (int n = 5; n <= 15; n++)
        {
            double[] a = BernsteinPolynomial.bernstein_matrix(n);
            double   a_norm_frobenius = typeMethods.r8mat_norm_fro(n, n, a);

            double[] b = BernsteinPolynomial.bernstein_matrix_inverse(n);
            double   b_norm_frobenius = typeMethods.r8mat_norm_fro(n, n, b);

            double[] c = typeMethods.r8mat_mm_new(n, n, n, a, b);
            double   error_norm_frobenius = typeMethods.r8mat_is_identity(n, c);

            Console.WriteLine("  " + n.ToString().PadLeft(4)
                              + "  " + a_norm_frobenius.ToString().PadLeft(14)
                              + "  " + b_norm_frobenius.ToString().PadLeft(14)
                              + "  " + error_norm_frobenius.ToString().PadLeft(14) + "");
        }
    }
示例#13
0
    private static void bernstein_matrix_determinant_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    BERNSTEIN_MATRIX_DETERMINANT_TEST tests BERNSTEIN_MATRIX_DETERMINANT.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    26 January 2016
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        Console.WriteLine("");
        Console.WriteLine("BERNSTEIN_MATRIX_DETERMINANT_TEST");
        Console.WriteLine("  BERNSTEIN_MATRIX_DETERMINANT computes the determinant of");
        Console.WriteLine("  the Bernstein matrix.");
        Console.WriteLine("");
        Console.WriteLine("     N         ||A||          det(A)");
        Console.WriteLine("                              computed");
        Console.WriteLine("");

        for (int n = 5; n <= 15; n++)
        {
            double[] a = BernsteinPolynomial.bernstein_matrix(n);
            double   a_norm_frobenius = typeMethods.r8mat_norm_fro(n, n, a);

            double d1 = BernsteinPolynomial.bernstein_matrix_determinant(n);

            Console.WriteLine("  " + n.ToString().PadLeft(4)
                              + "  " + a_norm_frobenius.ToString().PadLeft(14)
                              + "  " + d1.ToString().PadLeft(14) + "");
        }
    }
示例#14
0
    private static void bernstein_to_power_test()

    //*****************************************************************************/
    //
    //  Purpose:
    //
    //    BERNSTEIN_TO_POWER_TEST tests BERNSTEIN_TO_POWER.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    17 March 2016
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int n = 5;

        Console.WriteLine("");
        Console.WriteLine("BERNSTEIN_TO_POWER_TEST:");
        Console.WriteLine("  BERNSTEIN_TO_POWER returns the matrix A which maps");
        Console.WriteLine("  polynomial coefficients from Bernstein to Power form.");

        double[] a = BernsteinPolynomial.bernstein_to_power(n);
        typeMethods.r8mat_print(n + 1, n + 1, a, "  A = bernstein_to_power(5):");

        double[] b = BernsteinPolynomial.power_to_bernstein(n);
        typeMethods.r8mat_print(n + 1, n + 1, b, "  B = power_to_bernstein(5):");

        double[] c = typeMethods.r8mat_mm_new(n + 1, n + 1, n + 1, a, b);
        double   e = typeMethods.r8mat_is_identity(n + 1, c);

        Console.WriteLine("");
        Console.WriteLine("  ||A*B-I|| = " + e + "");
    }
示例#15
0
        public void ReshapeEdge(
            int edgeId,
            Func <double, Point3D> boundaryCurve,
            Func <double, Vector3D> boundaryDerivative,
            Vector3D a0,
            Vector3D b0,
            Vector3D a3,
            Vector3D b3
            )
        {
            var c0 = boundaryDerivative(0);
            var c2 = boundaryDerivative(1);
            var g0 = (a0 + b0) / 2.0;
            var g2 = (a3 + b3) / 2.0;
            var g1 = (g0 + g2) / 2.0;

            var gPolynomial = new List <Point3D>()
            {
                (Point3D)g0,
                (Point3D)g1,
                (Point3D)g2
            };

            double k0, k1, h0, h1;

            CalculateTangentVectorFieldCoefficients(b0, g0, c0, out k0, out h0);
            CalculateTangentVectorFieldCoefficients(b3, g2, c2, out k1, out h1);

            Func <double, double>   k = v => k0 * (1 - v) + k1 * v;
            Func <double, double>   h = v => h0 * (1 - v) + h1 * v;
            Func <double, Vector3D> g = v =>
                                        (Vector3D)BernsteinPolynomial.Evaluate3DPolynomial(
                gPolynomial, v
                );

            Func <double, Vector3D> d =
                v => k(v) * g(v) + h(v) * boundaryDerivative(v);

            int v0 = 3, v1 = 1, derivativeIndex = 0;

            if (edgeId == 0)
            {
                v0 = 0;
                v1 = 2;
                derivativeIndex = 0;
            }
            else if (edgeId == 1)
            {
                v0 = 1;
                v1 = 0;
                derivativeIndex = 1;
            }
            else if (edgeId == 2)
            {
                v0 = 2;
                v1 = 3;
                derivativeIndex = 1;
            }
            else if (edgeId == 3)
            {
                v0 = 1;
                v1 = 3;
                derivativeIndex = 0;
            }

            _cornerPoints[v0] = boundaryCurve(0);
            _cornerPoints[v1] = boundaryCurve(1);
            _cornerDerivatives[v0, derivativeIndex]  = c0;
            _cornerDerivatives[v1, derivativeIndex]  = c2;
            _cornerTwistVectors[v0, derivativeIndex] = d(1.0 / 3.0);
            _cornerTwistVectors[v1, derivativeIndex] = d(2.0 / 3.0);
        }
示例#16
0
    public static double bez_val(int n, double x, double a, double b, double[] y)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    BEZ_VAL evaluates a Bezier function at a point.
    //
    //  Discussion:
    //
    //    The Bezier function has the form:
    //
    //      BEZ(X) = Sum ( 0 <= I <= N ) Y(I) * BERN(N,I)( (X-A)/(B-A) )
    //
    //    BERN(N,I)(X) is the I-th Bernstein polynomial of order N
    //    defined on the interval [0,1],
    //
    //    Y(0:N) is a set of coefficients,
    //
    //    and if, for I = 0 to N, we define the N+1 points
    //
    //      X(I) = ( (N-I)*A + I*B) / N,
    //
    //    equally spaced in [A,B], the pairs ( X(I), Y(I) ) can be regarded as
    //    "control points".
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    12 February 2004
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Reference:
    //
    //    David Kahaner, Cleve Moler, Steven Nash,
    //    Numerical Methods and Software,
    //    Prentice Hall, 1989,
    //    ISBN: 0-13-627258-4,
    //    LC: TA345.K34.
    //
    //  Parameters:
    //
    //    Input, int N, the order of the Bezier function, which
    //    must be at least 0.
    //
    //    Input, double X, the point at which the Bezier function should
    //    be evaluated.  The best results are obtained within the interval
    //    [A,B] but X may be anywhere.
    //
    //    Input, double A, B, the interval over which the Bezier function
    //    has been defined.  This is the interval in which the control
    //    points have been set up.  Note BEZ(A) = Y(0) and BEZ(B) = Y(N),
    //    although BEZ will not, in general pass through the other
    //    control points.  A and B must not be equal.
    //
    //    Input, double Y[0:N], a set of data defining the Y coordinates
    //    of the control points.
    //
    //    Output, double BEZ_VAL, the value of the Bezier function at X.
    //
    {
        int i;

        switch (b - a)
        {
        case 0.0:
            Console.WriteLine("");
            Console.WriteLine("BEZ_VAL - Fatal error!");
            Console.WriteLine("  Null interval, A = B = " + a + "");
            return(1);
        }

        //
        //  X01 lies in [0,1], in the same relative position as X in [A,B].
        //
        double x01 = (x - a) / (b - a);

        double[] bval = BernsteinPolynomial.bernstein_poly_01(n, x01);

        double value = 0.0;

        for (i = 0; i <= n; i++)
        {
            value += y[i] * bval[i];
        }

        return(value);
    }
示例#17
0
    public static void bc_val(int n, double t, double[] xcon, double[] ycon, ref double xval,
                              ref double yval)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    BC_VAL evaluates a parameterized Bezier curve.
    //
    //  Discussion:
    //
    //    BC_VAL(T) is the value of a vector function of the form
    //
    //      BC_VAL(T) = ( X(T), Y(T) )
    //
    //    where
    //
    //      X(T) = Sum ( 0 <= I <= N ) XCON(I) * BERN(I,N)(T)
    //      Y(T) = Sum ( 0 <= I <= N ) YCON(I) * BERN(I,N)(T)
    //
    //    BERN(I,N)(T) is the I-th Bernstein polynomial of order N
    //    defined on the interval [0,1],
    //
    //    XCON(0:N) and YCON(0:N) are the coordinates of N+1 "control points".
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    12 February 2004
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Reference:
    //
    //    David Kahaner, Cleve Moler, Steven Nash,
    //    Numerical Methods and Software,
    //    Prentice Hall, 1989,
    //    ISBN: 0-13-627258-4,
    //    LC: TA345.K34.
    //
    //  Parameters:
    //
    //    Input, int N, the order of the Bezier curve, which
    //    must be at least 0.
    //
    //    Input, double T, the point at which the Bezier curve should
    //    be evaluated.  The best results are obtained within the interval
    //    [0,1] but T may be anywhere.
    //
    //    Input, double XCON[0:N], YCON[0:N], the X and Y coordinates
    //    of the control points.  The Bezier curve will pass through
    //    the points ( XCON(0), YCON(0) ) and ( XCON(N), YCON(N) ), but
    //    generally NOT through the other control points.
    //
    //    Output, double *XVAL, *YVAL, the X and Y coordinates of the point
    //    on the Bezier curve corresponding to the given T value.
    //
    {
        int i;

        double[] bval = BernsteinPolynomial.bernstein_poly_01(n, t);

        xval = 0.0;
        for (i = 0; i <= n; i++)
        {
            xval += xcon[i] * bval[i];
        }

        yval = 0.0;
        for (i = 0; i <= n; i++)
        {
            yval += ycon[i] * bval[i];
        }
    }
示例#18
0
    private static void bernstein_matrix_test2()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    BERNSTEIN_MATRIX_TEST2 tests BERNSTEIN_MATRIX.
    //
    //  Discussion:
    //
    //    Here we use the Bernstein matrix to describe a Bernstein polynomial
    //    in terms of the standard monomials.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    27 January 2016
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        Console.WriteLine("");
        Console.WriteLine("TEST06");
        Console.WriteLine("  BERNSTEIN_MATRIX returns a matrix A which");
        Console.WriteLine("  transforms a polynomial coefficient vector");
        Console.WriteLine("  from the the Bernstein basis to the power basis.");
        Console.WriteLine("  We can use this to get explicit values of the");
        Console.WriteLine("  4-th degree Bernstein polynomial coefficients as");
        Console.WriteLine("");
        Console.WriteLine("    b(4,K)(X) = C4 * x^4");
        Console.WriteLine("              + C3 * x^3");
        Console.WriteLine("              + C2 * x^2");
        Console.WriteLine("              + C1 * x");
        Console.WriteLine("              + C0 * 1");

        int n = 5;

        Console.WriteLine("");
        Console.WriteLine("     K       C4           C3            C2" +
                          "            C1             C0");
        Console.WriteLine("");

        double[] a = BernsteinPolynomial.bernstein_matrix(n);
        double[] x = new double[n];

        for (int k = 0; k < n; k++)
        {
            for (int i = 0; i < n; i++)
            {
                x[i] = 0.0;
            }

            x[k] = 1.0;

            double[] ax = typeMethods.r8mat_mv_new(n, n, a, x);

            string cout = "  " + k.ToString().PadLeft(4) + "  ";
            for (int i = 0; i < n; i++)
            {
                cout += "  " + ax[i].ToString().PadLeft(14);
            }

            Console.WriteLine(cout);
        }
    }
示例#19
0
    private static void bernstein_poly_ab_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    BERNSTEIN_POLY_AB_TEST tests BERNSTEIN_POLY_AB.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    05 July 2011
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int n = 10;

        Console.WriteLine("");
        Console.WriteLine("BERNSTEIN_POLY_AB_TEST");
        Console.WriteLine("  BERNSTEIN_POLY_AB evaluates Bernstein polynomials over an");
        Console.WriteLine("  arbitrary interval [A,B].");
        Console.WriteLine("");
        Console.WriteLine("  Here, we demonstrate that ");
        Console.WriteLine("    BPAB(N,K,A1,B1)(X1) = BPAB(N,K,A2,B2)(X2)");
        Console.WriteLine("  provided only that");
        Console.WriteLine("    (X1-A1)/(B1-A1) = (X2-A2)/(B2-A2).");

        double x = 0.3;
        double a = 0.0;
        double b = 1.0;

        double[] bern = BernsteinPolynomial.bernstein_poly_ab(n, a, b, x);

        Console.WriteLine("");
        Console.WriteLine("     N     K     A        B        X       BPAB(N,K,A,B)(X)");
        Console.WriteLine("");
        for (int k = 0; k <= n; k++)
        {
            Console.WriteLine("  " + n.ToString().PadLeft(4)
                              + "  " + k.ToString().PadLeft(4)
                              + "  " + a.ToString().PadLeft(7)
                              + "  " + b.ToString().PadLeft(7)
                              + "  " + x.ToString().PadLeft(7)
                              + "  " + bern[k].ToString().PadLeft(14) + "");
        }

        x    = 1.3;
        a    = 1.0;
        b    = 2.0;
        bern = BernsteinPolynomial.bernstein_poly_ab(n, a, b, x);

        Console.WriteLine("");
        Console.WriteLine("     N     K     A        B        X       BPAB(N,K,A,B)(X)");
        Console.WriteLine("");
        for (int k = 0; k <= n; k++)
        {
            Console.WriteLine("  " + n.ToString().PadLeft(4)
                              + "  " + k.ToString().PadLeft(4)
                              + "  " + a.ToString().PadLeft(7)
                              + "  " + b.ToString().PadLeft(7)
                              + "  " + x.ToString().PadLeft(7)
                              + "  " + bern[k].ToString().PadLeft(14) + "");
        }

        x    = 2.6;
        a    = 2.0;
        b    = 4.0;
        bern = BernsteinPolynomial.bernstein_poly_ab(n, a, b, x);

        Console.WriteLine("");
        Console.WriteLine("     N     K     A        B        X       BPAB(N,K,A,B)(X)");
        Console.WriteLine("");

        for (int k = 0; k <= n; k++)
        {
            Console.WriteLine("  " + n.ToString().PadLeft(4)
                              + "  " + k.ToString().PadLeft(4)
                              + "  " + a.ToString().PadLeft(7)
                              + "  " + b.ToString().PadLeft(7)
                              + "  " + x.ToString().PadLeft(7)
                              + "  " + bern[k].ToString().PadLeft(14) + "");
        }
    }
示例#20
0
    private static void bernstein_poly_ab_approx_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    BERNSTEIN_POLY_AB_APPROX_TEST tests BERNSTEIN_POLY_AB_APPROX.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    27 January 2016
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        int maxdata = 20;
        int nval    = 501;

        Console.WriteLine("");
        Console.WriteLine("BERNSTEIN_POLY_AB_APPROX_TEST");
        Console.WriteLine("  BERNSTEIN_POLY_AB_APPROX evaluates the Bernstein polynomial");
        Console.WriteLine("  approximant to a function F(X).");

        double a = 1.0;
        double b = 3.0;

        Console.WriteLine("");
        Console.WriteLine("     N      Max Error");
        Console.WriteLine("");

        for (int ndata = 0; ndata <= maxdata; ndata++)
        {
            //
            //  Generate data values.
            //
            double[] xdata = new double[ndata + 1];
            double[] ydata = new double[ndata + 1];
            for (int i = 0; i <= ndata; i++)
            {
                xdata[i] = ndata switch
                {
                    0 => 0.5 * (a + b),
                    _ => ((ndata - i) * a + i * b) / ndata
                };

                ydata[i] = Math.Sin(xdata[i]);
            }

            //
            //  Compare the true function and the approximant.
            //
            double[] xval = typeMethods.r8vec_linspace_new(nval, a, b);

            double error_max = 0.0;

            double[] yval = BernsteinPolynomial.bernstein_poly_ab_approx(ndata, a, b, ydata, nval, xval);

            error_max = 0.0;
            for (int i = 0; i < nval; i++)
            {
                error_max = Math.Max(error_max, Math.Abs(yval[i] - Math.Sin(xval[i])));
            }

            Console.WriteLine("  " + ndata.ToString().PadLeft(4)
                              + "  " + error_max.ToString().PadLeft(14) + "");
        }
    }