/* nag_matop_complex_gen_matrix_cond_usd (f01kcc) Example Program.
*
* Copyright 2022 Numerical Algorithms Group.
*
* Mark 28.3, 2022.
*/
#include <math.h>
#include <nag.h>
#ifdef __cplusplus
extern "C" {
#endif
static void NAG_CALL f(Integer m, Integer *iflag, Integer nz, const Complex z[],
Complex fz[], Nag_Comm *comm);
#ifdef __cplusplus
}
#endif
#define A(I, J) a[J * pda + I]
int main(void) {
/* Scalars */
Integer exit_status = 0;
Integer i, iflag, j, n, pda;
double conda, cond_rel, eps, norma, normfa;
/* Arrays */
static double ruser[1] = {-1.0};
Complex *a = 0;
/* Nag Types */
Nag_OrderType order = Nag_ColMajor;
Nag_Comm comm;
NagError fail;
INIT_FAIL(fail);
/* Output preamble */
printf("nag_matop_complex_gen_matrix_cond_usd (f01kcc) ");
printf("Example Program Results\n\n");
/* For communication with user-supplied functions: */
comm.user = ruser;
fflush(stdout);
/* Skip heading in data file */
scanf("%*[^\n] ");
/* Read in the problem size */
scanf("%" NAG_IFMT "%*[^\n]", &n);
pda = n;
if (!(a = NAG_ALLOC((pda) * (n), Complex))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
/* Read in the matrix A from data file */
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
scanf(" (%lf , %lf )", &A(i, j).re, &A(i, j).im);
scanf("%*[^\n] ");
/* Print matrix A using
* nag_file_print_matrix_complex_gen (x04dac)
* Print complex general matrix (easy-to-use)
*/
nag_file_print_matrix_complex_gen(order, Nag_GeneralMatrix, Nag_NonUnitDiag,
n, n, a, pda, "A", NULL, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_file_print_matrix_complex_gen (x04dac)\n%s\n",
fail.message);
exit_status = 2;
goto END;
}
/* Find absolute condition number estimate of f(A) for complex matrix A using
* nag_matop_complex_gen_matrix_cond_usd (f01kcc)
* Condition number for general function of a complex matrix
* using user-supplied derivatives.
*/
nag_matop_complex_gen_matrix_cond_usd(n, a, pda, f, &comm, &iflag, &conda,
&norma, &normfa, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_matop_complex_gen_matrix_cond_usd (f01kcc)\n%s\n",
fail.message);
exit_status = 1;
goto END;
}
/* Print absolute condition number estimate */
printf("\nF(A) = exp(3A)\n");
printf("Estimated absolute condition number is: %7.2f\n", conda);
/* nag_machine_precision (x02ajc) The machine precision */
eps = nag_machine_precision;
/* Find relative condition number estimate */
if (normfa > eps) {
cond_rel = conda * norma / normfa;
printf("Estimated relative condition number is: %7.2f\n", cond_rel);
} else {
printf("The estimated norm of f(A) is effectively zero");
printf("and so the relative condition number is undefined.\n");
}
END:
NAG_FREE(a);
return exit_status;
}
static void NAG_CALL f(Integer m, Integer *iflag, Integer nz, const Complex z[],
Complex fz[], Nag_Comm *comm) {
/* Scalars */
Integer j;
#ifdef _OPENMP
#pragma omp master
#endif
if (comm->user[0] == -1.0) {
printf("(User-supplied callback f, first invocation.)\n");
comm->user[0] = 0.0;
}
for (j = 0; j < nz; j++) {
/* The m^th derivative of exp 3z for complex z */
fz[j].re = pow(3.0, m) * exp(3.0 * z[j].re) * cos(3.0 * z[j].im);
fz[j].im = pow(3.0, m) * exp(3.0 * z[j].re) * sin(3.0 * z[j].im);
}
/* Set iflag nonzero to terminate execution for any reason. */
*iflag = 0;
}