Categoricals to take interactions instead of variables.
[pspp-builds.git] / src / math / interaction.c
index be3d3edb576bad70a4b15d9c6a8a8b27fde5d0a6..c06acde8b027850c82d1b05174bece68daee7210 100644 (file)
-/* PSPP - Creates data structures to store interactions for
-   statistical routines.  
+/* PSPP - a program for statistical analysis.
+   Copyright (C) 2011 Free Software Foundation, Inc.
 
-   Copyright (C) 2007 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 "interaction.h"
+
+#include "data/value.h"
+#include "data/variable.h"
+#include "libpspp/str.h"
+
+#include "gl/xalloc.h"
+
+#include <stdio.h>
 
-/*
-  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 <assert.h>
-#include <libpspp/alloc.h>
-#include <gsl/gsl_math.h>
-#include <gsl/gsl_vector.h>
-#include <data/category.h>
-#include <data/variable.h>
 
 /*
-  Convert a list of values to a binary vector. The order of VALS must
-  correspond to the order of V.
- */
-gsl_vector *
-get_interaction (union value **vals, const struct variable **v, size_t n_vars)
+  An interaction is a structure containing a "product" of other
+  variables. The variables can be either string or numeric.
+
+  Interaction is commutative.  That means, that from a mathematical point of
+  view,  the order of the variables is irrelevant.  However, for display
+  purposes, and for matching with an interaction's value the order is 
+  pertinent.
+  
+  Therefore, when using these functions, make sure the orders of variables 
+  and values match when appropriate.
+*/
+
+
+
+struct interaction *
+interaction_create (const struct variable *v)
 {
-  gsl_vector *result = NULL;
-  size_t *subs = NULL;
-  size_t length = 1;
-  size_t i;
-  size_t j;
-  double tmp = 1.0;
-
-  assert (n_vars > 0);
-  for (i = 0; i < n_vars; i++)
-    {
-      if (var_is_alpha (v[i]))
-       {
-         length *= cat_get_n_categories (v[i]);
-       }
-      else
-       {
-         length = (length > 0) ? length : 1;
-       }
-    }
-  if (length > 0)
+  struct interaction  *i = xmalloc (sizeof *i);
+  i->vars = xmalloc (sizeof *i->vars);
+  i->vars[0] = v;
+  i->n_vars = 1;
+  return i;
+}
+
+void
+interaction_destroy (struct interaction *i)
+{
+  free (i->vars);
+  free (i);
+}
+
+void
+interaction_add_variable (struct interaction *i, const struct variable *v)
+{
+  i->vars = xrealloc (i->vars, sizeof (*i->vars) * ++i->n_vars);
+  i->vars[i->n_vars - 1] = v;
+}
+
+
+void
+interaction_dump (const struct interaction *i)
+{
+  int v = 0;
+  printf ("%s", var_get_name (i->vars[v]));
+  for (v = 1; v < i->n_vars; ++v)
     {
-      length--;
+      printf (" * %s", var_get_name (i->vars[v]));
     }
+  printf ("\n");
+}
+
+/* Appends STR with a representation of the interaction, suitable for user
+   display.
 
-  result = gsl_vector_calloc (length);
-  subs = xnmalloc (n_vars, sizeof (*subs));
-  for (j = 0; j < n_vars; j++)
+   STR must have been initialised prior to calling this function.
+*/
+void
+interaction_to_string (const struct interaction *iact, struct string *str)
+{
+  int v = 0;
+  ds_put_cstr (str, var_to_string (iact->vars[v]));
+  for (v = 1; v < iact->n_vars; ++v)
     {
-      if (var_is_alpha (v[j]))
-       {
-         subs[j] = cat_value_find (v[j], vals[j]);
-       }
+      ds_put_cstr (str, " * ");
+      ds_put_cstr (str, var_to_string (iact->vars[v]));
     }
-  j = subs[0];
-  for (i = 1; i < n_vars; i++)
+}
+
+unsigned int
+interaction_value_hash (const struct interaction *iact, const union value *val)
+{
+  int i;
+  size_t hash = 0;
+  for (i = 0; i < iact->n_vars; ++i)
     {
-      j = j * cat_get_n_categories (v[i]) + subs[i];
+      hash = value_hash (&val[i], var_get_width (iact->vars[i]), hash);
     }
-  gsl_vector_set (result, j, 1.0);
-  /*
-     If any of the variables are numeric, the interaction of that
-     variable with another is just a scalar product.
-   */
-  for (i = 1; i < n_vars; i++)
+
+  return hash;
+}
+
+bool
+interaction_value_equal (const struct interaction *iact, const union value *val1, const union value *val2)
+{
+  int i;
+  bool same = true;
+
+  for (i = 0; i < iact->n_vars; ++i)
     {
-      if (var_is_numeric (v[i]))
+      if ( ! value_equal (&val1[i], &val2[i], var_get_width (iact->vars[i])))
        {
-         tmp *= vals[i]->f;
+         same = false;
+         break;
        }
     }
-  if (fabs (tmp - 1.0) > GSL_DBL_EPSILON)
+
+  return same;
+}
+
+
+bool
+interaction_value_is_missing (const struct interaction *iact, const union value *val, enum mv_class exclude)
+{
+  int i;
+  bool missing = false;
+
+  for (i = 0; i < iact->n_vars; ++i)
     {
-      gsl_vector_set (result, j, tmp);
+      if ( var_is_value_missing (iact->vars[i], &val[i], exclude))
+       {
+         missing = true;
+         break;
+       }
     }
-  free (subs);
 
-  return result;
+  return missing;
 }