void test03 ( )

/******************************************************************************/
/*
  Purpose:

    TEST03 tests the Partition-of-Unity property.

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    11 February 2012

  Author:

    John Burkardt
*/
{
  double *bvec;
  int n;
  int n_data;
  int seed;
  double x;

  printf ( "\n" );
  printf ( "TEST03:\n" );
  printf ( "  BERNSTEIN_POLY evaluates the Bernstein polynomials\n" );
  printf ( "  based on the interval [0,1].\n" );
  printf ( "\n" );
  printf ( "  Here we test the partition of unity property.\n" );
  printf ( "\n" );
  printf ( "     N     X          Sum ( 0 <= K <= N ) BP01(N,K)(X)\n" );
  printf ( "\n" );

  seed = 123456789;

  for ( n = 0; n <= 10; n++ )
  {
    x = r8_uniform_01 ( &seed );

    bvec = bernstein_poly_01 ( n, x );

    printf ( "  %4d  %7f  %14g\n", n, x, r8vec_sum ( n + 1, bvec ) );

    free ( bvec );
  }
  return;
}
void test01 ( )

/******************************************************************************/
/*
  Purpose:

    TEST01 compares exact and estimated monomial integrals.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    20 August 2014

  Author:

    John Burkardt
*/
{
  int e_max = 6;
  int e1;
  int e2;
  int e3;
  double error;
  double exact;
  int expon[3];
  int m = 3;
  int n = 500000;
  double q;
  int seed;
  double *value;
  double *x;

  printf ( "\n" );
  printf ( "TEST01\n" );
  printf ( "  Compare exact and estimated integrals \n" );
  printf ( "  over the unit wedge in 3D.\n" );
/*
  Get sample points.
*/
  seed = 123456789;
  x = wedge01_sample ( n, &seed );

  printf ( "\n" );
  printf ( "  Number of sample points used is %d\n", n );
  printf ( "\n" );
  printf ( "   E1  E2  E3     MC-Estimate      Exact           Error\n" );
  printf ( "\n" );
/*
  Check all monomials up to total degree E_MAX.
*/
  for ( e3 = 0; e3 <= e_max; e3++ )
  {
    expon[2] = e3;
    for ( e2 = 0; e2 <= e_max - e3; e2++ )
    {
      expon[1] = e2;
      for ( e1 = 0; e1 <= e_max - e3 - e2; e1++ )
      {
        expon[0] = e1;

        value = monomial_value ( m, n, expon, x );

        q = wedge01_volume ( ) * r8vec_sum ( n, value ) / ( double ) ( n );
        exact = wedge01_integral ( expon );
        error = fabs ( q - exact );

        printf ( "  %2d  %2d  %2d  %14.6g  %14.6g  %14.6g\n",
          expon[0], expon[1], expon[2], q, exact, error );

        free ( value );
      }
    }
  }

  free ( x );

  return;
}
void test01 ( )

/******************************************************************************/
/*
  Purpose:

    TEST01 uses WEDGE01_SAMPLE with an increasing number of points.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    18 August 2014

  Author:

    John Burkardt
*/
{
  int e[3];
  int e_test[3*8] = {
    0, 0, 0, 
    1, 0, 0, 
    0, 1, 0, 
    0, 0, 1, 
    2, 0, 0, 
    1, 1, 0, 
    0, 0, 2, 
    3, 0, 0 };
  int i;
  int j;
  int m = 3;
  int n;
  double result;
  int seed;
  double *value;
  double *x;

  printf ( "\n" );
  printf ( "TEST01\n" );
  printf ( "  Use WEDGE01_SAMPLE for a Monte Carlo estimate of an\n" );
  printf ( "  integral over the interior of the unit wedge in 3D.\n" );

  seed = 123456789;

  printf ( "\n" );
  printf ( "         N        1               X               Y " );
  printf ( "              Z                X^2            XY              Z^2    " );
  printf ( "        X^3\n" );
  printf ( "\n" );

  n = 1;

  while ( n <= 65536 )
  {
    x = wedge01_sample ( n, &seed );

    printf ( "  %8d", n );

    for ( j = 0; j < 8; j++ )
    {
      for ( i = 0; i < m; i++ )
      {
        e[i] = e_test[i+j*m];
      }
 
      value = monomial_value ( m, n, e, x );

      result = wedge01_volume ( ) * r8vec_sum ( n, value ) / ( double ) ( n );
      printf ( "  %14.6g", result );
      free ( value );
    }

    printf ( "\n" );

    free ( x );

    n = 2 * n;
  }

  printf ( "     Exact" );

  for ( j = 0; j < 8; j++ )
  {
    for ( i = 0; i < m; i++ )
    {
      e[i] = e_test[i+j*m];
    }
    result = wedge01_integral ( e );
    printf ( "  %14.6g", result );
  }
  printf ( "\n" );

  return;
}
void test01 ( )

/******************************************************************************/
/*
  Purpose:

    TEST01 uses SPHERE01_SAMPLE with an increasing number of points.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    02 January 2014

  Author:

    John Burkardt
*/
{
  int e[3];
  int e_test[3*7] = {
    0, 0, 0, 
    2, 0, 0, 
    0, 2, 0, 
    0, 0, 2, 
    4, 0, 0, 
    2, 2, 0, 
    0, 0, 4 };
  double error;
  double exact;
  int i;
  int j;
  int n;
  double result;
  int seed;
  double *value;
  double *x;

  printf ( "\n" );
  printf ( "TEST01\n" );
  printf ( "  Use SPHERE01_SAMPLE to estimate integrals on the unit sphere surface.\n" );

  seed = 123456789;

  printf ( "\n" );
  printf ( "         N        1              X^2             Y^2" );
  printf ( "             Z^2             X^4            X^2Y^2          Z^4\n" );
  printf ( "\n" );

  n = 1;

  while ( n <= 65536 )
  {
    x = sphere01_sample ( n, &seed );
    printf ( "  %8d", n );
    for ( j = 0; j < 7; j++ )
    {
      for ( i = 0; i < 3; i++ )
      {
        e[i] = e_test[i+j*3];
      }

      value = monomial_value ( 3, n, e, x );

      result = sphere01_area ( ) * r8vec_sum ( n, value ) / ( double ) ( n );
      printf ( "  %14.10g", result );

      free ( value );
    }
    printf ( "\n" );

    free ( x );

    n = 2 * n;
  }

  printf ( "\n" );
  printf ( "     Exact" );
  for ( j = 0; j < 7; j++ )
  {
    for ( i = 0; i < 3; i++ )
    {
      e[i] = e_test[i+j*3];
    }
    exact = sphere01_monomial_integral ( e );
    printf ( "  %14.10g", exact );
  }
  printf ( "\n" );

  return;
}
double *polygon_sample ( int nv, double v[], int n, int *seed )

/******************************************************************************/
/*
  Purpose:

    POLYGON_SAMPLE uniformly samples a polygon.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    07 May 2014

  Author:

    John Burkardt

  Parameters:

    Input, int NV, the number of vertices.

    Input, double V[2*NV], the vertices of the polygon, listed in
    counterclockwise order.

    Input, int N, the number of points to create.

    Input/output, int *SEED, a seed for the random
    number generator.

    Output, double POLYGON_SAMPLE[2*N], the points.
*/
{
  double *area_cumulative;
  double area_polygon;
  double *area_relative;
  double *area_triangle;
  double area_percent;
  int i;
  int ip1;
  int j;
  int k;
  double *r;
  double *s;
  int *triangles;
  double *x;
  double *y;
/*
  Triangulate the polygon.
*/
  x = ( double * ) malloc ( nv * sizeof ( double ) );
  y = ( double * ) malloc ( nv * sizeof ( double ) );
  for ( i = 0; i < nv; i++ )
  {
    x[i] = v[0+i*2];
    y[i] = v[1+i*2];
  }

  triangles = polygon_triangulate ( nv, x, y );
/*
  Determine the areas of each triangle.
*/
  area_triangle = ( double * ) malloc ( ( nv - 2 ) * sizeof ( double ) );

  for ( i = 0; i < nv - 2; i++ )
  {
    area_triangle[i] = triangle_area ( 
      v[0+triangles[0+i*3]*2], v[1+triangles[0+i*3]*2], 
      v[0+triangles[1+i*3]*2], v[1+triangles[1+i*3]*2], 
      v[0+triangles[2+i*3]*2], v[1+triangles[2+i*3]*2] );
  }
/*
  Normalize the areas.
*/
  area_polygon = r8vec_sum ( nv - 2, area_triangle );

  area_relative = ( double * ) malloc ( ( nv - 2 ) * sizeof ( double ) );
  for ( i = 0; i < nv - 2; i++ )
  {
    area_relative[i] = area_triangle[i] / area_polygon;
  }
/*
  Replace each area by the sum of itself and all previous ones.
*/
  area_cumulative = ( double * ) malloc ( ( nv - 2 ) * sizeof ( double ) );
  area_cumulative[0] = area_relative[0];
  for ( i = 1; i < nv - 2; i++ )
  {
    area_cumulative[i] = area_relative[i] + area_cumulative[i-1];
  }

  s = ( double * ) malloc ( 2 * n * sizeof ( double ) );

  for ( j = 0; j < n; j++ )
  {
/*
  Choose triangle I at random, based on areas.
*/
    area_percent = r8_uniform_01 ( seed );

    for ( k = 0; k < nv - 2; k++ )
    {
      i = k;

      if ( area_percent <= area_cumulative[k] )
      {
        break;
      }
    }
/*
  Now choose a point at random in triangle I.
*/
    r = r8vec_uniform_01_new ( 2, seed );

    if ( 1.0 < r[0] + r[1] )
    {
      r[0] = 1.0 - r[0];
      r[1] = 1.0 - r[1];
    }

    s[0+j*2] = ( 1.0 - r[0] - r[1] ) * v[0+triangles[0+i*3]*2]
                     + r[0]          * v[0+triangles[1+i*3]*2]
                            + r[1]   * v[0+triangles[2+i*3]*2];

    s[1+j*2] = ( 1.0 - r[0] - r[1] ) * v[1+triangles[0+i*3]*2]
                     + r[0]          * v[1+triangles[1+i*3]*2]
                            + r[1]   * v[1+triangles[2+i*3]*2];
    free ( r );
  }

  free ( area_cumulative );
  free ( area_relative );
  free ( area_triangle );
  free ( triangles );
  free ( x );
  free ( y );

  return s;
}
void test01 ( int degree, int n )

/******************************************************************************/
/*
  Purpose:

    TEST01 calls SQUARESYMQ for a quadrature rule of given order.

  Licensing:

    This code is distributed under the GNU GPL license.

  Modified:

    02 July 2014

  Author:

    Original FORTRAN77 version by Hong Xiao, Zydrunas Gimbutas.
    This C version by John Burkardt.

  Reference:

    Hong Xiao, Zydrunas Gimbutas,
    A numerical algorithm for the construction of efficient quadrature
    rules in two and higher dimensions,
    Computers and Mathematics with Applications,
    Volume 59, 2010, pages 663-676.

  Parameters:

    Input, int DEGREE, the desired total polynomial degree exactness
    of the quadrature rule.

    Input, int N, the number of nodes.
*/
{
  double area;
  double d;
  int j;
  double *w;
  double *x;

  printf ( "\n" );
  printf ( "TEST01\n" );
  printf ( "  Symmetric quadrature rule for a square.\n" );
  printf ( "  Polynomial exactness degree DEGREE = %d\n", degree );

  area = 4.0;
/*
  Retrieve and print a symmetric quadrature rule.
*/
  x = ( double * ) malloc ( 2 * n * sizeof ( double ) );
  w = ( double * ) malloc ( n * sizeof ( double ) );

  square_symq ( degree, n, x, w );

  printf ( "\n" );
  printf ( "  Number of nodes N = %d\n", n );

  printf ( "\n" );
  printf ( "     J  W       X       Y\n" );
  printf ( "\n" );
  for ( j = 0; j < n; j++ )
  {
    printf ( "  %4d  %14.6g  %14.6g  %14.6g\n",
      j, w[j], x[0+j*2], x[1+j*2] );
  }

  d = r8vec_sum ( n, w );

  printf ( "   Sum  %g\n", d );
  printf ( "  Area  %g\n", area );

  return;
}
void test01 ( int nv, double v[] )

/******************************************************************************/
/*
  Purpose:

    TEST01 estimates integrals over a polygon in 2D.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    09 May 2014

  Author:

    John Burkardt
*/
{
  int e[2];
  int e_test[2*7] = {
    0, 0, 
    2, 0, 
    0, 2, 
    4, 0, 
    2, 2, 
    0, 4, 
    6, 0 };
  double error;
  double exact;
  int j;
  int n;
  double result;
  int seed;
  double *value;
  double *x;

  printf ( "\n" );
  printf ( "TEST01\n" );
  printf ( "  Use POLYGON_SAMPLE to estimate integrals\n" );
  printf ( "  over the interior of a polygon in 2D.\n" );

  seed = 123456789;

  printf ( "\n" );
  printf ( "         N" );
  printf ( "        1" );
  printf ( "              X^2 " );
  printf ( "             Y^2" );
  printf ( "             X^4" );
  printf ( "           X^2Y^2" );
  printf ( "             Y^4" );
  printf ( "           X^6\n" );
  printf ( "\n" );

  n = 1;

  while ( n <= 65536 )
  {
    x = polygon_sample ( nv, v, n, &seed );

    printf ( "  %8d", n );

    for ( j = 0; j < 7; j++ )
    {
      e[0] = e_test[0+j*2];
      e[1] = e_test[1+j*2];

      value = monomial_value ( 2, n, e, x );

      result = polygon_area ( nv, v ) * r8vec_sum ( n, value ) / ( double ) ( n );
      printf ( "  %14.6g", result );
    }

    printf ( "\n" );

    free ( value );
    free ( x );

    n = 2 * n;

  }

  printf ( "\n" );
  printf ( "     Exact" );
  for ( j = 0; j < 7; j++ )
  {
    e[0] = e_test[0+j*2];
    e[1] = e_test[1+j*2];

    result = polygon_monomial_integral ( nv, v, e );
    printf ( "  %14.6g", result );
  }

  printf ( "\n" );

  return;
}
double *shepard_interp_1d ( int nd, double xd[], double yd[], double p, int ni, 
  double xi[] )

/******************************************************************************/
/*
  Purpose:

    SHEPARD_INTERP_1D evaluates a 1D Shepard interpolant.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    01 October 2012

  Author:

    John Burkardt

  Reference:

    Donald Shepard,
    A two-dimensional interpolation function for irregularly spaced data,
    ACM '68: Proceedings of the 1968 23rd ACM National Conference,
    ACM, pages 517-524, 1969.

  Parameters:

    Input, int ND, the number of data points.

    Input, double XD[ND], the data points.

    Input, double YD[ND], the data values.

    Input, double P, the power.

    Input, int NI, the number of interpolation points.

    Input, double XI[NI], the interpolation points.

    Output, double SHEPARD_INTERP_1D[NI], the interpolated values.
*/
{ 
  int i;
  int j;
  int k;
  double s;
  double *w;
  double *yi;
  int z;

  w = ( double * ) malloc ( nd * sizeof ( double ) );
  yi = ( double * ) malloc ( ni * sizeof ( double ) );

  for ( i = 0; i < ni; i++ )
  {
    if ( p == 0.0 )
    {
      for ( j = 0; j < nd; j++ )
      {
        w[j] = 1.0 / ( double ) ( nd );
      }
    }
    else
    {
      z = -1;
      for ( j = 0; j < nd; j++ )
      {
        w[j] = r8_abs ( xi[i] - xd[j] );
        if ( w[j] == 0.0 )
        {
          z = j;
          break;
        }
      }

      if ( z != -1 )
      {
        for ( j = 0; j < nd; j++ )
        {
          w[j] = 0.0;
        }
        w[z] = 1.0;
      }
      else
      {
        for ( j = 0; j < nd; j++ )
        {
          w[j] = 1.0 / pow ( w[j], p );
        }
        s = r8vec_sum ( nd, w );
        for ( j = 0; j < nd; j++ )
        {
          w[j] = w[j] / s;
        }
      }
    }
    yi[i] = r8vec_dot_product ( nd, w, yd );
  }
  free ( w );

  return yi;
}
Beispiel #9
0
void test02 ( )

/******************************************************************************/
/*
  Purpose:

    TEST02 uses random points as abscissas.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    15 April 2014

  Author:

    John Burkardt
*/
{
  double a;
  double b;
  int d;
  double e;
  double exact;
  double *f;
  int i;
  int n = 50;
  double q;
  int seed;
  double *w;
  double *x;

  printf ( "\n" );
  printf ( "TEST02\n" );
  printf ( "  WEIGHTS_LS computes the weights for a\n" );
  printf ( "  least squares quadrature rule.\n" );
  printf ( "\n" );
  printf ( "  Pick 50 random values in [-1,+1].\n" );
  printf ( "  Compare Monte Carlo (equal weight) integral estimate\n" );
  printf ( "  to least squares estimates of degree D = 0, 1, 2, 3, 4.\n" );
  printf ( "  For low values of D, the least squares estimate improves.\n" );
  printf ( "\n" );
  printf ( "  As D increases, the estimate can deteriorate.\n" );
/*
  Define the integration interval.
*/
  a = -5.0;
  b = +5.0;
/*
  Get random values.
*/
  seed = 123456789;
  x = r8vec_uniform_ab_new ( n, a, b, &seed );
/*
  Evaluate the function.
*/
  f = ( double * ) malloc ( n * sizeof ( double ) );
  for ( i = 0; i < n; i++ )
  {
    f[i] = 1.0 / ( 1.0 + x[i] * x[i] );
  }
  exact = atan ( b ) - atan ( a );

  printf ( "\n" );
  printf ( "  Rule         Estimate          Error\n" );
/*
  Get the MC estimate.
*/
  q = ( b - a ) * r8vec_sum ( n, f ) / ( double ) ( n );
  e = fabs ( q - exact );

  printf ( "\n" );
  printf ( "  MC     %14.6g  %14.6g\n", q, e );
  printf ( "\n" );
/*
  Using the same points, compute the least squares weights
  for polynomial approximation of degree D.
*/
  for ( d = 0; d <= 15; d++ )
  {
    w = weights_ls ( d, a, b, n, x );
    q = r8vec_dot_product ( n, w, f );
    e = fabs ( q - exact );
    printf ( "  LS%2d  %14.6g  %14.6g\n", d, q, e );
    free ( w );
  }

  q = exact;
  e = fabs ( q - exact );
  printf ( "\n" );
  printf ( "  EXACT %14.6g  %14.6g\n", q, e );

  free ( f );
  free ( x );

  return;
}
void sgmga_vcn_tests ( void )

/******************************************************************************/
/*
  Purpose:

    SGMGA_VCN_TESTS calls SGMGA_VCN_TEST.

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    27 November 2009

  Author:

    John Burkardt
*/
{
  int dim;
  int dim_num;
  int dim_num_array[12] = {
    2, 2, 2, 2, 2, 
    3, 3, 3, 3, 3, 
    4, 4 };
  double *importance;
  int level_max;
  int level_max_array[12] = {
    0, 1, 2, 3, 4, 
    0, 1, 2, 3, 4, 
    2, 3 };
  double *level_weight;
  double q_max;
  double q_min;
  int test;
  int test_num = 12;

  printf ( "\n" );
  printf ( "SGMGA_VCN_TESTS\n" );
  printf ( "  calls SGMGA_VCN_TEST.\n" );
/*
  Isotropic examples.
*/
  for ( test = 0; test < test_num; test++ )
  {
    dim_num = dim_num_array[test];
    importance = ( double * ) malloc ( dim_num * sizeof ( double ) );
    for ( dim = 0; dim < dim_num; dim++ )
    {
      importance[dim] = 1.0;
    }
    level_weight = ( double * ) malloc ( dim_num * sizeof ( double ) );
    sgmga_importance_to_aniso ( dim_num, importance, level_weight );
    level_max = level_max_array[test];
    q_min = ( double ) ( level_max ) - r8vec_sum ( dim_num, level_weight );
    q_max = ( double ) ( level_max );

    sgmga_vcn_test ( dim_num, importance, level_weight, q_min, q_max );

    free ( importance );
    free ( level_weight );
  }
/*
  Anisotropic examples.
*/
  for ( test = 0; test < test_num; test++ )
  {
    dim_num = dim_num_array[test];
    importance = ( double * ) malloc ( dim_num * sizeof ( double ) );
    for ( dim = 0; dim < dim_num; dim++ )
    {
      importance[dim] = ( double ) ( dim + 1 );
    }
    level_weight = ( double * ) malloc ( dim_num * sizeof ( double ) );
    sgmga_importance_to_aniso ( dim_num, importance, level_weight );
    level_max = level_max_array[test];
    q_min = ( double ) ( level_max ) - r8vec_sum ( dim_num, level_weight );
    q_max = ( double ) ( level_max );

    sgmga_vcn_test ( dim_num, importance, level_weight, q_min, q_max );

    free ( importance );
    free ( level_weight );
  }

  return;
}
void test01 ( )

/******************************************************************************/
/*
  Purpose:

    TEST01 compares exact and estimated monomial integrals.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    18 January 2014

  Author:

    John Burkardt
*/
{
  int e;
  double error;
  double exact;
  int m = 1;
  int n = 4192;
  double result;
  int seed;
  int test;
  int test_num = 11;
  double *value;
  double *x;

  printf ( "\n" );
  printf ( "TEST01\n" );
  printf ( "  Compare exact and estimated integrals \n" );
  printf ( "  over the length of the unit line in 1D.\n" );
/*
  Get sample points.
*/
  seed = 123456789;
  x = line01_sample ( n, &seed );

  printf ( "\n" );
  printf ( "  Number of sample points used is %d\n", n );
  printf ( "\n" );
  printf ( "   E     MC-Estimate      Exact           Error\n" );
  printf ( "\n" );

  for ( test = 1; test <= test_num; test++ )
  {
    e = test - 1;

    value = monomial_value_1d ( n, e, x );

    result = line01_length ( ) * r8vec_sum ( n, value ) / ( double ) ( n );
    exact = line01_monomial_integral ( e );
    error = fabs ( result - exact );

    printf ( "  %2d  %14.6g  %14.6g  %10.2e\n",
      e, result, exact, error );

    free ( value );
  }

  free ( x );

  return;
}
void test01 ( )

/******************************************************************************/
/*
  Purpose:

    TEST01 verifies LOCAL_BASIS_1D.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    04 July 2013

  Author:

    John Burkardt

  Parameters:

    None
*/
{
# define NODE_NUM 4

  double a;
  double b;
  int i;
  int j;
  int node_num = NODE_NUM;
  double node_x[NODE_NUM] = { 1.0, 2.0, 4.0, 4.5 };
  double *phi;
  double phi_matrix[NODE_NUM*NODE_NUM];
  double s;
  int seed;
  double x;

  printf ( "\n" );
  printf ( "TEST01:\n" );
  printf ( "  LOCAL_BASIS_1D evaluates the local basis functions\n" );
  printf ( "  for a 1D element.\n" );
  printf ( "\n" );
  printf ( "  Test that the basis functions, evaluated at the nodes,\n" );
  printf ( "  form the identity matrix.\n" );
  printf ( "\n" );
  printf ( "  Number of nodes = %d\n", node_num );

  printf ( "\n" );
  printf ( "  Node coordinates:\n" );
  printf ( "\n" );
  for ( j = 0; j < node_num; j++ )
  {
    printf ( "  %8d  %7g\n", j, node_x[j] );
  }

  for ( j = 0; j < node_num; j++ )
  {
    x = node_x[j];
    phi = local_basis_1d ( node_num, node_x, x );
    for ( i = 0; i < node_num; i++ )
    {
      phi_matrix[i+j*node_num] = phi[i];
    }
    free ( phi );
  }

  r8mat_print ( node_num, node_num, phi_matrix, "  A(I,J) = PHI(I) at node (J):" );

  seed = 123456789;

  printf ( "\n" );
  printf ( "  The PHI functions should sum to 1 at random X values:\n" );
  printf ( "\n" );
  printf ( "       X        Sum ( PHI(:)(X) )\n" );
  printf ( "\n" );

  a = 1.0;
  b = 4.5;
  for ( j = 1; j <= 5; j++ )
  {
    x = r8_uniform_ab ( a, b, &seed );
    phi = local_basis_1d ( node_num, node_x, x );
    s = r8vec_sum ( node_num, phi );
    printf ( "  %14g  %14g\n", x, s );
    free ( phi );
  }

  return;
# undef NODE_NUM
}
Beispiel #13
0
double fem_basis_md ( int m, int i[], double x[] )

/******************************************************************************/
/*
  Purpose:

    FEM_BASIS_MD evaluates an arbitrary M-dimensional basis function.

  Discussion:

    Given the maximum degree D for the polynomial basis defined
    on a reference tetrahedron, we have
    ( D + 1 ) * ( D + 2 ) * ( D + 3 ) / 6 monomials
    of degree at most D.  In each barycentric coordinate, we define
    D+1 planes, so that 0 <= I, J, K, L <= D and I+J+K+L = D, with
    (I,J,K,L) corresponding to
    * the basis point (X,Y,Z)(I,J,K,L) = ( I/D, J/D, K/D );
    * the basis monomial P(I,J,K,L)(X,Y,Z) = X^I Y^J Z^K.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    10 October 2010

  Author:

    John Burkardt

  Parameters:

    Input, int M, the spatial dimension.

    Input, int I[M+1], the integer barycentric coordinates of the
    basis function.  The polynomial degree D = sum ( I );

    Input, double X[M], the evaluation point.

    Output, double FEM_BASIS_MD, the value of the basis function at (X,Y,Z).
*/
{
  double c;
  int d;
  double l;
  int p;
  int q;
  double w;

  d = i4vec_sum ( m + 1, i );

  l = 1.0;
  c = 1.0;

  for ( q = 0; q < m; q++ )
  {
    for ( p = 0; p < i[q]; p++ )
    {
      l = l * ( d * x[q] - p );
      c = c * (     i[q] - p );
    }
  }

  w = 1.0 - r8vec_sum ( m, x );

  for ( p = 0; p < i[m]; p++ )
  {
    l = l * ( d * w    - p );
    c = c * (     i[m] - p );
  }

  l = l / c;

  return l;
}
void test02 ( void )

/******************************************************************************/
/*
  Purpose:

    TEST02 samples each function using each grid.

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    30 January 2012

  Author:

    John Burkardt
*/
{
  double *f;
  double f_ave;
  double f_max;
  double f_min;
  int f_num;
  int fi;
  char ft[100];
  int g_num;
  int gi;
  int gn;
  char gt[100];
  double *gx;
  double *gy;

  printf ( "\n" );
  printf ( "TEST02\n" );
  printf ( "  Sample each function over each grid.\n" );

  g_num = g00_num ( );
  f_num = f00_num ( );

  for ( fi = 1; fi <= f_num; fi++ )
  {
    f00_title ( fi, ft );
    printf ( "\n" );
    printf ( "  %2d  %s\n", fi, ft );
    printf ( "        Grid Title                     " );
    printf ( "Min(F)          Ave(F)           Max(F)\n" );
    printf ( "\n" );

    for ( gi = 1; gi <= g_num; gi++ )
    {
      g00_title ( gi, gt );
      gn = g00_size ( gi );

      gx = ( double * ) malloc ( gn * sizeof ( double ) );
      gy = ( double * ) malloc ( gn * sizeof ( double ) );

      g00_xy ( gi, gn, gx, gy );

      f = ( double * ) malloc ( gn * sizeof ( double ) );

      f00_f0 ( fi, gn, gx, gy, f );

      f_max = r8vec_max ( gn, f );
      f_min = r8vec_min ( gn, f );
      f_ave = r8vec_sum ( gn, f );
      f_ave = f_ave / ( double ) ( gn );

      printf ( "  %4d  %25s  %14g  %14g  %14g\n", gi, gt, f_min, f_ave, f_max );

      free ( f );
      free ( gx );
      free ( gy );
    }
  }
  return;
}