Change how checking for missing values works.
[pspp] / src / math / interaction.c
index 33da8423bd7c7545a69e4c51a1d0a9a5abd4318d..e61ab14e915565c284564e3359c6d15f09c13581 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2009 Free Software Foundation, Inc.
+   Copyright (C) 2011, 2012 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
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
-/*
-  An interaction is a gsl_vector containing a "product" of other
-  variables. The variables can be either categorical or numeric.
-  If the variables are all numeric, the interaction is just the
-  scalar product. If any of the variables are categorical, their
-  product is a vector containing 0's in all but one entry. This entry
-  is found by combining the vectors corresponding to the variables'
-  OBS_VALS member. If there are K categorical variables, each with
-  N_1, N_2, ..., N_K categories, then the interaction will have
-  N_1 * N_2 * N_3 *...* N_K - 1 entries.
+#include <config.h>
 
-  When using these functions, make sure the orders of variables and
-  values match when appropriate.
- */
+#include "data/case.h"
+#include "interaction.h"
 
-#include <config.h>
-#include <assert.h>
-#include <gsl/gsl_math.h>
-#include <gsl/gsl_vector.h>
-#include <data/value.h>
-#include <data/variable.h>
-#include <math/interaction.h>
-#include <string.h>
-#include <xalloc.h>
+#include "data/value.h"
+#include "data/variable.h"
+#include "libpspp/str.h"
 
-struct interaction_variable
-{
-  int n_vars;
-  const struct variable **members;
-  struct variable *intr;
-  size_t n_alpha;
-};
+#include "gl/xalloc.h"
 
-struct interaction_value
-{
-  const struct interaction_variable *intr;
-  union value *val; /* Concatenation of the string values in this
-                      interaction's value, or the product of a bunch
-                      of numeric values for a purely numeric
-                      interaction.
-                   */
-  double f; /* Product of the numerical values in this interaction's value. */
-};
+#include <stdio.h>
 
-/*
-  An interaction_variable has type alpha if any of members have type
-  alpha. Otherwise, its type is numeric.
- */
-struct interaction_variable *
-interaction_variable_create (const struct variable **vars, int n_vars)
+/* Creates and returns an interaction.  If V is nonnull, then the interaction
+   initially contains V, otherwise it is initially empty. */
+struct interaction *
+interaction_create (const struct variable *v)
 {
-  struct interaction_variable *result = NULL;
-  size_t i;
-
-  if (n_vars > 0)
+  struct interaction *iact = xmalloc (sizeof *iact);
+  iact->vars = xmalloc (sizeof *iact->vars);
+  iact->n_vars = 0;
+  if (v)
     {
-      result = xmalloc (sizeof (*result));
-      result->n_alpha = 0;
-      result->members = xnmalloc (n_vars, sizeof (*result->members));
-      result->intr = var_create_internal (0);
-      result->n_vars = n_vars;
-      for (i = 0; i < n_vars; i++)
-       {
-         result->members[i] = vars[i];
-         if (var_is_alpha (vars[i]))
-           {
-             result->n_alpha++;
-           }
-       }
+      iact->vars[0] = v;
+      iact->n_vars = 1;
     }
-  /*
-    VAR_SET_WIDTH sets the type of the variable.
-   */
-  var_set_width (result->intr, MAX_SHORT_STRING * result->n_alpha + 1);
-
-  return result;
+  return iact;
 }
-void interaction_variable_destroy (struct interaction_variable *iv)
+
+/* Returns a (deep) copy of interaction SRC. */
+struct interaction *
+interaction_clone (const struct interaction *src)
 {
-  var_destroy (iv->intr);
-  free (iv->members);
-  free (iv);
+  struct interaction *dst = xmalloc (sizeof *dst);
+  dst->vars = xmemdup (src->vars, src->n_vars * sizeof *src->vars);
+  dst->n_vars = src->n_vars;
+  return dst;
 }
 
-/*
-  Get one of the member variables.
- */
-const struct variable *
-interaction_variable_get_member (const struct interaction_variable *iv, size_t i)
+/* Frees IACT. */
+void
+interaction_destroy (struct interaction *iact)
 {
-  return iv->members[i];
+  if (iact)
+    {
+      free (iact->vars);
+      free (iact);
+    }
 }
 
-size_t
-interaction_get_n_vars (const struct interaction_variable *iv)
+/* Appends variable V to IACT.
+
+   V must not already be in IACT. */
+void
+interaction_add_variable (struct interaction *iact, const struct variable *v)
 {
-  return (iv == NULL) ? 0 : iv->n_vars;
+  iact->vars = xrealloc (iact->vars, (iact->n_vars + 1) * sizeof *iact->vars);
+  iact->vars[iact->n_vars++] = v;
 }
 
-size_t
-interaction_get_n_alpha (const struct interaction_variable *iv)
+/* Returns true iff the variables in X->VARS are a proper subset of the
+   variables in Y->VARS. */
+bool
+interaction_is_proper_subset (const struct interaction *x,
+                              const struct interaction *y)
 {
-  return iv->n_alpha;
+  return x->n_vars >= y->n_vars && interaction_is_subset (x, y);
 }
 
-size_t
-interaction_get_n_numeric (const struct interaction_variable *iv)
+static bool
+interaction_contains (const struct interaction *iact, const struct variable *v)
 {
-  return (interaction_get_n_vars (iv) - interaction_get_n_alpha (iv));
+  for (size_t i = 0; i < iact->n_vars; i++)
+    if (iact->vars[i] == v)
+      return true;
+  return false;
 }
 
-/*
-  Get the interaction varibale itself.
- */
-const struct variable *
-interaction_variable_get_var (const struct interaction_variable *iv)
+/* Returns true iff the variables in X->VARS are a subset (proper or otherwise)
+   of the variables in Y->VARS. */
+bool
+interaction_is_subset (const struct interaction *x,
+                       const struct interaction *y)
 {
-  return iv->intr;
+  if (x->n_vars > y->n_vars)
+    return false;
+
+  for (size_t i = 0; i < x->n_vars; i++)
+    if (!interaction_contains (y, x->vars[i]))
+      return false;
+
+  return true;
 }
-/*
-  Given list of values, compute the value of the corresponding
-  interaction.  This "value" is not stored as the typical vector of
-  0's and one double, but rather the string values are concatenated to
-  make one big string value, and the numerical values are multiplied
-  together to give the non-zero entry of the corresponding vector.
- */
-struct interaction_value *
-interaction_value_create (const struct interaction_variable *var, const union value **vals)
+
+/* Prints the variables in IACT on stdout, for debugging purposes. */
+void
+interaction_dump (const struct interaction *iact)
 {
-  struct interaction_value *result = NULL;
-  const struct variable *member;
-  size_t i;
-  size_t n_vars;
-  
-  if (var != NULL)
+  if (iact->n_vars == 0)
+    printf ("(empty)\n");
+  else
     {
-      result = xmalloc (sizeof (*result));
-      result->intr = var;
-      n_vars = interaction_get_n_vars (var);
-      result->val = value_create (n_vars * MAX_SHORT_STRING + 1);
-      result->f = 1.0;
-      for (i = 0; i < n_vars; i++)
-       {
-         member = interaction_variable_get_member (var, i);
-
-         if (var_is_value_missing (member, vals[i], MV_ANY))
-           {
-             value_set_missing (result->val, MAX_SHORT_STRING);
-             result->f = SYSMIS;
-             break;
-           }
-         else
-           {
-             if (var_is_alpha (var->members[i]))
-               {
-                 strncat (result->val->s, vals[i]->s, MAX_SHORT_STRING);
-               }
-             else if (var_is_numeric (var->members[i]))
-               {
-                 result->f *= vals[i]->f;
-               }
-           }
-       }
-      if (interaction_get_n_alpha (var) == 0)
-       {
-         /*
-           If there are no categorical variables, then the
-           interaction consists of only numeric data. In this case,
-           code that uses this interaction_value will see the union
-           member as the numeric value. If we were to store that
-           numeric value in result->f as well, the calling code may
-           inadvertently square this value by multiplying by
-           result->val->f. Such multiplication would be correct for an
-           interaction consisting of both categorical and numeric
-           data, but a mistake for purely numerical interactions. To
-           avoid the error, we set result->f to 1.0 for numeric
-           interactions.
-          */
-         result->val->f = result->f;
-         result->f = 1.0;
-       }
+      for (size_t v = 0; v < iact->n_vars; ++v)
+        {
+          printf ("%s", var_get_name (iact->vars[v]));
+          if (v + 1 < iact->n_vars)
+            printf (" * ");
+        }
+      printf ("\n");
     }
-  return result;
 }
 
-union value *
-interaction_value_get (const struct interaction_value *val)
-{
-  return val->val;
-}
+/* Appends STR with a representation of the interaction, suitable for user
+   display.
 
-/*
-  Returns the numeric value of the non-zero entry for the vector
-  corresponding to this interaction.  Do not use this function to get
-  the numeric value of a purley numeric interaction. Instead, use the
-  union value * returned by interaction_value_get.
- */
-double 
-interaction_value_get_nonzero_entry (const struct interaction_value *val)
+   STR must have been initialised prior to calling this function. */
+void
+interaction_to_string (const struct interaction *iact, struct string *str)
 {
-  if (val != NULL)
-    return val->f;
-  return 1.0;
+  for (size_t v = 0; v < iact->n_vars; ++v)
+    {
+      ds_put_cstr (str, var_to_string (iact->vars[v]));
+      if (v + 1 < iact->n_vars)
+        ds_put_cstr (str, " × ");
+    }
 }
 
-void 
-interaction_value_destroy (struct interaction_value *val)
+/* Returns a hash of the values in C given by variables in IACT, using BASE as
+   a basis for the hash. */
+unsigned int
+interaction_case_hash (const struct interaction *iact,
+                       const struct ccase *c, unsigned int base)
 {
-  if (val != NULL)
+  size_t hash = base;
+  for (size_t i = 0; i < iact->n_vars; ++i)
     {
-      free (val->val);
-      free (val);
+      const struct variable *var = iact->vars[i];
+      const union value *val = case_data (c, var);
+      hash = value_hash (val, var_get_width (var), hash);
     }
+  return hash;
 }
 
-/*
-  Return a value from a variable that is an interaction. 
- */
-struct interaction_value *
-interaction_case_data (const struct ccase *ccase, const struct variable *var, 
-                      const struct interaction_variable **intr_vars, size_t n_intr)
+/* Returns true iff all the variables in IACT have equal values in C1 and
+   C2. */
+bool
+interaction_case_equal (const struct interaction *iact,
+                        const struct ccase *c1, const struct ccase *c2)
 {
-  size_t i;
-  size_t n_vars;
-  const struct interaction_variable *iv;
-  const struct variable *intr;
-  const struct variable *member;
-  const union value **vals = NULL;
-
-  for (i = 0; i < n_intr; i++)
+  for (size_t i = 0; i < iact->n_vars; ++i)
     {
-      iv = intr_vars[i];
-      intr = interaction_variable_get_var (iv);
-      if (var_get_dict_index (intr) == var_get_dict_index (var))
-       {
-         break;
-       }
+      const struct variable *var = iact->vars[i];
+      if (!value_equal (case_data (c1, var), case_data (c2, var),
+                        var_get_width (var)))
+        return false;
     }
-  n_vars = interaction_get_n_vars (iv);
-  vals = xnmalloc (n_vars, sizeof (*vals));
-  for (i = 0; i < n_vars; i++)
+
+  return true;
+}
+
+/* Returns a strcmp()-like comparison result for the variables in IACT and
+   their values in C1 and C2. */
+int
+interaction_case_cmp_3way (const struct interaction *iact,
+                           const struct ccase *c1, const struct ccase *c2)
+{
+  for (size_t i = 0; i < iact->n_vars; ++i)
     {
-      member = interaction_variable_get_member (iv, i);
-      vals[i] = case_data (ccase, member);
+      const struct variable *var = iact->vars[i];
+      int cmp = value_compare_3way (case_data (c1, var), case_data (c2, var),
+                                    var_get_width (var));
+      if (cmp)
+        return cmp;
     }
-  return interaction_value_create (iv, vals);
+
+  return 0;
 }
 
+/* Returns true iff any of the variables in IACT have a missing value in C,
+   using EXCLUDE to decide which kinds of missing values to count. */
 bool
-is_interaction (const struct variable *var, const struct interaction_variable **iv, size_t n_intr)
+interaction_case_is_missing (const struct interaction *iact,
+                             const struct ccase *c, enum mv_class exclude)
 {
-  size_t i;
-  const struct variable *intr;
-  
-  for (i = 0; i < n_intr; i++)
-    {
-      intr = interaction_variable_get_var (iv[i]);
-      if (var_get_dict_index (intr) == var_get_dict_index (var))
-       {
-         return true;
-       }
-    }
+  for (size_t i = 0; i < iact->n_vars; ++i)
+    if (var_is_value_missing (iact->vars[i], case_data (c, iact->vars[i]))
+        & exclude)
+      return true;
+
   return false;
 }
-  
+