Added a --enable-debug option to configure and
[pspp-builds.git] / src / means.q
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 #include <config.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <assert.h>
24 #include "alloc.h"
25 #include "avl.h"
26 #include "command.h"
27 #include "lexer.h"
28 #include "error.h"
29 #include "magic.h"
30 #include "var.h"
31 /* (headers) */
32
33 #include "debug-print.h"
34
35 /* (specification)
36    means (mns_):
37      *tables=custom;
38      +variables=custom;
39      +crossbreak=custom;
40      +format=lab:!labels/nolabels/nocatlabs,
41             name:!names/nonames,
42             val:!values/novalues,
43             fmt:!table/tree;
44      +missing=miss:!table/include/dependent;
45      +cells[cl_]=default,count,sum,mean,stddev,variance,all;
46      +statistics[st_]=anova,linearity,all,none.
47 */
48 /* (declarations) */
49 /* (functions) */
50
51 #if DEBUGGING
52 static void debug_print (struct cmd_means *cmd);
53 #endif
54
55 /* TABLES: Variable lists for each dimension. */
56 int n_dim;              /* Number of dimensions. */
57 int *nv_dim;            /* Number of variables in each dimension. */
58 struct variable ***v_dim;       /* Variables in each dimension.  */
59
60 /* VARIABLES: List of variables. */
61 int n_var;
62 struct variable **v_var;
63
64 /* Parses and executes the T-TEST procedure. */
65 int
66 cmd_means (void)
67 {
68   struct cmd_means cmd;
69   int success = CMD_FAILURE;
70   
71   n_dim = 0;
72   nv_dim = NULL;
73   v_dim = NULL;
74   v_var = NULL;
75
76   lex_match_id ("MEANS");
77   if (!parse_means (&cmd))
78     goto free;
79
80   if (cmd.sbc_cells)
81     {
82       int i;
83       for (i = 0; i < MNS_CL_count; i++)
84         if (cmd.a_cells[i])
85           break;
86       if (i >= MNS_CL_count)
87         cmd.a_cells[MNS_CL_ALL] = 1;
88     }
89   else
90     cmd.a_cells[MNS_CL_DEFAULT] = 1;
91   if (cmd.a_cells[MNS_CL_DEFAULT] || cmd.a_cells[MNS_CL_ALL])
92     cmd.a_cells[MNS_CL_MEAN] = cmd.a_cells[MNS_CL_STDDEV] = cmd.a_cells[MNS_CL_COUNT] = 1;
93   if (cmd.a_cells[MNS_CL_ALL])
94     cmd.a_cells[MNS_CL_SUM] = cmd.a_cells[MNS_CL_VARIANCE] = 1;
95
96   if (cmd.sbc_statistics)
97     {
98       if (!cmd.a_statistics[MNS_ST_ANOVA] && !cmd.a_statistics[MNS_ST_LINEARITY])
99         cmd.a_statistics[MNS_ST_ANOVA] = 1;
100       if (cmd.a_statistics[MNS_ST_ALL])
101         cmd.a_statistics[MNS_ST_ANOVA] = cmd.a_statistics[MNS_ST_LINEARITY] = 1;
102     }
103
104   if (!cmd.sbc_tables)
105     {
106       msg (SE, _("Missing required subcommand TABLES."));
107       goto free;
108     }
109
110 #if DEBUGGING
111   debug_print (&cmd);
112 #endif
113   
114   success = CMD_SUCCESS;
115
116 free:
117   {
118     int i;
119     
120     for (i = 0; i < n_dim; i++)
121       free (v_dim[i]);
122     free (nv_dim);
123     free (v_dim);
124     free (v_var);
125   }
126   
127   return success;
128 }
129
130 /* Returns nonzero only if value V is valid as an endpoint for a
131    dependent variable in integer mode. */
132 int
133 validate_dependent_endpoint (double V)
134 {
135   return V == (int) V && V != LOWEST && V != HIGHEST;
136 }
137
138 /* Parses the TABLES subcommand. */
139 static int
140 mns_custom_tables (struct cmd_means *cmd)
141 {
142   struct dictionary *dict;
143   struct dictionary temp_dict;
144   
145   if (!lex_match_id ("TABLES")
146       && (token != T_ID || !is_varname (tokid))
147       && token != T_ALL)
148     return 2;
149   lex_match ('=');
150
151   if (cmd->sbc_tables || cmd->sbc_crossbreak)
152     {
153       msg (SE, _("TABLES or CROSSBREAK subcommand may not appear more "
154                  "than once."));
155       return 0;
156     }
157
158   if (cmd->sbc_variables)
159     {
160       dict = &temp_dict;
161       temp_dict.var = v_var;
162       temp_dict.nvar = n_var;
163       
164       {
165         int i;
166       
167         temp_dict.var_by_name = avl_create (NULL, cmp_variable, NULL);
168         for (i = 0; i < temp_dict.nvar; i++)
169           avl_force_insert (temp_dict.var_by_name, temp_dict.var[i]);
170       }
171     }
172   else
173     dict = &default_dict;
174
175   do
176     {
177       int nvl;
178       struct variable **vl;
179         
180       if (!parse_variables (dict, &vl, &nvl, PV_NO_DUPLICATE | PV_NO_SCRATCH))
181         return 0;
182       
183       n_dim++;
184       nv_dim = xrealloc (nv_dim, n_dim * sizeof (int));
185       v_dim = xrealloc (v_dim, n_dim * sizeof (struct variable **));
186
187       nv_dim[n_dim - 1] = nvl;
188       v_dim[n_dim - 1] = vl;
189
190       if (cmd->sbc_variables)
191         {
192           int i;
193
194           for (i = 0; i < nv_dim[0]; i++)
195             {
196               struct means_proc *v_inf = &v_dim[0][i]->p.mns;
197
198               if (v_inf->min == SYSMIS)
199                 {
200                   msg (SE, _("Variable %s specified on TABLES or "
201                              "CROSSBREAK, but not specified on "
202                              "VARIABLES."),
203                        v_dim[0][i]->name);
204                   return 0;
205                 }
206               
207               if (n_dim == 1)
208                 {
209                   v_inf->min = (int) v_inf->min;
210                   v_inf->max = (int) v_inf->max;
211                 } else {
212                   if (v_inf->min == LOWEST || v_inf->max == HIGHEST)
213                     {
214                       msg (SE, _("LOWEST and HIGHEST may not be used "
215                                  "for independent variables (%s)."),
216                            v_dim[0][i]->name);
217                       return 0;
218                     }
219                   if (v_inf->min != (int) v_inf->min
220                       || v_inf->max != (int) v_inf->max)
221                     {
222                       msg (SE, _("Independent variables (%s) may not "
223                                  "have noninteger endpoints in their "
224                                  "ranges."),
225                            v_dim[0][i]->name);
226                       return 0;
227                     }
228                 }
229             }
230         }
231     }
232   while (lex_match (T_BY));
233
234   /* Check for duplicates. */
235   {
236     int i;
237     
238     for (i = 0; i < default_dict.nvar; i++)
239       default_dict.var[i]->foo = 0;
240     for (i = 0; i < dict->nvar; i++)
241       if (dict->var[i]->foo++)
242         {
243           msg (SE, _("Variable %s is multiply specified on TABLES "
244                      "or CROSSBREAK."),
245                dict->var[i]->name);
246           return 0;
247         }
248   }
249   
250   if (cmd->sbc_variables)
251     avl_destroy (temp_dict.var_by_name, NULL);
252
253   return 1;
254 }
255
256 /* Parse CROSSBREAK subcommand. */
257 static int
258 mns_custom_crossbreak (struct cmd_means *cmd)
259 {
260   return mns_custom_tables (cmd);
261 }
262
263 /* Parses the VARIABLES subcommand. */
264 static int
265 mns_custom_variables (struct cmd_means *cmd)
266 {
267   if (cmd->sbc_tables)
268     {
269       msg (SE, _("VARIABLES must precede TABLES."));
270       return 0;
271     }
272
273   if (cmd->sbc_variables == 1)
274     {
275       int i;
276       
277       for (i = 0; i < default_dict.nvar; i++)
278         default_dict.var[i]->p.mns.min = SYSMIS;
279     }
280   
281   do
282     {
283       int orig_n = n_var;
284       
285       double min, max;
286       
287       if (!parse_variables (&default_dict, &v_var, &n_var,
288                             PV_APPEND | PV_NO_DUPLICATE | PV_NO_SCRATCH))
289         return 0;
290
291       if (!lex_force_match ('('))
292         return 0;
293
294       /* Lower value. */
295       if (token == T_ID
296           && (!strcmp (tokid, "LO") || lex_id_match ("LOWEST", tokid)))
297         min = LOWEST;
298       else
299         {
300           if (!lex_force_num ())
301             return 0;
302           min = tokval;
303         }
304       lex_get ();
305
306       lex_match (',');
307
308       /* Higher value. */
309       if (token == T_ID
310           && (!strcmp (tokid, "HI") || lex_id_match ("HIGHEST", tokid)))
311         max = HIGHEST;
312       else
313         {
314           if (!lex_force_num ())
315             return 0;
316           max = tokval;
317         }
318       lex_get ();
319
320       if (!lex_force_match (')'))
321         return 0;
322
323       /* Range check. */
324       if (max < min)
325         {
326           msg (SE, _("Upper value (%g) is less than lower value "
327                      "(%g) on VARIABLES subcommand."), max, min);
328           return 0;
329         }
330       
331       {
332         int i;
333
334         for (i = orig_n; i < n_var; i++)
335           {
336             struct means_proc *v_inf = &v_var[i]->p.mns;
337
338             v_inf->min = min;
339             v_inf->max = max;
340           }
341       }
342     }
343   while (token != '/' && token != '.');
344   
345   return 1;
346 }
347
348 #if DEBUGGING
349 static void
350 debug_print (struct cmd_means *cmd)
351 {
352   int i;
353   
354   printf ("MEANS");
355
356   if (cmd->sbc_variables)
357     {
358       int j = 0;
359       
360       printf (" VARIABLES=");
361       for (i = 0; i < default_dict.nvar; i++)
362         {
363           struct variable *v = default_dict.var[i];
364           
365           if (v->p.mns.min == SYSMIS)
366             continue;
367           if (j++)
368             printf (" ");
369           printf ("%s(", v->name);
370           if (v->p.mns.min == LOWEST)
371             printf ("LO");
372           else
373             printf ("%g", v->p.mns.min);
374           printf (",");
375           if (v->p.mns.max == HIGHEST)
376             printf ("HI");
377           else
378             printf ("%g", v->p.mns.max);
379           printf (")");
380         }
381       printf ("\n");
382     }
383
384   printf (" TABLES=");
385   for (i = 0; i < n_dim; i++)
386     {
387       int j;
388
389       if (i)
390         printf (" BY");
391
392       for (j = 0; j < nv_dim[i]; j++)
393         {
394           if (i || j)
395             printf (" ");
396           printf (v_dim[i][j]->name);
397         }
398     }
399   printf ("\n");
400 }
401 #endif /* DEBUGGING */
402
403 /* 
404    Local Variables:
405    mode: c
406    End:
407 */