/* nag_lapackeig_dgerqf (f08chc) Example Program.
*
* Copyright 2024 Numerical Algorithms Group.
*
* Mark 30.2, 2024.
*/
#include <nag.h>
#include <stdio.h>
int main(void) {
/* Scalars */
Integer i, j, m, n, nrhs, pda, pdb, pdx;
Integer exit_status = 0;
/* Arrays */
double *a = 0, *b = 0, *tau = 0, *x = 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_dgerqf (f08chc) Example Program Results\n\n");
/* Skip heading in data file */
scanf("%*[^\n]");
scanf("%" NAG_IFMT "%" NAG_IFMT "%" NAG_IFMT "%*[^\n]", &m, &n, &nrhs);
#ifdef NAG_COLUMN_MAJOR
pda = m;
pdb = m;
pdx = n;
#else
pda = n;
pdb = nrhs;
pdx = 1;
#endif
/* Allocate memory */
if (!(a = NAG_ALLOC(m * n, double)) || !(b = NAG_ALLOC(m * nrhs, double)) ||
!(tau = NAG_ALLOC(MAX(1, MIN(m, n)), double)) ||
!(x = NAG_ALLOC(n, double))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
/* Read the matrix A and the vectors b from data file */
for (i = 1; i <= m; ++i)
for (j = 1; j <= n; ++j)
scanf("%lf", &A(i, j));
scanf("%*[^\n]");
for (i = 1; i <= m; ++i)
for (j = 1; j <= nrhs; ++j)
scanf("%lf", &B(i, j));
scanf("%*[^\n]");
/* nag_lapackeig_dgerqf (f08chc).
* Compute the RQ factorization of A.
*/
nag_lapackeig_dgerqf(order, m, n, a, pda, tau, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_lapackeig_dgerqf (f08chc).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
/* nag_blast_dge_copy (f16qfc).
* Copy the m element vector b into x2, where x2 is the vector
* containing the elements x(n-m+1), ..., x(n) of x.
*/
nag_blast_dge_copy(order, Nag_NoTrans, m, 1, &B(1, 1), pdb, &x[n - m], pdx,
&fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_blast_dge_copy (f16qfc).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
/* nag_lapacklin_dtrtrs (f07tec).
* Solve R*y2 = b, storing the result in x2.
*/
nag_lapacklin_dtrtrs(order, Nag_Upper, Nag_NoTrans, Nag_NonUnitDiag, m, 1,
&A(1, n - m + 1), pda, &x[n - m], pdx, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_lapacklin_dtrtrs (f07tec).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
/* nag_blast_dload (f16fbc).
* Set y1 to zero (stored in rows 1 to (n-m) of x).
*/
nag_blast_dload(n - m, 0.0, x, 1, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_blast_dload (f16fbc).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
/* nag_lapackeig_dormrq (f08ckc).
* Compute the minimum-norm solution x = (Q^T)*y.
*/
nag_lapackeig_dormrq(order, Nag_LeftSide, Nag_Trans, n, 1, m, a, pda, tau, x,
pdx, &fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_lapackeig_dormrq (f08ckc).\n%s\n", fail.message);
exit_status = 1;
goto END;
}
/* Print minimum-norm solution */
printf("Minimum-norm solution\n");
for (i = 0; i < n; ++i)
printf("%9.4f%s", x[i], (i + 1) % 8 == 0 ? "\n" : " ");
END:
NAG_FREE(a);
NAG_FREE(b);
NAG_FREE(tau);
NAG_FREE(x);
return exit_status;
}
#undef A
#undef B