dnrm2.f (2768B)
1 !> 2 ! @license Apache-2.0 3 ! 4 ! Copyright (c) 2020 The Stdlib Authors. 5 ! 6 ! Licensed under the Apache License, Version 2.0 (the "License"); 7 ! you may not use this file except in compliance with the License. 8 ! You may obtain a copy of the License at 9 ! 10 ! http://www.apache.org/licenses/LICENSE-2.0 11 ! 12 ! Unless required by applicable law or agreed to in writing, software 13 ! distributed under the License is distributed on an "AS IS" BASIS, 14 ! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 ! See the License for the specific language governing permissions and 16 ! limitations under the License. 17 !< 18 19 !> Computes the L2-norm of a double-precision floating-point vector. 20 ! 21 ! ## Notes 22 ! 23 ! * Modified version of reference BLAS level1 routine (version 3.8.0). Updated to "free form" Fortran 95. 24 ! 25 ! ## Authors 26 ! 27 ! * Univ. of Tennessee 28 ! * Univ. of California Berkeley 29 ! * Univ. of Colorado Denver 30 ! * NAG Ltd. 31 ! 32 ! ## History 33 ! 34 ! * Original version written on 25-October-1982. 35 ! 36 ! ## License 37 ! 38 ! From <http://netlib.org/blas/faq.html>: 39 ! 40 ! > The reference BLAS is a freely-available software package. It is available from netlib via anonymous ftp and the World Wide Web. Thus, it can be included in commercial software packages (and has been). We only ask that proper credit be given to the authors. 41 ! > 42 ! > Like all software, it is copyrighted. It is not trademarked, but we do ask the following: 43 ! > 44 ! > * If you modify the source for these routines we ask that you change the name of the routine and comment the changes made to the original. 45 ! > 46 ! > * We will gladly answer any questions regarding the software. If a modification is done, however, it is the responsibility of the person who modified the routine to provide support. 47 ! 48 ! @param {integer} N - number of values over which to compute the norm 49 ! @param {Array<double>} dx - array 50 ! @param {integer} stride - stride length 51 ! @returns {double} the L2-norm 52 !< 53 double precision function dnrm2( N, dx, stride ) 54 implicit none 55 !.. 56 ! Scalar arguments: 57 integer :: N, stride 58 ! .. 59 ! Array arguments: 60 double precision, intent(in) :: dx(*) 61 ! .. 62 ! Local scalars: 63 double precision :: ax, scale, ssq 64 integer :: i 65 ! .. 66 ! Intrinsic functions: 67 intrinsic dabs, dsqrt 68 ! .. 69 dnrm2 = 0.0d0 70 ! .. 71 if ( N <= 0 .OR. stride <= 0 ) then 72 return 73 end if 74 !.. 75 if ( N == 1 ) then 76 dnrm2 = dabs( dx( 1 ) ) 77 return 78 end if 79 ! .. 80 scale = 0.0d0 81 ssq = 1.0d0 82 do i = 1, 1+((N-1)*stride), stride 83 if ( dx( i ) /= 0.0d0 ) then 84 ax = dabs( dx( i ) ) 85 if ( scale < ax ) then 86 ssq = 1.0d0 + ( ssq * (scale/ax)**2 ) 87 scale = ax 88 else 89 ssq = ssq + (ax/scale)**2 90 end if 91 end if 92 end do 93 dnrm2 = scale * dsqrt( ssq ) 94 end function dnrm2