snrm2.f 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. *DECK SNRM2
  2. REAL FUNCTION SNRM2 (N, SX, INCX)
  3. C***BEGIN PROLOGUE SNRM2
  4. C***PURPOSE Compute the Euclidean length (L2 norm) of a vector.
  5. C***LIBRARY SLATEC (BLAS)
  6. C***CATEGORY D1A3B
  7. C***TYPE SINGLE PRECISION (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 SX single precision vector with N elements
  22. C INCX storage spacing between elements of SX
  23. C
  24. C --Output--
  25. C SNRM2 single precision result (zero if N .LE. 0)
  26. C
  27. C Euclidean norm of the N-vector stored in SX 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 SNRM2
  76. INTEGER NEXT
  77. REAL SX(*), CUTLO, CUTHI, HITEST, SUM, XMAX, ZERO, ONE
  78. SAVE CUTLO, CUTHI, ZERO, ONE
  79. DATA ZERO, ONE /0.0E0, 1.0E0/
  80. C
  81. DATA CUTLO, CUTHI /4.441E-16, 1.304E19/
  82. C***FIRST EXECUTABLE STATEMENT SNRM2
  83. IF (N .GT. 0) GO TO 10
  84. SNRM2 = ZERO
  85. GO TO 300
  86. C
  87. 10 ASSIGN 30 TO NEXT
  88. SUM = ZERO
  89. NN = N * INCX
  90. C
  91. C BEGIN MAIN LOOP
  92. C
  93. I = 1
  94. 20 GO TO NEXT,(30, 50, 70, 110)
  95. 30 IF (ABS(SX(I)) .GT. CUTLO) GO TO 85
  96. ASSIGN 50 TO NEXT
  97. XMAX = ZERO
  98. C
  99. C PHASE 1. SUM IS ZERO
  100. C
  101. 50 IF (SX(I) .EQ. ZERO) GO TO 200
  102. IF (ABS(SX(I)) .GT. CUTLO) GO TO 85
  103. C
  104. C PREPARE FOR PHASE 2.
  105. C
  106. ASSIGN 70 TO NEXT
  107. GO TO 105
  108. C
  109. C PREPARE FOR PHASE 4.
  110. C
  111. 100 I = J
  112. ASSIGN 110 TO NEXT
  113. SUM = (SUM / SX(I)) / SX(I)
  114. 105 XMAX = ABS(SX(I))
  115. GO TO 115
  116. C
  117. C PHASE 2. SUM IS SMALL.
  118. C SCALE TO AVOID DESTRUCTIVE UNDERFLOW.
  119. C
  120. 70 IF (ABS(SX(I)) .GT. CUTLO) GO TO 75
  121. C
  122. C COMMON CODE FOR PHASES 2 AND 4.
  123. C IN PHASE 4 SUM IS LARGE. SCALE TO AVOID OVERFLOW.
  124. C
  125. 110 IF (ABS(SX(I)) .LE. XMAX) GO TO 115
  126. SUM = ONE + SUM * (XMAX / SX(I))**2
  127. XMAX = ABS(SX(I))
  128. GO TO 200
  129. C
  130. 115 SUM = SUM + (SX(I)/XMAX)**2
  131. GO TO 200
  132. C
  133. C PREPARE FOR PHASE 3.
  134. C
  135. 75 SUM = (SUM * XMAX) * XMAX
  136. C
  137. C FOR REAL OR D.P. SET HITEST = CUTHI/N
  138. C FOR COMPLEX SET HITEST = CUTHI/(2*N)
  139. C
  140. 85 HITEST = CUTHI / N
  141. C
  142. C PHASE 3. SUM IS MID-RANGE. NO SCALING.
  143. C
  144. DO 95 J = I,NN,INCX
  145. IF (ABS(SX(J)) .GE. HITEST) GO TO 100
  146. 95 SUM = SUM + SX(J)**2
  147. SNRM2 = SQRT( SUM )
  148. GO TO 300
  149. C
  150. 200 CONTINUE
  151. I = I + INCX
  152. IF (I .LE. NN) GO TO 20
  153. C
  154. C END OF MAIN LOOP.
  155. C
  156. C COMPUTE SQUARE ROOT AND ADJUST FOR SCALING.
  157. C
  158. SNRM2 = XMAX * SQRT(SUM)
  159. 300 CONTINUE
  160. RETURN
  161. END