/* nag_lambertW_complex (c05bbc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 23, 2011.
 */

#include <stdio.h>
#include <math.h>
#include <nag.h>
#include <nagx04.h>
#include <nag_stdlib.h>
#include <nagc05.h>

int main(void)
{
  /* Scalars */
  Complex     w, z;
  double      resid;
  Integer     branch;
  Integer     exit_status = 0;
  char        offset[10];
  Nag_Boolean offsetenum;
  NagError    fail;

  INIT_FAIL(fail);

  printf("nag_lambertW_complex (c05bbc) Example Program Results\n");

  /* Skip heading in data file*/
  scanf("%*[^\n] ");
  scanf("%ld%*[^\n] ", &branch);
  scanf("%9s%*[^\n] ", offset);

  /*
   * nag_enum_name_to_value (x04nac).
   * Converts NAG enum member name to value
   */
  offsetenum = (Nag_Boolean) nag_enum_name_to_value(offset);

  printf("\n");
  printf("branch = %ld\n", branch);
  printf("offset = %s\n", offset);
  printf("\n               z                              w(z)"
          "                    resid\n\n");
  while (scanf(" (%lf,%lf)%*[^\n] ", &z.re, &z.im) != EOF)
    {
      /*
       * nag_lambertW_complex (c05bbc)
       * Values of Lambert's W function, W(z)
       */
      nag_lambertW_complex(branch, offsetenum, z, &w, &resid, &fail);
      if (fail.code == NE_NOERROR ||
          fail.code == NW_REAL ||
          fail.code == NW_TOO_MANY_ITER)
        {
          printf("(%14.5e,%14.5e) (%14.5e,%14.5e) %14.5e\n",
                  z.re, z.im, w.re, w.im, resid);
        }
      else
        {
          printf("Error from nag_lambertW_complex (c05bbc).\n%s\n",
                  fail.message);
          exit_status = 1;
          goto END;
        }
    }

 END:
  return exit_status;
}