Change license from GPLv2+ to GPLv3+.
[pspp-builds.git] / src / language / stats / means.q
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <stdlib.h>
20 #include <stdio.h>
21
22 #include <data/dictionary.h>
23 #include <data/procedure.h>
24 #include <data/variable.h>
25 #include <language/command.h>
26 #include <language/lexer/lexer.h>
27 #include <libpspp/alloc.h>
28 #include <libpspp/hash.h>
29 #include <libpspp/magic.h>
30 #include <libpspp/message.h>
31
32 #include "gettext.h"
33 #define _(msgid) gettext (msgid)
34
35 /* (headers) */
36
37 /* (specification)
38    means (mns_):
39      *tables=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 /* TABLES: Variable lists for each dimension. */
52 static int n_dim;               /* Number of dimensions. */
53 static size_t *nv_dim;          /* Number of variables in each dimension. */
54 static const struct variable ***v_dim;  /* Variables in each dimension.  */
55
56 /* VARIABLES: List of variables. */
57 static struct variable **v_var;
58
59 /* Parses and executes the T-TEST procedure. */
60 int
61 cmd_means (struct lexer *lexer, struct dataset *ds)
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 (lexer, ds, &cmd, NULL))
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 lexer *lexer, struct dataset *ds, struct cmd_means *cmd, void *aux UNUSED)
123 {
124   struct const_var_set *var_set;
125
126   if (!lex_match_id (lexer, "TABLES")
127       && (lex_token (lexer) != T_ID || dict_lookup_var (dataset_dict (ds), lex_tokid (lexer)) == NULL)
128       && lex_token (lexer) != T_ALL)
129     return 2;
130   lex_match (lexer, '=');
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 = const_var_set_create_from_dict (dataset_dict (ds));
140   assert (var_set != NULL);
141
142   do
143     {
144       size_t nvl;
145       const struct variable **vl;
146
147       if (!parse_const_var_set_vars (lexer, var_set, &vl, &nvl,
148                                PV_NO_DUPLICATE | PV_NO_SCRATCH))
149         goto lossage;
150
151       n_dim++;
152       nv_dim = xnrealloc (nv_dim, n_dim, sizeof *nv_dim);
153       v_dim = xnrealloc (v_dim, n_dim, sizeof *v_dim);
154
155       nv_dim[n_dim - 1] = nvl;
156       v_dim[n_dim - 1] = vl;
157     }
158   while (lex_match (lexer, T_BY));
159
160   const_var_set_destroy (var_set);
161   return 1;
162
163  lossage:
164   const_var_set_destroy (var_set);
165   return 0;
166 }
167
168 /*
169    Local Variables:
170    mode: c
171    End:
172 */