/* nag_lapackeig_zgels (f08anc) Example Program.
*
* Copyright 2023 Numerical Algorithms Group.
*
* Mark 29.3, 2023.
*/
#include <nag.h>
#include <stdio.h>
int main(void) {
/* Scalars */
double rnorm;
Integer exit_status = 0, i, j, m, n, nrhs, pda, pdb;
/* Arrays */
Complex *a = 0, *b = 0;
/* Nag Types */
Nag_OrderType order;
NagError fail;
#ifdef NAG_COLUMN_MAJOR
#define A(I, J) a[(J - 1) * pda + I - 1]
#define B(I, J) b[(J - 1) * pdb + 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]
order = Nag_RowMajor;
#endif
INIT_FAIL(fail);
printf("nag_lapackeig_zgels (f08anc) Example Program Results\n\n");
/* Skip heading in data file */
scanf("%*[^\n]");
scanf("%" NAG_IFMT "%" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &m, &n, &nrhs);
/* Allocate memory */
if (!(a = NAG_ALLOC(m * n, Complex)) || !(b = NAG_ALLOC(m * nrhs, Complex))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
#ifdef NAG_COLUMN_MAJOR
pda = m;
pdb = MAX(m,n);
#else
pda = n;
pdb = nrhs;
#endif
/* Read A and B 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]");
for (i = 1; i <= m; ++i)
for (j = 1; j <= nrhs; ++j)
scanf(" ( %lf , %lf )", &B(i, j).re, &B(i, j).im);
scanf("%*[^\n]");
/* nag_lapackeig_zgels (f08anc).
* Solve the least squares problem min( norm2(b - Ax) ) for x.
*/
nag_lapackeig_zgels(order, Nag_NoTrans, m, n, nrhs, a, pda, b, pdb, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_lapackeig_zgels (f08anc).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
/* Print solution */
printf("Least squares solution\n");
for (i = 1; i <= n; ++i) {
for (j = 1; j <= nrhs; ++j)
printf("(%7.4f, %7.4f)%s", B(i, j).re, B(i, j).im,
j % 4 == 0 ? "\n" : " ");
printf("\n");
}
/* nag_blast_zge_norm (f16uac).
* Compute and print estimate of the square root of the residual
* sum of squares.
*/
nag_blast_zge_norm(order, Nag_FrobeniusNorm, m - n, 1, &B(n + 1, 1), pdb,
&rnorm, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_blast_zge_norm (f16uac).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
printf("\nSquare root of the residual sum of squares\n");
printf("%11.2e\n", rnorm);
END:
NAG_FREE(a);
NAG_FREE(b);
return exit_status;
}
#undef A
#undef B