/* nag_linsys_real_gen_norm_rcomm (f04ydc) Example Program.
*
* Copyright 2023 Numerical Algorithms Group.
*
* Mark 29.3, 2023.
*/
#include <math.h>
#include <nag.h>
int main(void) {
/* Scalars */
Integer exit_status = 0, irevcm = 0, seed = 354;
Integer i, j, m, n, pda, pdx, pdy, t;
double cond = 0.0, nrma = 0.0, nrminv = 0.0;
/* Local Arrays */
Integer *icomm = 0, *ipiv = 0;
double *a = 0, *work = 0, *x = 0, *y = 0;
/* Nag Types */
Nag_OrderType order;
NagError fail;
Nag_TransType trans;
INIT_FAIL(fail);
#define A(I, J) a[(J - 1) * pda + I - 1]
order = Nag_ColMajor;
/* Output preamble */
printf("nag_linsys_real_gen_norm_rcomm (f04ydc) ");
printf("Example Program Results\n\n");
fflush(stdout);
/* Skip heading in data file */
scanf("%*[^\n] ");
/* Read in the problem size and the value of the parameter t */
scanf("%" NAG_IFMT " %" NAG_IFMT " %" NAG_IFMT " %*[^\n] ", &m, &n, &t);
pda = n;
pdx = n;
pdy = m;
if (!(a = NAG_ALLOC(m * n, double)) || !(x = NAG_ALLOC(n * t, double)) ||
!(y = NAG_ALLOC(m * t, double)) || !(work = NAG_ALLOC(m * t, double)) ||
!(ipiv = NAG_ALLOC(n, Integer)) ||
!(icomm = NAG_ALLOC(2 * n + 5 * t + 20, Integer))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
/* Read in the matrix a from data file */
for (i = 1; i <= m; i++)
for (j = 1; j <= n; j++)
scanf(" %lf ", &A(i, j));
scanf("%*[^\n]");
/* Compute the 1-norm of A */
nag_blast_dge_norm(order, Nag_OneNorm, m, n, a, pda, &nrma, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_blast_dge_norm\n%s\n", fail.message);
exit_status = 1;
goto END;
}
printf("Estimated norm of A is: %7.2f\n\n", nrma);
/*
* Estimate the norm of A^(-1) without explicitly forming A^(-1)
*/
/* Compute an LU factorization of A using nag_lapacklin_dgetrf (f07adc) */
nag_lapacklin_dgetrf(order, m, n, a, pda, ipiv, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_lapacklin_dgetrf\n%s\n", fail.message);
exit_status = 2;
goto END;
}
/* Estimate the norm of A^(-1) using the LU factors of A
* nag_linsys_real_gen_norm_rcomm (f04ydc)
* Estimate of the 1-norm of a real matrix
*/
do {
nag_linsys_real_gen_norm_rcomm(&irevcm, m, n, x, pdx, y, pdy, &nrminv, t,
seed, work, icomm, &fail);
if (irevcm == 1) {
/* Compute y = inv(A)*x by solving Ay = x */
trans = Nag_NoTrans;
nag_lapacklin_dgetrs(order, trans, n, t, a, pda, ipiv, x, pdx, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_lapacklin_dgetrs\n%s\n", fail.message);
exit_status = 3;
goto END;
}
for (i = 0; i < n * t; i++)
y[i] = x[i];
}
else if (irevcm == 2) {
/* Compute x = inv(A)^T y by solving A^T x = y */
trans = Nag_Trans;
nag_lapacklin_dgetrs(order, trans, n, t, a, pda, ipiv, y, pdy, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_lapacklin_dgetrs\n%s\n", fail.message);
exit_status = 4;
goto END;
}
for (i = 0; i < n * t; i++)
x[i] = y[i];
}
} while (irevcm != 0);
if (fail.code != NE_NOERROR) {
printf("Error from nag_linsys_real_gen_norm_rcomm (f04ydc)\n%s\n",
fail.message);
exit_status = 5;
goto END;
}
printf("Estimated norm of inverse of A is: %7.2f\n\n", nrminv);
/* Compute and print the estimated condition number */
cond = nrma * nrminv;
printf("Estimated condition number of A is: %7.2f\n", cond);
END:
NAG_FREE(a);
NAG_FREE(x);
NAG_FREE(y);
NAG_FREE(work);
NAG_FREE(icomm);
NAG_FREE(ipiv);
return exit_status;
}