public static void roots_to_r8poly(int nroots, double[] roots, ref int nc, ref double[] c) //****************************************************************************80 // // Purpose: // // ROOTS_TO_R8POLY converts polynomial roots to polynomial coefficients. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 06 September 2004 // // Author: // // John Burkardt // // Parameters: // // Input, int NROOTS, the number of roots specified. // // Input, double ROOTS[NROOTS], the roots. // // Output, integer *NC, the order of the polynomial, which will be NROOTS+1. // // Output, double C[*NC], the coefficients of the polynomial. // { int i; nc = nroots + 1; // // Initialize C to (0, 0, ..., 0, 1). // Essentially, we are setting up a divided difference table. // double[] xtab = new double[nroots + 1]; for (i = 0; i < nc - 1; i++) { xtab[i] = roots[i]; } xtab[nc - 1] = 0.0; for (i = 0; i < nc - 1; i++) { c[i] = 0.0; } c[nc - 1] = 1.0; // // Convert to standard polynomial form by shifting the abscissas // of the divided difference table to 0. // Dif.dif_shift_zero(nc, ref xtab, ref c); }
private static void test01() //****************************************************************************80 // // Purpose: // // TEST01 tests DIF*; // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 01 June 2013 // // Author: // // John Burkardt // { const int MAXTAB = 10; double[] diftab = new double[MAXTAB]; double[] diftab2 = new double[MAXTAB]; double[] diftab3 = new double[MAXTAB]; int i; int ntab3 = 0; double[] xtab = new double[MAXTAB]; double[] xtab2 = new double[MAXTAB]; double[] xtab3 = new double[MAXTAB]; double[] ytab = new double[MAXTAB]; Console.WriteLine(""); Console.WriteLine("TEST01"); Console.WriteLine(" DATA_TO_DIF_DISPLAY sets up a difference table"); Console.WriteLine(" and displays intermediate calculations;"); Console.WriteLine(" DIF_APPEND appends a new data point;"); Console.WriteLine(" DIF_ANTIDERIV computes the antiderivative;"); Console.WriteLine(" DIF_DERIV_TABLE computes the derivative;"); Console.WriteLine(" DIF_SHIFT_ZERO shifts all the abscissas to 0;"); Console.WriteLine(" DIF_VAL evaluates at a point;"); Console.WriteLine(""); // // Set XTAB, YTAB to X, X^2. // int ntab = 4; for (i = 0; i < ntab; i++) { xtab[i] = i + 1; ytab[i] = xtab[i] * xtab[i]; } Data.data_to_dif_display(ntab, xtab, ytab, ref diftab); Dif.dif_print(ntab, xtab, diftab, " The divided difference polynomial:"); // // Add (5,25) to the table. // Console.WriteLine(""); Console.WriteLine(" DIF_APPEND can add the data (5,25) to the table."); Console.WriteLine(""); double xval = 5.0; double yval = 25.0; Dif.dif_append(ntab, xtab, diftab, xval, yval, ref ntab, ref xtab, ref diftab); Dif.dif_print(ntab, xtab, diftab, " The updated divided difference polynomial:"); // // Evaluate the polynomial at 2.5. // Console.WriteLine(""); Console.WriteLine(" DIF_VAL can evaluate the table at a point."); Console.WriteLine(""); xval = 2.5; yval = Dif.dif_val(ntab, xtab, diftab, xval); Console.WriteLine(""); Console.WriteLine(" DIF_VAL reports P(" + xval + ") = " + yval + ""); // // Shift the base to zero. // Dif.dif_shift_zero(ntab, ref xtab, ref diftab); Dif.dif_print(ntab, xtab, diftab, " The divided difference table after DIF_SHIFT_ZERO:"); // // Compute a table for the derivative. // int ntab2 = ntab - 1; Dif.dif_deriv_table(ntab, xtab, diftab, ref xtab2, diftab2); Dif.dif_print(ntab2, xtab2, diftab2, " The divided difference table for the derivative:"); yval = Dif.dif_val(ntab2, xtab2, diftab2, xval); Console.WriteLine(""); Console.WriteLine(" DIF_VAL reports P'(" + xval + ") = " + yval + ""); // // Compute the antiderivative. // Dif.dif_antideriv(ntab, xtab, diftab, ref ntab3, xtab3, ref diftab3); Dif.dif_print(ntab3, xtab3, diftab3, " The divided difference table for the antiderivative:"); yval = Dif.dif_val(ntab3, xtab3, diftab3, xval); Console.WriteLine(""); Console.WriteLine(" DIF_VAL reports (Anti)P(" + xval + ") = " + yval + ""); }
private static void test05() //****************************************************************************80 // // Purpose: // // TEST05 tests DATA_TO_DIF_DISPLAY, DIF_PRINT, DIF_SHIFT_ZERO, DIF_TO_R8POLY; // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 18 January 2007 // // Author: // // John Burkardt // { const int MAXTAB = 10; double[] c = new double[MAXTAB]; double[] diftab1 = new double[MAXTAB]; double[] diftab2 = new double[MAXTAB]; int i; double[] xtab1 = new double[MAXTAB]; double[] xtab2 = new double[MAXTAB]; double[] ytab1 = new double[MAXTAB]; double[] ytab2 = new double[MAXTAB]; Console.WriteLine(""); Console.WriteLine("TEST05"); Console.WriteLine(" DIF_TO_R8POLY converts a difference table to a polynomial;"); Console.WriteLine(" DIF_SHIFT_ZERO shifts a divided difference table to 0;"); Console.WriteLine(""); Console.WriteLine(" These are equivalent operations"); Console.WriteLine(""); // // Set XTAB, YTAB to X, F(X) // const int ntab = 4; for (i = 0; i < ntab; i++) { double x = i + 1; xtab1[i] = x; xtab2[i] = x; ytab1[i] = -4.0 + x * (3.0 + x * (-2.0 + x)); ytab2[i] = ytab1[i]; } // // Compute and display the finite difference table. // Data.data_to_dif_display(ntab, xtab1, ytab1, ref diftab1); Data.data_to_dif_display(ntab, xtab2, ytab2, ref diftab2); // // Examine the corresponding polynomial. // Dif.dif_print(ntab, xtab1, diftab1, " The divided difference table:"); // // Shift to zero using DIF_SHIFT_ZERO. // Dif.dif_shift_zero(ntab, ref xtab1, ref diftab1); typeMethods.r8poly_print(ntab, diftab1, " The polynomial using DIF_SHIFT_ZERO:"); // // Shift to zero using DIF_TO_R8POLY. // Dif.dif_to_r8poly(ntab, xtab2, diftab2, ref c); typeMethods.r8poly_print(ntab, c, " The polynomial using DIF_TO_R8POLY:"); }