コード例 #1
0
ファイル: md_proxy.c プロジェクト: SeanWallace/gemtc
void MD_InitParticles(void* params){
  printf("MD_InitParticles: Params: %p\n", params);
 // return; 
  
  //Params| &table | box[] | seed |
  //Bytes |   8    | 8*nd  |  4   |
  
  void *table = *((void**)params); 

  //Unpack Table  
  int np = *((int*)table);
  int nd = *(((int*)table) + 1);
  double *pos = ((double*)table) + 2;
 
  //Unpack Params
  double *box = (double*)(((void**)params)+1);
  int *seed = (int*)(box + nd);

  int i,j; 
  //Update values
  for ( j = 0; j < np ; j++){
    for ( i = 0; i < nd ; i++){
      printf("MD_InitParticles: Writting to pos @ i=%d, j=%d, nd=%d: %d", i, j, nd, i+j*nd);
      pos[i+j*nd] = box[i] * r8_uniform_01(seed);
    }
  }
}
コード例 #2
0
ファイル: md.c プロジェクト: sophiayyxy/swift-on-cloud
void initialize ( int np, int nd, double box[], int *seed, double pos[], 
  double vel[], double acc[] )

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

    INITIALIZE initializes the positions, velocities, and accelerations.

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    20 July 2008

  Author:

    Original FORTRAN90 version by Bill Magro.
    C version by John Burkardt.

  Parameters:

    Input, int NP, the number of particles.

    Input, int ND, the number of spatial dimensions.

    Input, double BOX[ND], specifies the maximum position
    of particles in each dimension.

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

    Output, double POS[ND*NP], the position of each particle.

    Output, double VEL[ND*NP], the velocity of each particle.

    Output, double ACC[ND*NP], the acceleration of each particle.
*/
{
  int i;
  int j;
/*
  Give the particles random positions within the box.
*/
  for ( j = 0; j < np; j++ )
  {
    for ( i = 0; i < nd; i++ )
    {
      pos[i+j*nd] = box[i] * r8_uniform_01 ( seed );
      vel[i+j*nd] = 0.0;
      acc[i+j*nd] = 0.0;
    }
  }
  return;
}
コード例 #3
0
int poisson_fixed_time ( double lambda, double time, int *seed )

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

    POISSON_FIXED_TIME counts the Poisson events in a fied time.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    28 September 2012

  Author:

    John Burkardt

  Parameters:

    Input, double LAMBDA, the average number of events 
    per unit time.

    Input, double TIME, the amount of time to observe.

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

    Output, int POISSON_FIXED_TIME, the number of Poisson events observed.
*/
{
  double dt;
  int n;
  double t;
  double u;

  n = 0;
  t = 0.0;

  while ( t < time )
  {
    u = r8_uniform_01 ( seed );
    dt = - log ( u ) / lambda;
    n = n + 1;
    t = t + dt;
  }

  return n;
}
コード例 #4
0
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;
}
コード例 #5
0
int *life_init ( double prob, int m, int n, int *seed )

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

    LIFE_INIT initializes the life grid.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    08 September 2013

  Author:

    John Burkardt

  Parameters:

    Input, double PROB, the probability that a grid cell
    should be alive.

    Input, int M, N, the number of rows and columns
    of interior grid cells.

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

    Output, int LIFE_INIT[(1+M+1)*(1+N+1)], the initial grid.
*/
{
  int *grid;
  int i;
  int j;
  double r;

  grid = ( int * ) malloc ( ( m + 2 ) * ( n + 2 ) * sizeof ( int ) );
  for ( j = 0; j <= n + 1; j++ )
  {
    for ( i = 0; i <= m + 1; i++ )
    {
      grid[i+j*(m+2)] = 0;
    }
  }

  for ( j = 1; j <= n; j++ )
  {
    for ( i = 1; i <= m; i++ )
    {
      r = r8_uniform_01 ( seed );
      if ( r <= prob )
      {
        grid[i+j*(m+2)] = 1;
      }
    }
  }

  return grid;
}
コード例 #6
0
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;
}
コード例 #7
0
double r8_normal_01 ( int *seed )

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

    R8_NORMAL_01 returns a unit pseudonormal R8.

  Discussion:

    The standard normal probability distribution function (PDF) has 
    mean 0 and standard deviation 1.

    Because this routine uses the Box Muller method, it requires pairs
    of uniform random values to generate a pair of normal random values.
    This means that on every other call, the code can use the second
    value that it calculated.

    However, if the user has changed the SEED value between calls,
    the routine automatically resets itself and discards the saved data.

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    10 June 2010

  Author:

    John Burkardt

  Parameters:

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

    Output, double R8_NORMAL_01, a normally distributed random value.
*/
{
# define R8_PI 3.141592653589793

  double r1;
  double r2;
  static int seed1 = 0;
  static int seed2 = 0;
  static int seed3 = 0;
  static int used = 0;
  double v1;
  static double v2 = 0.0;
/*
  If USED is odd, but the input SEED does not match
  the output SEED on the previous call, then the user has changed
  the seed.  Wipe out internal memory.
*/
  if ( ( used % 2 ) == 1 )
  {
    if ( *seed != seed2 )
    {
      used = 0;
      seed1 = 0;
      seed2 = 0;
      seed3 = 0;
      v2 = 0.0;
    }
  }
/*
  If USED is even, generate two uniforms, create two normals,
  return the first normal and its corresponding seed.
*/
  if ( ( used % 2 )== 0 )
  {
    seed1 = *seed;

    r1 = r8_uniform_01 ( seed );

    if ( r1 == 0.0 )
    {
      printf ( "\n" );
      printf ( "R8_NORMAL_01 - Fatal error!\n" );
      printf ( "  R8_UNIFORM_01 returned a value of 0.\n" );
      exit ( 1 );
    }

    seed2 = *seed;
    r2 = r8_uniform_01 ( seed );
    seed3 = *seed;
    *seed = seed2;

    v1 = sqrt ( - 2.0 * log ( r1 ) ) * cos ( 2.0 * R8_PI * r2 );
    v2 = sqrt ( - 2.0 * log ( r1 ) ) * sin ( 2.0 * R8_PI * r2 );
  }
/*
  If USED is odd (and the input SEED matched the output value from
  the previous call), return the second normal and its corresponding seed.
*/
  else
  {
    v1 = v2;
    *seed = seed3;
  }

  used = used + 1;

  return v1;
# undef R8_PI
}
コード例 #8
0
ファイル: asa159.c プロジェクト: johannesgerer/jburkardt-c
void rcont2 ( int nrow, int ncol, int nrowt[], int ncolt[], int *key,
  int *seed, int matrix[],  int *ierror )

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

    RCONT2 constructs a random two-way contingency table with given sums.

  Discussion:

    It is possible to specify row and column sum vectors which
    correspond to no table at all.  As far as I can see, this routine does
    not detect such a case.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    12 November 2010

  Author:

    Original FORTRAN77 version by WM Patefield.
    C version by John Burkardt.

  Reference:

    WM Patefield,
    Algorithm AS 159:
    An Efficient Method of Generating RXC Tables with
    Given Row and Column Totals,
    Applied Statistics,
    Volume 30, Number 1, 1981, pages 91-97.

  Parameters:

    Input, int NROW, NCOL, the number of rows and columns
    in the table.  NROW and NCOL must each be at least 2.

    Input, int NROWT[NROW], NCOLT[NCOL], the row and column
    sums.  Each entry must be positive.

    Input/output, int *KEY, a flag that indicates whether data has
    been initialized for this problem.  Set KEY = .FALSE. before the first
    call.

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

    Output, int MATRIX[NROW*NCOL], the matrix.

    Output, int *IERROR, an error flag, which is returned
    as 0 if no error occurred.
*/
{
  int done1;
  int done2;
  static double *fact = NULL;
  int i;
  int ia;
  int iap;
  int ib;
  int ic;
  int id;
  int idp;
  int ie;
  int igp;
  int ihp;
  int ii;
  int iip;
  int j;
  int jc;
  int *jwork;
  int l;
  int lsm;
  int lsp;
  int m;
  int nll;
  int nlm;
  int nlmp;
  int nrowtl;
  static int ntotal = 0;
  double r;
  double sumprb;
  double x;
  double y;

  *ierror = 0;
/*
  On user's signal, set up the factorial table.
*/
  if ( !(*key) )
  {

    *key = 1;

    if ( nrow <= 1 )
    {
      printf ( "\n" );
      printf ( "RCONT - Fatal error!\n" );
      printf ( "  Input number of rows is less than 2.\n" );
      *ierror = 1;
      return;
    }

    if ( ncol <= 1 )
    {
      printf ( "\n" );
      printf ( "RCONT - Fatal error!\n" );
      printf ( "  The number of columns is less than 2.\n" );
      *ierror = 2;
      return;
    }

    for ( i = 0; i < nrow; i++ )
    {
      if ( nrowt[i] <= 0 )
      {
        printf ( "\n" );
        printf ( "RCONT - Fatal error!\n" );
        printf ( "  An entry in the row sum vector is not positive.\n" );
        *ierror = 3;
        return;
      }
    }

    for ( j = 0; j < ncol; j++ )
    {
      if ( ncolt[j] <= 0 )
      {
        printf ( "\n" );
        printf ( "RCONT - Fatal error!\n" );
        printf ( "  An entry in the column sum vector is not positive.\n" );
        *ierror = 4;
        return;
      }
    }

    if ( i4vec_sum ( ncol, ncolt ) != i4vec_sum ( nrow, nrowt ) )
    {
      printf ( "\n" );
      printf ( "RCONT - Fatal error!\n" );
      printf ( "  The row and column sum vectors do not have the same sum.\n" );
      *ierror = 6;
      return;
    }

    ntotal = i4vec_sum ( ncol, ncolt );

    if ( fact )
    {
      free ( fact );
    }

    fact = ( double * ) malloc ( ( ntotal + 1 ) * sizeof ( double ) );
/*
  Calculate log-factorials.
*/
    x = 0.0;
    fact[0] = 0.0;
    for ( i = 1; i <= ntotal; i++ )
    {
      x = x + log ( ( double ) ( i ) );
      fact[i] = x;
    }

  }
/*
  Construct a random matrix.
*/
  jwork = ( int * ) malloc ( ncol * sizeof ( int ) );;

  for ( i = 0; i < ncol - 1; i++ )
  {
    jwork[i] = ncolt[i];
  }

  jc = ntotal;

  for ( l = 0; l < nrow - 1; l++ )
  {
    nrowtl = nrowt[l];
    ia = nrowtl;
    ic = jc;
    jc = jc - nrowtl;

    for ( m = 0; m < ncol - 1; m++ )
    {
      id = jwork[m];
      ie = ic;
      ic = ic - id;
      ib = ie - ia;
      ii = ib - id;
/*
  Test for zero entries in matrix.
*/
      if ( ie == 0 )
      {
        ia = 0;
        for ( j = m; j < ncol; j++ )
        {
          matrix[l+j*nrow] = 0;
        }
        break;
      }
/*
  Generate a pseudo-random number.
*/
      r = r8_uniform_01 ( seed );
/*
  Compute the conditional expected value of MATRIX(L,M).
*/
      done1 = 0;

      for ( ; ; )
      {
        nlm = ( int ) ( ( double ) ( ia * id ) / ( double ) ( ie ) + 0.5 );
        iap = ia + 1;
        idp = id + 1;
        igp = idp - nlm;
        ihp = iap - nlm;
        nlmp = nlm + 1;
        iip = ii + nlmp;
        x = exp ( fact[iap-1] + fact[ib] + fact[ic] + fact[idp-1] -
          fact[ie] - fact[nlmp-1] - fact[igp-1] - fact[ihp-1] - fact[iip-1] );

        if ( r <= x )
        {
          break;
        }

        sumprb = x;
        y = x;
        nll = nlm;
        lsp = 0;
        lsm = 0;
/*
  Increment entry in row L, column M.
*/
        while ( !lsp )
        {
          j = ( id - nlm ) * ( ia - nlm );

          if ( j == 0 )
          {
            lsp = 1;
          }
          else
          {
            nlm = nlm + 1;
            x = x * ( double ) ( j ) / ( double ) ( nlm * ( ii + nlm ) );
            sumprb = sumprb + x;

            if ( r <= sumprb )
            {
              done1 = 1;
              break;
            }
          }

          done2 = 0;

          while ( !lsm )
          {
/*
  Decrement the entry in row L, column M.
*/
            j = nll * ( ii + nll );

            if ( j == 0 )
            {
              lsm = 1;
              break;
            }

            nll = nll - 1;
            y = y * ( double ) ( j ) / ( double ) ( ( id - nll ) * ( ia - nll ) );
            sumprb = sumprb + y;

            if ( r <= sumprb )
            {
              nlm = nll;
              done2 = 1;
              break;
            }

            if ( !lsp )
            {
              break;
            }

          }

          if ( done2 )
          {
            break;
          }

        }

        if ( done1 )
        {
          break;
        }

        if ( done2 )
        {
          break;
        }

        r = r8_uniform_01 ( seed );
        r = sumprb * r;

      }

      matrix[l+m*nrow] = nlm;
      ia = ia - nlm;
      jwork[m] = jwork[m] - nlm;

    }
    matrix[l+(ncol-1)*nrow] = ia;
  }
/*
  Compute the last row.
*/
  for ( j = 0; j < ncol - 1; j++ )
  {
    matrix[nrow-1+j*nrow] = jwork[j];
  }
  matrix[nrow-1+(ncol-1)*nrow] = ib - matrix[nrow-1+(ncol-2)*nrow];

  free ( jwork );

  return;
}
コード例 #9
0
ファイル: asa053.c プロジェクト: johannesgerer/jburkardt-c
void rnorm ( int *seed, double *u1, double *u2 )

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

    RNORM returns two independent standard random normal deviates.

  Discussion:

    This routine sets U1 and U2 to two independent standardized 
    random normal deviates.   This is a version of the 
    method given in Knuth.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    16 April 2014

  Author:

    Original FORTRAN77 version by William Smith, Ronald Hocking.
    This C version by John Burkardt.

  Reference:

    Donald Knuth,
    The Art of Computer Programming,
    Volume 2, Seminumerical Algorithms,
    Third Edition,
    Addison Wesley, 1997,
    ISBN: 0201896842,
    LC: QA76.6.K64.

  Parameters:

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

    Output, double *U1, *U2, two standard random normal deviates.
*/
{
  double s;
  double x;
  double y;

  for ( ; ; )
  {
    x = r8_uniform_01 ( seed );
    y = r8_uniform_01 ( seed );
    x = 2.0 * x - 1.0;
    y = 2.0 * y - 1.0;
    s = x * x + y * y;

    if ( s <= 1.0 )
    {
      s = sqrt ( - 2.0 * log ( s ) / s );
      *u1 = x * s;
      *u2 = y * s;
      break;
    }
  }
  return;
}
コード例 #10
0
void test15 ( )

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

    TEST15 tests PHYSICAL_TO_REFERENCE_T3 and REFERENCE_TO_PHYSICAL_T3.

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    15 January 2013

  Author:

    John Burkardt
*/
{
# define N 10

  int i;
  int j;
  double phy[2*N];
  double ref[2*N];
  double ref2[2*N];
  int seed;
  double t[2*3] = {
    1.0, 1.0, 
    3.0, 1.0, 
    2.0, 5.0 };

  seed = 123456789;

  printf ( "\n" );
  printf ( "TEST15\n" );
  printf ( "  For an order 3 triangle,\n" );
  printf ( "  PHYSICAL_TO_REFERENCE_T3 maps a physical point to\n" );
  printf ( "  a reference point.\n" );
  printf ( "  REFERENCE_TO_PHYSICAL_T3 maps a reference point to\n" );
  printf ( "  a physical point.\n" );
  printf ( "\n" );
  printf ( "      XSI     ETA  ==>  X       Y    ==>  XSI2    ETA2\n" );
  printf ( "\n" );

  for ( j = 0; j < N; j++ )
  {
    for ( i = 0; i < 2; i++ )
    {
      ref[i+j*2] = r8_uniform_01 ( &seed );
    }

    if ( 1.0 < ref[0+j*2] + ref[1+j*2] )
    {
      ref[0+j*2] = 1.0 - ref[0+j*2];
      ref[1+j*2] = 1.0 - ref[1+j*2];
    }
  }

  reference_to_physical_t3 ( t, N, ref, phy );

  physical_to_reference_t3 ( t, N, phy, ref2 );

  for ( j = 0; j < N; j++ )
  {
    printf ( "  %10f  %10f  %10f  %10f  %10f  %10f\n",
     ref[0+j*2], ref[1+j*2], phy[0+j*2], phy[1+j*2], ref2[0+j*2], ref2[1+j*2] );
  }

  return;
# undef N
}
コード例 #11
0
ファイル: st_to_cc.c プロジェクト: johannesgerer/jburkardt-c
double *wathen_st ( int nx, int ny, int nz_num, int *seed, int row[], 
  int col[] )

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

    WATHEN_ST: Wathen matrix stored in sparse triplet (ST) format.

  Discussion:

    When dealing with sparse matrices in MATLAB, it can be much more efficient
    to work first with a triple of I, J, and X vectors, and only once
    they are complete, convert to MATLAB's sparse format.

    The Wathen matrix is a finite element matrix which is sparse.

    The entries of the matrix depend in part on a physical quantity
    related to density.  That density is here assigned random values between
    0 and 100.

    The matrix order N is determined by the input quantities NX and NY,
    which would usually be the number of elements in the X and Y directions.

    The value of N is

      N = 3*NX*NY + 2*NX + 2*NY + 1,

    The matrix is the consistent mass matrix for a regular NX by NY grid
    of 8 node serendipity elements.

    The local element numbering is

      3--2--1
      |     |
      4     8
      |     |
      5--6--7

    Here is an illustration for NX = 3, NY = 2:

     23-24-25-26-27-28-29
      |     |     |     |
     19    20    21    22
      |     |     |     |
     12-13-14-15-16-17-18
      |     |     |     |
      8     9    10    11
      |     |     |     |
      1--2--3--4--5--6--7

    For this example, the total number of nodes is, as expected,

      N = 3 * 3 * 2 + 2 * 2 + 2 * 3 + 1 = 29

    The matrix is symmetric positive definite for any positive values of the
    density RHO(X,Y).

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    02 July 2014

  Author:

    John Burkardt.

  Reference:

    Nicholas Higham,
    Algorithm 694: A Collection of Test Matrices in MATLAB,
    ACM Transactions on Mathematical Software,
    Volume 17, Number 3, September 1991, pages 289-305.

    Andrew Wathen,
    Realistic eigenvalue bounds for the Galerkin mass matrix,
    IMA Journal of Numerical Analysis,
    Volume 7, Number 4, October 1987, pages 449-457.

  Parameters:

    Input, int NX, NY, values which determine the size of 
    the matrix.

    Input, int NZ_NUM, the number of values used to 
    describe the matrix.

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

    Output, int ROW[NZ_NUM], COL[NZ_NUM], the row and 
    column indices of the nonzero entries.

    Output, double WATHEN_ST[NZ_NUM], the nonzero entries of the matrix.
*/
{
  double *a;
  const double em[8*8] = {
     6.0, -6.0,  2.0, -8.0,  3.0, -8.0,  2.0, -6.0, 
    -6.0, 32.0, -6.0, 20.0, -8.0, 16.0, -8.0, 20.0, 
     2.0, -6.0,  6.0, -6.0,  2.0, -8.0,  3.0, -8.0, 
    -8.0, 20.0, -6.0, 32.0, -6.0, 20.0, -8.0, 16.0, 
     3.0, -8.0,  2.0, -6.0,  6.0, -6.0,  2.0, -8.0, 
    -8.0, 16.0, -8.0, 20.0, -6.0, 32.0, -6.0, 20.0, 
     2.0, -8.0,  3.0, -8.0,  2.0, -6.0,  6.0, -6.0, 
    -6.0, 20.0, -8.0, 16.0, -8.0, 20.0, -6.0, 32.0 };
  int i;
  int j;
  int k;
  int kcol;
  int krow;
  int node[8];
  double rho;

  a = ( double * ) malloc ( nz_num * sizeof ( double ) );
 
  for ( k = 0; k < nz_num; k++ )
  {
    row[k] = 0;
    col[k] = 0;
    a[k] = 0.0;
  }

  k = 0;

  for ( j = 0; j < nx; j++ )
  {
    for ( i = 0; i < nx; i++ )
    {
      node[0] = 3 * ( j + 1 ) * nx + 2 * ( j + 1 ) + 2 * ( i + 1 );
      node[1] = node[0] - 1;
      node[2] = node[0] - 2;
      node[3] = ( 3 * ( j + 1 ) - 1 ) * nx + 2 * ( j + 1 ) + ( i + 1 ) - 2;
      node[4] = ( 3 * ( j + 1 ) - 3 ) * nx + 2 * ( j + 1 ) + 2 * ( i + 1 ) - 4;
      node[5] = node[4] + 1;
      node[6] = node[4] + 2;
      node[7] = node[3] + 1;

      rho = 100.0 * r8_uniform_01 ( seed );

      for ( krow = 0; krow < 8; krow++ )
      {
        for ( kcol = 0; kcol < 8; kcol++ )
        {
          row[k] = node[krow];
          col[k] = node[kcol];
          a[k] = rho * em[krow+kcol*8];
          k = k + 1;
        }
      }
    }
  }

  return a;
}
コード例 #12
0
ファイル: test_eigen.c プロジェクト: spino327/jburkardt-c
double r8_normal_01 ( int *seed )

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

    R8_NORMAL_01 samples the standard normal probability distribution.

  Discussion:

    The standard normal probability distribution function (PDF) has
    mean 0 and standard deviation 1.

    The Box-Muller method is used, which is efficient, but
    generates two values at a time.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    01 July 2008

  Author:

    John Burkardt

  Parameters:

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

    Output, double R8_NORMAL_01, a normally distributed random value.
*/
{
  double pi = 3.141592653589793;
  double r1;
  double r2;
  static int used = -1;
  double x;
  static double y = 0.0;

  if ( used == -1 )
  {
    used = 0;
  }
/*
  If we've used an even number of values so far, generate two more, return one,
  and save one.
*/
  if ( ( used % 2 )== 0 )
  {
    for ( ; ; )
    {
      r1 = r8_uniform_01 ( seed );
      if ( r1 != 0.0 )
      {
        break;
      }
    }

    r2 = r8_uniform_01 ( seed );

    x = sqrt ( -2.0 * log ( r1 ) ) * cos ( 2.0 * pi * r2 );
    y = sqrt ( -2.0 * log ( r1 ) ) * sin ( 2.0 * pi * r2 );
  }
  else
  {

    x = y;

  }

  used = used + 1;

  return x;
}
コード例 #13
0
double *disk01_sample ( int n, int *seed )

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

    DISK01_SAMPLE uniformly samples the unit disk in 2D.

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    03 January 2014

  Author:

    John Burkardt

  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.
*/
{
  int i;
  int j;
  double norm;
  double r;
  double *v;
  double *x;

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

  for ( j = 0; j < n; j++ )
  {
    v = r8vec_normal_01_new ( 2, seed );
/*
  Compute the length of the vector.
*/
    norm = sqrt ( pow ( v[0], 2 ) + pow ( v[1], 2 ) );
/*
  Normalize the vector.
*/
    for ( i = 0; i < 2; i++ )
    {
      v[i] = v[i] / norm;
    }
/*
  Now compute a value to map the point ON the circle INTO the circle.
*/
    r = r8_uniform_01 ( seed );

    for ( i = 0; i < 2; i++ )
    {
      x[i+j*2] = sqrt ( r ) * v[i];
    }
    free ( v );
  }
  return x;
}