X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fmath%2Fcategoricals.c;h=2227d4bb20e43ce9e04602f834326ef363901141;hb=cc62e6937ff0410a356682b5757cf99788faa888;hp=d89dfdbb26b2c583513ff6d266ca4f2ac2e5a032;hpb=0b0ca44889e637251cb5f2dbf3c7fdc4ec8b9bd7;p=pspp diff --git a/src/math/categoricals.c b/src/math/categoricals.c index d89dfdbb26..2227d4bb20 100644 --- a/src/math/categoricals.c +++ b/src/math/categoricals.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2009, 2010, 2011, 2012 Free Software Foundation, Inc. + Copyright (C) 2009, 2010, 2011, 2012, 2014 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 @@ -19,6 +19,7 @@ #include "math/categoricals.h" #include "math/interaction.h" +#include #include #include "data/case.h" @@ -65,7 +66,7 @@ lookup_value (const struct hmap *map, const union value *val, unsigned int hash, if (value_equal (&vn->val, val, width)) break; } - + return vn; } @@ -102,10 +103,10 @@ lookup_variable (const struct hmap *map, const struct variable *var, unsigned in { if (vn->var == var) break; - + fprintf (stderr, "%s:%d Warning: Hash table collision\n", __FILE__, __LINE__); } - + return vn; } @@ -124,9 +125,9 @@ struct interact_params int n_cats; /* An array of integers df_n * df_{n-1} * df_{n-2} ... - These are the products of the degrees of freedom for the current + These are the products of the degrees of freedom for the current variable and all preceeding variables */ - int *df_prod; + int *df_prod; double *enc_sum; @@ -195,6 +196,31 @@ struct categoricals const struct payload *payload; }; + +bool +categoricals_isbalanced (const struct categoricals *cat) +{ + int i; + + for (i = 0 ; i < cat->n_iap; ++i) + { + int v; + const struct interact_params *iap = &cat->iap[i]; + + double oval = -1.0; + for (v = 0; v < hmap_count (&iap->ivmap); ++v) + { + const struct interaction_value *iv = iap->reverse_interaction_value_map[v]; + if (oval == -1.0) + oval = iv->cc; + if (oval != iv->cc) + return false; + } + } + return true; +} + + static void categoricals_dump (const struct categoricals *cat) { @@ -216,7 +242,7 @@ categoricals_dump (const struct categoricals *cat) } printf ("\n"); - printf ("Number of interactions %d\n", cat->n_iap); + printf ("Number of interactions %zu\n", cat->n_iap); for (i = 0 ; i < cat->n_iap; ++i) { int v; @@ -252,7 +278,7 @@ categoricals_dump (const struct categoricals *cat) assert (vn->var == var); - printf ("%g(%d)", val->f, valn->index); + printf ("%.*g(%d)", DBL_DIG + 1, val->f, valn->index); if (vv < iact->n_vars - 1) printf (", "); } @@ -319,7 +345,7 @@ lookup_case (const struct hmap *map, const struct interaction *iact, const struc return iv; } -bool +bool categoricals_sane (const struct categoricals *cat) { return cat->sane; @@ -331,7 +357,7 @@ categoricals_create (struct interaction *const*inter, size_t n_inter, { size_t i; struct categoricals *cat = xmalloc (sizeof *cat); - + cat->n_iap = n_inter; cat->wv = wv; cat->n_cats_total = 0; @@ -387,6 +413,7 @@ categoricals_update (struct categoricals *cat, const struct ccase *c) return; weight = cat->wv ? case_data (c, cat->wv)->f : 1.0; + weight = var_force_valid_weight (cat->wv, weight, NULL); assert (NULL == cat->reverse_variable_map_short); assert (NULL == cat->reverse_variable_map_long); @@ -403,14 +430,14 @@ categoricals_update (struct categoricals *cat, const struct ccase *c) if (valn == NULL) { valn = pool_malloc (cat->pool, sizeof *valn); - valn->index = -1; + valn->index = -1; vn->n_vals++; value_init (&valn->val, width); value_copy (&valn->val, val, width); hmap_insert (&vn->valmap, &valn->node, hash); } } - + for (i = 0 ; i < cat->n_iap; ++i) { const struct interaction *iact = cat->iap[i].iact; @@ -432,7 +459,7 @@ categoricals_update (struct categoricals *cat, const struct ccase *c) hmap_insert (&cat->iap[i].ivmap, &node->node, hash); - if (cat->payload) + if (cat->payload) { node->user_data = cat->payload->create (cat->aux1, cat->aux2); } @@ -445,10 +472,8 @@ categoricals_update (struct categoricals *cat, const struct ccase *c) if (cat->payload) { - double weight = cat->wv ? case_data (c, cat->wv)->f : 1.0; cat->payload->update (cat->aux1, cat->aux2, node->user_data, c, weight); } - } } @@ -526,7 +551,7 @@ categoricals_done (const struct categoricals *cat_) cat->iap[i].df_prod = iact->n_vars ? xcalloc (iact->n_vars, sizeof (int)) : NULL; cat->iap[i].n_cats = 1; - + for (v = 0 ; v < iact->n_vars; ++v) { int x; @@ -550,12 +575,12 @@ categoricals_done (const struct categoricals *cat_) x = 0; HMAP_FOR_EACH (valnd, struct value_node, node, &vn->valmap) { - /* Note: This loop is probably superfluous, it could be done in the + /* Note: This loop is probably superfluous, it could be done in the update stage (at the expense of a realloc) */ array[x++] = valnd; } - sort (array, vn->n_vals, sizeof (*array), + sort (array, vn->n_vals, sizeof (*array), compare_value_node_3way, vn); for (x = 0; x < vn->n_vals; ++x) @@ -748,7 +773,7 @@ categoricals_get_code_for_case (const struct categoricals *cat, int subscript, bin = -1.0; else if ( valn->index != index ) bin = 0; - + result *= bin; } @@ -766,7 +791,7 @@ categoricals_get_dummy_code_for_case (const struct categoricals *cat, int subscr } /* Returns unity if the value in case C at SUBSCRIPT is equal to the category - for that subscript. + for that subscript. Else if it is the last category, return -1. Otherwise return 0. */ @@ -786,7 +811,7 @@ categoricals_get_n_variables (const struct categoricals *cat) } -/* Return a case containing the set of values corresponding to +/* Return a case containing the set of values corresponding to the Nth Category of the IACTth interaction */ const struct ccase * categoricals_get_case_by_category_real (const struct categoricals *cat, int iact, int n)