public void Test_VandermondeMatrix_4_ToMatrix()
        {
            VandermondeMatrix vm = new VandermondeMatrix(2, 3, 5, 7);
            Matrix            m  = (Matrix)vm;

            Assert.Equal(1, m[0, 0]);
            Assert.Equal(1, m[1, 0]);
            Assert.Equal(1, m[2, 0]);
            Assert.Equal(1, m[3, 0]);

            Assert.Equal(2, m[0, 1]);
            Assert.Equal(3, m[1, 1]);
            Assert.Equal(5, m[2, 1]);
            Assert.Equal(7, m[3, 1]);

            Assert.Equal(4, m[0, 2]);
            Assert.Equal(9, m[1, 2]);
            Assert.Equal(25, m[2, 2]);
            Assert.Equal(49, m[3, 2]);

            Assert.Equal(8, m[0, 3]);
            Assert.Equal(27, m[1, 3]);
            Assert.Equal(125, m[2, 3]);
            Assert.Equal(343, m[3, 3]);
        }
Ejemplo n.º 2
0
    private static void pvand_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    PVAND_TEST tests PVAND.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    23 February 2014
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int N = 5;

        double[] alpha  = null;
        double[] alpha1 = { 0.0, 1.0, 2.0, 3.0, 4.0 };
        int      test;

        double[] x1 = { 5.0, 3.0, 4.0, 1.0, 2.0 };

        Console.WriteLine("");
        Console.WriteLine("PVAND_TEST:");
        Console.WriteLine("  Solve a Vandermonde linear system A*x=b");

        for (test = 1; test <= 2; test++)
        {
            switch (test)
            {
            case 1:
                alpha = typeMethods.r8vec_copy_new(N, alpha1);
                break;

            case 2:
                int seed = 123456789;
                alpha = UniformRNG.r8vec_uniform_01_new(N, ref seed);
                break;
            }

            typeMethods.r8vec_print(N, alpha, "  Vandermonde vector ALPHA:");

            double[] a = VandermondeMatrix.vand1(N, alpha);

            double[] x = typeMethods.r8vec_copy_new(N, x1);
            double[] b = typeMethods.r8mat_mv_new(N, N, a, x);
            typeMethods.r8vec_print(N, b, "  Right hand side B:");

            x = VandermondeMatrix.pvand(N, alpha, b);
            typeMethods.r8vec_print(N, x, "  Solution X:");
        }
    }
        public void Test_VandermondeMatrix_4_Determinant()
        {
            VandermondeMatrix vm       = new VandermondeMatrix(2, 3, 5, 7);
            double            actual   = vm.GetDeterminant();
            double            expected = (7 - 5) * (7 - 3) * (7 - 2) * (5 - 3) * (5 - 2) * (3 - 2);

            Assert.Equal(expected, actual);
        }
        public void Test_VandermondeMatrix_3_LowerInverse()
        {
            VandermondeMatrix vm       = new VandermondeMatrix(new double[] { 0, 2, 4 });
            Matrix            expected = new Matrix(new double[, ] {
                { 1, 0, 0 },
                { -0.5, 0.5, 0 },
                { 0.125, -0.25, 0.5 }
            });
            Matrix actual = vm.GetLowerInverse();

            Assert.Equal(expected, actual);
        }
        public void Test_VandermondeMatrix_4_UpperInverse()
        {
            VandermondeMatrix vm       = new VandermondeMatrix(2, 3, 5, 7);
            Matrix            expected = new Matrix(new double[, ] {
                { 1, -2, 6, -30 },
                { 0, 1, -5, 31 },
                { 0, 0, 1, -10 },
                { 0, 0, 0, 1 }
            });
            Matrix actual = vm.GetUpperInverse();

            Assert.Equal(expected, actual);
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            Console.WriteLine("Please input points in x y representation.");
            Console.WriteLine("Type END to finish.");

            int index = 1;

            try
            {
                VandermondeMatrix matrix = new VandermondeMatrix();

                while (true)
                {
                    Console.Write($"P#{index++}: ");

                    String input = Console.ReadLine();

                    if (input.Trim().ToUpper() == "END" || input.Trim() == "")
                    {
                        break;
                    }

                    matrix.AddEquation(new Point(input));
                }

                matrix.toVandermonde();

                Console.WriteLine("Resulting polynomial will be of the order - " + Polynomial.CalculateOrder(matrix));
                Console.WriteLine("Calculated polynomial:");

                double[] coefficients = matrix.GetResults();

                Console.WriteLine(Polynomial.Format(coefficients));

                Dictionary <int, double> calculatedPolynomial = Polynomial.Calculate(coefficients);

                foreach (KeyValuePair <int, double> value in calculatedPolynomial)
                {
                    Console.WriteLine($"f({value.Key}) = {value.Value.ToString("0.000")}");
                }

                Console.WriteLine("Derivative:\n" + Polynomial.CalculateDerivative(coefficients));
                double inititalGuess = 2;
                Console.WriteLine("Looking for a root with initial guess 2");
                Console.WriteLine("Root found for x = " + Polynomial.CalculateRoot(coefficients, inititalGuess).ToString("0.00000"));
            }
            catch (Exception e)
            {
                Console.WriteLine("Error occured: " + e.Message);
            }
        }
Ejemplo n.º 7
0
        private Vector GetCoeffs(double[] data)
        {
            Polynomial polynomial = new Polynomial();
            int        length     = data.Length / 2;

            double[] x = new double[length];
            double[] y = new double[length];

            for (int i = 0; i < length; i++)
            {
                x[i] = data[i * 2];
                y[i] = data[i * 2 + 1];
            }

            VandermondeMatrix vm = new VandermondeMatrix(x);
            Vector            v  = new Vector(y);

            return(vm.GetInverse() * v);
        }
        public void Test_VandermondeMatrix_4_Inverse()
        {
            // (-2, -39), (0, 3), (1, 6), (3, 36)
            VandermondeMatrix vm = new VandermondeMatrix(-2, 0, 1, 3);
            Vector            y  = new Vector(new double[] { -39, 3, 6, 36 });

            double determinant = vm.GetDeterminant();

            Assert.NotEqual(0, determinant);

            Matrix m      = vm.GetInverse();
            Vector actual = m * y;

            // p(x) = 2x^3-4x^2+5x+3
            Assert.Equal(3, actual[0]);
            Assert.Equal(5, actual[1]);
            Assert.Equal(-4, actual[2]);
            Assert.Equal(2, actual[3]);
        }
        public void Test_VandermondeMatrix_4_Inverse2()
        {
            // (0, 3), (1, 6), (3, 36), (4, 9)
            VandermondeMatrix vm = new VandermondeMatrix(0, 1, 3, 4);
            Vector            y  = new Vector(new double[] { 3, 6, 36, 9 });

            double determinant = vm.GetDeterminant();

            Assert.NotEqual(0, determinant);

            Matrix m      = vm.GetInverse();
            Vector actual = m * y;

            // p(x) = -4.5x^3 + 22x^2 -14.5x + 3
            Assert.Equal(3, actual[0]);
            Assert.Equal(-14.5, actual[1]);
            Assert.Equal(0, 22 - actual[2], 14);
            Assert.Equal(-4.5, actual[3]);
        }
Ejemplo n.º 10
0
    private static void bivand1_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    BIVAND1_TEST tests BIVAND1.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    23 February 2014
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int N = 3;

        double[] alpha = { 1.0, 2.0, 3.0 };
        double[] beta  = { 10.0, 20.0, 30.0 };

        Console.WriteLine("");
        Console.WriteLine("BIVAND1_TEST:");
        Console.WriteLine("  Compute a bidimensional Vandermonde matrix");
        Console.WriteLine("  associated with polynomials of");
        Console.WriteLine("  total degree less than N.");

        typeMethods.r8vec_print(N, alpha, "  Vandermonde vector ALPHA:");
        typeMethods.r8vec_print(N, beta, "  Vandermonde vector BETA:");

        double[] a = VandermondeMatrix.bivand1(N, alpha, beta);

        const int n2 = N * (N + 1) / 2;

        typeMethods.r8mat_print(n2, n2, a, "  Bidimensional Vandermonde matrix:");
    }
Ejemplo n.º 11
0
    private static void test01(int prob, int grd, int m)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    VANDERMONDE_APPROX_2D_TEST01 tests VANDERMONDE_APPROX_2D_MATRIX.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    11 October 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int PROB, the problem number.
    //
    //    Input, int GRD, the grid number.
    //    (Can't use GRID as the name because that's also a plotting function.)
    //
    //    Input, int M, the total polynomial degree.
    //
    {
        Console.WriteLine("");
        Console.WriteLine("TEST01:");
        Console.WriteLine("  Approximate data from TEST_INTERP_2D problem #" + prob + "");
        Console.WriteLine("  Use grid from TEST_INTERP_2D with index #" + grd + "");
        Console.WriteLine("  Using polynomial approximant of total degree " + m + "");

        int nd = Data_2D.g00_size(grd);

        Console.WriteLine("  Number of data points = " + nd + "");

        double[] xd = new double[nd];
        double[] yd = new double[nd];
        Data_2D.g00_xy(grd, nd, ref xd, ref yd);

        double[] zd = new double[nd];
        Data_2D.f00_f0(prob, nd, xd, yd, ref zd);

        switch (nd)
        {
        case < 10:
            typeMethods.r8vec3_print(nd, xd, yd, zd, "  X, Y, Z data:");
            break;
        }

        //
        //  Compute the Vandermonde matrix.
        //
        int tm = typeMethods.triangle_num(m + 1);

        double[] a = VandermondeMatrix.vandermonde_approx_2d_matrix(nd, m, tm, xd, yd);
        //
        //  Solve linear system.
        //
        double[] c = QRSolve.qr_solve(nd, tm, a, zd);
        //
        //  #1:  Does approximant match function at data points?
        //
        int ni = nd;

        double[] xi = typeMethods.r8vec_copy_new(ni, xd);
        double[] yi = typeMethods.r8vec_copy_new(ni, yd);
        double[] zi = Polynomial.r8poly_values_2d(m, c, ni, xi, yi);

        double app_error = typeMethods.r8vec_norm_affine(ni, zi, zd) / ni;

        Console.WriteLine("");
        Console.WriteLine("  L2 data approximation error = " + app_error + "");
    }
Ejemplo n.º 12
0
    private static void test01(int prob, int m)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST01 tests VANDERMONDE_APPROX_1D_MATRIX.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    10 October 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int PROB, the problem number.
    //
    //    Input, int M, the polynomial degree.
    //
    {
        const bool debug = false;
        int        i;

        Console.WriteLine("");
        Console.WriteLine("TEST01:");
        Console.WriteLine("  Approximate data from TEST_INTERP problem #" + prob + "");

        int nd = TestInterp.p00_data_num(prob);

        Console.WriteLine("  Number of data points = " + nd + "");

        double[] xy = TestInterp.p00_data(prob, 2, nd);

        switch (debug)
        {
        case true:
            typeMethods.r8mat_transpose_print(2, nd, xy, "  Data array:");
            break;
        }

        double[] xd = new double[nd];
        double[] yd = new double[nd];
        for (i = 0; i < nd; i++)
        {
            xd[i] = xy[0 + i * 2];
            yd[i] = xy[1 + i * 2];
        }

        //
        //  Compute the Vandermonde matrix.
        //
        Console.WriteLine("  Using polynomial approximant of degree " + m + "");

        double[] a = VandermondeMatrix.vandermonde_approx_1d_matrix(nd, m, xd);
        //
        //  Solve linear system.
        //
        double[] c = QRSolve.qr_solve(nd, m + 1, a, yd);
        //
        //  #1:  Does approximant match function at data points?
        //
        int ni = nd;

        double[] xi = typeMethods.r8vec_copy_new(ni, xd);
        double[] yi = Polynomial.r8poly_values(m, c, ni, xi);

        double app_error = typeMethods.r8vec_norm_affine(ni, yi, yd) / ni;

        Console.WriteLine("");
        Console.WriteLine("  L2 data approximation error = " + app_error + "");

        //
        //  #2: Compare estimated curve length to piecewise linear (minimal) curve length.
        //  Assume data is sorted, and normalize X and Y dimensions by (XMAX-XMIN) and
        //  (YMAX-YMIN).
        //
        double xmin = typeMethods.r8vec_min(nd, xd);
        double xmax = typeMethods.r8vec_max(nd, xd);
        double ymin = typeMethods.r8vec_min(nd, yd);
        double ymax = typeMethods.r8vec_max(nd, yd);

        ni = 501;
        xi = typeMethods.r8vec_linspace_new(ni, xmin, xmax);

        yi = Polynomial.r8poly_values(m, c, ni, xi);

        double ld = 0.0;

        for (i = 0; i < nd - 1; i++)
        {
            ld += Math.Sqrt(Math.Pow((xd[i + 1] - xd[i]) / (xmax - xmin), 2)
                            + Math.Pow((yd[i + 1] - yd[i]) / (ymax - ymin), 2));
        }

        double li = 0.0;

        for (i = 0; i < ni - 1; i++)
        {
            li += Math.Sqrt(Math.Pow((xi[i + 1] - xi[i]) / (xmax - xmin), 2)
                            + Math.Pow((yi[i + 1] - yi[i]) / (ymax - ymin), 2));
        }

        Console.WriteLine("");
        Console.WriteLine("  Normalized length of piecewise linear interpolant = " + ld + "");
        Console.WriteLine("  Normalized length of polynomial interpolant       = " + li + "");
    }
Ejemplo n.º 13
0
    public static double[] vandermonde_approx_1d_coef(int n, int m, double[] x, double[] y)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    VANDERMONDE_APPROX_1D_COEF computes a 1D polynomial approximant.
    //
    //  Discussion:
    //
    //    We assume the approximating function has the form
    //
    //      p(x) = c0 + c1 * x + c2 * x^2 + ... + cm * x^m.
    //
    //    We have n data values (x(i),y(i)) which must be approximated:
    //
    //      p(x(i)) = c0 + c1 * x(i) + c2 * x(i)^2 + ... + cm * x(i)^m = y(i)
    //
    //    This can be cast as an Nx(M+1) linear system for the polynomial
    //    coefficients:
    //
    //      [ 1 x1 x1^2 ... x1^m ] [  c0 ] = [  y1 ]
    //      [ 1 x2 x2^2 ... x2^m ] [  c1 ] = [  y2 ]
    //      [ .................. ] [ ... ] = [ ... ]
    //      [ 1 xn xn^2 ... xn^m ] [  cm ] = [  yn ]
    //
    //    In the typical case, N is greater than M+1 (we have more data and equations
    //    than degrees of freedom) and so a least squares solution is appropriate,
    //    in which case the computed polynomial will be a least squares approximant
    //    to the data.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    10 October 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int N, the number of data points.
    //
    //    Input, int M, the degree of the polynomial.
    //
    //    Input, double X[N], Y[N], the data values.
    //
    //    Output, double VANDERMONDE_APPROX_1D_COEF[M+1], the coefficients of
    //    the approximating polynomial.  C(0) is the constant term, and C(M)
    //    multiplies X^M.
    //
    {
        double[] a = VandermondeMatrix.vandermonde_approx_1d_matrix(n, m, x);

        double[] c = QRSolve.qr_solve(n, m + 1, a, y);

        return(c);
    }
Ejemplo n.º 14
0
    public static double[] vandermonde_approx_2d_coef(int n, int m, double[] x, double[] y,
                                                      double[] z)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    VANDERMONDE_APPROX_2D_COEF computes a 2D polynomial approximant.
    //
    //  Discussion:
    //
    //    We assume the approximating function has the form of a polynomial
    //    in X and Y of total degree M.
    //
    //      p(x,y) = c00
    //             + c10 * x                + c01 *  y
    //             + c20 * x^2   + c11 * xy + c02 * y^2
    //             + ...
    //             + cm0 * x^(m) + ...      + c0m * y^m.
    //
    //    If we let T(K) = the K-th triangular number
    //            = sum ( 1 <= I <= K ) I
    //    then the number of coefficients in the above polynomial is T(M+1).
    //
    //    We have n data locations (x(i),y(i)) and values z(i) to approximate:
    //
    //      p(x(i),y(i)) = z(i)
    //
    //    This can be cast as an NxT(M+1) linear system for the polynomial
    //    coefficients:
    //
    //      [ 1 x1 y1  x1^2 ... y1^m ] [ c00 ] = [  z1 ]
    //      [ 1 x2 y2  x2^2 ... y2^m ] [ c10 ] = [  z2 ]
    //      [ 1 x3 y3  x3^2 ... y3^m ] [ c01 ] = [  z3 ]
    //      [ ...................... ] [ ... ] = [ ... ]
    //      [ 1 xn yn  xn^2 ... yn^m ] [ c0m ] = [  zn ]
    //
    //    In the typical case, N is greater than T(M+1) (we have more data and
    //    equations than degrees of freedom) and so a least squares solution is
    //    appropriate, in which case the computed polynomial will be a least squares
    //    approximant to the data.
    //
    //    The polynomial defined by the T(M+1) coefficients C could be evaluated
    //    at the Nx2-vector x by the command
    //
    //      pval = r8poly_value_2d ( m, c, n, x )
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    12 October 2012
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Parameters:
    //
    //    Input, int N, the number of data points.
    //
    //    Input, int M, the maximum degree of the polynomial.
    //
    //    Input, double X[N], Y[N] the data locations.
    //
    //    Input, double Z[N], the data values.
    //
    //    Output, double VANDERMONDE_APPROX_2D_COEF[T(M+1)], the
    //    coefficients of the approximating polynomial.
    //
    {
        int tm = typeMethods.triangle_num(m + 1);

        double[] a = VandermondeMatrix.vandermonde_approx_2d_matrix(n, m, tm, x, y);

        double[] c = QRSolve.qr_solve(n, tm, a, z);

        return(c);
    }
Ejemplo n.º 15
0
    private static void dvandprg_test()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    DVANDPRG_TEST tests DVANDPRG.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    18 April 2014
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        const int N = 5;

        double[] alpha  = null;
        double[] alpha1 = { 0.0, 1.0, 2.0, 3.0, 4.0 };
        int      test;

        double[] x1 = { 5.0, 3.0, 4.0, 1.0, 2.0 };

        Console.WriteLine("");
        Console.WriteLine("DVANDPRG_TEST:");
        Console.WriteLine("  Solve a Vandermonde linear system A'*x=b");
        Console.WriteLine("  progressively.");
        Console.WriteLine("  First we use ALPHA = 0, 1, 2, 3, 4.");
        Console.WriteLine("  Then we choose ALPHA at random.");

        for (test = 1; test <= 2; test++)
        {
            switch (test)
            {
            case 1:
                alpha = typeMethods.r8vec_copy_new(N, alpha1);
                break;

            case 2:
                int seed = 123456789;
                alpha = UniformRNG.r8vec_uniform_01_new(N, ref seed);
                break;
            }

            typeMethods.r8vec_print(N, alpha, "  Vandermonde vector ALPHA:");

            double[] a = VandermondeMatrix.vand1(N, alpha);

            double[] x = typeMethods.r8vec_copy_new(N, x1);
            double[] b = typeMethods.r8mat_mtv_new(N, N, a, x);
            typeMethods.r8vec_print(N, b, "  Right hand side B:");

            x = new double[N];
            double[] c = new double[N];
            double[] m = new double[N];

            int nsub;
            for (nsub = 1; nsub <= N; nsub++)
            {
                VandermondeMatrix.dvandprg(nsub, alpha, b, ref x, ref c, ref m);
                typeMethods.r8vec_print(nsub, x, "  Solution X:");
            }
        }
    }
Ejemplo n.º 16
0
    public static void line_fekete_chebyshev(int m, double a, double b, int n, double[] x,
                                             ref int nf, ref double[] xf, ref double[] wf)

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    LINE_FEKETE_CHEBYSHEV: approximate Fekete points in an interval [A,B].
    //
    //  Discussion:
    //
    //    We use the Chebyshev basis.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    13 April 2014
    //
    //  Author:
    //
    //    John Burkardt
    //
    //  Reference:
    //
    //    Len Bos, Norm Levenberg,
    //    On the calculation of approximate Fekete points: the univariate case,
    //    Electronic Transactions on Numerical Analysis,
    //    Volume 30, pages 377-397, 2008.
    //
    //  Parameters:
    //
    //    Input, int M, the number of basis polynomials.
    //
    //    Input, double A, B, the endpoints of the interval.
    //
    //    Input, int N, the number of sample points.
    //    M <= N.
    //
    //    Input, double X(N), the coordinates of the sample points.
    //
    //    Output, int &NF, the number of Fekete points.
    //    If the computation is successful, NF = M.
    //
    //    Output, double XF(NF), the coordinates of the Fekete points.
    //
    //    Output, double WF(NF), the weights of the Fekete points.
    //
    {
        int i;
        int j;

        if (n < m)
        {
            Console.WriteLine("");
            Console.WriteLine("LINE_FEKETE_CHEBYSHEV - Fatal error!");
            Console.WriteLine("  N < M.");
            return;
        }

        //
        //  Compute the Chebyshev-Vandermonde matrix.
        //
        double[] v = VandermondeMatrix.cheby_van1(m, a, b, n, x);
        //
        //  MOM(I) = Integral ( A <= x <= B ) Tab(A,B,I;x) dx
        //
        double[] mom = new double[m];

        mom[0] = Math.PI * (b - a) / 2.0;
        for (i = 1; i < m; i++)
        {
            mom[i] = 0.0;
        }

        //
        //  Solve the system for the weights W.
        //
        double[] w = QRSolve.qr_solve(m, n, v, mom);
        //
        //  Extract the data associated with the nonzero weights.
        //
        nf = 0;
        for (j = 0; j < n; j++)
        {
            if (w[j] == 0.0)
            {
                continue;
            }

            if (nf >= m)
            {
                continue;
            }

            xf[nf] = x[j];
            wf[nf] = w[j];
            nf    += 1;
        }
    }