home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / mesch12a.zip / znorm.c < prev    next >
C/C++ Source or Header  |  1994-01-13  |  5KB  |  209 lines

  1.  
  2. /**************************************************************************
  3. **
  4. ** Copyright (C) 1993 David E. Steward & Zbigniew Leyk, all rights reserved.
  5. **
  6. **                 Meschach Library
  7. ** 
  8. ** This Meschach Library is provided "as is" without any express 
  9. ** or implied warranty of any kind with respect to this software. 
  10. ** In particular the authors shall not be liable for any direct, 
  11. ** indirect, special, incidental or consequential damages arising 
  12. ** in any way from use of the software.
  13. ** 
  14. ** Everyone is granted permission to copy, modify and redistribute this
  15. ** Meschach Library, provided:
  16. **  1.  All copies contain this copyright notice.
  17. **  2.  All modified copies shall carry a notice stating who
  18. **      made the last modification and the date of such modification.
  19. **  3.  No charge is made for this software or works derived from it.  
  20. **      This clause shall not be construed as constraining other software
  21. **      distributed on the same medium as this software, nor is a
  22. **      distribution fee considered a charge.
  23. **
  24. ***************************************************************************/
  25.  
  26.  
  27. /*
  28.     A collection of functions for computing norms: scaled and unscaled
  29.     Complex version
  30. */
  31. static    char    rcsid[] = "$Id: znorm.c,v 1.1 1994/01/13 04:21:31 des Exp $";
  32.  
  33. #include    <stdio.h>
  34. #include    <math.h>
  35. #include    "zmatrix.h"
  36.  
  37.  
  38.  
  39. /* _zv_norm1 -- computes (scaled) 1-norms of vectors */
  40. double    _zv_norm1(x,scale)
  41. ZVEC    *x;
  42. VEC    *scale;
  43. {
  44.     int    i, dim;
  45.     Real    s, sum;
  46.     
  47.     if ( x == ZVNULL )
  48.     error(E_NULL,"_zv_norm1");
  49.     dim = x->dim;
  50.     
  51.     sum = 0.0;
  52.     if ( scale == VNULL )
  53.     for ( i = 0; i < dim; i++ )
  54.         sum += zabs(x->ve[i]);
  55.     else if ( scale->dim < dim )
  56.     error(E_SIZES,"_zv_norm1");
  57.     else
  58.     for ( i = 0; i < dim; i++ )
  59.     {
  60.         s = scale->ve[i];
  61.         sum += ( s== 0.0 ) ? zabs(x->ve[i]) : zabs(x->ve[i])/fabs(s);
  62.     }
  63.     
  64.     return sum;
  65. }
  66.  
  67. /* square -- returns x^2 */
  68. /******************************
  69. double    square(x)
  70. double    x;
  71. {    return x*x;    }
  72. ******************************/
  73.  
  74. #define    square(x)    ((x)*(x))
  75.  
  76. /* _zv_norm2 -- computes (scaled) 2-norm (Euclidean norm) of vectors */
  77. double    _zv_norm2(x,scale)
  78. ZVEC    *x;
  79. VEC    *scale;
  80. {
  81.     int    i, dim;
  82.     Real    s, sum;
  83.     
  84.     if ( x == ZVNULL )
  85.     error(E_NULL,"_zv_norm2");
  86.     dim = x->dim;
  87.     
  88.     sum = 0.0;
  89.     if ( scale == VNULL )
  90.     for ( i = 0; i < dim; i++ )
  91.         sum += square(x->ve[i].re) + square(x->ve[i].im);
  92.     else if ( scale->dim < dim )
  93.     error(E_SIZES,"_v_norm2");
  94.     else
  95.     for ( i = 0; i < dim; i++ )
  96.     {
  97.         s = scale->ve[i];
  98.         sum += ( s== 0.0 ) ? square(x->ve[i].re) + square(x->ve[i].im) :
  99.         (square(x->ve[i].re) + square(x->ve[i].im))/square(s);
  100.     }
  101.     
  102.     return sqrt(sum);
  103. }
  104.  
  105. #define    max(a,b)    ((a) > (b) ? (a) : (b))
  106.  
  107. /* _zv_norm_inf -- computes (scaled) infinity-norm (supremum norm) of vectors */
  108. double    _zv_norm_inf(x,scale)
  109. ZVEC    *x;
  110. VEC    *scale;
  111. {
  112.     int    i, dim;
  113.     Real    s, maxval, tmp;
  114.     
  115.     if ( x == ZVNULL )
  116.     error(E_NULL,"_zv_norm_inf");
  117.     dim = x->dim;
  118.     
  119.     maxval = 0.0;
  120.     if ( scale == VNULL )
  121.     for ( i = 0; i < dim; i++ )
  122.     {
  123.         tmp = zabs(x->ve[i]);
  124.         maxval = max(maxval,tmp);
  125.     }
  126.     else if ( scale->dim < dim )
  127.     error(E_SIZES,"_zv_norm_inf");
  128.     else
  129.     for ( i = 0; i < dim; i++ )
  130.     {
  131.         s = scale->ve[i];
  132.         tmp = ( s == 0.0 ) ? zabs(x->ve[i]) : zabs(x->ve[i])/fabs(s);
  133.         maxval = max(maxval,tmp);
  134.     }
  135.     
  136.     return maxval;
  137. }
  138.  
  139. /* zm_norm1 -- compute matrix 1-norm -- unscaled
  140.     -- complex version */
  141. double    zm_norm1(A)
  142. ZMAT    *A;
  143. {
  144.     int    i, j, m, n;
  145.     Real    maxval, sum;
  146.     
  147.     if ( A == ZMNULL )
  148.     error(E_NULL,"zm_norm1");
  149.  
  150.     m = A->m;    n = A->n;
  151.     maxval = 0.0;
  152.     
  153.     for ( j = 0; j < n; j++ )
  154.     {
  155.     sum = 0.0;
  156.     for ( i = 0; i < m; i ++ )
  157.         sum += zabs(A->me[i][j]);
  158.     maxval = max(maxval,sum);
  159.     }
  160.     
  161.     return maxval;
  162. }
  163.  
  164. /* zm_norm_inf -- compute matrix infinity-norm -- unscaled
  165.     -- complex version */
  166. double    zm_norm_inf(A)
  167. ZMAT    *A;
  168. {
  169.     int    i, j, m, n;
  170.     Real    maxval, sum;
  171.     
  172.     if ( A == ZMNULL )
  173.     error(E_NULL,"zm_norm_inf");
  174.     
  175.     m = A->m;    n = A->n;
  176.     maxval = 0.0;
  177.     
  178.     for ( i = 0; i < m; i++ )
  179.     {
  180.     sum = 0.0;
  181.     for ( j = 0; j < n; j ++ )
  182.         sum += zabs(A->me[i][j]);
  183.     maxval = max(maxval,sum);
  184.     }
  185.     
  186.     return maxval;
  187. }
  188.  
  189. /* zm_norm_frob -- compute matrix frobenius-norm -- unscaled */
  190. double    zm_norm_frob(A)
  191. ZMAT    *A;
  192. {
  193.     int    i, j, m, n;
  194.     Real    sum;
  195.     
  196.     if ( A == ZMNULL )
  197.     error(E_NULL,"zm_norm_frob");
  198.     
  199.     m = A->m;    n = A->n;
  200.     sum = 0.0;
  201.     
  202.     for ( i = 0; i < m; i++ )
  203.     for ( j = 0; j < n; j ++ )
  204.         sum += square(A->me[i][j].re) + square(A->me[i][j].im);
  205.     
  206.     return sqrt(sum);
  207. }
  208.  
  209.