X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fmath%2Fcategoricals.c;h=83326c3f006789814c4930feed8d85d15db95155;hb=03384f9680feba65ee79b9680f366435a7a60442;hp=3f16c9437b40d3efdf49270c77df9c6ff992f955;hpb=6590347b1a0ed5037743f671ab846813c89f9112;p=pspp diff --git a/src/math/categoricals.c b/src/math/categoricals.c index 3f16c9437b..83326c3f00 100644 --- a/src/math/categoricals.c +++ b/src/math/categoricals.c @@ -59,6 +59,7 @@ struct variable_node struct hmap_node node; /* In struct categorical's 'varmap'. */ const struct variable *var; /* The variable. */ struct hmap valmap; /* Contains "struct value_node"s. */ + union value *values; /* Values in valmap, as a sorted array. */ }; static int @@ -74,10 +75,11 @@ compare_value_node_3way (const void *vn1_, const void *vn2_, const void *aux) } static struct variable_node * -lookup_variable (const struct hmap *map, const struct variable *var, unsigned int hash) +lookup_variable (const struct hmap *map, const struct variable *var) { struct variable_node *vn; - HMAP_FOR_EACH_WITH_HASH (vn, struct variable_node, node, hash, map) + HMAP_FOR_EACH_WITH_HASH (vn, struct variable_node, node, + hash_pointer (var, 0), map) if (vn->var == var) return vn; return NULL; @@ -180,13 +182,13 @@ categoricals_isbalanced (const struct categoricals *cat) double oval = -1.0; for (v = 0; v < hmap_count (&iap->ivmap); ++v) - { - const struct interaction_value *iv = iap->ivs[v]; - if (oval == -1.0) - oval = iv->cc; - if (oval != iv->cc) - return false; - } + { + const struct interaction_value *iv = iap->ivs[v]; + if (oval == -1.0) + oval = iv->cc; + if (oval != iv->cc) + return false; + } } return true; } @@ -211,46 +213,46 @@ categoricals_dump (const struct categoricals *cat) printf ("Number of interactions %zu\n", cat->n_iap); for (i = 0 ; i < cat->n_iap; ++i) - { - int v; - struct string str; - const struct interact_params *iap = &cat->iap[i]; - const struct interaction *iact = iap->iact; - - ds_init_empty (&str); - interaction_to_string (iact, &str); - - printf ("\nInteraction: \"%s\" (number of categories: %d); ", ds_cstr (&str), iap->n_cats); - ds_destroy (&str); - printf ("Base index (df/categories): %d/%d\n", iap->base_df, iap->base_cats); - - printf ("\t("); - for (v = 0; v < hmap_count (&iap->ivmap); ++v) - { - int vv; - const struct interaction_value *iv = iap->ivs[v]; - - if (v > 0) printf (" "); - printf ("{"); - for (vv = 0; vv < iact->n_vars; ++vv) - { - const struct variable *var = iact->vars[vv]; - const union value *val = case_data (iv->ccase, var); - struct variable_node *vn = iap->varnodes[vv]; - const int width = var_get_width (var); - unsigned int valhash = value_hash (val, width, 0); - struct value_node *valn = lookup_value (&vn->valmap, val, valhash, width); - - assert (vn->var == var); - - printf ("%.*g(%d)", DBL_DIG + 1, val->f, valn->index); - if (vv < iact->n_vars - 1) - printf (", "); - } - printf ("}"); - } - printf (")\n"); - } + { + int v; + struct string str; + const struct interact_params *iap = &cat->iap[i]; + const struct interaction *iact = iap->iact; + + ds_init_empty (&str); + interaction_to_string (iact, &str); + + printf ("\nInteraction: \"%s\" (number of categories: %d); ", ds_cstr (&str), iap->n_cats); + ds_destroy (&str); + printf ("Base index (df/categories): %d/%d\n", iap->base_df, iap->base_cats); + + printf ("\t("); + for (v = 0; v < hmap_count (&iap->ivmap); ++v) + { + int vv; + const struct interaction_value *iv = iap->ivs[v]; + + if (v > 0) printf (" "); + printf ("{"); + for (vv = 0; vv < iact->n_vars; ++vv) + { + const struct variable *var = iact->vars[vv]; + const union value *val = case_data (iv->ccase, var); + struct variable_node *vn = iap->varnodes[vv]; + const int width = var_get_width (var); + unsigned int valhash = value_hash (val, width, 0); + struct value_node *valn = lookup_value (&vn->valmap, val, valhash, width); + + assert (vn->var == var); + + printf ("%.*g(%d)", DBL_DIG + 1, val->f, valn->index); + if (vv < iact->n_vars - 1) + printf (", "); + } + printf ("}"); + } + printf (")\n"); + } } } @@ -265,15 +267,16 @@ categoricals_destroy (struct categoricals *cat) /* Unref any cases that we reffed. */ struct interaction_value *iv; HMAP_FOR_EACH (iv, struct interaction_value, node, &cat->iap[i].ivmap) - { - if (cat->payload && cat->payload->destroy) - cat->payload->destroy (cat->aux1, cat->aux2, iv->user_data); - case_unref (iv->ccase); - } + { + if (cat->payload && cat->payload->destroy) + cat->payload->destroy (cat->aux1, cat->aux2, iv->user_data); + case_unref (iv->ccase); + } free (cat->iap[i].enc_sum); hmap_destroy (&cat->iap[i].ivmap); } + free (cat->iap); /* Interate over each variable and delete its value map. @@ -318,9 +321,9 @@ categoricals_sane (const struct categoricals *cat) categoricals_update(). */ struct categoricals * categoricals_create (struct interaction *const *inter, size_t n_inter, - const struct variable *wv, enum mv_class fctr_excl) + const struct variable *wv, enum mv_class fctr_excl) { - struct categoricals *cat = xzalloc (sizeof *cat); + struct categoricals *cat = XZALLOC (struct categoricals); cat->iap = pool_calloc (cat->pool, n_inter, sizeof *cat->iap); cat->n_iap = n_inter; cat->wv = wv; @@ -337,19 +340,19 @@ categoricals_create (struct interaction *const *inter, size_t n_inter, iap->varnodes = pool_nmalloc (cat->pool, iap->iact->n_vars, sizeof *iap->varnodes); for (size_t v = 0; v < inter[i]->n_vars; ++v) - { - const struct variable *var = inter[i]->vars[v]; - unsigned int hash = hash_pointer (var, 0); - struct variable_node *vn = lookup_variable (&cat->varmap, var, hash); - if (!vn) - { - vn = pool_malloc (cat->pool, sizeof *vn); - vn->var = var; - hmap_init (&vn->valmap); - hmap_insert (&cat->varmap, &vn->node, hash); - } + { + const struct variable *var = inter[i]->vars[v]; + struct variable_node *vn = lookup_variable (&cat->varmap, var); + if (!vn) + { + vn = pool_malloc (cat->pool, sizeof *vn); + vn->var = var; + vn->values = NULL; + hmap_init (&vn->valmap); + hmap_insert (&cat->varmap, &vn->node, hash_pointer (var, 0)); + } iap->varnodes[v] = vn; - } + } } return cat; @@ -364,7 +367,7 @@ categoricals_update (struct categoricals *cat, const struct ccase *c) assert (!cat->cat_to_iact); double weight; - weight = cat->wv ? case_data (c, cat->wv)->f : 1.0; + weight = cat->wv ? case_num (c, cat->wv) : 1.0; weight = var_force_valid_weight (cat->wv, weight, NULL); /* Update the frequency table for each variable. */ @@ -377,13 +380,13 @@ categoricals_update (struct categoricals *cat, const struct ccase *c) struct value_node *valn = lookup_value (&vn->valmap, val, hash, width); if (valn == NULL) - { - valn = pool_malloc (cat->pool, sizeof *valn); - valn->index = -1; - value_init (&valn->val, width); - value_copy (&valn->val, val, width); - hmap_insert (&vn->valmap, &valn->node, hash); - } + { + valn = pool_malloc (cat->pool, sizeof *valn); + valn->index = -1; + value_init_pool (cat->pool, &valn->val, width); + value_copy (&valn->val, val, width); + hmap_insert (&vn->valmap, &valn->node, hash); + } } /* Update the frequency table for full interactions. */ @@ -392,21 +395,21 @@ categoricals_update (struct categoricals *cat, const struct ccase *c) struct interact_params *iap = &cat->iap[i]; const struct interaction *iact = iap->iact; if (interaction_case_is_missing (iact, c, cat->fctr_excl)) - continue; + continue; unsigned int hash = interaction_case_hash (iact, c, 0); struct interaction_value *node = lookup_case (&iap->ivmap, iact, c); if (!node) - { - node = pool_malloc (cat->pool, sizeof *node); - node->ccase = case_ref (c); - node->cc = weight; + { + node = pool_malloc (cat->pool, sizeof *node); + node->ccase = case_ref (c); + node->cc = weight; - hmap_insert (&iap->ivmap, &node->node, hash); + hmap_insert (&iap->ivmap, &node->node, hash); - if (cat->payload) + if (cat->payload) node->user_data = cat->payload->create (cat->aux1, cat->aux2); - } + } else node->cc += weight; iap->cc += weight; @@ -477,7 +480,7 @@ categoricals_done (const struct categoricals *cat_) return; } - struct value_node **nodes = xcalloc (sizeof *nodes, n_vals); + struct value_node **nodes = XCALLOC (n_vals, struct value_node *); int x = 0; struct value_node *valnd; HMAP_FOR_EACH (valnd, struct value_node, node, &vn->valmap) @@ -499,15 +502,15 @@ categoricals_done (const struct categoricals *cat_) iap->df_prod = 1; iap->n_cats = 1; for (int v = 0 ; v < iact->n_vars; ++v) - { + { size_t n_vals = hmap_count (&iap->varnodes[v]->valmap); iap->df_prod *= n_vals - 1; - iap->n_cats *= n_vals; - } + iap->n_cats *= n_vals; + } if (iact->n_vars > 0) - cat->df_sum += iap->df_prod; + cat->df_sum += iap->df_prod; cat->n_cats_total += iap->n_cats; } @@ -544,7 +547,7 @@ categoricals_done (const struct categoricals *cat_) cat->df_to_iact[idx_df++] = i; for (int j = 0; j < iap->n_cats; ++j) - cat->cat_to_iact[idx_cat++] = i; + cat->cat_to_iact[idx_cat++] = i; } categoricals_dump (cat); @@ -560,23 +563,40 @@ categoricals_done (const struct categoricals *cat_) iap->enc_sum = xcalloc (df, sizeof *iap->enc_sum); for (int y = 0; y < hmap_count (&iap->ivmap); ++y) - { - struct interaction_value *iv = iap->ivs[y]; - for (int x = iap->base_df; + { + struct interaction_value *iv = iap->ivs[y]; + for (int x = iap->base_df; x < iap->base_df + df; ++x) - { - const double bin = categoricals_get_effects_code_for_case ( + { + const double bin = categoricals_get_effects_code_for_case ( cat, x, iv->ccase); - iap->enc_sum[x - iap->base_df] += bin * iv->cc; - } - if (cat->payload && cat->payload->calculate) - cat->payload->calculate (cat->aux1, cat->aux2, iv->user_data); - } + iap->enc_sum[x - iap->base_df] += bin * iv->cc; + } + if (cat->payload && cat->payload->calculate) + cat->payload->calculate (cat->aux1, cat->aux2, iv->user_data); + } } cat->sane = true; } +union value * +categoricals_get_var_values (const struct categoricals *cat, + const struct variable *var, size_t *np) +{ + struct variable_node *vn = lookup_variable (&cat->varmap, var); + *np = hmap_count (&vn->valmap); + if (!vn->values) + { + vn->values = pool_nalloc (cat->pool, *np, sizeof *vn->values); + + struct value_node *valnd; + HMAP_FOR_EACH (valnd, struct value_node, node, &vn->valmap) + vn->values[valnd->index] = valnd->val; + } + return vn->values; +} + static struct interact_params * df_to_iap (const struct categoricals *cat, int subscript) { @@ -623,8 +643,8 @@ categoricals_get_sum_by_subscript (const struct categoricals *cat, for that subscript */ static double categoricals_get_code_for_case (const struct categoricals *cat, int subscript, - const struct ccase *c, - bool effects_coding) + const struct ccase *c, + bool effects_coding) { const struct interaction *iact = categoricals_get_interaction_by_subscript (cat, subscript); @@ -648,7 +668,7 @@ categoricals_get_code_for_case (const struct categoricals *cat, int subscript, const int dfpn = dfp * df; if (effects_coding && valn->index == df) - result = -result; + result = -result; else { /* Translate subscript into an index for the individual variable. */ @@ -704,6 +724,21 @@ categoricals_get_user_data_by_category_real (const struct categoricals *cat, return n < hmap_count (&iap->ivmap) ? iap->ivs[n]->user_data : NULL; } +int +categoricals_get_value_index_by_category_real (const struct categoricals *cat, + int iact_idx, int cat_idx, + int var_idx) +{ + const struct interact_params *iap = &cat->iap[iact_idx]; + const struct interaction_value *ivn = iap->ivs[cat_idx]; + const struct variable *var = iap->iact->vars[var_idx]; + const struct variable_node *vn = iap->varnodes[var_idx]; + const union value *val = case_data (ivn->ccase, var); + int width = var_get_width (var); + unsigned int hash = value_hash (val, width, 0); + return lookup_value (&vn->valmap, val, hash, width)->index; +} + /* Return a case containing the set of values corresponding to CAT_INDEX. */ const struct ccase * categoricals_get_case_by_category (const struct categoricals *cat, @@ -725,7 +760,7 @@ categoricals_get_user_data_by_category (const struct categoricals *cat, void categoricals_set_payload (struct categoricals *cat, const struct payload *p, - const void *aux1, void *aux2) + const void *aux1, void *aux2) { cat->payload = p; cat->aux1 = aux1;