/* PSPP - a program for statistical analysis.
- Copyright (C) 2009 Free Software Foundation, Inc.
+ Copyright (C) 2011 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/>. */
+#include <config.h>
+
+#include "data/case.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.
-
- When using these functions, make sure the orders of variables and
- values match when appropriate.
- */
+ An interaction is a structure containing a "product" of other
+ variables. The variables can be either string or numeric.
-#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>
-
-struct interaction_variable
+ 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)
{
- int n_vars;
- const struct variable **members;
- struct variable *intr;
-};
+ struct interaction *i = xmalloc (sizeof *i);
+ i->vars = xmalloc (sizeof *i->vars);
+ i->vars[0] = v;
+ i->n_vars = 1;
+ return i;
+}
-struct interaction_value
+void
+interaction_destroy (struct interaction *i)
{
- const struct interaction_variable *intr;
- union value *strings; /* Concatenation of the string values in this interaction's value. */
- double f; /* Product of the numerical values in this interaction's value. */
-};
+ free (i->vars);
+ free (i);
+}
-struct interaction_variable *
-interaction_variable_create (const struct variable **vars, int n_vars)
+void
+interaction_add_variable (struct interaction *i, const struct variable *v)
{
- struct interaction_variable *result = NULL;
- size_t i;
+ i->vars = xrealloc (i->vars, sizeof (*i->vars) * ++i->n_vars);
+ i->vars[i->n_vars - 1] = v;
+}
+
- if (n_vars > 0)
+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)
{
- result = xmalloc (sizeof (*result));
- 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];
- }
+ printf (" * %s", var_get_name (i->vars[v]));
}
- return result;
+ printf ("\n");
}
-void interaction_variable_destroy (struct interaction_variable *iv)
+/* Appends STR with a representation of the interaction, suitable for user
+ display.
+
+ STR must have been initialised prior to calling this function.
+*/
+void
+interaction_to_string (const struct interaction *iact, struct string *str)
{
- var_destroy (iv->intr);
- free (iv->members);
- free (iv);
+ int v = 0;
+ ds_put_cstr (str, var_to_string (iact->vars[v]));
+ for (v = 1; v < iact->n_vars; ++v)
+ {
+ ds_put_cstr (str, " * ");
+ ds_put_cstr (str, var_to_string (iact->vars[v]));
+ }
}
-size_t
-interaction_variable_get_n_vars (const struct interaction_variable *iv)
+unsigned int
+interaction_case_hash (const struct interaction *iact, const struct ccase *c)
{
- return (iv == NULL) ? 0 : iv->n_vars;
+ int i;
+ size_t hash = 0;
+ for (i = 0; i < iact->n_vars; ++i)
+ {
+ 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;
}
-/*
- 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)
+bool
+interaction_case_equal (const struct interaction *iact, const struct ccase *c1, const struct ccase *c2)
{
- struct interaction_value *result = NULL;
- size_t i;
- size_t n_vars;
-
- if (var != NULL)
+ int i;
+ bool same = true;
+
+ for (i = 0; i < iact->n_vars; ++i)
{
- result = xmalloc (sizeof (*result));
- result->intr = var;
- n_vars = interaction_variable_get_n_vars (var);
- result->strings = value_create (n_vars * MAX_SHORT_STRING + 1);
- result->f = 1.0;
- for (i = 0; i < n_vars; i++)
+ const struct variable *var = iact->vars[i];
+ if ( ! value_equal (case_data (c1, var), case_data (c2, var), var_get_width (var)))
{
- if (var_is_alpha (var->members[i]))
- {
- strncat (result->strings->s, vals[i]->s, MAX_SHORT_STRING);
- }
- else if (var_is_numeric (var->members[i]))
- {
- result->f *= vals[i]->f;
- }
+ same = false;
+ break;
}
}
+
+ return same;
+}
+
+
+int
+interaction_case_cmp_3way (const struct interaction *iact, const struct ccase *c1, const struct ccase *c2)
+{
+ int i;
+ int result = 0;
+
+ for (i = 0; i < iact->n_vars; ++i)
+ {
+ const struct variable *var = iact->vars[i];
+ result = value_compare_3way (case_data (c1, var), case_data (c2, var), var_get_width (var));
+ if (result != 0)
+ break;
+ }
+
return result;
}
-void
-interaction_value_destroy (struct interaction_value *val)
+
+bool
+interaction_case_is_missing (const struct interaction *iact, const struct ccase *c, enum mv_class exclude)
{
- if (val != NULL)
+ int i;
+ bool missing = false;
+
+ for (i = 0; i < iact->n_vars; ++i)
{
- free (val->strings);
- free (val);
+ if ( var_is_value_missing (iact->vars[i], case_data (c, iact->vars[i]), exclude))
+ {
+ missing = true;
+ break;
+ }
}
+
+ return missing;
}