/* nag_zggglm (f08zpc) Example Program. * * Copyright 2008 Numerical Algorithms Group. * * Mark 9, 2009. */ #include #include #include #include #include #include int main(int argc, char *argv[]) { FILE *fpin, *fpout; /* Scalars */ double rnorm; Integer i, j, m, n, p, pda, pdb; Integer exit_status = 0; NagError fail; Nag_OrderType order; /* Arrays */ Complex *a = 0, *b = 0, *d = 0, *x = 0, *y = 0; #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); fpin = nag_example_file_io(argc, argv, "-data", NULL); fpout = nag_example_file_io(argc, argv, "-results", NULL); fprintf(fpout, "nag_zggglm (f08zpc) Example Program Results\n\n"); /* Skip heading in data file */ fscanf(fpin, "%*[^\n] "); fscanf(fpin, "%ld%ld%ld%*[^\n] ", &n, &m, &p); #ifdef NAG_COLUMN_MAJOR pda = n; pdb = n; #else pda = m; pdb = p; #endif /* Allocate memory */ if (!(a = NAG_ALLOC(n*m, Complex)) || !(b = NAG_ALLOC(n*p, Complex)) || !(d = NAG_ALLOC(n, Complex)) || !(x = NAG_ALLOC(m, Complex)) || !(y = NAG_ALLOC(p, Complex))) { fprintf(fpout, "Allocation failure\n"); exit_status = -1; goto END; } /* Read A, B and D from data file */ for (i = 1; i <= n; ++i) { for (j = 1; j <= m; ++j) fscanf(fpin, " ( %lf , %lf )", &A(i, j).re, &A(i, j).im); } fscanf(fpin, "%*[^\n] "); for (i = 1; i <= n; ++i) { for (j = 1; j <= p; ++j) fscanf(fpin, " ( %lf , %lf )", &B(i, j).re, &B(i, j).im); } fscanf(fpin, "%*[^\n] "); for (i = 1; i <= n; ++i) fscanf(fpin, " ( %lf , %lf )", &d[i - 1].re, &d[i - 1].im); fscanf(fpin, "%*[^\n] "); /* Solve the weighted least-squares problem */ /* minimize ||inv(B)*(d - A*x)|| (in the 2-norm) */ nag_zggglm(order, n, m, p, a, pda, b, pdb, d, x, y, &fail); if (fail.code == NE_NOERROR) { /* Print least-squares solution */ fprintf(fpout, "Weighted least-squares solution\n"); for (i = 1; i <= m; ++i) fprintf(fpout, "(%9.4f, %9.4f)%s", x[i - 1].re, x[i - 1].im, i%3 == 0 || i == m?"\n":" "); /* Print residual vector y = inv(B)*(d - A*x) */ fprintf(fpout, "\nResidual vector\n"); for (i = 1; i <= p; ++i) fprintf(fpout, "(%11.2e, %11.2e)%s", y[i - 1].re, y[i - 1].im, i%3 == 0 || i == p?"\n":" "); /* Compute and print the square root of the residual sum of */ /* squares */ nag_zge_norm(Nag_ColMajor, Nag_FrobeniusNorm, 1, p, y, 1, &rnorm, &fail); if (fail.code != NE_NOERROR) { fprintf(fpout, "Error from nag_zge_norm (f16uac).\n%s\n", fail.message); exit_status = 1; goto END; } fprintf(fpout, "\nSquare root of the residual sum of squares\n"); fprintf(fpout, "%11.2e\n", rnorm); } else { fprintf(fpout, "Error from nag_zggglm (f08zpc).\n%s\n", fail.message); exit_status = 1; } END: if (fpin != stdin) fclose(fpin); if (fpout != stdout) fclose(fpout); if (a) NAG_FREE(a); if (b) NAG_FREE(b); if (d) NAG_FREE(d); if (x) NAG_FREE(x); if (y) NAG_FREE(y); return exit_status; }