[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[misc 04/18] covariance: Fix const-ness of covariance_calculate[_unnorma
From: |
Ben Pfaff |
Subject: |
[misc 04/18] covariance: Fix const-ness of covariance_calculate[_unnormalized] retval. |
Date: |
Sat, 11 Dec 2010 22:19:58 -0800 |
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.
---
src/language/stats/correlations.c | 3 ++-
src/language/stats/factor.c | 4 +++-
src/language/stats/glm.c | 2 ++
src/language/stats/oneway.c | 2 ++
src/language/stats/regression.q | 2 +-
src/math/covariance.c | 34 +++++++++++++++++-----------------
src/math/covariance.h | 6 +++---
7 files changed, 30 insertions(+), 23 deletions(-)
diff --git a/src/language/stats/correlations.c
b/src/language/stats/correlations.c
index d8f1348..4f47643 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 e09eaae..eae6840 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 678ecce..862f49c 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 f390a3c..12170bb 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 e5b86ea..b6ee6aa 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 aa7f417..b0b0b46 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 cb83e15..f12e994 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);
--
1.7.1
- [misc 00/18] miscellaneous minor improvements, Ben Pfaff, 2010/12/12
- [misc 06/18] expressions: Make extract_min_valid() parameter const., Ben Pfaff, 2010/12/12
- [misc 01/18] friedman: Fix GCC warning about uninitialized fr.w., Ben Pfaff, 2010/12/12
- [misc 15/18] i18n: Remove unnecessary #ifs., Ben Pfaff, 2010/12/12
- [misc 12/18] DO REPEAT: Make this command usable anywhere., Ben Pfaff, 2010/12/12
- [misc 16/18] AGGREGATE: Align arg_func_tab[] in columns., Ben Pfaff, 2010/12/12
- [misc 10/18] NPAR TESTS: Prefer lex_match_id() over lex_match_hyphenated_word(), Ben Pfaff, 2010/12/12
- [misc 14/18] ECHO: Use text_item, as intended., Ben Pfaff, 2010/12/12
- [misc 04/18] covariance: Fix const-ness of covariance_calculate[_unnormalized] retval.,
Ben Pfaff <=
- [misc 08/18] DEBUG XFORM FAIL: Issue an error message as part of transformation., Ben Pfaff, 2010/12/12
- [misc 18/18] value-parser: Remove redundant call to lex_force_string() in parse_value()., Ben Pfaff, 2010/12/12
- [misc 17/18] GET DATA: Fix punctuation in error message., Ben Pfaff, 2010/12/12
- [misc 07/18] INPUT PROGRAM: Remove unused enumeration., Ben Pfaff, 2010/12/12
- [misc 03/18] ONEWAY: Fix warning for passing "const" pointer as non-const parameter., Ben Pfaff, 2010/12/12
- [misc 09/18] q2c: Prefer lex_match_id() over lex_match_hyphenated_word() in emitted code., Ben Pfaff, 2010/12/12
- [misc 02/18] mann-whitney: Suppress GCC warning about unused parameter., Ben Pfaff, 2010/12/12
- [misc 05/18] categoricals: Make parameter to categoricals_done() const., Ben Pfaff, 2010/12/12
- [misc 13/18] float-format: Don't translate test strings., Ben Pfaff, 2010/12/12
- [misc 11/18] identifier: New function token_type_to_string()., Ben Pfaff, 2010/12/12