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

NAG CL Interface Introduction
Example description
/* nag_mv_distance_mat (g03eac) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.2, 2024.
 *
 */

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

#define X(I, J) x[(I)*tdx + J]
int main(void) {
  Integer exit_status = 0, i, *isx = 0, j, m, n, tdx;
  double *d = 0, *s = 0, *x = 0;
  char nag_enum_arg[40];
  Nag_DistanceType dist;
  Nag_MatUpdate update;
  Nag_VarScaleType scale;
  NagError fail;

  INIT_FAIL(fail);

  printf("nag_mv_distance_mat (g03eac) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT "", &n);
  scanf("%" NAG_IFMT "", &m);
  if (n >= 2 && m >= 1) {
    if (!(d = NAG_ALLOC(n * (n - 1) / 2, double)) ||
        !(s = NAG_ALLOC(m, double)) || !(x = NAG_ALLOC((n) * (m), double)) ||
        !(isx = NAG_ALLOC(m, 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
   */
  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("%" NAG_IFMT "", &isx[i]);
  for (i = 0; i < m; ++i)
    scanf("%lf", &s[i]);

  /* 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;
  }

  /* Print the distance matrix */

  printf("\n");
  printf(" Distance Matrix ");
  printf("\n");
  printf("\n");
  printf("    %s\n", "   1       2       3       4");
  printf("\n");
  for (i = 2; i <= n; ++i) {
    printf("%2" NAG_IFMT "     ", i);
    for (j = (i - 1) * (i - 2) / 2 + 1; j <= i * (i - 1) / 2; ++j)
      printf("%5.2f   ", d[j - 1]);
    printf("\n");
  }

END:
  NAG_FREE(d);
  NAG_FREE(s);
  NAG_FREE(x);
  NAG_FREE(isx);

  return exit_status;
}