/* nag_zunghr (f08ntc) Example Program.
*
* Copyright 2017 Numerical Algorithms Group.
*
* Mark 26.2, 2017.
*/
#include <stdio.h>
#include <math.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nagf08.h>
#include <nagf16.h>
#include <nagx04.h>
#include <nagx02.h>
int main(void)
{
/* Scalars */
Complex alpha, beta;
double norm;
Integer i, j, n, pda, pdc, pdd, pdz, tau_len, w_len;
Integer exit_status = 0;
NagError fail;
Nag_OrderType order;
/* Arrays */
Complex *a = 0, *c = 0, *d = 0, *tau = 0, *w = 0, *z = 0;
#ifdef NAG_COLUMN_MAJOR
#define A(I, J) a[(J - 1) * pda + I - 1]
#define D(I, J) d[(J - 1) * pdd + I - 1]
#define Z(I, J) z[(J - 1) * pdz + I - 1]
order = Nag_ColMajor;
#else
#define A(I, J) a[(I - 1) * pda + J - 1]
#define D(I, J) d[(I - 1) * pdd + J - 1]
#define Z(I, J) z[(I - 1) * pdz + J - 1]
order = Nag_RowMajor;
#endif
INIT_FAIL(fail);
printf("nag_zunghr (f08ntc) Example Program Results\n\n");
/* Skip heading in data file */
scanf("%*[^\n] ");
scanf("%" NAG_IFMT "%*[^\n] ", &n);
#ifdef NAG_COLUMN_MAJOR
pda = n;
pdc = n;
pdd = n;
pdz = n;
#else
pda = n;
pdc = n;
pdd = n;
pdz = n;
#endif
tau_len = n - 1;
w_len = n;
/* Allocate memory */
if (!(a = NAG_ALLOC(n * n, Complex)) ||
!(c = NAG_ALLOC(n * n, Complex)) ||
!(d = NAG_ALLOC(n * n, Complex)) ||
!(tau = NAG_ALLOC(tau_len, Complex)) ||
!(w = NAG_ALLOC(w_len, Complex)) || !(z = NAG_ALLOC(n * n, Complex)))
{
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
/* Read A from data file */
for (i = 1; i <= n; ++i) {
for (j = 1; j <= n; ++j)
scanf(" ( %lf , %lf )", &A(i, j).re, &A(i, j).im);
}
scanf("%*[^\n] ");
/* Copy A into D */
for (i = 1; i <= n; ++i) {
for (j = 1; j <= n; ++j) {
D(i, j).re = A(i, j).re;
D(i, j).im = A(i, j).im;
}
}
/* nag_gen_complx_mat_print_comp (x04dbc): Print matrix A */
fflush(stdout);
nag_gen_complx_mat_print_comp(order, Nag_GeneralMatrix, Nag_NonUnitDiag, n,
n, a, pda, Nag_BracketForm, "%7.4f",
"Matrix A", Nag_IntegerLabels, 0,
Nag_IntegerLabels, 0, 80, 0, 0, &fail);
printf("\n");
if (fail.code != NE_NOERROR) {
printf("Error from nag_gen_complx_mat_print_comp (x04dbc).\n%s\n",
fail.message);
exit_status = 1;
goto END;
}
/* Reduce A to upper Hessenberg form H = (Q^T)*A*Q */
/* nag_zgehrd (f08nsc).
* Unitary reduction of complex general matrix to upper
* Hessenberg form
*/
nag_zgehrd(order, n, 1, n, a, pda, tau, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_zgehrd (f08nsc).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
/* Copy A into Z */
for (i = 1; i <= n; ++i) {
for (j = 1; j <= n; ++j) {
Z(i, j).re = A(i, j).re;
Z(i, j).im = A(i, j).im;
}
}
/* Form Q explicitly, storing the result in Z */
/* nag_zunghr (f08ntc).
* Generate unitary transformation matrix from reduction to
* Hessenberg form determined by nag_zgehrd (f08nsc)
*/
nag_zunghr(order, n, 1, n, z, pdz, tau, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_zunghr (f08ntc).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
/* Calculate the Schur factorization of H = Y*T*(Y^T) and form */
/* Q*Y explicitly, storing the result in Z */
/* Note that A = Z*T*(Z^T), where Z = Q*Y */
/* nag_zhseqr (f08psc).
* Eigenvalues and Schur factorization of complex upper
* Hessenberg matrix reduced from complex general matrix
*/
nag_zhseqr(order, Nag_Schur, Nag_UpdateZ, n, 1, n, a, pda,
w, z, pdz, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_zhseqr (f08psc).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
/* nag_zgemm (f16zac): Compute A - Z*T*Z^H from the factorization of */
/* A and store in matrix D */
alpha.re = 1.0;
alpha.im = 0.0;
beta.re = 0.0;
beta.im = 0.0;
nag_zgemm(order, Nag_NoTrans, Nag_NoTrans, n, n, n, alpha, z, pdz,
a, pda, beta, c, pdc, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_zgemm (f16zac).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
alpha.re = -1.0;
beta.re = 1.0;
nag_zgemm(order, Nag_NoTrans, Nag_ConjTrans, n, n, n, alpha, c, pdc,
z, pdz, beta, d, pdd, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_zgemm (f16zac).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
/* nag_zge_norm (f16uac): Find norm of matrix D and print warning if */
/* it is too large */
nag_zge_norm(order, Nag_OneNorm, n, n, d, pdd, &norm, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_zge_norm (f16uac).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
if (norm > pow(x02ajc(), 0.8)) {
printf("%s\n%s\n", "Norm of A-(Z*T*Z^H) is much greater than 0.",
"Schur factorization has failed.");
}
END:
NAG_FREE(a);
NAG_FREE(c);
NAG_FREE(d);
NAG_FREE(tau);
NAG_FREE(w);
NAG_FREE(z);
return exit_status;
}