X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdescript.q;h=9ac10325fffe72c656be79ff3e28f1609a0644f5;hb=6d34b6cd00bb26603f8acedd3f0ed69a4484e484;hp=168f29aa30e42e2790f7e5a065652ca91d52e38d;hpb=4944c86a9318bc5b5578ab145a95c116ffd2c9fd;p=pspp diff --git a/src/descript.q b/src/descript.q index 168f29aa30..9ac10325ff 100644 --- a/src/descript.q +++ b/src/descript.q @@ -24,12 +24,12 @@ #include #include #include +#include "algorithm.h" #include "alloc.h" #include "bitvector.h" #include "command.h" #include "lexer.h" #include "error.h" -#include "approx.h" #include "magic.h" #include "stats.h" #include "som.h" @@ -177,9 +177,9 @@ static void dump_z_table (void); static void run_z_pass (void); /* Procedure execution functions. */ -static int calc (struct ccase *); -static void precalc (void); -static void postcalc (void); +static int calc (struct ccase *, void *); +static void precalc (void *); +static void postcalc (void *); static void display (void); /* Parser and outline. */ @@ -193,8 +193,6 @@ cmd_descriptives (void) v_variables = NULL; n_variables = 0; - lex_match_id ("DESCRIPTIVES"); - lex_match_id ("CONDESCRIPTIVES"); if (!parse_descriptives (&cmd)) goto lossage; @@ -287,9 +285,8 @@ cmd_descriptives (void) } /* Data pass! */ - update_weighting (&default_dict); bad_weight = 0; - procedure (precalc, calc, postcalc); + procedure_with_splits (precalc, calc, postcalc, NULL); if (bad_weight) msg (SW, _("At least one case in the data file had a weight value " @@ -312,10 +309,10 @@ cmd_descriptives (void) /* Parses the VARIABLES subcommand. */ static int -dsc_custom_variables (struct cmd_descriptives *cmd unused) +dsc_custom_variables (struct cmd_descriptives *cmd UNUSED) { if (!lex_match_id ("VARIABLES") - && (token != T_ID || !is_varname (tokid)) + && (token != T_ID || dict_lookup_var (default_dict, tokid) == NULL) && token != T_ALL) return 2; lex_match ('='); @@ -325,7 +322,7 @@ dsc_custom_variables (struct cmd_descriptives *cmd unused) int i, n; n = n_variables; - if (!parse_variables (NULL, &v_variables, &n_variables, + if (!parse_variables (default_dict, &v_variables, &n_variables, PV_DUPLICATE | PV_SINGLE | PV_APPEND | PV_NUMERIC | PV_NO_SCRATCH)) return 0; @@ -344,7 +341,7 @@ dsc_custom_variables (struct cmd_descriptives *cmd unused) msg (SE, _("Name for z-score variable expected.")); return 0; } - if (is_varname (tokid)) + if (dict_lookup_var (default_dict, tokid)) { msg (SE, _("Z-score variable name `%s' is a " "duplicate variable name with a current variable."), @@ -383,7 +380,7 @@ try_name (char *name) { int i; - if (is_varname (name)) + if (dict_lookup_var (default_dict, name) != NULL) return 0; for (i = 0; i < n_variables; i++) { @@ -477,7 +474,8 @@ dump_z_table (void) /* Transformation function to calculate Z-scores. */ static int -descriptives_trns_proc (struct trns_header * trns, struct ccase * c) +descriptives_trns_proc (struct trns_header * trns, struct ccase * c, + int case_num UNUSED) { struct descriptives_trns *t = (struct descriptives_trns *) trns; struct dsc_z_score *z; @@ -539,9 +537,9 @@ run_z_pass (void) struct variable *d; t->z[count].s = v; - t->z[count].d = d = force_create_variable (&default_dict, - v->p.dsc.zname, - NUMERIC, 0); + t->z[count].d = d = dict_create_var_assert (default_dict, + v->p.dsc.zname, 0); + d->init = 0; if (v->label) { d->label = xmalloc (strlen (v->label) + 12); @@ -556,8 +554,7 @@ run_z_pass (void) } t->z[count].mean = v->p.dsc.stats[dsc_mean]; t->z[count].std_dev = v->p.dsc.stats[dsc_stddev]; - if (t->z[count].std_dev == SYSMIS - || approx_eq (t->z[count].std_dev, 0.0)) + if (t->z[count].std_dev == SYSMIS || t->z[count].std_dev == 0.0) t->z[count].mean = SYSMIS; count++; } @@ -569,7 +566,7 @@ run_z_pass (void) /* Statistical calculation. */ static void -precalc (void) +precalc (void *aux UNUSED) { int i; @@ -593,7 +590,7 @@ precalc (void) } static int -calc (struct ccase * c) +calc (struct ccase * c, void *aux UNUSED) { int i; @@ -601,20 +598,13 @@ calc (struct ccase * c) static int case_id; /* Get the weight for this case. */ - double w; + double weight = dict_get_case_weight (default_dict, c); - if (default_dict.weight_index == -1) - w = 1.0; - else + if (weight <= 0.0) { - w = c->data[default_dict.weight_index].f; - if (w <= 0.0 || w == SYSMIS) - { - w = 0.0; - bad_weight = 1; - } + weight = 0.0; + bad_weight = 1; } - case_id++; /* Handle missing values. */ @@ -627,17 +617,17 @@ calc (struct ccase * c) { if (opt[op_excl_miss]) { - d_glob_missing += w; + d_glob_missing += weight; return 1; } else { - d_glob_miss_list += w; + d_glob_miss_list += weight; goto iterate; } } } - d_glob_valid += w; + d_glob_valid += weight; iterate: for (i = 0; i < n_variables; i++) @@ -657,7 +647,7 @@ iterate: if (X == SYSMIS || (!opt[op_incl_miss] && is_num_user_missing (X, v_variables[i]))) { - inf->miss += w; + inf->miss += weight; continue; } @@ -669,19 +659,19 @@ iterate: I am happy that mathematical formulas may not be copyrighted. */ W_old = inf->valid; - W_new = inf->valid += w; - v = (w / W_new) * (X - inf->X_bar); + W_new = inf->valid += weight; + v = (weight / W_new) * (X - inf->X_bar); v2 = v * v; v3 = v2 * v; v4 = v3 * v; - w2 = w * w; - w3 = w2 * w; - w4 = w3 * w; + w2 = weight * weight; + w3 = w2 * weight; + w4 = w3 * weight; inf->M4 += (-4.0 * v * inf->M3 + 6.0 * v2 * inf->M2 - + (W_new * W_new - 3 * w * W_old / w3) * v4 * W_old * W_new); + + (W_new * W_new - 3 * weight * W_old / w3) * v4 * W_old * W_new); inf->M3 += (-3.0 * v * inf->M2 + W_new * W_old / w2 - * (W_new - 2 * w) * v3); - inf->M2 += W_new * W_old / w * v2; + * (W_new - 2 * weight) * v3); + inf->M2 += W_new * W_old / weight * v2; inf->X_bar += v; if (X < inf->min) inf->min = X; @@ -692,7 +682,7 @@ iterate: } static void -postcalc (void) +postcalc (void *aux UNUSED) { int i; @@ -756,7 +746,7 @@ postcalc (void) /* Statistical display. */ -static int compare_func (struct variable ** a, struct variable ** b); +static algo_compare_func descriptives_compare_variables; static void display (void) @@ -774,8 +764,8 @@ display (void) is zero. d_glob_miss_list is (potentially) nonzero. */ if (sortby_stat != -2) - qsort (v_variables, n_variables, sizeof (struct variable *), - (int (*)(const void *, const void *)) compare_func); + sort (v_variables, n_variables, sizeof *v_variables, + descriptives_compare_variables, &cmd); for (nc = i = 0; i < dsc_n_stats; i++) if (stats & BIT_INDEX (i)) @@ -834,29 +824,33 @@ display (void) tab_submit (t); } +/* Compares variables A and B according to the ordering specified + by CMD. */ static int -compare_func (struct variable ** a, struct variable ** b) +descriptives_compare_variables (const void *a_, const void *b_, void *cmd_) { - double temp; + struct variable *const *ap = a_; + struct variable *const *bp = b_; + struct variable *a = *ap; + struct variable *b = *bp; + struct cmd_descriptives *cmd = cmd_; + + int result; - if (cmd.order == DSC_D) + if (cmd->sortby == DSC_NAME) + result = strcmp (a->name, b->name); + else { - struct variable **t; - t = a; - a = b; - b = t; + double as = a->p.dsc.stats[sortby_stat]; + double bs = b->p.dsc.stats[sortby_stat]; + + result = as < bs ? -1 : as > bs; } - if (cmd.sortby == DSC_NAME) - return strcmp ((*a)->name, (*b)->name); - temp = ((*a)->p.dsc.stats[sortby_stat] - - (*b)->p.dsc.stats[sortby_stat]); - if (temp > 0) - return 1; - else if (temp < 0) - return -1; - else - return 0; + if (cmd->order == DSC_D) + result = -result; + + return result; } /*