home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / mesch12a.zip / norm.c < prev    next >
C/C++ Source or Header  |  1994-01-13  |  4KB  |  199 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. */
  30. static    char    rcsid[] = "$Id: norm.c,v 1.6 1994/01/13 05:34:35 des Exp $";
  31.  
  32. #include    <stdio.h>
  33. #include    <math.h>
  34. #include    "matrix.h"
  35.  
  36.  
  37. /* _v_norm1 -- computes (scaled) 1-norms of vectors */
  38. double    _v_norm1(x,scale)
  39. VEC    *x, *scale;
  40. {
  41.     int    i, dim;
  42.     Real    s, sum;
  43.  
  44.     if ( x == (VEC *)NULL )
  45.         error(E_NULL,"_v_norm1");
  46.     dim = x->dim;
  47.  
  48.     sum = 0.0;
  49.     if ( scale == (VEC *)NULL )
  50.         for ( i = 0; i < dim; i++ )
  51.             sum += fabs(x->ve[i]);
  52.     else if ( scale->dim < dim )
  53.         error(E_SIZES,"_v_norm1");
  54.     else
  55.         for ( i = 0; i < dim; i++ )
  56.         {    s = scale->ve[i];
  57.             sum += ( s== 0.0 ) ? fabs(x->ve[i]) : fabs(x->ve[i]/s);
  58.         }
  59.  
  60.     return sum;
  61. }
  62.  
  63. /* square -- returns x^2 */
  64. double    square(x)
  65. double    x;
  66. {    return x*x;    }
  67.  
  68. /* cube -- returns x^3 */
  69. double cube(x)
  70. double x;
  71. {  return x*x*x;   }
  72.  
  73. /* _v_norm2 -- computes (scaled) 2-norm (Euclidean norm) of vectors */
  74. double    _v_norm2(x,scale)
  75. VEC    *x, *scale;
  76. {
  77.     int    i, dim;
  78.     Real    s, sum;
  79.  
  80.     if ( x == (VEC *)NULL )
  81.         error(E_NULL,"_v_norm2");
  82.     dim = x->dim;
  83.  
  84.     sum = 0.0;
  85.     if ( scale == (VEC *)NULL )
  86.         for ( i = 0; i < dim; i++ )
  87.             sum += square(x->ve[i]);
  88.     else if ( scale->dim < dim )
  89.         error(E_SIZES,"_v_norm2");
  90.     else
  91.         for ( i = 0; i < dim; i++ )
  92.         {    s = scale->ve[i];
  93.             sum += ( s== 0.0 ) ? square(x->ve[i]) :
  94.                             square(x->ve[i]/s);
  95.         }
  96.  
  97.     return sqrt(sum);
  98. }
  99.  
  100. #define    max(a,b)    ((a) > (b) ? (a) : (b))
  101.  
  102. /* _v_norm_inf -- computes (scaled) infinity-norm (supremum norm) of vectors */
  103. double    _v_norm_inf(x,scale)
  104. VEC    *x, *scale;
  105. {
  106.     int    i, dim;
  107.     Real    s, maxval, tmp;
  108.  
  109.     if ( x == (VEC *)NULL )
  110.         error(E_NULL,"_v_norm_inf");
  111.     dim = x->dim;
  112.  
  113.     maxval = 0.0;
  114.     if ( scale == (VEC *)NULL )
  115.         for ( i = 0; i < dim; i++ )
  116.         {    tmp = fabs(x->ve[i]);
  117.             maxval = max(maxval,tmp);
  118.         }
  119.     else if ( scale->dim < dim )
  120.         error(E_SIZES,"_v_norm_inf");
  121.     else
  122.         for ( i = 0; i < dim; i++ )
  123.         {    s = scale->ve[i];
  124.             tmp = ( s== 0.0 ) ? fabs(x->ve[i]) : fabs(x->ve[i]/s);
  125.             maxval = max(maxval,tmp);
  126.         }
  127.  
  128.     return maxval;
  129. }
  130.  
  131. /* m_norm1 -- compute matrix 1-norm -- unscaled */
  132. double    m_norm1(A)
  133. MAT    *A;
  134. {
  135.     int    i, j, m, n;
  136.     Real    maxval, sum;
  137.  
  138.     if ( A == (MAT *)NULL )
  139.         error(E_NULL,"m_norm1");
  140.  
  141.     m = A->m;    n = A->n;
  142.     maxval = 0.0;
  143.  
  144.     for ( j = 0; j < n; j++ )
  145.     {
  146.         sum = 0.0;
  147.         for ( i = 0; i < m; i ++ )
  148.             sum += fabs(A->me[i][j]);
  149.         maxval = max(maxval,sum);
  150.     }
  151.  
  152.     return maxval;
  153. }
  154.  
  155. /* m_norm_inf -- compute matrix infinity-norm -- unscaled */
  156. double    m_norm_inf(A)
  157. MAT    *A;
  158. {
  159.     int    i, j, m, n;
  160.     Real    maxval, sum;
  161.  
  162.     if ( A == (MAT *)NULL )
  163.         error(E_NULL,"m_norm_inf");
  164.  
  165.     m = A->m;    n = A->n;
  166.     maxval = 0.0;
  167.  
  168.     for ( i = 0; i < m; i++ )
  169.     {
  170.         sum = 0.0;
  171.         for ( j = 0; j < n; j ++ )
  172.             sum += fabs(A->me[i][j]);
  173.         maxval = max(maxval,sum);
  174.     }
  175.  
  176.     return maxval;
  177. }
  178.  
  179. /* m_norm_frob -- compute matrix frobenius-norm -- unscaled */
  180. double    m_norm_frob(A)
  181. MAT    *A;
  182. {
  183.     int    i, j, m, n;
  184.     Real    sum;
  185.  
  186.     if ( A == (MAT *)NULL )
  187.         error(E_NULL,"m_norm_frob");
  188.  
  189.     m = A->m;    n = A->n;
  190.     sum = 0.0;
  191.  
  192.     for ( i = 0; i < m; i++ )
  193.         for ( j = 0; j < n; j ++ )
  194.             sum += square(A->me[i][j]);
  195.  
  196.     return sqrt(sum);
  197. }
  198.  
  199.