Esempio n. 1
0
void test01 ( )

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

    TEST01 tests NORMAL_SOLVE.

  Licensing:

    This code is distributed under the GNU LGPL license.

  Modified:

    11 September 2012

  Author:

    John Burkardt
*/
{
  double *a;
  double *b;
  double b_norm;
  int flag;
  int i;
  int m;
  int n;
  int prob;
  int prob_num;
  double *r1;
  double r1_norm;
  double *r2;
  double r2_norm;
  double x_diff_norm;
  double *x1;
  double x1_norm;
  double *x2;
  double x2_norm;

  printf ( "\n" );
  printf ( "TEST01\n" );
  printf ( "  NORMAL_SOLVE is a function with a simple interface which\n" );
  printf ( "  solves a linear system A*x = b in the least squares sense.\n" );
  printf ( "  Compare a tabulated solution X1 to the NORMAL_SOLVE result X2.\n" );
  printf ( "\n" );
  printf ( "  NORMAL_SOLVE cannot be applied when N < M,\n" );
  printf ( "  or if the matrix does not have full column rank.\n" );

  prob_num = p00_prob_num ( );

  printf ( "\n" );
  printf ( "  Number of problems = %d\n", prob_num );
  printf ( "\n" );
  printf ( "  Index     M     N     ||B||         ||X1 - X2||   ||X1||       ||X2||        ||R1||        ||R2||\n" );
  printf ( "\n" );

  for ( prob = 1; prob <= prob_num; prob++ )
  {
/*
  Get problem size.
*/
    m = p00_m ( prob );
    n = p00_n ( prob );
/*
  Retrieve problem data.
*/
    a = p00_a ( prob, m, n );
    b = p00_b ( prob, m );
    x1 = p00_x ( prob, n );

    b_norm = r8vec_norm ( m, b );
    x1_norm = r8vec_norm ( n, x1 );
    r1 = r8mat_mv_new ( m, n, a, x1 );
    for ( i = 0; i < m; i++ )
    {
      r1[i] = r1[i] - b[i];
    }
    r1_norm = r8vec_norm ( m, r1 );
/*
  Use NORMAL_SOLVE on the problem.
*/
    x2 = normal_solve ( m, n, a, b, &flag );

    if ( flag != 0 )
    {
      printf ( "  %5d  %4d  %4d  %12g  ------------  %12g   ------------  %12g  ------------\n",
      prob, m, n, b_norm, x1_norm, r1_norm );
    }
    else
    {
      x2_norm = r8vec_norm ( n, x2 );
      r2 = r8mat_mv_new ( m, n, a, x2 );
      for ( i = 0; i < m; i++ )
      {
        r2[i] = r2[i] - b[i];
      }
      r2_norm = r8vec_norm ( m, r2 );
/*
  Compare tabulated and computed solutions.
*/
      x_diff_norm = r8vec_norm_affine ( n, x1, x2 );
/*
  Report results for this problem.
*/
      printf ( "  %5d  %4d  %4d  %12g  %12g  %12g  %12g  %12g  %12g\n",
        prob, m, n, b_norm, x_diff_norm, x1_norm, x2_norm, r1_norm, r2_norm );

      free ( r2 );
      free ( x2 );
    }
/*
  Deallocate memory.
*/
    free ( a );
    free ( b );
    free ( r1 );
    free ( x1 );
  }
  return;
}
Esempio n. 2
0
void test02 ( void )

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

    TEST02 evaluates the objective function at each starting point.

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    17 February 2012

  Author:

    John Burkardt
*/
{
  double *a;
  double *b;
  double *f;
  double *fs;
  int i;
  int know;
  int m;
  int n = 100000;
  int problem;
  int problem_num;
  int seed;
  char title[100];
  double *x;
  double *xs;

  printf ( "\n" );
  printf ( "TEST02\n" );
  printf ( "  For each problem, evaluate the function at many points.\n" );
  printf ( "  Number of sample points = %d\n", n );
/*
  Get the number of problems.
*/
  problem_num = p00_problem_num ( );

  for ( problem = 1; problem <= problem_num; problem++ )
  {
    printf ( "\n" );
    printf ( "  Problem %d\n", problem );

    p00_title ( problem, title );

    printf ( "  %s\n", title );

    m = p00_m ( problem );

    printf ( "  M =     %d\n", m );

    a = ( double * ) malloc ( m * sizeof ( double ) );
    b = ( double * ) malloc ( m * sizeof ( double ) );
 
    p00_ab ( problem, m, a, b );

    printf ( "\n" );
    printf ( "    I      A(i)      B(i)\n" );
    printf ( "\n" );

    for ( i = 0; i < m; i++ )
    {
      printf ( "  %4d  %10g  %10g\n", i, a[i], b[i] );
    }

    seed = 123456789;
    x = r8col_uniform_new ( m, n, a, b, &seed );
    f = p00_f ( problem, m, n, x );

    printf ( "\n" );
    printf ( "  Max(F) = %g\n", r8vec_max ( n, f ) );
    printf ( "  Min(F) = %g\n", r8vec_min ( n, f ) );

    know = 0;
    xs = p00_sol ( problem, m, &know );
    if ( know != 0 )
    {
      fs = p00_f ( problem, m, 1, xs );
      printf ( "  F(X*)  = %g\n", fs[0] );
      free ( fs );
      free ( xs );
    }
    else
    {
      printf ( "  X* is not given.\n" );
    }

    free ( a );
    free ( b );
    free ( f );
    free ( x );
  }
  return;
}