Merge commit 'origin/stable'
[pspp-builds.git] / src / math / levene.c
index 15f9a583027b4da5b07333c82a01b03fe457f13d..7bd582105f940c28ffeb26506d4c5fe941ff728d 100644 (file)
@@ -1,22 +1,18 @@
-/* This file is part of GNU PSPP 
-   Computes Levene test  statistic.
+/* PSPP - a program for statistical analysis.
+   Copyright (C) 2004, 2009 Free Software Foundation, Inc.
 
-   Copyright (C) 2004 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
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
 
-   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 the Free Software Foundation; either version 2 of the
-   License, or (at your option) any later version.
-
-   This program is distributed in the hope that it will be useful, but
-   WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   General Public License for more details.
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-   02110-1301, USA. */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
 #include "levene.h"
 #include <libpspp/str.h>
 #include <data/variable.h>
 #include <data/procedure.h>
-#include <libpspp/alloc.h>
 #include <libpspp/misc.h>
 #include "group.h"
 
 #include <math.h>
 #include <stdlib.h>
 
+#include "xalloc.h"
+
 
 /* This module calculates the Levene statistic for variables.
 
@@ -64,7 +61,7 @@ struct levene_info
   struct t_test_proc **group_stats;
 
   /* The independent variable */
-  const struct variable *v_indep; 
+  const struct variable *v_indep;
 
   /* Number of dependent variables */
   size_t n_dep;
@@ -93,7 +90,7 @@ struct lz_stats
   double grand_mean;
 
   /* The total number of cases */
-  double total_n ; 
+  double total_n ;
 
   /* Number of groups */
   int n_groups;
@@ -101,27 +98,27 @@ struct lz_stats
 
 /* First pass */
 static void  levene_precalc (const struct levene_info *l);
-static int levene_calc (const struct dictionary *dict, const struct ccase *, 
+static int levene_calc (const struct dictionary *dict, const struct ccase *,
                        const struct levene_info *l);
 static void levene_postcalc (struct levene_info *);
 
 
 /* Second pass */
 static void levene2_precalc (struct levene_info *l);
-static int levene2_calc (const struct dictionary *, const struct ccase *, 
+static int levene2_calc (const struct dictionary *, const struct ccase *,
                         struct levene_info *l);
 static void levene2_postcalc (struct levene_info *);
 
 
 void
-levene(const struct dictionary *dict, 
+levene(const struct dictionary *dict,
        struct casereader *reader,
-       const struct variable *v_indep, size_t n_dep, 
+       const struct variable *v_indep, size_t n_dep,
        const struct variable **v_dep,
        enum mv_class exclude)
 {
   struct casereader *pass1, *pass2;
-  struct ccase c;
+  struct ccase *c;
   struct levene_info l;
 
   l.n_dep      = n_dep;
@@ -134,14 +131,14 @@ levene(const struct dictionary *dict,
   casereader_split (reader, &pass1, &pass2);
 
   levene_precalc (&l);
-  for (; casereader_read (pass1, &c); case_destroy (&c)) 
-    levene_calc (dict, &c, &l);
+  for (; (c = casereader_read (pass1)) != NULL; case_unref (c))
+    levene_calc (dict, c, &l);
   casereader_destroy (pass1);
   levene_postcalc (&l);
 
   levene2_precalc(&l);
-  for (; casereader_read (pass2, &c); case_destroy (&c)) 
-    levene2_calc (dict, &c, &l);
+  for (; (c = casereader_read (pass2)) != NULL; case_unref (c))
+    levene2_calc (dict, c, &l);
   casereader_destroy (pass2);
   levene2_postcalc (&l);
 
@@ -149,12 +146,12 @@ levene(const struct dictionary *dict,
   free (l.lz);
 }
 
-static void 
+static void
 levene_precalc (const struct levene_info *l)
 {
   size_t i;
 
-  for(i = 0; i < l->n_dep ; ++i ) 
+  for(i = 0; i < l->n_dep ; ++i )
     {
       const struct variable *var = l->v_dep[i];
       struct group_proc *gp = group_proc_get (var);
@@ -163,33 +160,33 @@ levene_precalc (const struct levene_info *l)
 
       l->lz[i].grand_total = 0;
       l->lz[i].total_n = 0;
-      l->lz[i].n_groups = gp->n_groups ; 
+      l->lz[i].n_groups = gp->n_groups ;
+
 
-      
       for ( gs = hsh_first(gp->group_hash, &hi);
            gs != 0;
            gs = hsh_next(gp->group_hash, &hi))
        {
          gs->lz_total = 0;
        }
-           
+
     }
 
 }
 
-static int 
-levene_calc (const struct dictionary *dict, const struct ccase *c, 
+static int
+levene_calc (const struct dictionary *dict, const struct ccase *c,
             const struct levene_info *l)
 {
   size_t i;
   bool warn = false;
   const union value *gv = case_data (c, l->v_indep);
   struct group_statistics key;
-  double weight = dict_get_case_weight (dict, c, &warn); 
+  double weight = dict_get_case_weight (dict, c, &warn);
 
   key.id = *gv;
 
-  for (i = 0; i < l->n_dep; ++i) 
+  for (i = 0; i < l->n_dep; ++i)
     {
       const struct variable *var = l->v_dep[i];
       struct group_proc *gp = group_proc_get (var);
@@ -199,14 +196,14 @@ levene_calc (const struct dictionary *dict, const struct ccase *c,
 
       gs = hsh_find(gp->group_hash,(void *) &key );
 
-      if ( 0 == gs ) 
+      if ( 0 == gs )
        continue ;
 
       if ( !var_is_value_missing (var, v, l->exclude))
        {
          levene_z= fabs(v->f - gs->mean);
          l->lz[i].grand_total += levene_z * weight;
-         l->lz[i].total_n += weight; 
+         l->lz[i].total_n += weight;
 
          gs->lz_total += levene_z * weight;
        }
@@ -215,32 +212,32 @@ levene_calc (const struct dictionary *dict, const struct ccase *c,
 }
 
 
-static void 
+static void
 levene_postcalc (struct levene_info *l)
 {
   size_t v;
 
-  for (v = 0; v < l->n_dep; ++v) 
+  for (v = 0; v < l->n_dep; ++v)
     {
       /* This is Z_LL */
       l->lz[v].grand_mean = l->lz[v].grand_total / l->lz[v].total_n ;
     }
 
-  
+
 }
 
 
 
-static void 
+static void
 levene2_precalc (struct levene_info *l)
 {
   size_t v;
 
 
   /* This stuff could go in the first post calc . . . */
-  for (v = 0; 
-       v < l->n_dep; 
-       ++v) 
+  for (v = 0;
+       v < l->n_dep;
+       ++v)
     {
       struct hsh_iterator hi;
       struct group_statistics *g;
@@ -259,21 +256,21 @@ levene2_precalc (struct levene_info *l)
   }
 }
 
-static int 
-levene2_calc (const struct dictionary *dict, const struct ccase *c, 
+static int
+levene2_calc (const struct dictionary *dict, const struct ccase *c,
              struct levene_info *l)
 {
   size_t i;
   bool warn = false;
 
-  double weight = dict_get_case_weight (dict, c, &warn); 
+  double weight = dict_get_case_weight (dict, c, &warn);
 
   const union value *gv = case_data (c, l->v_indep);
   struct group_statistics key;
 
   key.id = *gv;
 
-  for (i = 0; i < l->n_dep; ++i) 
+  for (i = 0; i < l->n_dep; ++i)
     {
       double levene_z;
       const struct variable *var = l->v_dep[i] ;
@@ -282,12 +279,12 @@ levene2_calc (const struct dictionary *dict, const struct ccase *c,
 
       gs = hsh_find(group_proc_get (var)->group_hash,(void *) &key );
 
-      if ( 0 == gs ) 
+      if ( 0 == gs )
        continue;
 
       if ( !var_is_value_missing (var, v, l->exclude))
        {
-         levene_z = fabs(v->f - gs->mean); 
+         levene_z = fabs(v->f - gs->mean);
          l->lz_denominator[i] += weight * pow2 (levene_z - gs->lz_mean);
        }
     }
@@ -296,12 +293,12 @@ levene2_calc (const struct dictionary *dict, const struct ccase *c,
 }
 
 
-static void 
+static void
 levene2_postcalc (struct levene_info *l)
 {
   size_t v;
 
-  for (v = 0; v < l->n_dep; ++v) 
+  for (v = 0; v < l->n_dep; ++v)
     {
       double lz_numerator = 0;
       struct hsh_iterator hi;