/* nag_opt_lin_lsq (e04ncc) Example Program. * * Copyright 1998 Numerical Algorithms Group. * * Mark 5, 1998. * * Mark 6 revised, 2000. * Mark 8 revised, 2004. * */ #include #include #include #include #include #include #define A(I, J) a[(I) *tda + J] #define H(I, J) h[(I) *tdh + J] int main(int argc, char *argv[]) { FILE *fpin, *fpout; char *outfile = 0; char *optionsfile = 0; Integer exit_status = 0; Integer i, j, *kx = 0, m, n, nbnd, nclin, tda, tdh; Nag_E04_Opt options; double *a = 0, *bl = 0, *bu = 0, *cvec = 0, *h = 0, objf, *x = 0; NagError fail; INIT_FAIL(fail); /* Check for command-line IO options */ fpin = nag_example_file_io(argc, argv, "-data", NULL); fpout = nag_example_file_io(argc, argv, "-results", NULL); (void) nag_example_file_io(argc, argv, "-options", &optionsfile); (void) nag_example_file_io(argc, argv, "-nag_write", &outfile); if (!outfile) { outfile = NAG_ALLOC(7, char); strcpy(outfile, "stdout"); } fprintf(fpout, "nag_opt_lin_lsq (e04ncc) Example Program Results\n"); fscanf(fpin, " %*[^\n]"); /* Skip heading in data file */ /* Read problem dimensions */ fscanf(fpin, " %*[^\n]"); fscanf(fpin, "%ld%ld%ld%*[^\n]", &m, &n, &nclin); if (m > 0 && n > 0 && nclin >= 0) { nbnd = n + nclin; if (!(a = NAG_ALLOC(nclin*n, double)) || !(bl = NAG_ALLOC(nbnd, double)) || !(bu = NAG_ALLOC(nbnd, double)) || !(cvec = NAG_ALLOC(n, double)) || !(h = NAG_ALLOC(m*n, double)) || !(x = NAG_ALLOC(n, double)) || !(kx = NAG_ALLOC(n, Integer))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } tda = n; tdh = n; } else { fprintf(fpout, "Invalid m or n or nclin.\n"); exit_status = 1; return exit_status; } /* We solve a QP2 type problem in this example */ /* Read cvec, h, a, bl, bu and x from data file */ fscanf(fpin, " %*[^\n]"); for (i = 0; i < m; ++i) fscanf(fpin, "%lf", &cvec[i]); fscanf(fpin, " %*[^\n]"); for (i = 0; i < m; ++i) for (j = 0; j < n; ++j) fscanf(fpin, "%lf", &H(i, j)); if (nclin > 0) { fscanf(fpin, " %*[^\n]"); for (i = 0; i < nclin; ++i) for (j = 0; j < n; ++j) fscanf(fpin, "%lf", &A(i, j)); } /* Read lower bounds */ fscanf(fpin, " %*[^\n]"); for (i = 0; i < nbnd; ++i) fscanf(fpin, "%lf", &bl[i]); /* Read upper bounds */ fscanf(fpin, " %*[^\n]"); for (i = 0; i < nbnd; ++i) fscanf(fpin, "%lf", &bu[i]); /* Read the initial point x */ fscanf(fpin, " %*[^\n]"); for (i = 0; i < n; ++i) fscanf(fpin, "%lf", &x[i]); /* Change the problem type */ /* nag_opt_init (e04xxc). * Initialization function for option setting */ nag_opt_init(&options); strcpy(options.outfile, outfile); options.prob = Nag_QP2; /* nag_opt_lin_lsq (e04ncc), see above. */ if (strcmp(outfile, "stdout")) fclose(fpout); nag_opt_lin_lsq(m, n, nclin, a, tda, bl, bu, cvec, (double *) 0, h, tdh, kx, x, &objf, &options, NAGCOMM_NULL, &fail); if (strcmp(outfile, "stdout")) { fpout = fopen(outfile, "a"); } if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_opt_lin_lsq (e04ncc).\n%s\n", fail.message); exit_status = 1; } /* Free options memory */ /* nag_opt_free (e04xzc). * Memory freeing function for use with option setting */ nag_opt_free(&options, "all", &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_opt_free (e04xzc).\n%s\n", fail.message); exit_status = 1; goto END; } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (a) NAG_FREE(a); if (bl) NAG_FREE(bl); if (bu) NAG_FREE(bu); if (cvec) NAG_FREE(cvec); if (h) NAG_FREE(h); if (x) NAG_FREE(x); if (kx) NAG_FREE(kx); if (optionsfile) NAG_FREE(optionsfile); if (outfile) NAG_FREE(outfile); return exit_status; }