From: Ben Pfaff Date: Sun, 12 Dec 2010 05:29:55 +0000 (-0800) Subject: covariance: Fix const-ness of covariance_calculate[_unnormalized] retval. X-Git-Tag: v0.7.7~114 X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3c5fcaa67efcee56981c16b543fb9f679787a486;p=pspp-builds.git covariance: Fix const-ness of covariance_calculate[_unnormalized] retval. The covariance_calculate() and covariance_calculate_unnormalized() functions documented that their return values were owned by the covariance object itself and thus should not be freed by the caller. However, this documentation was incorrect, because in fact the covariance object did not retain the pointer at all. This commit fixes the comments, updates the return values to be non-"const", and changes the callers to free the returned "gsl_matrix" objects. --- diff --git a/src/language/stats/correlations.c b/src/language/stats/correlations.c index d8f13488..4f476433 100644 --- a/src/language/stats/correlations.c +++ b/src/language/stats/correlations.c @@ -278,7 +278,7 @@ run_corr (struct casereader *r, const struct corr_opts *opts, const struct corr { struct ccase *c; const gsl_matrix *var_matrix, *samples_matrix, *mean_matrix; - const gsl_matrix *cov_matrix; + gsl_matrix *cov_matrix; gsl_matrix *corr_matrix; struct covariance *cov = covariance_2pass_create (corr->n_vars_total, corr->vars, NULL, @@ -315,6 +315,7 @@ run_corr (struct casereader *r, const struct corr_opts *opts, const struct corr covariance_destroy (cov); gsl_matrix_free (corr_matrix); + gsl_matrix_free (cov_matrix); } int diff --git a/src/language/stats/factor.c b/src/language/stats/factor.c index e09eaae8..eae6840f 100644 --- a/src/language/stats/factor.c +++ b/src/language/stats/factor.c @@ -175,7 +175,7 @@ struct idata /* Intermediate values used in calculation */ const gsl_matrix *corr ; /* The correlation matrix */ - const gsl_matrix *cov ; /* The covariance matrix */ + gsl_matrix *cov ; /* The covariance matrix */ const gsl_matrix *n ; /* Matrix of number of samples */ gsl_vector *eval ; /* The eigenvalues */ @@ -206,6 +206,8 @@ idata_free (struct idata *id) gsl_vector_free (id->msr); gsl_vector_free (id->eval); gsl_matrix_free (id->evec); + if (id->cov != NULL) + gsl_matrix_free (id->cov); free (id); } diff --git a/src/language/stats/glm.c b/src/language/stats/glm.c index 678ecce7..862f49ce 100644 --- a/src/language/stats/glm.c +++ b/src/language/stats/glm.c @@ -263,6 +263,8 @@ run_glm (const struct glm_spec *cmd, struct casereader *input, const struct data reg_sweep (cm, 0); dump_matrix (cm); + + gsl_matrix_free (cm); } if (!taint_has_tainted_successor (taint)) diff --git a/src/language/stats/oneway.c b/src/language/stats/oneway.c index f390a3c8..12170bb4 100644 --- a/src/language/stats/oneway.c +++ b/src/language/stats/oneway.c @@ -495,6 +495,8 @@ run_oneway (const struct oneway_spec *cmd, pvw->n_groups = categoricals_total (cats); pvw->mse = (pvw->sst - pvw->ssa) / (n - pvw->n_groups); + + gsl_matrix_free (cm); } for (v = 0; v < cmd->n_vars; ++v) diff --git a/src/language/stats/regression.q b/src/language/stats/regression.q index e5b86ea2..b6ee6aaa 100644 --- a/src/language/stats/regression.q +++ b/src/language/stats/regression.q @@ -810,7 +810,7 @@ fill_covariance (gsl_matrix *cov, struct covariance *all_cov, size_t dep_subscript; size_t *rows; const gsl_matrix *ssizes; - const gsl_matrix *cm; + gsl_matrix *cm; const gsl_matrix *mean_matrix; const gsl_matrix *ssize_matrix; double result = 0.0; diff --git a/src/math/covariance.c b/src/math/covariance.c index aa7f4177..b0b0b466 100644 --- a/src/math/covariance.c +++ b/src/math/covariance.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2009 Free Software Foundation, Inc. + Copyright (C) 2009, 2010 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -529,7 +529,7 @@ cm_to_gsl (struct covariance *cov) } -static const gsl_matrix * +static gsl_matrix * covariance_calculate_double_pass (struct covariance *cov) { size_t i, j; @@ -553,7 +553,7 @@ covariance_calculate_double_pass (struct covariance *cov) return cm_to_gsl (cov); } -static const gsl_matrix * +static gsl_matrix * covariance_calculate_single_pass (struct covariance *cov) { size_t i, j; @@ -598,12 +598,12 @@ covariance_calculate_single_pass (struct covariance *cov) } -/* - Return a pointer to gsl_matrix containing the pairwise covariances. - The matrix remains owned by the COV object, and must not be freed. - Call this function only after all data have been accumulated. -*/ -const gsl_matrix * +/* Return a pointer to gsl_matrix containing the pairwise covariances. The + caller owns the returned matrix and must free it when it is no longer + needed. + + Call this function only after all data have been accumulated. */ +gsl_matrix * covariance_calculate (struct covariance *cov) { if ( cov->state <= 0 ) @@ -625,7 +625,7 @@ covariance_calculate (struct covariance *cov) /* Covariance computed without dividing by the sample size. */ -static const gsl_matrix * +static gsl_matrix * covariance_calculate_double_pass_unnormalized (struct covariance *cov) { size_t i, j; @@ -647,7 +647,7 @@ covariance_calculate_double_pass_unnormalized (struct covariance *cov) return cm_to_gsl (cov); } -static const gsl_matrix * +static gsl_matrix * covariance_calculate_single_pass_unnormalized (struct covariance *cov) { size_t i, j; @@ -679,12 +679,12 @@ covariance_calculate_single_pass_unnormalized (struct covariance *cov) } -/* - Return a pointer to gsl_matrix containing the pairwise covariances. - The matrix remains owned by the COV object, and must not be freed. - Call this function only after all data have been accumulated. -*/ -const gsl_matrix * +/* Return a pointer to gsl_matrix containing the pairwise covariances. The + caller owns the returned matrix and must free it when it is no longer + needed. + + Call this function only after all data have been accumulated. */ +gsl_matrix * covariance_calculate_unnormalized (struct covariance *cov) { if ( cov->state <= 0 ) diff --git a/src/math/covariance.h b/src/math/covariance.h index cb83e151..f12e994d 100644 --- a/src/math/covariance.h +++ b/src/math/covariance.h @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2009 Free Software Foundation, Inc. + Copyright (C) 2009, 2010 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -40,8 +40,8 @@ void covariance_accumulate (struct covariance *, const struct ccase *); void covariance_accumulate_pass1 (struct covariance *, const struct ccase *); void covariance_accumulate_pass2 (struct covariance *, const struct ccase *); -const gsl_matrix * covariance_calculate (struct covariance *cov); -const gsl_matrix * covariance_calculate_unnormalized (struct covariance *); +gsl_matrix * covariance_calculate (struct covariance *); +gsl_matrix * covariance_calculate_unnormalized (struct covariance *); void covariance_destroy (struct covariance *cov);