znorm.c 4.5 KB

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