Beispiel #1
0
void test023 ( )

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

    TEST023 tests R4_SIGN and R4_SIGN3.

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    28 September 2014

  Author:

    John Burkardt
*/
{
  float r4;
  float r4_test[5] = { -1.25E+00, -0.25E+00, 0.0E+00, +0.5E+00, +9.0E+00 };
  float s1;
  float s2;
  int test;
  const int test_num = 5;

  printf ( "\n" );
  printf ( "TEST023\n" );
  printf ( "  R4_SIGN returns the sign of an R4.\n" );
  printf ( "  R4_SIGN3 returns the three-way sign of an R4.\n" );
  printf ( "\n" );
  printf ( "      R4    R4_SIGN(R4)  R4_SIGN3(R4)\n" );
  printf ( "\n" );

  for ( test = 0; test < test_num; test++ )
  {
    r4 = r4_test[test];
    s1 = r4_sign ( r4 );
    s2 = r4_sign3 ( r4 );
    printf ( "  %8.4f  %8.0f  %8.0f\n", r4, s1, s2 );
  }

  return;
}
void srotg ( float *sa, float *sb, float *c, float *s )

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

    SROTG constructs a Givens plane rotation.

  Discussion:

    Given values A and B, this routine computes

    SIGMA = sign ( A ) if abs ( A ) >  abs ( B )
          = sign ( B ) if abs ( A ) <= abs ( B );

    R     = SIGMA * ( A * A + B * B );

    C = A / R if R is not 0
      = 1     if R is 0;

    S = B / R if R is not 0,
        0     if R is 0.

    The computed numbers then satisfy the equation

    (  C  S ) ( A ) = ( R )
    ( -S  C ) ( B ) = ( 0 )

    The routine also computes

    Z = S     if abs ( A ) > abs ( B ),
      = 1 / C if abs ( A ) <= abs ( B ) and C is not 0,
      = 1     if C is 0.

    The single value Z encodes C and S, and hence the rotation:

    If Z = 1, set C = 0 and S = 1;
    If abs ( Z ) < 1, set C = sqrt ( 1 - Z * Z ) and S = Z;
    if abs ( Z ) > 1, set C = 1/ Z and S = sqrt ( 1 - C * C );

  Licensing:

    This code is distributed under the GNU LGPL license. 

  Modified:

    29 March 2007

  Author:

    C version by John Burkardt

  Reference:

    Jack Dongarra, Cleve Moler, Jim Bunch, Pete Stewart,
    LINPACK User's Guide,
    SIAM, 1979,
    ISBN13: 978-0-898711-72-1,
    LC: QA214.L56.

    Charles Lawson, Richard Hanson, David Kincaid, Fred Krogh,
    Algorithm 539: 
    Basic Linear Algebra Subprograms for Fortran Usage,
    ACM Transactions on Mathematical Software,
    Volume 5, Number 3, September 1979, pages 308-323.

  Parameters:

    Input/output, float *SA, *SB,  On input, SA and SB are the values
    A and B.  On output, SA is overwritten with R, and SB is
    overwritten with Z.

    Output, float *C, *S, the cosine and sine of the Givens rotation.
*/
{
  float r;
  float roe;
  float scale;
  float z;

  if ( r4_abs ( *sb ) < r4_abs ( *sa ) )
  {
    roe = *sa;
  }
  else
  {
    roe = *sb;
  }

  scale = r4_abs ( *sa ) + r4_abs ( *sb );

  if ( scale == 0.0 )
  {
    *c = 1.0;
    *s = 0.0;
    r = 0.0;
  }
  else
  {
    r = scale * sqrt ( ( *sa / scale ) * ( *sa / scale ) 
                     + ( *sb / scale ) * ( *sb / scale ) );
    r = r4_sign ( roe ) * r;
    *c = *sa / r;
    *s = *sb / r;
  }

  if ( 0.0 < r4_abs ( *c ) && r4_abs ( *c ) <= *s )
  {
    z = 1.0 / *c;
  }
  else
  {
    z = *s;
  }

  *sa = r;
  *sb = z;

  return;
}