/* nag_sum_sqs_update (g02btc) Example Program. * * Copyright 2002 Numerical Algorithms Group. * * Mark 7, 2002. */ #include #include #include #include #include #include #include int main(void) { /* Scalars */ double alpha, sw, wt; Integer exit_status, i, j, m, mm, n, nprint, incx; NagError fail; Nag_SumSquare mean_enum; /* Arrays */ char mean[2]; double *c=0, *v=0, *x=0, *xbar=0; INIT_FAIL(fail); exit_status = 0; Vprintf("nag_sum_sqs_update (g02btc) Example Program Results\n"); /* Skip heading in data file */ Vscanf("%*[^\n] "); incx = 1; while (scanf("' %1s '%ld%ld%ld%*[^\n]", mean, &m, &n, &nprint) != EOF) { /* Allocate memory */ if ( !(c = NAG_ALLOC((m*m+m)/2, double)) || !(v = NAG_ALLOC((m*m+m)/2, double)) || !(x = NAG_ALLOC(m*incx, double)) || !(xbar = NAG_ALLOC(m, double)) ) { Vprintf("Allocation failure\n"); exit_status = -1; goto END; } sw = 0.0; for (i = 1; i <= n; ++i) { Vscanf("%lf", &wt); for (j = 1; j <= m; ++j) Vscanf("%lf", &x[j - 1]); Vscanf("%*[^\n] "); if (mean[0] == 'M') mean_enum = Nag_AboutMean; else if (mean[0] == 'Z') mean_enum = Nag_AboutZero; else { Vprintf("Incorrect value for mean\n"); exit_status = -1; goto END; } /* Calculate the sums of squares and cross-products matrix */ /* nag_sum_sqs_update (g02btc). * Update a weighted sum of squares matrix with a new * observation */ nag_sum_sqs_update(mean_enum, m, wt, x, incx, &sw, xbar, c, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_sum_sqs_update (g02btc).\n%s\n", fail.message); exit_status = 1; goto END; } if (i % nprint == 0 || i == n) { Vprintf("\n"); Vprintf("---------------------------------------------\n"); Vprintf("Observation: %4ld Weight = %13.4f\n", i, wt); Vprintf("\n"); Vprintf("---------------------------------------------\n"); Vprintf("\n"); Vprintf("Means\n"); for (j = 1; j <= m; ++j) Vprintf("%14.4f%s", xbar[j - 1], j%4 == 0 || j == m ? "\n":" "); Vprintf("\n"); /* Print the sums of squares and cross products matrix */ /* nag_pack_real_mat_print (x04ccc). * Print real packed triangular matrix (easy-to-use) */ nag_pack_real_mat_print(Nag_ColMajor, Nag_Upper, Nag_NonUnitDiag, m, c, "Sums of squares and cross-products", 0, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_pack_real_mat_print (x04ccc).\n%s\n", fail.message); exit_status = 1; goto END; } if (sw > 1.0) { /* Calculate the variance matrix */ alpha = 1.0 / (sw - 1.0); mm = m * (m + 1) / 2; f06fdc(mm, alpha, c, 1, v, 1); /* Print the variance matrix */ Vprintf("\n"); /* nag_pack_real_mat_print (x04ccc), see above. */ nag_pack_real_mat_print(Nag_ColMajor, Nag_Upper, Nag_NonUnitDiag, m, v, "Variance matrix", 0, &fail); if (fail.code != NE_NOERROR) { Vprintf("Error from nag_pack_real_mat_print (x04ccc)." "\n%s\n", fail.message); exit_status = 1; goto END; } } } } if (c) NAG_FREE(c); if (v) NAG_FREE(v); if (x) NAG_FREE(x); if (xbar) NAG_FREE(xbar); } END: if (c) NAG_FREE(c); if (v) NAG_FREE(v); if (x) NAG_FREE(x); if (xbar) NAG_FREE(xbar); return exit_status; }