NAG Library Manual, Mark 30.3
Interfaces:  FL   CL   CPP   AD 

NAG FL Interface Introduction
Example description
    Program g03ejfe

!     G03EJF Example Program Text

!     Mark 30.3 Release. nAG Copyright 2024.

!     .. Use Statements ..
      Use nag_library, Only: g03eaf, g03ecf, g03ejf, nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: nin = 5, nout = 6, rnlen = 3
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: dlevel
      Integer                          :: i, ifail, k, ld, ldx, liwk, m,       &
                                          method, n, n1
      Character (1)                    :: dist, scal, update
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: cd(:), d(:), dord(:), s(:), x(:,:)
      Integer, Allocatable             :: ic(:), ilc(:), iord(:), isx(:),      &
                                          iuc(:), iwk(:)
      Character (rnlen), Allocatable   :: row_name(:)
!     .. Executable Statements ..
      Write (nout,*) 'G03EJF Example Program Results'
      Write (nout,*)

!     Skip heading in data file
      Read (nin,*)

!     Read in the problem size
      Read (nin,*) n, m

!     Read in information on the type of distance matrix to use
      Read (nin,*) update, dist, scal

      ldx = n
      ld = n*(n-1)/2
      n1 = n - 1
      liwk = 2*n
      Allocate (x(ldx,m),isx(m),s(m),d(ld),ilc(n1),iuc(n1),cd(n1),iord(n),     &
        dord(n),iwk(liwk),ic(n),row_name(n))

!     Read in the data used to construct distance matrix
      Read (nin,*)(x(i,1:m),i=1,n)

!     Read in variable inclusion flags
      Read (nin,*) isx(1:m)

!     Read in scaling
      If (scal=='G' .Or. scal=='g') Then
        Read (nin,*) s(1:m)
      End If

!     Compute the distance matrix
      ifail = 0
      Call g03eaf(update,dist,scal,n,m,x,ldx,isx,s,d,ifail)

!     Read in information on the clustering method to use
      Read (nin,*) method

!     Read in first RNLEN characters of row names. Used to make example
!     output easier to read
      Read (nin,*) row_name(1:n)

!     Perform clustering
      ifail = 0
      Call g03ecf(method,n,d,ilc,iuc,cd,iord,dord,iwk,ifail)

!     Display full clustering information
      Write (nout,*) '  Distance   Clusters Joined'
      Write (nout,*)
      Do i = 1, n - 1
        Write (nout,99999) cd(i), row_name(ilc(i)), row_name(iuc(i))
      End Do
      Write (nout,*)

!     Read in number of clusters required (K) and
!     distance (DLEVEL). If K > 0 then DLEVEL is
!     ignored (i.e. attempt to find K clusters,
!     irrespective of distance), else all clusters at
!     level DLEVEL are used
      Read (nin,*) k, dlevel

!     Compute cluster indicator
      ifail = 0
      Call g03ejf(n,cd,iord,dord,k,dlevel,ic,ifail)

!     Display the indicators
      Write (nout,99998) ' Allocation to ', k, ' clusters'
      Write (nout,99996) ' Clusters found at distance ', dlevel
      Write (nout,*)
      Write (nout,*) ' Object  Cluster'
      Write (nout,*)
      Write (nout,99997)(row_name(i),ic(i),i=1,n)

99999 Format (1X,F10.3,5X,2A)
99998 Format (1X,A,I0,A)
99997 Format (6X,A,5X,I2)
99996 Format (1X,A,F0.3)
    End Program g03ejfe