X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fmath%2Finteraction.c;h=ee5b2bf034e1c407d386b4792baaaeb9ad01cd0f;hb=32ea29485bdd610b7d05dd806c5f06f68da0f0fe;hp=444edbfa308723a5638bb726a84fdc8e59ff729e;hpb=64322c8ee0af7bf5ecc825ccc90513118732f335;p=pspp diff --git a/src/math/interaction.c b/src/math/interaction.c index 444edbfa30..ee5b2bf034 100644 --- a/src/math/interaction.c +++ b/src/math/interaction.c @@ -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 @@ -14,267 +14,235 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ +#include + +#include "data/case.h" +#include "interaction.h" + +#include "data/value.h" +#include "data/variable.h" +#include "libpspp/str.h" + +#include "gl/xalloc.h" + +#include + + /* - 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 -#include -#include -#include -#include -#include -#include -#include -#include - -struct interaction_variable -{ - int n_vars; - const struct variable **members; - struct variable *intr; - size_t n_alpha; -}; + 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. -struct interaction_value + Therefore, when using these functions, make sure the orders of variables + and values match when appropriate. +*/ + + +struct interaction * +interaction_create (const struct variable *v) { - 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. */ -}; + struct interaction *i = xmalloc (sizeof *i); + i->vars = xmalloc (sizeof *i->vars); + i->n_vars = 0; + if ( v ) + { + i->vars[0] = v; + i->n_vars = 1; + } + return i; +} -/* - 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) +/* Deep copy an interaction */ +struct interaction * +interaction_clone (const struct interaction *iact) { - struct interaction_variable *result = NULL; - size_t i; - int width = 0; + int v; + struct interaction *i = xmalloc (sizeof *i); + i->vars = xcalloc (iact->n_vars, sizeof *i->vars); + i->n_vars = iact->n_vars; - if (n_vars > 0) + for (v = 0; v < iact->n_vars; ++v) { - result = xmalloc (sizeof (*result)); - result->n_alpha = 0; - result->members = xnmalloc (n_vars, sizeof (*result->members)); - 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++; - width = 1; - } - } + i->vars[v] = iact->vars[v]; } - result->intr = var_create_internal (0, width); - return result; -} -void interaction_variable_destroy (struct interaction_variable *iv) -{ - var_destroy (iv->intr); - free (iv->members); - free (iv); + return i; } -/* - Get one of the member variables. - */ -const struct variable * -interaction_get_member (const struct interaction_variable *iv, size_t i) -{ - return iv->members[i]; -} -size_t -interaction_get_n_vars (const struct interaction_variable *iv) +void +interaction_destroy (struct interaction *i) { - return (iv == NULL) ? 0 : iv->n_vars; -} + if (NULL == i) + return; -size_t -interaction_get_n_alpha (const struct interaction_variable *iv) -{ - return iv->n_alpha; + free (i->vars); + free (i); } -size_t -interaction_get_n_numeric (const struct interaction_variable *iv) +void +interaction_add_variable (struct interaction *i, const struct variable *v) { - return (interaction_get_n_vars (iv) - interaction_get_n_alpha (iv)); + i->vars = xrealloc (i->vars, sizeof (*i->vars) * ++i->n_vars); + i->vars[i->n_vars - 1] = v; } + /* - Get the interaction variable itself. + Do the variables in X->VARS constitute a proper + subset of the variables in Y->VARS? */ -const struct variable * -interaction_get_variable (const struct interaction_variable *iv) +bool +interaction_is_proper_subset (const struct interaction *x, const struct interaction *y) { - return iv->intr; + if (x->n_vars >= y->n_vars) + return false; + + return interaction_is_subset (x, y); } + /* - 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. + Do the variables in X->VARS constitute a + subset (proper or otherwise) of the variables in Y->VARS? */ -struct interaction_value * -interaction_value_create (const struct interaction_variable *var, const union value **vals) +bool +interaction_is_subset (const struct interaction *x, const struct interaction *y) { - struct interaction_value *result = NULL; - const struct variable *member; size_t i; - size_t n_vars; - - if (var != NULL) + size_t j; + size_t n = 0; + + /* By definition, a subset cannot have more members than its superset */ + if (x->n_vars > y->n_vars) + return false; + + /* Count the number of values which are members of both sets */ + for (i = 0; i < x->n_vars; i++) { - int val_width = 1; - char *val; - - result = xmalloc (sizeof (*result)); - result->intr = var; - n_vars = interaction_get_n_vars (var); - value_init (&result->val, val_width); - val = value_str_rw (&result->val, val_width); - val[0] = '\0'; - result->f = 1.0; - for (i = 0; i < n_vars; i++) + for (j = 0; j < y->n_vars; j++) { - member = interaction_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 (x->vars [i] == y->vars [j]) { - if (var_is_alpha (var->members[i])) - { - int w = var_get_width (var->members[i]); - value_resize (result, val_width, val_width + w); - strncat (val, value_str (vals[i], w), w); - val = value_str_rw (&result->val, val_width); - } - else if (var_is_numeric (var->members[i])) - { - result->f *= vals[i]->f; - } + n++; } } - 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; - } } - return result; + + /* If ALL the members of X were also found in Y, then this must be a subset */ + if (n >= x->n_vars) + return true; + + return false; } -const union value * -interaction_value_get (const struct interaction_value *val) + + + +void +interaction_dump (const struct interaction *i) { - return &val->val; + int v = 0; + if ( i->n_vars == 0) + { + printf ("(empty)\n"); + return; + } + printf ("%s", var_get_name (i->vars[v])); + for (v = 1; v < i->n_vars; ++v) + printf (" * %s", var_get_name (i->vars[v])); + printf ("\n"); } -/* - 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) +/* 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) { - if (val != NULL) - return val->f; - return 1.0; + int v = 0; + if ( iact->n_vars == 0) + return; + 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])); + } } -void -interaction_value_destroy (struct interaction_value *val) +unsigned int +interaction_case_hash (const struct interaction *iact, const struct ccase *c, unsigned int base) { - if (val != NULL) + int i; + size_t hash = base; + for (i = 0; i < iact->n_vars; ++i) { - size_t n_vars = interaction_get_n_vars (val->intr); - int val_width = n_vars * MAX_SHORT_STRING + 1; - - value_destroy (&val->val, val_width); - 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 interaction_variable *iv) +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 variable *member; - const union value **vals = NULL; - - n_vars = interaction_get_n_vars (iv); - vals = xnmalloc (n_vars, sizeof (*vals)); + int i; + bool same = true; - for (i = 0; i < n_vars; i++) + for (i = 0; i < iact->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))) { - member = interaction_get_member (iv, i); - vals[i] = case_data (ccase, member); + 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; - return interaction_value_create (iv, vals); + 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; } + 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++) + int i; + bool missing = false; + + for (i = 0; i < iact->n_vars; ++i) { - intr = interaction_get_variable (iv[i]); - if (var_get_dict_index (intr) == var_get_dict_index (var)) + if ( var_is_value_missing (iact->vars[i], case_data (c, iact->vars[i]), exclude)) { - return true; + missing = true; + break; } } - return false; + + return missing; } - +