pspp-dev
[Top][All Lists]
Advanced

[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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]