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

NAG CL Interface Introduction
Example description
/* nag_lapackeig_zgesvdx (f08kzc) Example Program.
 *
 * Copyright 2021 Numerical Algorithms Group.
 *
 * Mark 27.3, 2021.
 */

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

int main(void) {

  /* Scalars */
  double serrbd, vl, vu;
  Integer exit_status = 0, i, j, mn;
  Integer il, iu, m, n, ns, pda, pdu, pdvt;

  /* Arrays */
  double *rcondu = 0, *rcondv = 0, *uerrbd = 0, *verrbd = 0;
  Complex *a = 0, *u = 0, *vt = 0;
  double *s = 0, *rwork = 0;
  Integer *jfail = 0;

  /* Nag Types */
  NagError fail;
  Nag_OrderType order;

#ifdef NAG_COLUMN_MAJOR
#define A(I, J) a[(J - 1) * pda + I - 1]
  order = Nag_ColMajor;
#else
#define A(I, J) a[(I - 1) * pda + J - 1]
  order = Nag_RowMajor;
#endif

  INIT_FAIL(fail);

  printf("nag_lapackeig_zgesvdx (f08kzc) Example Program Results\n\n");
  /* Skip heading in data file */
  scanf("%*[^\n]");
  scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &m, &n);
  mn = MIN(m, n);
  if (mn < 0) {
    printf("Invalid m or n\n");
    exit_status = 1;
    goto END;
  }

  /* Allocate memory */
  if (!(a = NAG_ALLOC(m * n, Complex)) || !(s = NAG_ALLOC(mn, double)) ||
      !(u = NAG_ALLOC(m * mn, Complex)) || !(vt = NAG_ALLOC(mn * n, Complex)) ||
      !(rwork = NAG_ALLOC(mn, double)) ||
      !(jfail = NAG_ALLOC(2 * mn, Integer)) ||
      !(rcondu = NAG_ALLOC(n, double)) || !(rcondv = NAG_ALLOC(n, double)) ||
      !(uerrbd = NAG_ALLOC(n, double)) || !(verrbd = NAG_ALLOC(n, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

#ifdef NAG_COLUMN_MAJOR
  pda = m;
  pdu = m;
  pdvt = mn;
#else
  pda = n;
  pdu = mn;
  pdvt = n;
#endif

  /* Read the m by n matrix A from data file */
  for (i = 1; i <= m; ++i)
    for (j = 1; j <= n; ++j)
      scanf(" ( %lf , %lf )", &A(i, j).re, &A(i, j).im);
  scanf("%*[^\n]");

  /* Choose singular values in the interval [0,3.5] */
  vl = 0.0;
  vu = 3.5;
  il = 1;
  iu = 0;

  /* nag_lapackeig_zgesvdx (f08kzc).
   * Compute the singular values and left and right singular vectors
   * of A (A = U*S*(V^T), m.ge.n)
   */
  nag_lapackeig_zgesvdx(order, Nag_SingularVecs, Nag_SingularVecs, Nag_Interval,
                        m, n, a, pda, vl, vu, il, iu, &ns, s, u, pdu, vt, pdvt,
                        rwork, jfail, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_zgesvdx (f08kzc).\n%s\n", fail.message);
    exit_status = 2;
    goto END;
  }

  /* Print solution */
  printf("Number of singular values in the range [%7.3f,%7.3f] = "
         " %2" NAG_IFMT "\n\n",
         vl, vu, ns);
  printf("Selected singular values\n");
  for (j = 0; j < ns; j++)
    printf("%8.4f", s[j]);
  printf("\n\n");
  /* Compute the approximate error bound (as multiple of machine precision)
   * for the computed singular values.
   * Note that for the 2-norm, s[0] = norm(A).
   */
  serrbd = s[0];

  /* Estimate reciprocal condition numbers for the singular vectors using
   * nag_lapackeig_ddisna (f08flc).
   */
  nag_lapackeig_ddisna(Nag_LeftSingVecs, m, ns, s, rcondu, &fail);
  if (fail.code == NE_NOERROR) {
    nag_lapackeig_ddisna(Nag_RightSingVecs, m, ns, s, rcondv, &fail);
  }
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_lapackeig_ddisna (f08flc).\n%s\n", fail.message);
    exit_status = 3;
    goto END;
  }

  /* Compute the error estimates for the singular vectors */
  for (i = 0; i < ns; ++i) {
    uerrbd[i] = serrbd / rcondu[i];
    verrbd[i] = serrbd / rcondv[i];
  }

  /* Print the approximate error bounds for the singular values and vectors */
  printf("\n\nError estimates (as multiple of machine precision)\n\n");
  printf("     for the singular values\n%4ld\n", lrint(serrbd));

  printf("\n     for the left singular vectors\n");
  for (i = 0; i < ns; ++i)
    printf("%4ld", lrint(uerrbd[i]));

  printf("\n\n     for the right singular vectors\n");
  for (i = 0; i < ns; ++i)
    printf("%4ld", lrint(verrbd[i]));
  printf("\n");

END:
  NAG_FREE(a);
  NAG_FREE(rcondu);
  NAG_FREE(rcondv);
  NAG_FREE(s);
  NAG_FREE(u);
  NAG_FREE(uerrbd);
  NAG_FREE(verrbd);
  NAG_FREE(vt);
  NAG_FREE(rwork);
  NAG_FREE(jfail);

  return exit_status;
}

#undef A