/* nag_fit_glin_l1sol (e02gac) Example Program.
*
* Copyright 2024 Numerical Algorithms Group.
*
* Mark 30.3, 2024.
*/
#include <math.h>
#include <nag.h>
#include <stdio.h>
int main(void) {
/* Scalars */
double resid, t, tol;
Integer exit_status, i, iter, m, rank, n, nplus2, pda;
NagError fail;
Nag_OrderType order;
/* Arrays */
double *a = 0, *b = 0, *x = 0;
#ifdef NAG_COLUMN_MAJOR
#define A(I, J) a[(J - 1) * pda + I - 1]
order = Nag_ColMajor;
#else
#define A(I, J) a[(I - 1) * pda + J - 1]
order = Nag_RowMajor;
#endif
INIT_FAIL(fail);
exit_status = 0;
printf("nag_fit_glin_l1sol (e02gac) Example Program Results\n");
/* Skip heading in data file */
scanf("%*[^\n] ");
n = 3;
nplus2 = n + 2;
scanf("%" NAG_IFMT "%*[^\n] ", &m);
if (m > 0) {
/* Allocate memory */
if (!(a = NAG_ALLOC((m + 2) * nplus2, double)) ||
!(b = NAG_ALLOC(m, double)) || !(x = NAG_ALLOC(nplus2, double))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
if (order == Nag_ColMajor)
pda = m + 2;
else
pda = nplus2;
for (i = 1; i <= m; ++i) {
scanf("%lf%lf%*[^\n] ", &t, &b[i - 1]);
A(i, 1) = exp(t);
A(i, 2) = exp(-t);
A(i, 3) = 1.0;
}
tol = 0.0;
/* nag_fit_glin_l1sol (e02gac).
* L_1-approximation by general linear function
*/
nag_fit_glin_l1sol(order, m, a, b, nplus2, tol, x, &resid, &rank, &iter,
&fail);
if (fail.code == NE_INT || fail.code == NE_INT_2 ||
fail.code == NE_TOO_MANY_ITER || fail.code == NE_NO_LICENCE) {
printf("Error from nag_fit_glin_l1sol (e02gac).\n%s\n", fail.message);
exit_status = 1;
goto END;
} else {
printf("\n");
printf("resid = %11.2e Rank = %5" NAG_IFMT " Iterations ="
" %5" NAG_IFMT "\n",
resid, rank, iter);
printf("\n");
printf("Solution\n");
for (i = 1; i <= n; ++i)
printf("%10.4f", x[i - 1]);
printf("\n");
}
}
END:
NAG_FREE(a);
NAG_FREE(b);
NAG_FREE(x);
return exit_status;
}