home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / biology / gsrc208a.zip / IDAMAX.C < prev    next >
C/C++ Source or Header  |  1992-07-16  |  1KB  |  69 lines

  1. #include <math.h>
  2.  
  3. int idamax( n, dx, incx )
  4.  
  5. double *dx;
  6. int n, incx;
  7.  
  8. /* Purpose : Find largest component of double vector dx
  9.  
  10.  
  11.    --- Input ---
  12.  
  13.    n    : number of elements in input vector
  14.    dx   : double vector with n+1 elements, dx[0] is not used
  15.    incx : storage spacing between elements of dx
  16.  
  17.  
  18.    --- Output ---
  19.  
  20.    idamax : smallest index, 0 if n <= 0
  21.  
  22.  
  23.    Find smallest index of maximum magnitude of dx.
  24.    idamax = first i, i=1 to n, to minimize fabs( dx[1-incx+i*incx] ).
  25.  
  26. */
  27.  
  28. {
  29.    double dmax, xmag;
  30.    int i, ii, xindex;
  31.  
  32.    xindex = 0;
  33.    if ( n <= 0 )
  34.       return xindex;
  35.    xindex = 1;
  36.    if ( n <= 1 || incx <= 0 )
  37.       return xindex;
  38.  
  39. /* Code for increments not equal to 1.   */
  40.  
  41.    if ( incx != 1 ) {
  42.       dmax = fabs( dx[1] );
  43.       ii = 2;
  44.       for ( i = 1 + incx ; i <= n * incx ; i = i + incx ) {
  45.          xmag = fabs( dx[i] );
  46.          if ( xmag > dmax ) {
  47.             xindex = ii;
  48.             dmax = xmag;
  49.          }
  50.          ii++;
  51.       }
  52.       return xindex;
  53.    }
  54.  
  55. /* Code for increments equal to 1.  */
  56.  
  57.    dmax = fabs( dx[1] );
  58.    for ( i = 2 ; i <= n ; i++ ) {
  59.       xmag = fabs( dx[i] );
  60.       if ( xmag > dmax ) {
  61.          xindex = i;
  62.          dmax = xmag;
  63.       }
  64.    }
  65.    return xindex;
  66.  
  67. }
  68.  
  69.