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

NAG CL Interface Introduction
Example description
/* nag_lapackeig_dbdsvdx (f08mbc) Example Program.
 *
 * Copyright 2024 Numerical Algorithms Group.
 *
 * Mark 30.0, 2024.
 */

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

int main(void) {
  /* Scalars */
  double vl, vu;
  Integer exit_status = 0, i, il = 0, iu = 0, j, n, ns, pdz;
  /* Arrays */
  char nag_enum_arg[40];
  double *d = 0, *e = 0, *s = 0, *z = 0;
  Integer *index = 0;
  /* Nag Types */
  Nag_OrderType order;
  Nag_RangeType range;
  NagError fail;

#ifdef NAG_COLUMN_MAJOR
  order = Nag_ColMajor;
#else
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_lapackeig_dbdsvdx (f08mbc) Example Program Results\n\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT "%*[^\n]", &n);

#ifdef NAG_COLUMN_MAJOR
  pdz = 2 * n;
#else
  pdz = n + 1;
#endif
  /* Allocate memory */
  if (!(d = NAG_ALLOC(n, double)) || !(e = NAG_ALLOC(n, double)) ||
      !(s = NAG_ALLOC(n, double)) ||
      !(z = NAG_ALLOC(2 * n * (n + 1), double)) ||
      !(index = NAG_ALLOC(2 * n, Integer))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read the diagonal and off-diagonal elements of the matrix A */
  for (i = 0; i < n; ++i)
    scanf("%lf", &d[i]);
  scanf("%*[^\n]");

  for (i = 0; i < n - 1; ++i)
    scanf("%lf", &e[i]);
  scanf("%*[^\n]");
  scanf("%39s%*[^\n]", nag_enum_arg);
  /* nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value.
   */
  range = (Nag_RangeType)nag_enum_name_to_value(nag_enum_arg);

  if (range == Nag_Interval) {
    /* Read the lower and upper bounds of the interval to be searched. */
    scanf("%lf%lf%*[^\n]", &vl, &vu);
  } else if (range == Nag_Indices) {
    /* Read the lower and upper indices to be selected. */
    scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &il, &iu);
  } else {
    il = 1;
    iu = n;
  }

  /* nag_lapackeig_dbdsvdx (f08mbc).
   * Solve the bidiagonal SVD.
   */
  nag_lapackeig_dbdsvdx(order, Nag_Lower, Nag_SingularVecs, Nag_Interval, n, d,
                        e, vl, vu, il, iu, &ns, s, z, pdz, index, &fail);
  if (fail.code != NE_NOERROR && fail.code != NE_CONVERGENCE) {
    printf("Error from nag_lapackeig_dbdsvdx (f08mbc).\n%s\n", fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print solution */
  if (range == Nag_Interval) {
    printf("%5" NAG_IFMT " singular values in the range (%7.3f,%7.3f]:\n", ns,
           vl, vu);
  } else {
    printf("%5" NAG_IFMT " singular values in the index range ["
           "%5" NAG_IFMT ",%5" NAG_IFMT "]:\n",
           ns, il, iu);
  }
  for (j = 0; j < ns; ++j)
    printf("%8.4f%s", s[j], (j + 1) % 8 == 0 ? "\n" : " ");
  printf("\n\n");

END:
  NAG_FREE(d);
  NAG_FREE(e);
  NAG_FREE(s);
  NAG_FREE(z);
  NAG_FREE(index);
  return exit_status;
}