873798c6cd37784f7e4f7a44f4f1dc24268a1c8e
[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., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include "dictionary.h"
24 #include "error.h"
25 #include "alloc.h"
26 #include "command.h"
27 #include "hash.h"
28 #include "lexer.h"
29 #include "error.h"
30 #include "magic.h"
31 #include "var.h"
32 /* (headers) */
33
34 #include "debug-print.h"
35
36 /* (specification)
37    means (mns_):
38      *tables=custom;
39      +format=lab:!labels/nolabels/nocatlabs,
40             name:!names/nonames,
41             val:!values/novalues,
42             fmt:!table/tree;
43      +missing=miss:!table/include/dependent;
44      +cells[cl_]=default,count,sum,mean,stddev,variance,all;
45      +statistics[st_]=anova,linearity,all,none.
46 */
47 /* (declarations) */
48 /* (functions) */
49
50 /* TABLES: Variable lists for each dimension. */
51 int n_dim;              /* Number of dimensions. */
52 int *nv_dim;            /* Number of variables in each dimension. */
53 struct variable ***v_dim;       /* Variables in each dimension.  */
54
55 /* VARIABLES: List of variables. */
56 int n_var;
57 struct variable **v_var;
58
59 /* Parses and executes the T-TEST procedure. */
60 int
61 cmd_means (void)
62 {
63   struct cmd_means cmd;
64   int success = CMD_FAILURE;
65   
66   n_dim = 0;
67   nv_dim = NULL;
68   v_dim = NULL;
69   v_var = NULL;
70
71   if (!parse_means (&cmd))
72     goto free;
73
74   if (cmd.sbc_cells)
75     {
76       int i;
77       for (i = 0; i < MNS_CL_count; i++)
78         if (cmd.a_cells[i])
79           break;
80       if (i >= MNS_CL_count)
81         cmd.a_cells[MNS_CL_ALL] = 1;
82     }
83   else
84     cmd.a_cells[MNS_CL_DEFAULT] = 1;
85   if (cmd.a_cells[MNS_CL_DEFAULT] || cmd.a_cells[MNS_CL_ALL])
86     cmd.a_cells[MNS_CL_MEAN] = cmd.a_cells[MNS_CL_STDDEV] = cmd.a_cells[MNS_CL_COUNT] = 1;
87   if (cmd.a_cells[MNS_CL_ALL])
88     cmd.a_cells[MNS_CL_SUM] = cmd.a_cells[MNS_CL_VARIANCE] = 1;
89
90   if (cmd.sbc_statistics)
91     {
92       if (!cmd.a_statistics[MNS_ST_ANOVA] && !cmd.a_statistics[MNS_ST_LINEARITY])
93         cmd.a_statistics[MNS_ST_ANOVA] = 1;
94       if (cmd.a_statistics[MNS_ST_ALL])
95         cmd.a_statistics[MNS_ST_ANOVA] = cmd.a_statistics[MNS_ST_LINEARITY] = 1;
96     }
97
98   if (!cmd.sbc_tables)
99     {
100       msg (SE, _("Missing required subcommand TABLES."));
101       goto free;
102     }
103
104   success = CMD_SUCCESS;
105
106 free:
107   {
108     int i;
109     
110     for (i = 0; i < n_dim; i++)
111       free (v_dim[i]);
112     free (nv_dim);
113     free (v_dim);
114     free (v_var);
115   }
116   
117   return success;
118 }
119
120 /* Parses the TABLES subcommand. */
121 static int
122 mns_custom_tables (struct cmd_means *cmd)
123 {
124   struct var_set *var_set;
125   
126   if (!lex_match_id ("TABLES")
127       && (token != T_ID || dict_lookup_var (default_dict, tokid) == NULL)
128       && token != T_ALL)
129     return 2;
130   lex_match ('=');
131
132   if (cmd->sbc_tables)
133     {
134       msg (SE, _("TABLES subcommand may not appear more "
135                  "than once."));
136       return 0;
137     }
138
139   var_set = var_set_create_from_dict (default_dict);
140   assert (var_set != NULL);
141
142   do
143     {
144       int nvl;
145       struct variable **vl;
146
147       if (!parse_var_set_vars (var_set, &vl, &nvl,
148                                PV_NO_DUPLICATE | PV_NO_SCRATCH)) 
149         goto lossage;
150       
151       n_dim++;
152       nv_dim = xrealloc (nv_dim, n_dim * sizeof (int));
153       v_dim = xrealloc (v_dim, n_dim * sizeof (struct variable **));
154
155       nv_dim[n_dim - 1] = nvl;
156       v_dim[n_dim - 1] = vl;
157     }
158   while (lex_match (T_BY));
159
160   var_set_destroy (var_set);
161   return 1;
162
163  lossage:
164   var_set_destroy (var_set);
165   return 0;
166 }
167
168 /* 
169    Local Variables:
170    mode: c
171    End:
172 */