double *square01_sample ( int n, int *seed )

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

    SQUARE01_SAMPLE samples points in the unit square in 2D.

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    18 January 2014

  Author:

    John Burkardt

  Reference:

    Russell Cheng,
    Random Variate Generation,
    in Handbook of Simulation,
    edited by Jerry Banks,
    Wiley, 1998, pages 168.

    Reuven Rubinstein,
    Monte Carlo Optimization, Simulation, and Sensitivity 
    of Queueing Networks,
    Krieger, 1992,
    ISBN: 0894647644,
    LC: QA298.R79.

  Parameters:

    Input, int N, the number of points.

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

    Output, double X[2*N], the points.
*/
{
  const int m = 2;
  double *x;

  x = r8mat_uniform_01_new ( m, n, seed );

  return x;
}
示例#2
0
double *latin_random_new ( int dim_num, int point_num, int *seed )

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

    LATIN_RANDOM_NEW returns points in a Latin Random square.

  Discussion:

    In each spatial dimension, there will be exactly one
    point whose coordinate value lies between consecutive
    values in the list:

      ( 0, 1, 2, ..., point_num ) / point_num

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    08 April 2003

  Author:

    John Burkardt

  Parameters:

    Input, int DIM_NUM, the spatial dimension.

    Input, int POINT_NUM, the number of points.

    Input/output, int *SEED, a seed for UNIFORM.

    Output, double LATIN_RANDOM_NEW[DIM_NUM,POINT_NUM], the points.
*/
{
  int i;
  int j;
  int *perm;
  double r;
  double *x;

  x = r8mat_uniform_01_new ( dim_num, point_num, seed );
/*
  For spatial dimension I, 
    pick a random permutation of 1 to POINT_NUM,
    force the corresponding I-th components of X to lie in the
    interval ( PERM[J]-1, PERM[J] ) / POINT_NUM.
*/
  for ( i = 0; i < dim_num; i++ )
  {
    perm = perm_uniform_new ( point_num, seed );

    for ( j = 0; j < point_num; j++ )
    {
      x[i+j*dim_num] = ( ( ( double ) perm[j] ) + x[i+j*dim_num] ) 
                       / ( ( double ) point_num );
    }
    free ( perm );
  }
  return x;
}
示例#3
0
void test0616 ( )

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

    TEST0616 tests PLANE_NORMAL_QR_TO_XYZ and PLANE_NORMAL_XYZ_TO_QR.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    12 November 2010

  Author:

    John Burkardt
*/
{
  double dif;
  int i;
  int j;
  int m = 3;
  int n = 5;
  double *normal;
  double *pp;
  double pq[3];
  double pr[3];
  double *qr1;
  double *qr2;
  int seed;
  double t;
  double *xyz;

  seed = 123456789;

  printf ( "\n" );
  printf ( "TEST0616\n" );
  printf ( "  For a normal plane, with point PP and NORMAL vector,\n" );
  printf ( "  and in-plane basis vectors PQ and PR,\n" );
  printf ( "  PLANE_NORMAL_QR_TO_XYZ converts QR to XYZ coordinates;\n" );
  printf ( "  PLANE_NORMAL_XYZ_TO_QR converts XYZ to QR coordinates.\n" );
//
//  Choose PP and NORMAL at random.
//
  pp = r8vec_uniform_01_new ( m, &seed );

  normal = r8vec_uniform_01_new ( m, &seed );
//
//  Compute in-plane basis vectors PQ and PR.
//
  plane_normal_basis_3d ( pp, normal, pq, pr );
//
//  Choose random Q, R coordinates.
//
  qr1 = r8mat_uniform_01_new ( m - 1, n, &seed );
//
//  Convert to XYZ.
//
  xyz = plane_normal_qr_to_xyz ( pp, normal, pq, pr, n, qr1 );
//
//  Convert XYZ to QR.
//
  qr2 = plane_normal_xyz_to_qr ( pp, normal, pq, pr, n, xyz );

  dif = 0.0;
  for ( j = 0; j < n; j++ )
  {
    t = 0.0;
    for ( i = 0; i < m - 1; i++ )
    {
      t = t + pow ( qr1[0+j*2] - qr2[0+j*2], 2 );
    }
    t = sqrt ( t );
    dif = r8_max ( dif, t );
  }

  printf ( "\n" );
  printf ( "  Maximum difference was %f\n", dif );

  free ( normal );
  free ( pp );
  free ( qr1 );
  free ( qr2 );
  free ( xyz );

  return;
}
示例#4
0
void test21011 ( )

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

    TEST21011 tests TRIANGLE_CIRCUMCENTER.

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    28 October 2010

  Author:

    John Burkardt
*/
{
# define M1 2
# define TEST_NUM 4

  double *a12;
  int i;
  int j;
  int k;
  int m2;
  double *o1;
  double *o2;
  int m1 = M1;
  double pc1[M1];
  double *pc2;
  int seed;
  double t1[M1*3];
  double *t2;
  double t_test[M1*3*TEST_NUM] = {
         10.0,  5.0, 
         11.0,  5.0, 
         10.0,  6.0, 
         10.0,  5.0, 
         11.0,  5.0, 
         10.5,  5.86602539, 
         10.0,  5.0, 
         11.0,  5.0, 
         10.5, 15.0, 
         10.0,  5.0, 
         11.0,  5.0, 
        20.0,   7.0 };
  int test;
  int test_num = TEST_NUM;

  printf ( "\n" );
  printf ( "TEST21011\n" );
  printf ( "  For a triangle in M dimensions, the circumenter can be computed by:\n" );
  printf ( "  TRIANGLE_CIRCUMCENTER;\n" );
//
//  Vary the dimension.
//
  for ( m2 = 2; m2 <= 5; m2++ )
  {
    seed = 123456789;

    printf ( "\n" );
    printf ( "  M2 = %d\n", m2 );

    t2 = ( double * ) malloc ( m2 * 3 * sizeof ( double ) );
//
//  Randomly choose a mapping P2 = O2 + A12 * ( P1 - O1 )
//
    a12 = r8mat_uniform_01_new ( m2, m1, &seed );
    o1 = r8vec_uniform_01_new ( m1, &seed );
    o2 = r8vec_uniform_01_new ( m2, &seed );
//
//  Map each M1-dimensional triangle into M2 space.
//
    for ( test = 0; test < test_num; test++ )
    {
      for ( j = 0; j < 3; j++ )
      {
        for ( i = 0; i < m1; i++ )
        {
          t1[i+j*m1] = t_test[i+j*m1+test*m1*3];
        }
      }
      for ( j = 0; j < 3; j++ )
      {
        t1[i+j*m1] = t1[i+j*m1] - o1[i];
      }

      for ( j = 0; j < 3; j++ )
      {
        for ( i = 0; i < m2; i++ )
        {
          t2[i+j*m2] = 0.0;
          for ( k = 0; k < m1; k++ )
          {
            t2[i+j*m2] = t2[i+j*m2] + a12[i+k*m2] * t1[k+j*m1];
          }
        }
      }
      for ( j = 0; j < 3; j++ )
      {
        for ( i = 0; i < m2; i++ )
        {
          t2[i+j*m2] = t2[i+j*m2] + o2[i];
        }
      }

      pc2 = triangle_circumcenter ( m2, t2 );

      r8vec_print ( m2, pc2, "  Circumcenter by TRIANGLE_CIRCUMCENTER:" );
      printf ( "\n" );
      printf ( "  Distances from circumcenter to vertices:\n" );
      printf ( "\n" );
      for ( j = 0; j < 3; j++ )
      {
        printf ( "  %f\n", r8vec_norm_affine ( m2, pc2, t2+j*m2 ) );
      }
      free ( pc2 );
    }
    free ( a12 );
    free ( o1 );
    free ( o2 );
    free ( t2 );
  }
  return;
}
void test03 ( )

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

    TEST03 tests R82VEC_SORT_QUICK_A.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    23 October 2012

  Author:

    John Burkardt
*/
{
# define N 12
# define DIM_NUM 2

  double *a;
  double b = 0.0;
  double c = 10.0;
  int i;
  int j;
  int seed = 123456789;

  printf ( "\n" );
  printf ( "TEST03\n" );
  printf ( "  R82VEC_SORT_QUICK_A sorts a vector\n" );
  printf ( "    as part of a quick sort.\n" );
  printf ( "  Using initial random number seed = %d", seed );

  a = r8mat_uniform_01_new ( DIM_NUM, N, &seed );

  for ( j = 0; j < N; j++ )
  {
    for ( i = 0; i < DIM_NUM; i++ )
    {
      a[i+j*DIM_NUM] = 10.0 * a[i+j*DIM_NUM];
    }
  }
/*
  For better testing, give a few elements the same first component.
*/
  a[0+2*(3-1)] = a[0+2*(5-1)];
  a[0+2*(4-1)] = a[0+2*(12-1)];
/*
  Make two entries equal.
*/
  a[0+2*(7-1)] = a[0+2*(11-1)];
  a[1+2*(7-1)] = a[1+2*(11-1)];

  r8mat_transpose_print ( DIM_NUM, N, a, "  Before sorting:" );

  r82vec_sort_quick_a ( N, a );

  r8mat_transpose_print ( DIM_NUM, N, a, "  Sorted array:" );

  free ( a );

  return;
# undef N
# undef DIM_NUM
}
void test02 ( )

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

    TEST02 tests R82VEC_PART_QUICK_A.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    23 October 2012

  Author:

    John Burkardt
*/
{
# define N 12
# define DIM_NUM 2

  double *a;
  int i;
  int j;
  int l;
  int r;
  int seed = 123456789;

  printf ( "\n" );
  printf ( "TEST02\n" );
  printf ( "  D2VEC_PART_QUICK_A reorders a D2 vector\n" );
  printf ( "    as part of a quick sort.\n" );
  printf ( "  Using initial random number seed = %d\n", seed );

  a = r8mat_uniform_01_new ( DIM_NUM, N, &seed );

  for ( j = 0; j < N; j++ )
  {
    for ( i = 0; i < DIM_NUM; i++ )
    {
      a[i+j*DIM_NUM] = 10.0 * a[i+j*DIM_NUM];
    }
  }

  r8mat_transpose_print ( DIM_NUM, N, a, "  Before rearrangment:" );

  r82vec_part_quick_a ( N, a, &l, &r );

  printf ( "\n" );
  printf ( "  Rearranged array\n" );
  printf ( "  Left index =  %d\n", l );
  printf ( "  Key index =   %d\n", l + 1 );
  printf ( "  Right index = %d\n", r );

  r8mat_transpose_print ( DIM_NUM, l,     a,         "  Left half:" );
  r8mat_transpose_print ( DIM_NUM, 1,     a+2*l,     "  Key:" );
  r8mat_transpose_print ( DIM_NUM, N-l-1, a+2*(l+1), "  Right half:" );

  free ( a );

  return;
# undef N
# undef DIM_NUM
}