dnrm2.f 5.1 KB

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