/* nag_det_real_gen (f03bac) Example Program.
*
* Copyright 2024 Numerical Algorithms Group.
*
* Mark 30.2, 2024.
*/
#include <math.h>
#include <nag.h>
int main(void) {
/* Scalars */
Integer exit_status = 0;
Integer i, id, j, n, pda;
double d;
/* Arrays */
Integer *ipiv = 0;
double *a = 0;
/* NAG types */
NagError fail;
Nag_OrderType order;
Nag_MatrixType matrix = Nag_GeneralMatrix;
Nag_DiagType diag = Nag_NonUnitDiag;
printf("nag_det_real_gen (f03bac) Example Program Results\n");
fflush(stdout);
/* Skip heading in data file */
scanf("%*[^\n] ");
scanf("%" NAG_IFMT "%*[^\n]", &n);
pda = n;
if (!(a = NAG_ALLOC(n * n, double)) || !(ipiv = NAG_ALLOC(n, Integer))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
/* Define matrix element A_ij in terms of elements of array a[k] */
#ifdef NAG_COLUMN_MAJOR
order = Nag_ColMajor;
#define A(I, J) a[(J - 1) * pda + (I - 1)]
#else
order = Nag_RowMajor;
#define A(J, I) a[(J - 1) * pda + (I - 1)]
#endif
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf("%lf", &A(i, j));
scanf("%*[^\n] ");
INIT_FAIL(fail);
/* nag_lapacklin_dgetrf (f07adc) - LU factorization of real m by n matrix */
nag_lapacklin_dgetrf(order, n, n, a, pda, ipiv, &fail);
if (fail.code != NE_NOERROR) {
printf("%s\n", fail.message);
exit_status = 1;
goto END;
}
/* nag_file_print_matrix_real_gen (x04cac).
* Print real general matrix (easy-to-use)
*/
printf("\n");
fflush(stdout);
nag_file_print_matrix_real_gen(order, matrix, diag, n, n, a, pda,
"Array A after factorization", NULL, &fail);
if (fail.code != NE_NOERROR) {
printf("%s\n", fail.message);
exit_status = 2;
goto END;
}
printf("\nPivots:\n ");
for (j = 0; j < n; j++)
printf("%11" NAG_IFMT " ", ipiv[j]);
printf("\n");
/* nag_det_real_gen (f03bac).
* LU factorization and determinant of real matrix
*/
nag_det_real_gen(order, n, a, pda, ipiv, &d, &id, &fail);
if (fail.code != NE_NOERROR) {
printf("%s\n", fail.message);
exit_status = 3;
goto END;
}
printf("d = %12.5f id = %12" NAG_IFMT "\n", d, id);
printf("Value of determinant = %13.5e\n", d * pow((double)2.0, id));
END:
NAG_FREE(a);
NAG_FREE(ipiv);
return exit_status;
}