5 ! Check implementation of L2 norm (Euclidean vector norm)
9 real :: a(3) = [real :: 1, 2, huge(3.0)]
10 real :: b(3) = [real :: 1, 2, 3]
11 real :: c(4) = [real :: 1, 2, 3, -1]
12 real :: e(0) = [real :: ]
13 real :: f(4) = [real :: 0, 0, 3, 0 ]
15 real :: d(4,1) = RESHAPE ([real :: 1, 2, 3, -1], [4,1])
16 real :: g(4,1) = RESHAPE ([real :: 0, 0, 4, -1], [4,1])
18 ! Check compile-time version
20 if (abs (NORM2 ([real :: 1, 2, huge(3.0)]) - huge(3.0)) &
21 > epsilon(0.0)*huge(3.0)) call abort()
23 if (abs (SNORM2([real :: 1, 2, huge(3.0)],3) - huge(3.0)) &
24 > epsilon(0.0)*huge(3.0)) call abort()
26 if (abs (SNORM2([real :: 1, 2, 3],3) - NORM2([real :: 1, 2, 3])) &
27 > epsilon(0.0)*SNORM2([real :: 1, 2, 3],3)) call abort()
29 if (NORM2([real :: ]) /= 0.0) call abort()
30 if (abs (NORM2([real :: 0, 0, 3, 0]) - 3.0) > epsilon(0.0)) call abort()
34 if (abs (NORM2 (a
) - huge(3.0)) &
35 > epsilon(0.0)*huge(3.0)) call abort()
37 if (abs (SNORM2(b
,3) - NORM2(b
)) &
38 > epsilon(0.0)*SNORM2(b
,3)) call abort()
40 if (abs (SNORM2(c
,4) - NORM2(c
)) &
41 > epsilon(0.0)*SNORM2(c
,4)) call abort()
43 if (ANY (abs (abs(d(:,1)) - NORM2(d
, 2)) &
44 > epsilon(0.0))) call abort()
46 ! Check libgfortran version
48 if (ANY (abs (SNORM2(d
,4) - NORM2(d
, 1)) &
49 > epsilon(0.0)*SNORM2(d
,4))) call abort()
51 if (abs (SNORM2(f
,4) - NORM2(f
, 1)) &
52 > epsilon(0.0)*SNORM2(d
,4)) call abort()
54 if (ANY (abs (abs(g(:,1)) - NORM2(g
, 2)) &
55 > epsilon(0.0))) call abort()
58 ! NORM2 algorithm based on BLAS, cf.
59 ! http://www.netlib.org/blas/snrm2.f
60 REAL FUNCTION SNORM2 (X
,n
)
61 INTEGER, INTENT(IN
) :: n
62 REAL, INTENT(IN
) :: X(n
)
64 REAL :: absXi
, scale
, SSQ
67 INTRINSIC :: ABS
, SQRT
80 IF (scale
< absXi
) THEN
81 SSQ
= 1.0 + SSQ
* (scale
/absXi
)**2
84 SSQ
= SSQ
+ (absXi
/scale
)**2
88 snorm2
= scale
* SQRT(SSQ
)