# include # include # include # include # include # include # include # include "gegenbauer_polynomial.h" int main ( ); void gegenbauer_alpha_check_test ( ); void gegenbauer_ek_compute_test ( ); void gegenbauer_integral_test ( ); void gegenbauer_polynomial_value_test ( ); void gegenbauer_ss_compute_test ( ); void gegenbauer_to_monomial_matrix_test ( ); void imtqlx_test ( ); void monomial_to_gegenbauer_matrix_test ( ); void r8_hyper_2f1_test ( ); void r8_uniform_ab_test ( ); double *r8mat_mm_new ( int n1, int n2, int n3, double a[], double b[] ); double *r8mat_mv_new ( int m, int n, double a[], double x[] ); void r8mat_print ( int m, int n, double a[], char *title ); void r8mat_print_some ( int m, int n, double a[], int ilo, int jlo, int ihi, int jhi, char *title ); void r8poly_print ( int n, double a[], char *title ); void timestamp ( ); /******************************************************************************/ int main ( ) /******************************************************************************/ /* Purpose: gegenbauer_polynomial_test() tests gegenbauer_polynomial(). Licensing: This code is distributed under the MIT license. Modified: 24 April 2024 Author: John Burkardt */ { timestamp ( ); printf ( "\n" ); printf ( "gegenbauer_polynomial_test():\n" ); printf ( " C version.\n" ); printf ( " Test gegenbauer_polynomial().\n" ); gegenbauer_alpha_check_test ( ); gegenbauer_ek_compute_test ( ); gegenbauer_integral_test ( ); gegenbauer_polynomial_value_test ( ); gegenbauer_ss_compute_test ( ); gegenbauer_to_monomial_matrix_test ( ); imtqlx_test ( ); monomial_to_gegenbauer_matrix_test ( ); r8_hyper_2f1_test ( ); r8_uniform_ab_test ( ); /* Terminate. */ printf ( "\n" ); printf ( "gegenbauer_polynomial_test():\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; } /******************************************************************************/ void gegenbauer_alpha_check_test ( ) /******************************************************************************/ /* Purpose: gegenbauer_alpha_check_test() tests gegenbauer_alpha_check(). Licensing: This code is distributed under the MIT license. Modified: 30 November 2015 Author: John Burkardt */ { double alpha; bool check; int n; int seed; printf ( "\n" ); printf ( "gegenbauer_alpha_check_test():\n" ); printf ( " gegenbauer_alpha_check() checks that ALPHA is legal;\n" ); printf ( "\n" ); printf ( " ALPHA Check?\n" ); printf ( "\n" ); seed = 123456789; for ( n = 1; n <= 10; n++ ) { alpha = r8_uniform_ab ( -5.0, +5.0, &seed ); check = gegenbauer_alpha_check ( alpha ); printf ( " %10.4f %1d\n", alpha, check ); } return; } /******************************************************************************/ void gegenbauer_ek_compute_test ( ) /******************************************************************************/ /* Purpose: gegenbauer_ek_compute_test() tests gegenbauer_ek_compute(). Licensing: This code is distributed under the MIT license. Modified: 19 November 2015 Author: John Burkardt */ { double alpha; int i; int n; double *w; double *x; alpha = 0.5; printf ( "\n" ); printf ( "gegenbauer_ek_compute_test():\n" ); printf ( " gegenbauer_ek_compute() computes a Gauss-Gegenbauer rule;\n" ); printf ( "\n" ); printf ( " Abscissas X and weights W for a Gauss Gegenbauer rule\n" ); printf ( " with ALPHA = %f\n", alpha ); printf ( " Integration interval = [-1,+1]\n" ); printf ( "\n" ); printf ( " W X\n" ); for ( n = 1; n <= 10; n++ ) { printf ( "\n" ); w = ( double * ) malloc ( n * sizeof ( double ) ); x = ( double * ) malloc ( n * sizeof ( double ) ); gegenbauer_ek_compute ( n, alpha, x, w ); for ( i = 0; i < n; i++ ) { printf ( " %2d %24.16g %24.16g\n", i, w[i], x[i] ); } free ( w ); free ( x ); } return; } /******************************************************************************/ void gegenbauer_integral_test ( ) /******************************************************************************/ /* Purpose: gegenbauer_integral_test() tests gegenbauer_integral(). Licensing: This code is distributed under the MIT license. Modified: 14 June 2015 Author: John Burkardt */ { double alpha; int n; double value; alpha = 0.25; printf ( "\n" ); printf ( "GEGENBAUER_INTEGRAL_TEST\n" ); printf ( " GEGENBAUER_INTEGRAL evaluates\n" ); printf ( " Integral ( -1 < x < +1 ) x^n * (1-x*x)^alpha dx\n" ); printf ( "\n" ); printf ( " N Value\n" ); printf ( "\n" ); for ( n = 0; n <= 10; n++ ) { value = gegenbauer_integral ( n, alpha ); printf ( " %8d %24.16g\n", n, value ); } return; } /******************************************************************************/ void gegenbauer_polynomial_value_test ( ) /******************************************************************************/ /* Purpose: gegenbauer_polynomial_value_test() tests gegenbauer_polynomial_value(). Licensing: This code is distributed under the MIT license. Modified: 30 November 2015 Author: John Burkardt */ { double alpha; double *c; double fx; int m; int n; int n_data; double x[1]; double xscalar; printf ( "\n" ); printf ( "GEGENBAUER_POLYNOMIAL_VALUE_TEST:\n" ); printf ( " GEGENBAUER_POLYNOMIAL_VALUE evaluates the Gegenbauer polynomial.\n" ); printf ( "\n" ); printf ( " M ALPHA X GPV GEGENBAUER\n" ); printf ( "\n" ); n_data = 0; for ( ; ; ) { gegenbauer_polynomial_values ( &n_data, &m, &alpha, &xscalar, &fx ); if ( n_data == 0 ) { break; } /* Since GEGENBAUER_POLYNOMIAL_VALUE expects a vector input X, we have to do a little "rewrapping" of the input. */ n = 1; x[0] = xscalar; c = gegenbauer_polynomial_value ( m, n, alpha, x ); printf ( " %6d %8.2f %8.2f %12f %12f\n", m, alpha, x[0], fx, c[m+0*(m+1)] ); free ( c ); } return; } /******************************************************************************/ void gegenbauer_ss_compute_test ( ) /******************************************************************************/ /* Purpose: gegenbauer_ss_compute_test() tests gegenbauer_ss_compute(). Licensing: This code is distributed under the MIT license. Modified: 24 June 2008 Author: John Burkardt */ { double alpha; int i; int n; double *w; double *x; alpha = 0.5; printf ( "\n" ); printf ( "GEGENBAUER_SS_COMPUTE_TEST\n" ); printf ( " GEGENBAUER_SS_COMPUTE computes a Gauss-Gegenbauer rule;\n" ); printf ( "\n" ); printf ( " Abscissas X and weights W for a Gauss Gegenbauer rule\n" ); printf ( " with ALPHA = %f\n", alpha ); printf ( "\n" ); printf ( " W X\n" ); for ( n = 1; n <= 10; n++ ) { printf ( "\n" ); w = ( double * ) malloc ( n * sizeof ( double ) ); x = ( double * ) malloc ( n * sizeof ( double ) ); gegenbauer_ss_compute ( n, alpha, x, w ); for ( i = 0; i < n; i++ ) { printf ( " %2d %24.16g %24.16g\n", i, w[i], x[i] ); } free ( w ); free ( x ); } return; } /******************************************************************************/ void gegenbauer_to_monomial_matrix_test ( ) /******************************************************************************/ /* Purpose: gegenbauer_to_monomial_matrix_test() tests gegenbauer_to_monomial_matrix(). Licensing: This code is distributed under the MIT license. Modified: 15 April 2024 Author: John Burkardt */ { double *A; double alpha; double *g; int i; int j; int n = 5; double *mono; printf ( "\n" ); printf ( "gegenbauer_to_monomial_matrix_test():\n" ); printf ( " gegenbauer_to_monomial_matrix() evaluates the matrix\n" ); printf ( " which converts Gegenbauer polyjomial coefficients\n" ); printf ( " to monomial coefficients.\n" ); alpha = 0.5; A = gegenbauer_to_monomial_matrix ( n, alpha ); r8mat_print ( n, n, A, " Gegenbauer to Monomial matrix G:" ); g = ( double * ) malloc ( n * sizeof ( double ) ); for ( i = 0; i < n; i++ ) { for ( j = 0; j < n; j++ ) { g[j] = 0.0; } g[i] = 1.0; mono = r8mat_mv_new ( n, n, A, g ); printf ( "\n" ); printf ( " Monomial form of Gegenbauer polynomial %d\n", i ); r8poly_print ( i, mono, "" ); free ( mono ); } free ( A ); free ( g ); return; } /******************************************************************************/ void imtqlx_test ( ) /******************************************************************************/ /* Purpose: imtqlx_test() tests imtqlx(). Licensing: This code is distributed under the MIT license. Modified: 10 June 2015 Author: John Burkardt. */ { double angle; double d[5]; double e[5]; int i; double lam[5]; double lam2[5]; int n = 5; double qtz[5]; double r8_pi = 3.141592653589793; double z[5]; printf ( "\n" ); printf ( "IMTQLX_TEST\n" ); printf ( " IMTQLX takes a symmetric tridiagonal matrix A\n" ); printf ( " and computes its eigenvalues LAM.\n" ); printf ( " It also accepts a vector Z and computes Q'*Z,\n" ); printf ( " where Q is the matrix that diagonalizes A.\n" ); for ( i = 0; i < n; i++ ) { d[i] = 2.0; } for ( i = 0; i < n - 1; i++ ) { e[i] = -1.0; } e[n-1] = 0.0; for ( i = 0; i < n; i++ ) { z[i] = 1.0; } /* On input, LAM is D, and QTZ is Z. */ for ( i = 0; i < n; i++ ) { lam[i] = d[i]; } for ( i = 0; i < n; i++ ) { qtz[i] = z[i]; } imtqlx ( n, lam, e, qtz ); r8vec_print ( n, lam, " Computed eigenvalues:" ); for ( i = 0; i < n; i++ ) { angle = ( double ) ( i + 1 ) * r8_pi / ( double ) ( 2 * ( n + 1 ) ); lam2[i] = 4.0 * pow ( sin ( angle ), 2 ); } r8vec_print ( n, lam2, " Exact eigenvalues:" ); r8vec_print ( n, z, " Vector Z:" ); r8vec_print ( n, qtz, " Vector Q'*Z:" ); return; } /******************************************************************************/ void monomial_to_gegenbauer_matrix_test ( ) /******************************************************************************/ /* Purpose: monomial_to_gegenbauer_matrix_test() tests monomial_to_gegenbauer_matrix(). Licensing: This code is distributed under the MIT license. Modified: 24 April 2024 Author: John Burkardt */ { double alpha; double *G; double *Ginv; double *I; int n = 5; printf ( "\n" ); printf ( "monomial_to_gegenbauer_matrix_test():\n" ); printf ( " monomial_to_gegenbauer_matrix() evaluates the matrix\n" ); printf ( " which converts monomial polynomial coefficients\n" ); printf ( " to Gegenbauer coefficients.\n" ); alpha = 0.5; printf ( " Using parameter alpha = %g\n", alpha ); G = gegenbauer_to_monomial_matrix ( n, alpha ); r8mat_print ( n, n, G, " Gegenbauer to Monomial matrix G:" ); Ginv = monomial_to_gegenbauer_matrix ( n, alpha ); r8mat_print ( n, n, Ginv, " Monomial to Gegenbauer matrix Ginv:" ); I = r8mat_mm_new ( n, n, n, G, Ginv ); r8mat_print ( n, n, I, " I = G * Ginv:" ); free ( G ); free ( Ginv ); free ( I ); return; } /******************************************************************************/ void r8_hyper_2f1_test ( ) /******************************************************************************/ /* Purpose: r8_hyper_2f1_test() tests r8_hyper_2f1(). Licensing: This code is distributed under the MIT license. Modified: 11 May 2012 Author: John Burkardt */ { double a; double b; double c; double fx; double fx2; int n_data; double x; printf ( "\n" ); printf ( "R8_HYPER_2F1_TEST:\n" ); printf ( " R8_HYPER_2F1 evaluates the hypergeometric function 2F1.\n" ); printf ( "\n" ); printf ( " A B C X " ); printf ( " 2F1 2F1 DIFF\n" ); printf ( " " ); printf ( "(tabulated) (computed)\n" ); printf ( "\n" ); n_data = 0; for ( ; ; ) { hyper_2f1_values ( &n_data, &a, &b, &c, &x, &fx ); if ( n_data == 0 ) { break; } fx2 = r8_hyper_2f1 ( a, b, c, x ); printf ( " %6f %6f %6f %6f %24.16g %24.16g %10.4g\n", a, b, c, x, fx, fx2, fabs ( fx - fx2 ) ); } return; } /******************************************************************************/ void r8_uniform_ab_test ( ) /******************************************************************************/ /* Purpose: r8_uniform_ab_test() tests r8_uniform_ab(). Licensing: This code is distributed under the MIT license. Modified: 05 May 2006 Author: John Burkardt */ { double a; double b; double c; int i; int seed; b = 10.0; c = 25.0; seed = 17; printf ( "\n" ); printf ( "R8_UNIFORM_AB_TEST\n" ); printf ( " R8_UNIFORM_AB produces a random real in a given range.\n" ); printf ( "\n" ); printf ( " Using range %f <= A <= %f.\n", b, c ); printf ( "\n" ); printf ( "\n" ); printf ( " I A\n" ); printf ( "\n" ); for ( i = 0; i < 10; i++ ) { a = r8_uniform_ab ( b, c, &seed ); printf ( " %4d %10f\n", i, a ); } return; } /******************************************************************************/ double *r8mat_mm_new ( int n1, int n2, int n3, double a[], double b[] ) /******************************************************************************/ /* Purpose: r8mat_mm_new() multiplies two matrices. Discussion: An R8MAT is a doubly dimensioned array of R8 values, stored as a vector in column-major order. For this routine, the result is returned as the function value. Licensing: This code is distributed under the MIT license. Modified: 08 April 2009 Author: John Burkardt Input: int N1, N2, N3, the order of the matrices. double A[N1*N2], double B[N2*N3], the matrices to multiply. Output: double R8MAT_MM_NEW[N1*N3], the product matrix C = A * B. */ { double *c; int i; int j; int k; c = ( double * ) malloc ( n1 * n3 * sizeof ( double ) ); for ( i = 0; i < n1; i++ ) { for ( j = 0; j < n3; j++ ) { c[i+j*n1] = 0.0; for ( k = 0; k < n2; k++ ) { c[i+j*n1] = c[i+j*n1] + a[i+k*n1] * b[k+j*n2]; } } } return c; } /******************************************************************************/ double *r8mat_mv_new ( int m, int n, double a[], double x[] ) /******************************************************************************/ /* Purpose: r8mat_mv_new() multiplies a matrix times a vector. Discussion: An R8MAT is a doubly dimensioned array of R8 values, stored as a vector in column-major order. For this routine, the result is returned as the function value. Licensing: This code is distributed under the MIT license. Modified: 15 April 2014 Author: John Burkardt Input: int M, N, the number of rows and columns of the matrix. double A[M,N], the M by N matrix. double X[N], the vector to be multiplied by A. Output: double R8MAT_MV_NEW[M], the product A*X. */ { int i; int j; double *y; y = ( double * ) malloc ( m * sizeof ( double ) ); for ( i = 0; i < m; i++ ) { y[i] = 0.0; for ( j = 0; j < n; j++ ) { y[i] = y[i] + a[i+j*m] * x[j]; } } return y; } /******************************************************************************/ void r8mat_print ( int m, int n, double a[], char *title ) /******************************************************************************/ /* Purpose: r8mat_print() prints an R8MAT. Discussion: An R8MAT is a doubly dimensioned array of R8 values, stored as a vector in column-major order. Entry A(I,J) is stored as A[I+J*M] Licensing: This code is distributed under the MIT license. Modified: 28 May 2008 Author: John Burkardt Input: int M, the number of rows in A. int N, the number of columns in A. double A[M*N], the M by N matrix. char *TITLE, a title. */ { r8mat_print_some ( m, n, a, 1, 1, m, n, title ); return; } /******************************************************************************/ void r8mat_print_some ( int m, int n, double a[], int ilo, int jlo, int ihi, int jhi, char *title ) /******************************************************************************/ /* Purpose: r8mat_print_some() prints some of an R8MAT. Discussion: An R8MAT is a doubly dimensioned array of R8 values, stored as a vector in column-major order. Licensing: This code is distributed under the MIT license. Modified: 26 June 2013 Author: John Burkardt Input: int M, the number of rows of the matrix. M must be positive. int N, the number of columns of the matrix. N must be positive. double A[M*N], the matrix. int ILO, JLO, IHI, JHI, designate the first row and column, and the last row and column to be printed. char *TITLE, a title. */ { # define INCX 5 int i; int i2hi; int i2lo; int j; int j2hi; int j2lo; fprintf ( stdout, "\n" ); fprintf ( stdout, "%s\n", title ); if ( m <= 0 || n <= 0 ) { fprintf ( stdout, "\n" ); fprintf ( stdout, " (None)\n" ); return; } /* Print the columns of the matrix, in strips of 5. */ for ( j2lo = jlo; j2lo <= jhi; j2lo = j2lo + INCX ) { j2hi = j2lo + INCX - 1; if ( n < j2hi ) { j2hi = n; } if ( jhi < j2hi ) { j2hi = jhi; } fprintf ( stdout, "\n" ); /* For each column J in the current range... Write the header. */ fprintf ( stdout, " Col: "); for ( j = j2lo; j <= j2hi; j++ ) { fprintf ( stdout, " %7d ", j - 1 ); } fprintf ( stdout, "\n" ); fprintf ( stdout, " Row\n" ); fprintf ( stdout, "\n" ); /* Determine the range of the rows in this strip. */ if ( 1 < ilo ) { i2lo = ilo; } else { i2lo = 1; } if ( m < ihi ) { i2hi = m; } else { i2hi = ihi; } for ( i = i2lo; i <= i2hi; i++ ) { /* Print out (up to) 5 entries in row I, that lie in the current strip. */ fprintf ( stdout, "%5d:", i - 1 ); for ( j = j2lo; j <= j2hi; j++ ) { fprintf ( stdout, " %14g", a[i-1+(j-1)*m] ); } fprintf ( stdout, "\n" ); } } return; # undef INCX } /******************************************************************************/ void r8poly_print ( int n, double a[], char *title ) /******************************************************************************/ /* Purpose: r8poly_print() prints a polynomial. Licensing: This code is distributed under the MIT license. Modified: 15 July 2015 Author: John Burkardt Input: int N, the dimension of A. double A[N+1], the polynomial coefficients. A(0) is the constant term and A(N) is the coefficient of X**N. char *TITLE, a title. */ { int i; int m; double mag; char plus_minus; if ( 0 < strlen ( title ) ) { printf ( "\n" ); printf ( "%s\n", title ); } printf ( "\n" ); if ( n < 0 ) { printf ( " p(x) = 0\n" ); return; } m = n; while ( a[m] == 0.0 ) { m = m - 1; } if ( a[m] < 0.0 ) { plus_minus = '-'; } else { plus_minus = ' '; } mag = fabs ( a[m] ); if ( 2 <= m ) { printf ( " p(x) = %c%g * x^%d\n", plus_minus, mag, m ); } else if ( m == 1 ) { printf ( " p(x) = %c%g * x\n", plus_minus, mag ); } else if ( m == 0 ) { printf ( " p(x) = %c%g\n", plus_minus, mag ); } for ( i = m - 1; 0 <= i; i-- ) { if ( a[i] < 0.0 ) { plus_minus = '-'; } else { plus_minus = '+'; } mag = fabs ( a[i] ); if ( mag != 0.0 ) { if ( 2 <= i ) { printf ( " %c%g * x^%d\n", plus_minus, mag, i ); } else if ( i == 1 ) { printf ( " %c%g * x\n", plus_minus, mag ); } else if ( i == 0 ) { printf ( " %c%g\n", plus_minus, mag ); } } } return; } /******************************************************************************/ void timestamp ( ) /******************************************************************************/ /* Purpose: timestamp() prints the current YMDHMS date as a time stamp. Example: 17 June 2014 09:45:54 AM Licensing: This code is distributed under the MIT license. Modified: 01 May 2021 Author: John Burkardt */ { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct tm *tm; time_t now; now = time ( NULL ); tm = localtime ( &now ); strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); printf ( "%s\n", time_buffer ); return; # undef TIME_SIZE }