/* nag_mv_dendrogram (g03ehc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 5, 1998.
 * Mark 8 revised, 2004.
 *
 */

#include <nag.h>
#include <stdio.h>
#include <nag_stdlib.h>
#include <nagg03.h>

#define X(I, J) x[(I) *tdx + J]
int main(void)
{
  Integer           exit_status = 0, i, j, m, n, nsym, tdx;
  Integer           *ilc = 0, *iord = 0, *isx = 0, *iuc = 0;
  char              **c = 0;
  double            *cd = 0, *d = 0, dmin_, *dord = 0, dstep, *s = 0, *x = 0;
  char              nag_enum_arg[40];
  Nag_ClusterMethod method;
  Nag_DistanceType  dist;
  Nag_MatUpdate     update;
  Nag_VarScaleType  scale;
  NagError          fail;

  INIT_FAIL(fail);

  printf("nag_mv_dendrogram (g03ehc) Example Program Results\n\n");
  scanf("%*[^\n]");
  scanf("%ld", &n);
  scanf("%ld", &m);

  if (n >= 2 && m >= 1)
    {
      if (!(cd = NAG_ALLOC(n-1, double)) ||
          !(d = NAG_ALLOC(n*(n-1)/2, double)) ||
          !(dord = NAG_ALLOC(n, double)) ||
          !(s = NAG_ALLOC(m, double)) ||
          !(x = NAG_ALLOC(n*m, double)) ||
          !(ilc = NAG_ALLOC(n-1, Integer)) ||
          !(iord = NAG_ALLOC(n, Integer)) ||
          !(isx = NAG_ALLOC(m, Integer)) ||
          !(iuc = NAG_ALLOC(n-1, Integer)))
        {
          printf("Allocation failure\n");
          exit_status = -1;
          goto END;
        }
      tdx = m;
    }
  else
    {
      printf("Invalid n or m.\n");
      exit_status = 1;
      return exit_status;
    }
  scanf("%39s", nag_enum_arg);
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  method = (Nag_ClusterMethod) nag_enum_name_to_value(nag_enum_arg);
  scanf("%39s", nag_enum_arg);
  update = (Nag_MatUpdate) nag_enum_name_to_value(nag_enum_arg);
  scanf("%39s", nag_enum_arg);
  dist = (Nag_DistanceType) nag_enum_name_to_value(nag_enum_arg);
  scanf("%39s", nag_enum_arg);
  scale = (Nag_VarScaleType) nag_enum_name_to_value(nag_enum_arg);

  for (j = 0; j < n; ++j)
    {
      for (i = 0; i < m; ++i)
        scanf("%lf", &X(j, i));
    }
  for (i = 0; i < m; ++i)
    scanf("%ld", &isx[i]);
  for (i = 0; i < m; ++i)
    scanf("%lf", &s[i]);
  scanf("%lf", &dmin_);
  scanf("%lf", &dstep);
  scanf("%ld", &nsym);

  /* Compute the distance matrix */
  /* nag_mv_distance_mat (g03eac).
   * Compute distance (dissimilarity) matrix
   */
  nag_mv_distance_mat(update, dist, scale, n, m, x, tdx, isx, s, d, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_mv_distance_mat (g03eac).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }


  /* Perform clustering */
  /* nag_mv_hierar_cluster_analysis (g03ecc).
   * Hierarchical cluster analysis
   */
  nag_mv_hierar_cluster_analysis(method, n, d, ilc, iuc, cd, iord, dord, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf(
              "Error from nag_mv_hierar_cluster_analysis (g03ecc).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }

  /* Produce dendrograms */
  /* nag_mv_dendrogram (g03ehc).
   * Construct dendogram following
   * nag_mv_hierar_cluster_analysis (g03ecc)
   */
  nag_mv_dendrogram(Nag_DendEast, n, dord, dmin_, dstep, nsym, &c, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_mv_dendrogram (g03ehc).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }

  printf("\nDendrogram, Orientation East\n\n");
  for (i = 0; i < n; i++)
    {
      printf("%s\n", c[i]);
    }
  scanf("%lf", &dmin_);
  scanf("%lf", &dstep);
  scanf("%ld", &nsym);
  /* nag_mv_dend_free (g03xzc).
   * Frees memory allocated to the dendrogram array in
   * nag_mv_dendrogram (g03ehc)
   */
  nag_mv_dend_free(&c);
  /* nag_mv_dendrogram (g03ehc), see above. */
  nag_mv_dendrogram(Nag_DendSouth, n, dord, dmin_, dstep, nsym, &c, &fail);
  if (fail.code != NE_NOERROR)
    {
      printf("Error from nag_mv_dendrogram (g03ehc).\n%s\n",
              fail.message);
      exit_status = 1;
      goto END;
    }
  printf("\n\n Dendrogram, Orientation South\n\n");
  for (i = 0; i < nsym; i++)
    {
      printf("%s\n", c[i]);
    }
  /* nag_mv_dend_free (g03xzc), see above. */
  nag_mv_dend_free(&c);

 END:
  NAG_FREE(cd);
  NAG_FREE(d);
  NAG_FREE(dord);
  NAG_FREE(s);
  NAG_FREE(x);
  NAG_FREE(ilc);
  NAG_FREE(iord);
  NAG_FREE(isx);
  NAG_FREE(iuc);

  return exit_status;
}