Added caculations for the grouped T-TEST
[pspp-builds.git] / src / var.h
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18    02111-1307, USA. */
19
20 #if !var_h
21 #define var_h 1
22
23 #include <stddef.h>
24 #include "format.h"
25 #include "t-test.h"
26
27 /* Values. */
28
29 /* Definition of the max length of a short string value, generally
30    eight characters.  */
31 #define MAX_SHORT_STRING ((SIZEOF_DOUBLE)>=8 ? ((SIZEOF_DOUBLE)+1)/2*2 : 8)
32 #define MIN_LONG_STRING (MAX_SHORT_STRING+1)
33
34 /* FYI: It is a bad situation if sizeof(flt64) < MAX_SHORT_STRING:
35    then short string missing values can be truncated in system files
36    because there's only room for as many characters as can fit in a
37    flt64. */
38 #if MAX_SHORT_STRING > 8
39 #error MAX_SHORT_STRING must be less than 8.
40 #endif
41
42 /* Special values. */
43 #define SYSMIS (-DBL_MAX)
44 #define LOWEST second_lowest_value
45 #define HIGHEST DBL_MAX
46
47 /* Describes one value, which is either a floating-point number or a
48    short string. */
49 union value
50   {
51     /* A numeric value. */
52     double f;
53
54     /* A short-string value. */
55     unsigned char s[MAX_SHORT_STRING];
56
57     /* This member is used by data-in.c to return a string result,
58        since it may need to return a long string.  As currently
59        implemented, it's a pointer to a static internal buffer in
60        data-in.c.
61
62        Also used by evaluate_expression() to return a string result.
63        As currently implemented, it's a pointer to a dynamic buffer in
64        the appropriate expression.
65
66        Also used by the AGGREGATE procedure in handling string
67        values. */
68     unsigned char *c;
69
70     /* Sometimes we insert value's in a hash table. */
71     unsigned long hash[SIZEOF_DOUBLE / SIZEOF_LONG];
72   };
73 \f
74 /* Frequency tables. */
75
76 /* Frequency table entry. */
77 struct freq
78   {
79     union value v;              /* The value. */
80     double c;                   /* The number of occurrences of the value. */
81   };
82
83 /* Types of frequency tables. */
84 enum
85   {
86     FRQM_GENERAL,
87     FRQM_INTEGER
88   };
89
90 /* Entire frequency table. */
91 struct freq_tab
92   {
93     int mode;                   /* FRQM_GENERAL or FRQM_INTEGER. */
94
95     /* General mode. */
96     struct hsh_table *data;     /* Undifferentiated data. */
97
98     /* Integer mode. */
99     double *vector;             /* Frequencies proper. */
100     int min, max;               /* The boundaries of the table. */
101     double out_of_range;        /* Sum of weights of out-of-range values. */
102     double sysmis;              /* Sum of weights of SYSMIS values. */
103
104     /* All modes. */
105     struct freq *valid;         /* Valid freqs. */
106     int n_valid;                /* Number of total freqs. */
107
108     struct freq *missing;       /* Missing freqs. */
109     int n_missing;              /* Number of missing freqs. */
110
111     /* Statistics. */
112     double total_cases;         /* Sum of weights of all cases. */
113     double valid_cases;         /* Sum of weights of valid cases. */
114   };
115 \f
116 /* Procedures' private per-variable data. */
117
118 /* Structure name suffixes for private data:
119    _proc: for a procedure (i.e., LIST -> list_proc).
120    _trns: for a transformation (i.e., COMPUTE -> compute_trns.
121    _pgm: for an input program (i.e., DATA LIST -> data_list_pgm). */
122
123 /* CROSSTABS private data. */
124 struct crosstab_proc
125   {
126     /* Integer mode only. */
127     int min;                    /* Minimum value. */
128     int max;                    /* Maximum value + 1. */
129     int count;                  /* max - min. */
130   };
131
132
133 /* FREQUENCIES private data. */
134 enum
135   {
136     frq_mean = 0, frq_semean, frq_median, frq_mode, frq_stddev, frq_variance,
137     frq_kurt, frq_sekurt, frq_skew, frq_seskew, frq_range, frq_min, frq_max,
138     frq_sum, frq_n_stats
139   };
140
141 struct frequencies_proc
142   {
143     int used;                   /* 1=This variable already used. */
144
145     /* Freqency table. */
146     struct freq_tab tab;        /* Frequencies table to use. */
147
148     /* Percentiles. */
149     int n_groups;               /* Number of groups. */
150     double *groups;             /* Groups. */
151
152     /* Statistics. */
153     double stat[frq_n_stats];
154   };
155
156 /* LIST private data. */
157 struct list_proc
158   {
159     int newline;                /* Whether a new line begins here. */
160     int width;                  /* Field width. */
161     int vert;                   /* Whether to print the varname vertically. */
162   };
163
164 /* DESCRIPTIVES private data.  Note that the DESCRIPTIVES procedure also
165    has a transformation, descriptives_trns. */
166 enum
167   {
168     /* As these are used as bit indexes, there must be 32 or fewer.
169        Be very careful in adjusting these, see the structure below
170        and the table in descriptives.q. */
171     dsc_mean = 0, dsc_semean, dsc_stddev, dsc_variance, dsc_kurt,
172     dsc_sekurt, dsc_skew, dsc_seskew, dsc_range, dsc_min,
173     dsc_max, dsc_sum, dsc_n_stats
174   };
175
176 struct descriptives_proc
177   {
178     /* Miscellaneous. */
179     int dup;                    /* Finds duplicates in list of
180                                    variables. */
181     char zname[10];             /* Name for z-score variable. */
182
183     /* Counts. */
184     double valid, miss;         /* Valid, missing--general. */
185
186     /* Mean, moments about the mean. */
187     double X_bar, M2, M3, M4;
188     double min, max;
189
190     /* Statistics. */
191     double stats[dsc_n_stats];  /* Everything glommed together. */
192   };
193
194 /* GET private data. */
195 struct get_proc
196   {
197     int fv, nv;                 /* First, # of values. */
198   };
199
200 /* Sort order. */
201 enum
202   {
203     SRT_ASCEND,                 /* A, B, C, ..., X, Y, Z. */
204     SRT_DESCEND                 /* Z, Y, X, ..., C, B, A. */
205   };
206
207 /* SORT CASES private data. */
208 struct sort_cases_proc
209   {
210     int order;                  /* SRT_ASCEND or SRT_DESCEND. */
211   };
212
213 /* MEANS private data. */
214 struct means_proc
215   {
216     double min, max;            /* Range for integer mode. */
217   };
218
219 /* Different types of variables for MATRIX DATA procedure.  Order is
220    important: these are used for sort keys. */
221 enum
222   {
223     MXD_SPLIT,                  /* SPLIT FILE variables. */
224     MXD_ROWTYPE,                /* ROWTYPE_. */
225     MXD_FACTOR,                 /* Factor variables. */
226     MXD_VARNAME,                /* VARNAME_. */
227     MXD_CONTINUOUS,             /* Continuous variables. */
228
229     MXD_COUNT
230   };
231
232 /* MATRIX DATA private data. */
233 struct matrix_data_proc
234   {
235     int vartype;                /* Variable type. */
236     int subtype;                /* Subtype. */
237   };
238
239 /* MATCH FILES private data. */
240 struct match_files_proc
241   {
242     struct variable *master;    /* Corresponding master file variable. */
243   };
244
245 \f
246 /* Script variables. */
247
248 /* Variable type. */
249 enum
250   {
251     NUMERIC,                    /* A numeric variable. */
252     ALPHA                       /* A string variable.  (STRING is pre-empted by lexer.h) */
253   };
254
255 /* Types of missing values.  Order is significant, see
256    mis-val.c:parse_numeric(), sfm-read.c:sfm_read_dictionary()
257    sfm-write.c:sfm_write_dictionary(),
258    sysfile-info.c:cmd_sysfile_info(), mis-val.c:copy_missing_values(),
259    pfm-read.c:read_variables(), pfm-write.c:write_variables(),
260    apply-dict.c:cmd_apply_dictionary(), and more (?). */
261 enum
262   {
263     MISSING_NONE,               /* No user-missing values. */
264     MISSING_1,                  /* One user-missing value. */
265     MISSING_2,                  /* Two user-missing values. */
266     MISSING_3,                  /* Three user-missing values. */
267     MISSING_RANGE,              /* [a,b]. */
268     MISSING_LOW,                /* (-inf,a]. */
269     MISSING_HIGH,               /* (a,+inf]. */
270     MISSING_RANGE_1,            /* [a,b], c. */
271     MISSING_LOW_1,              /* (-inf,a], b. */
272     MISSING_HIGH_1,             /* (a,+inf), b. */
273     MISSING_COUNT
274   };
275
276 /* A variable's dictionary entry.  Note: don't reorder name[] from the
277    first element; a pointer to `variable' should be a pointer to
278    member `name'.*/
279 struct variable
280   {
281     /* Required by parse_variables() to be in this order.  */
282     char name[9];               /* As a string. */
283     int index;                  /* Index into its dictionary's var[]. */
284     int type;                   /* NUMERIC or ALPHA. */
285
286     /* Also important but parse_variables() doesn't need it.  Still,
287        check before reordering. */
288     int width;                  /* Size of string variables in chars. */
289     int fv, nv;                 /* Index into `value's, number of values. */
290     int left;                   /* 0=reinitialize each case, 1=don't. */
291
292     /* Missing values. */
293     int miss_type;              /* One of the MISSING_* constants. */
294     union value missing[3];     /* User-missing value. */
295
296     /* Display formats. */
297     struct fmt_spec print;      /* Default format for PRINT. */
298     struct fmt_spec write;      /* Default format for WRITE. */
299
300     /* Labels. */
301     struct val_labs *val_labs;
302     char *label;                /* Variable label. */
303
304     /* Per-procedure info. */
305     void *aux;
306     struct get_proc get;
307     union
308       {
309         struct crosstab_proc crs;
310         struct descriptives_proc dsc;
311         struct frequencies_proc frq;
312         struct list_proc lst;
313         struct means_proc mns;
314         struct sort_cases_proc srt;
315         struct matrix_data_proc mxd;
316         struct match_files_proc mtf;
317         struct t_test_proc t_t;
318       }
319     p;
320   };
321
322 int compare_variables (const void *, const void *, void *);
323 unsigned hash_variable (const void *, void *);
324
325 /* Classes of variables. */
326 enum dict_class 
327   {
328     DC_ORDINARY,                /* Ordinary identifier. */
329     DC_SYSTEM,                  /* System variable. */
330     DC_SCRATCH                  /* Scratch variable. */
331   };
332
333 enum dict_class dict_class_from_id (const char *name);
334 const char *dict_class_to_name (enum dict_class dict_class);
335 \f
336 /* Vector of variables. */
337 struct vector
338   {
339     int idx;                    /* Index for dict_get_vector(). */
340     char name[9];               /* Name. */
341     struct variable **var;      /* Vector of variables. */
342     int cnt;                    /* Number of variables. */
343   };
344 \f
345 /* Cases. */
346
347 /* A single case.  (This doesn't need to be a struct anymore, but it
348    remains so for hysterical raisins.) */
349 struct ccase
350   {
351     union value data[1];
352   };
353 \f
354 /* Dictionary. */ 
355
356 /* Complete dictionary state. */
357 struct dictionary;
358
359 struct dictionary *dict_create (void);
360 struct dictionary *dict_clone (const struct dictionary *);
361 void dict_clear (struct dictionary *);
362 void dict_destroy (struct dictionary *);
363
364 size_t dict_get_var_cnt (const struct dictionary *);
365 struct variable *dict_get_var (const struct dictionary *, size_t idx);
366 void dict_get_vars (const struct dictionary *,
367                     struct variable ***vars, size_t *cnt,
368                     unsigned exclude_classes);
369
370 struct variable *dict_create_var (struct dictionary *, const char *,
371                                   int width);
372 struct variable *dict_clone_var (struct dictionary *, const struct variable *,
373                                  const char *);
374 void dict_rename_var (struct dictionary *, struct variable *, const char *);
375
376 struct variable *dict_lookup_var (const struct dictionary *, const char *);
377 int dict_contains_var (const struct dictionary *, const struct variable *);
378 void dict_delete_var (struct dictionary *, struct variable *);
379 void dict_delete_vars (struct dictionary *,
380                        struct variable *const *, size_t count);
381 void dict_reorder_vars (struct dictionary *,
382                         struct variable *const *, size_t count);
383 int dict_rename_vars (struct dictionary *,
384                       struct variable **, char **new_names,
385                       size_t count, char **err_name);
386
387 struct variable *dict_get_weight (const struct dictionary *);
388 double dict_get_case_weight (const struct dictionary *, const struct ccase *);
389 void dict_set_weight (struct dictionary *, struct variable *);
390
391 struct variable *dict_get_filter (const struct dictionary *);
392 void dict_set_filter (struct dictionary *, struct variable *);
393
394 int dict_get_case_limit (const struct dictionary *);
395 void dict_set_case_limit (struct dictionary *, int);
396
397 int dict_get_value_cnt (const struct dictionary *);
398 void dict_compact_values (struct dictionary *);
399
400 struct variable *const *dict_get_split_vars (const struct dictionary *);
401 size_t dict_get_split_cnt (const struct dictionary *);
402 void dict_set_split_vars (struct dictionary *,
403                           struct variable *const *, size_t cnt);
404
405 const char *dict_get_label (const struct dictionary *);
406 void dict_set_label (struct dictionary *, const char *);
407
408 const char *dict_get_documents (const struct dictionary *);
409 void dict_set_documents (struct dictionary *, const char *);
410
411 int dict_create_vector (struct dictionary *,
412                         const char *name,
413                         struct variable **, size_t cnt);
414 const struct vector *dict_get_vector (const struct dictionary *,
415                                       size_t idx);
416 size_t dict_get_vector_cnt (const struct dictionary *);
417 const struct vector *dict_lookup_vector (const struct dictionary *,
418                                          const char *name);
419 void dict_clear_vectors (struct dictionary *);
420 \f
421 void discard_variables (void);
422
423 /* This is the active file dictionary. */
424 extern struct dictionary *default_dict;
425 \f
426 /* Transformation state. */
427
428 /* Default file handle for DATA LIST, REREAD, REPEATING DATA
429    commands. */
430 extern struct file_handle *default_handle;
431
432 /* PROCESS IF expression. */
433 extern struct expression *process_if_expr;
434 \f
435 /* TEMPORARY support. */
436
437 /* 1=TEMPORARY has been executed at some point. */
438 extern int temporary;
439
440 /* If temporary!=0, the saved dictionary. */
441 extern struct dictionary *temp_dict;
442
443 /* If temporary!=0, index into t_trns[] (declared far below) that
444    gives the point at which data should be written out.  -1 means that
445    the data shouldn't be changed since all transformations are
446    temporary. */
447 extern int temp_trns;
448
449 /* If FILTER is active, whether it was executed before or after
450    TEMPORARY. */
451 extern int FILTER_before_TEMPORARY;
452
453 void cancel_temporary (void);
454 \f
455 /* Functions. */
456
457 void dump_split_vars (const struct ccase *);
458
459 int is_num_user_missing (double, const struct variable *);
460 int is_str_user_missing (const unsigned char[], const struct variable *);
461 int is_missing (const union value *, const struct variable *);
462 int is_system_missing (const union value *, const struct variable *);
463 int is_user_missing (const union value *, const struct variable *);
464 void copy_missing_values (struct variable *dest, const struct variable *src);
465
466 #if GLOBAL_DEBUGGING
467 struct variable *force_create_variable (struct dictionary *, const char *name,
468                                         int type, int width);
469 struct variable *force_dup_variable (struct dictionary *,
470                                      const struct variable *src,
471                                      const char *name);
472 #else
473 #define force_create_variable(A, B, C, D)       \
474         create_variable (A, B, C, D)
475 #define force_dup_variable(A, B, C)             \
476         dup_variable (A, B, C)
477 #endif
478
479 \f
480 /* Transformations. */
481
482 /* Header for all transformations. */
483 struct trns_header
484   {
485     /* Index into t_trns[]. */
486     int index;
487
488     /* Transformation proc. */
489     int (*proc) (struct trns_header *, struct ccase *);
490
491     /* Garbage collector proc. */
492     void (*free) (struct trns_header *);
493   };
494
495 /* Array of transformations */
496 extern struct trns_header **t_trns;
497
498 /* Number of transformations, maximum number in array currently. */
499 extern int n_trns, m_trns;
500
501 /* Index of first transformation that is really a transformation.  Any
502    transformations before this belong to INPUT PROGRAM. */
503 extern int f_trns;
504
505 void add_transformation (struct trns_header *trns);
506 void cancel_transformations (void);
507 \f
508 struct var_set;
509
510 struct var_set *var_set_create_from_dict (struct dictionary *d);
511 struct var_set *var_set_create_from_array (struct variable **var, size_t);
512
513 size_t var_set_get_cnt (struct var_set *vs);
514 struct variable *var_set_get_var (struct var_set *vs, size_t idx);
515 struct variable *var_set_lookup_var (struct var_set *vs, const char *name);
516 void var_set_destroy (struct var_set *vs);
517
518 \f
519 /* Variable parsers. */
520
521 enum
522   {
523     PV_NONE = 0,                /* No options. */
524     PV_SINGLE = 0001,           /* Restrict to a single name or TO use. */
525     PV_DUPLICATE = 0002,        /* Don't merge duplicates. */
526     PV_APPEND = 0004,           /* Append to existing list. */
527     PV_NO_DUPLICATE = 0010,     /* Error on duplicates. */
528     PV_NUMERIC = 0020,          /* Vars must be numeric. */
529     PV_STRING = 0040,           /* Vars must be string. */
530     PV_SAME_TYPE = 00100,       /* All vars must be the same type. */
531     PV_NO_SCRATCH = 00200,      /* Disallow scratch variables. */
532   };
533
534 struct variable *parse_variable (void);
535 struct variable *parse_dict_variable (struct dictionary *);
536 int parse_variables (struct dictionary *, struct variable ***, int *,
537                      int opts);
538 int parse_var_set_vars (struct var_set *, struct variable ***, int *,
539                         int opts);
540 int parse_DATA_LIST_vars (char ***names, int *cnt, int opts);
541 int parse_mixed_vars (char ***names, int *cnt, int opts);
542
543 #endif /* !var_h */