scnrm2.f 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. *DECK SCNRM2
  2. REAL FUNCTION SCNRM2 (N, CX, INCX)
  3. C***BEGIN PROLOGUE SCNRM2
  4. C***PURPOSE Compute the unitary norm of a complex vector.
  5. C***LIBRARY SLATEC (BLAS)
  6. C***CATEGORY D1A3B
  7. C***TYPE COMPLEX (SNRM2-S, DNRM2-D, SCNRM2-C)
  8. C***KEYWORDS BLAS, EUCLIDEAN LENGTH, EUCLIDEAN NORM, L2,
  9. C LINEAR ALGEBRA, UNITARY, VECTOR
  10. C***AUTHOR Lawson, C. L., (JPL)
  11. C Hanson, R. J., (SNLA)
  12. C Kincaid, D. R., (U. of Texas)
  13. C Krogh, F. T., (JPL)
  14. C***DESCRIPTION
  15. C
  16. C B L A S Subprogram
  17. C Description of Parameters
  18. C
  19. C --Input--
  20. C N number of elements in input vector(s)
  21. C CX complex vector with N elements
  22. C INCX storage spacing between elements of CX
  23. C
  24. C --Output--
  25. C SCNRM2 single precision result (zero if N .LE. 0)
  26. C
  27. C Unitary norm of the complex N-vector stored in CX with storage
  28. C increment INCX.
  29. C If N .LE. 0, return with result = 0.
  30. C If N .GE. 1, then INCX must be .GE. 1
  31. C
  32. C Four phase method using two built-in constants that are
  33. C hopefully applicable to all machines.
  34. C CUTLO = maximum of SQRT(U/EPS) over all known machines.
  35. C CUTHI = minimum of SQRT(V) over all known machines.
  36. C where
  37. C EPS = smallest no. such that EPS + 1. .GT. 1.
  38. C U = smallest positive no. (underflow limit)
  39. C V = largest no. (overflow limit)
  40. C
  41. C Brief outline of algorithm.
  42. C
  43. C Phase 1 scans zero components.
  44. C Move to phase 2 when a component is nonzero and .LE. CUTLO
  45. C Move to phase 3 when a component is .GT. CUTLO
  46. C Move to phase 4 when a component is .GE. CUTHI/M
  47. C where M = N for X() real and M = 2*N for complex.
  48. C
  49. C Values for CUTLO and CUTHI.
  50. C From the environmental parameters listed in the IMSL converter
  51. C document the limiting values are as follows:
  52. C CUTLO, S.P. U/EPS = 2**(-102) for Honeywell. Close seconds are
  53. C Univac and DEC at 2**(-103)
  54. C Thus CUTLO = 2**(-51) = 4.44089E-16
  55. C CUTHI, S.P. V = 2**127 for Univac, Honeywell, and DEC.
  56. C Thus CUTHI = 2**(63.5) = 1.30438E19
  57. C CUTLO, D.P. U/EPS = 2**(-67) for Honeywell and DEC.
  58. C Thus CUTLO = 2**(-33.5) = 8.23181D-11
  59. C CUTHI, D.P. same as S.P. CUTHI = 1.30438D19
  60. C DATA CUTLO, CUTHI /8.232D-11, 1.304D19/
  61. C DATA CUTLO, CUTHI /4.441E-16, 1.304E19/
  62. C
  63. C***REFERENCES C. L. Lawson, R. J. Hanson, D. R. Kincaid and F. T.
  64. C Krogh, Basic linear algebra subprograms for Fortran
  65. C usage, Algorithm No. 539, Transactions on Mathematical
  66. C Software 5, 3 (September 1979), pp. 308-323.
  67. C***ROUTINES CALLED (NONE)
  68. C***REVISION HISTORY (YYMMDD)
  69. C 791001 DATE WRITTEN
  70. C 890531 Changed all specific intrinsics to generic. (WRB)
  71. C 890831 Modified array declarations. (WRB)
  72. C 890831 REVISION DATE from Version 3.2
  73. C 891214 Prologue converted to Version 4.0 format. (BAB)
  74. C 920501 Reformatted the REFERENCES section. (WRB)
  75. C***END PROLOGUE SCNRM2
  76. LOGICAL IMAG, SCALE
  77. INTEGER NEXT
  78. REAL CUTLO, CUTHI, HITEST, SUM, XMAX, ABSX, ZERO, ONE
  79. COMPLEX CX(*)
  80. SAVE CUTLO, CUTHI, ZERO, ONE
  81. DATA ZERO, ONE /0.0E0, 1.0E0/
  82. C
  83. DATA CUTLO, CUTHI /4.441E-16, 1.304E19/
  84. C***FIRST EXECUTABLE STATEMENT SCNRM2
  85. IF (N .GT. 0) GO TO 10
  86. SCNRM2 = ZERO
  87. GO TO 300
  88. C
  89. 10 ASSIGN 30 TO NEXT
  90. SUM = ZERO
  91. NN = N * INCX
  92. C
  93. C BEGIN MAIN LOOP
  94. C
  95. DO 210 I = 1,NN,INCX
  96. ABSX = ABS(REAL(CX(I)))
  97. IMAG = .FALSE.
  98. GO TO NEXT,(30, 50, 70, 90, 110)
  99. 30 IF (ABSX .GT. CUTLO) GO TO 85
  100. ASSIGN 50 TO NEXT
  101. SCALE = .FALSE.
  102. C
  103. C PHASE 1. SUM IS ZERO
  104. C
  105. 50 IF (ABSX .EQ. ZERO) GO TO 200
  106. IF (ABSX .GT. CUTLO) GO TO 85
  107. C
  108. C PREPARE FOR PHASE 2.
  109. C
  110. ASSIGN 70 TO NEXT
  111. GO TO 105
  112. C
  113. C PREPARE FOR PHASE 4.
  114. C
  115. 100 ASSIGN 110 TO NEXT
  116. SUM = (SUM / ABSX) / ABSX
  117. 105 SCALE = .TRUE.
  118. XMAX = ABSX
  119. GO TO 115
  120. C
  121. C PHASE 2. SUM IS SMALL.
  122. C SCALE TO AVOID DESTRUCTIVE UNDERFLOW.
  123. C
  124. 70 IF (ABSX .GT. CUTLO) GO TO 75
  125. C
  126. C COMMON CODE FOR PHASES 2 AND 4.
  127. C IN PHASE 4 SUM IS LARGE. SCALE TO AVOID OVERFLOW.
  128. C
  129. 110 IF (ABSX .LE. XMAX) GO TO 115
  130. SUM = ONE + SUM * (XMAX / ABSX)**2
  131. XMAX = ABSX
  132. GO TO 200
  133. C
  134. 115 SUM = SUM + (ABSX/XMAX)**2
  135. GO TO 200
  136. C
  137. C PREPARE FOR PHASE 3.
  138. C
  139. 75 SUM = (SUM * XMAX) * XMAX
  140. C
  141. 85 ASSIGN 90 TO NEXT
  142. SCALE = .FALSE.
  143. C
  144. C FOR REAL OR D.P. SET HITEST = CUTHI/N
  145. C FOR COMPLEX SET HITEST = CUTHI/(2*N)
  146. C
  147. HITEST = CUTHI / N
  148. C
  149. C PHASE 3. SUM IS MID-RANGE. NO SCALING.
  150. C
  151. 90 IF (ABSX .GE. HITEST) GO TO 100
  152. SUM = SUM + ABSX**2
  153. 200 CONTINUE
  154. C
  155. C CONTROL SELECTION OF REAL AND IMAGINARY PARTS.
  156. C
  157. IF (IMAG) GO TO 210
  158. ABSX = ABS(AIMAG(CX(I)))
  159. IMAG = .TRUE.
  160. GO TO NEXT,( 50, 70, 90, 110 )
  161. C
  162. 210 CONTINUE
  163. C
  164. C END OF MAIN LOOP.
  165. C COMPUTE SQUARE ROOT AND ADJUST FOR SCALING.
  166. C
  167. SCNRM2 = SQRT(SUM)
  168. IF (SCALE) SCNRM2 = SCNRM2 * XMAX
  169. 300 CONTINUE
  170. RETURN
  171. END