/* nag_ztrsyl (f08qvc) Example Program.
*
* Copyright 2017 Numerical Algorithms Group.
*
* Mark 26.1, 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;
Integer i, j, m, n, pda, pdb, pdc, pdd, pde, pdf;
Integer exit_status = 0;
double norm, scale;
NagError fail;
Nag_OrderType order;
Nag_SignType sign = Nag_Minus;
/* Arrays */
Complex *a = 0, *b = 0, *c = 0, *d = 0, *e = 0, *f = 0;
#ifdef NAG_COLUMN_MAJOR
#define A(I, J) a[(J-1)*pda + I - 1]
#define B(I, J) b[(J-1)*pdb + I - 1]
#define C(I, J) c[(J-1)*pdc + I - 1]
#define D(I, J) d[(J-1)*pdd + I - 1]
#define E(I, J) e[(J-1)*pde + I - 1]
#define F(I, J) f[(J-1)*pdf + I - 1]
order = Nag_ColMajor;
#else
#define A(I, J) a[(I-1)*pda + J - 1]
#define B(I, J) b[(I-1)*pdb + J - 1]
#define C(I, J) c[(I-1)*pdc + J - 1]
#define D(I, J) d[(I-1)*pdd + J - 1]
#define E(I, J) e[(I-1)*pde + J - 1]
#define F(I, J) f[(I-1)*pdf + J - 1]
order = Nag_RowMajor;
#endif
INIT_FAIL(fail);
printf("nag_ztrsyl (f08qvc) Example Program Results\n\n");
/* Skip heading in data file */
scanf("%*[^\n] ");
scanf("%" NAG_IFMT "%" NAG_IFMT "%*[^\n] ", &m, &n);
pda = m;
pdb = n;
#ifdef NAG_COLUMN_MAJOR
pdc = m;
pdd = m;
pde = m;
pdf = m;
#else
pdc = n;
pdd = n;
pde = n;
pdf = n;
#endif
/* Allocate memory */
if (!(a = NAG_ALLOC(m * m, Complex)) ||
!(b = NAG_ALLOC(n * m, Complex)) ||
!(c = NAG_ALLOC(m * n, Complex)) ||
!(d = NAG_ALLOC(m * n, Complex)) ||
!(e = NAG_ALLOC(m * n, Complex)) || !(f = NAG_ALLOC(m * n, Complex))
)
{
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
/* Read A, B and C from data file */
for (i = 1; i <= m; ++i) {
for (j = 1; j <= m; ++j)
scanf(" ( %lf , %lf ) ", &A(i, j).re, &A(i, j).im);
}
scanf("%*[^\n] ");
for (i = 1; i <= n; ++i) {
for (j = 1; j <= n; ++j)
scanf(" ( %lf , %lf ) ", &B(i, j).re, &B(i, j).im);
}
scanf("%*[^\n] ");
for (i = 1; i <= m; ++i) {
for (j = 1; j <= n; ++j)
scanf(" ( %lf , %lf ) ", &C(i, j).re, &C(i, j).im);
}
scanf("%*[^\n] ");
/* Copy C into F */
for (i = 1; i <= m; i++) {
for (j = 1; j <= m; j++) {
F(i, j).re = C(i, j).re;
F(i, j).im = C(i, j).im;
}
}
/* nag_gen_complx_mat_print_comp (x04dbc): Print matrix C */
fflush(stdout);
nag_gen_complx_mat_print_comp(order, Nag_GeneralMatrix, Nag_NonUnitDiag, m,
n, c, pdc, Nag_BracketForm, "%7.4f",
"Matrix C", 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;
}
/* Reorder the Schur factorization T */
/* nag_ztrsyl (f08qvc).
* Solve complex Sylvester matrix equation AX + XB = C, A
* and B are upper triangular or conjugate-transposes
*/
nag_ztrsyl(order, Nag_NoTrans, Nag_NoTrans, sign, m, n, a, pda,
b, pdb, c, pdc, &scale, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_ztrsyl (f08qvc).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
/* nag_zgemm (f16zac): Compute aC - (A*X + X*B*sign) from the solution */
/* and store in matrix E */
alpha.re = 1.0;
alpha.im = 0.0;
beta.re = 0.0;
beta.im = 0.0;
nag_zgemm(order, Nag_NoTrans, Nag_NoTrans, m, n, m, alpha, a, pda,
c, pdc, 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;
}
if (sign == Nag_Minus)
alpha.re = -1.0;
else
alpha.re = 1.0;
beta.re = 1.0;
nag_zgemm(order, Nag_NoTrans, Nag_NoTrans, m, n, n, alpha, c, pdc,
b, pdb, 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;
}
for (i = 1; i <= m; i++) {
for (j = 1; j <= n; j++) {
E(i, j).re = scale * F(i, j).re - D(i, j).re;
E(i, j).im = scale * F(i, j).im - D(i, j).im;
}
}
/* nag_zge_norm (f16uac): Find norm of matrix E and print warning if */
/* it is too large */
nag_zge_norm(order, Nag_OneNorm, m, n, e, pde, &norm, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_dge_norm (f16rac).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
if (norm > pow(x02ajc(), 0.8)) {
printf("%s\n%s\n",
"Norm of aC - (A*X + X*B*sign) is much greater than 0.",
"Schur factorization has failed.");
}
else {
printf(" SCALE = %11.2e\n", scale);
}
END:
NAG_FREE(a);
NAG_FREE(b);
NAG_FREE(c);
NAG_FREE(d);
NAG_FREE(e);
NAG_FREE(f);
return exit_status;
}