/* nag_matop_real_symm_matrix_fun (f01efc) Example Program.
*
* Copyright 2023 Numerical Algorithms Group.
*
* Mark 29.3, 2023.
*/
#include <math.h>
#include <nag.h>
#ifdef __cplusplus
extern "C" {
#endif
static void NAG_CALL f(Integer *flag, Integer n, const double x[], double fx[],
Nag_Comm *comm);
#ifdef __cplusplus
}
#endif
int main(void) {
/* Scalars */
Integer exit_status = 0;
double k = 1.0;
Integer i, flag, j, n, pda;
/* Arrays */
char uplo_c[40];
Integer iuser[1];
double user[1];
double *a = 0;
char *outfile = 0;
/* NAG types */
Nag_UploType uplo;
Nag_MatrixType matrix;
Nag_Comm comm;
Nag_OrderType order;
NagError fail;
INIT_FAIL(fail);
/* Communicate constant k and initialize function counter through comm */
comm.user = user;
comm.iuser = iuser;
user[0] = k;
iuser[0] = 0;
printf("nag_matop_real_symm_matrix_fun (f01efc) Example Program Results");
printf("\n\n");
fflush(stdout);
/* Read problem parameters from data file */
scanf("%*[^\n]%" NAG_IFMT "%*[^\n] %39s%*[^\n] ", &n, uplo_c);
/* nag_enum_name_to_value (x04nac).
* Converts NAG enum member name to value
*/
uplo = (Nag_UploType)nag_enum_name_to_value(uplo_c);
pda = n;
if (!(a = NAG_ALLOC((pda) * (n), double))) {
printf("Allocation failure\n");
exit_status = -1;
goto END;
}
#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
/* Read A from data file */
if (uplo == Nag_Upper) {
matrix = Nag_UpperMatrix;
for (i = 1; i <= n; i++)
for (j = i; j <= n; j++)
scanf("%lf", &A(i, j));
} else {
matrix = Nag_LowerMatrix;
for (i = 1; i <= n; i++)
for (j = 1; j <= i; j++)
scanf("%lf", &A(i, j));
}
scanf("%*[^\n]");
/* nag_matop_real_symm_matrix_fun (f01efc).
* Function of a real symmetric matrix
*/
nag_matop_real_symm_matrix_fun(order, uplo, n, a, pda, f, &comm, &flag,
&fail);
if (fail.code != NE_NOERROR) {
printf("Error from nag_matop_real_symm_matrix_fun (f01efc).\n%s\n",
fail.message);
exit_status = 1;
goto END;
}
/* Expected number of function evaluations is n. */
if (iuser[0] != n)
printf("\nNumber of function evaluations = %" NAG_IFMT "\n\n", iuser[0]);
/* nag_file_print_matrix_real_gen (x04cac).
* Print real general matrix (easy-to-use)
*/
nag_file_print_matrix_real_gen(order, matrix, Nag_NonUnitDiag, n, n, a, pda,
"Symmetric f(A)=cos(kA)", outfile, &fail);
if (fail.code != NE_NOERROR) {
printf("%s\n", fail.message);
exit_status = 2;
goto END;
}
END:
NAG_FREE(a);
return exit_status;
}
static void NAG_CALL f(Integer *flag, Integer n, const double x[], double fx[],
Nag_Comm *comm) {
Integer j;
double k;
if (!comm->user[0]) {
*flag = -1;
} else {
k = comm->user[0];
for (j = 0; j < n; j++) {
comm->iuser[0]++;
fx[j] = cos(k * x[j]);
}
}
}