From: Jason H Stover <jhs@math.gcsu.edu>
Date: Thu, 17 Jul 2008 22:11:21 +0000 (-0400)
Subject: Recurse to simplify flow of covariance_pass_two(). Move tests for V2 into for loop.
X-Git-Tag: sav-api~631^2~50
X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=28726cd68ed7ee301b0ecb79b58e5dfea5ab9779;p=pspp

Recurse to simplify flow of covariance_pass_two(). Move tests for V2 into for loop.
---

diff --git a/src/math/ChangeLog b/src/math/ChangeLog
index f2443b09f0..124abbf505 100644
--- a/src/math/ChangeLog
+++ b/src/math/ChangeLog
@@ -1,3 +1,15 @@
+2008-07-17  Jason H Stover  <jhs@math.gcsu.edu>
+
+	* covariance-matrix.c (covariance_update_categorical_numeric):
+	Altered to match new code in covariance_pass_two();
+
+	* covariance-matrix.h: Fix declaration of covariance_pass_two.
+
+	* covariance-matrix.c (covariance_pass_two): Moved numeric and
+	categorical scopes of V2 inside for loop. If V1 is not
+	categorical, but V2 is, recurse with the order of the variables
+	reversed to use the code in the previous "if" scope.
+
 2008-07-16  Jason Stover  <jhs@math.gcsu.edu>
 
 	* covariance-matrix.c (covariance_pass_two): Renamed
diff --git a/src/math/covariance-matrix.c b/src/math/covariance-matrix.c
index 69aaf533a0..4af44abc18 100644
--- a/src/math/covariance-matrix.c
+++ b/src/math/covariance-matrix.c
@@ -39,35 +39,21 @@ void covariance_matrix_destroy (struct design_matrix *x)
 }
 
 /*
-  Update the covariance matrix with the new entries, assuming that V1 
-  is categorical and V2 is numeric.
+  Update the covariance matrix with the new entries, assuming that ROW
+  corresponds to a categorical variable and V2 is numeric.
  */
 static void
 covariance_update_categorical_numeric (struct design_matrix *cov, double mean,
-			  double weight, double ssize, const struct variable *v1, 
-			  const struct variable *v2, const union value *val1, const union value *val2)
+			  double weight, size_t row, 
+			  const struct variable *v2, double x, const union value *val2)
 {
-  double x;
-  size_t i;
   size_t col;
-  size_t row;
   
-  assert (var_is_alpha (v1));
   assert (var_is_numeric (v2));
 
-  row = design_matrix_var_to_column (cov, v1);  
   col = design_matrix_var_to_column (cov, v2);
-  for (i = 0; i < cat_get_n_categories (v1); i++)
-    {
-      row += i;
-      x = -1.0 * cat_get_n_categories (v1) / ssize;
-      if (i == cat_value_find (v1, val1))
-	{
-	  x += 1.0;
-	}
-      assert (val2 != NULL);
-      gsl_matrix_set (cov->m, row, col, (val2->f - mean) * x * weight);
-    }
+  assert (val2 != NULL);
+  gsl_matrix_set (cov->m, row, col, (val2->f - mean) * x * weight);
 }
 static void
 column_iterate (struct design_matrix *cov, const struct variable *v, double weight,
@@ -109,23 +95,23 @@ void covariance_pass_two (struct design_matrix *cov, double mean1, double mean2,
 
   if (var_is_alpha (v1))
     {
-      if (var_is_numeric (v2))
-	{
-	  covariance_update_categorical_numeric (cov, mean2, weight, ssize, v1, 
-						 v2, val1, val2);
-	}
-      else
+      row = design_matrix_var_to_column (cov, v1);
+      for (i = 0; i < cat_get_n_categories (v1) - 1; i++)
 	{
-	  row = design_matrix_var_to_column (cov, v1);
-	  for (i = 0; i < cat_get_n_categories (v1) - 1; i++)
+	  row += i;
+	  x = -1.0 * cat_get_category_count (i, v1) / ssize;
+	  tmp_val = cat_subscript_to_value (i, v1);
+	  if (compare_values (tmp_val, val1, var_get_width (v1)))
+	    {
+	      x += 1.0;
+	    }
+	  if (var_is_numeric (v2))
+	    {
+	      covariance_update_categorical_numeric (cov, mean2, weight, row, 
+						     v2, x, val2);
+	    }
+	  else
 	    {
-	      row += i;
-	      x = -1.0 * cat_get_category_count (i, v1) / ssize;
-	      tmp_val = cat_subscript_to_value (i, v1);
-	      if (compare_values (tmp_val, val1, var_get_width (v1)))
-		{
-		  x += 1.0;
-		}
 	      column_iterate (cov, v1, weight, ssize, x, val1, row);
 	      column_iterate (cov, v2, weight, ssize, x, val2, row);
 	    }
@@ -133,8 +119,11 @@ void covariance_pass_two (struct design_matrix *cov, double mean1, double mean2,
     }
   else if (var_is_alpha (v2))
     {
-      covariance_update_categorical_numeric (cov, mean1, weight, ssize, v2, 
-					     v1, val2, val1);
+      /*
+	Reverse the orders of V1, V2, etc. and put ourselves back
+	in the previous IF scope.
+       */
+      covariance_pass_two (cov, mean2, mean1, weight, ssize, v2, v1, val2, val1);
     }
   else
     {
diff --git a/src/math/covariance-matrix.h b/src/math/covariance-matrix.h
index eee5e16a64..60f6b0d144 100644
--- a/src/math/covariance-matrix.h
+++ b/src/math/covariance-matrix.h
@@ -28,7 +28,7 @@ covariance_matrix_create (int, const struct variable *[]);
 
 void covariance_matrix_destroy (struct design_matrix *);
 
-void covariance_pass_one (struct design_matrix *, double, double,
+void covariance_pass_two (struct design_matrix *, double, double,
 			  double, double, const struct variable *, 
 			  const struct variable *, const union value *, const union value *);
 #endif