dnrm2 Function

public function dnrm2(n, x, incx)

Function that returns the Euclidean norm of a vector .

Further details

  • this version written on 25-october-1982.
  • modified on 14-october-1993 to inline the call to dlassq. sven hammarling, nag ltd.
  • Converted to modern Fortran, Jacob Williams, Jan. 2016.

Note

Replaced original SLSQP routine with this one from BLAS.

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: n
real(kind=wp), intent(in), dimension(*) :: x
integer, intent(in) :: incx

Return Value real(kind=wp)


Source Code

    real(wp) function dnrm2(n,x,incx)

        implicit none

        integer,intent(in)               :: incx
        integer,intent(in)               :: n
        real(wp),dimension(*),intent(in) :: x

        real(wp) :: absxi , norm , scale , ssq
        integer :: ix

        if ( n<1 .or. incx<1 ) then
           norm = zero
        else if ( n==1 ) then
           norm = abs(x(1))
        else
           scale = zero
           ssq = one
           ! the following loop is equivalent to this call to the lapack
           ! auxiliary routine:
           ! call dlassq( n, x, incx, scale, ssq )
           do ix = 1 , 1 + (n-1)*incx , incx
              if ( x(ix)/=zero ) then
                 absxi = abs(x(ix))
                 if ( scale<absxi ) then
                    ssq = one + ssq*(scale/absxi)**2
                    scale = absxi
                 else
                    ssq = ssq + (absxi/scale)**2
                 end if
              end if
           end do
           norm = scale*sqrt(ssq)
        end if

        dnrm2 = norm

    end function dnrm2