checkin of 0.3.0
[pspp-builds.git] / src / correlations.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 "alloc.h"
23 #include "file-handle.h"
24 #include "command.h"
25 #include "lexer.h"
26 #include "var.h"
27 /* (headers) */
28
29 #undef DEBUGGING
30 #define DEBUGGING 1
31 #include "debug-print.h"
32
33 struct cor_set
34   {
35     struct cor_set *next;
36     struct variable **v1, **v2;
37     int nv1, nv2;
38   };
39
40 struct cor_set *cor_list, *cor_last;
41
42 struct file_handle *matrix_file;
43
44 static void free_correlations_state (void);
45 static int internal_cmd_correlations (void);
46
47 int
48 cmd_correlations (void)
49 {
50   int result = internal_cmd_correlations ();
51   free_correlations_state ();
52   return result;
53 }
54
55 /* (specification)
56    "CORRELATIONS" (cor_):
57      *variables=custom;
58      +missing=miss:!pairwise/listwise,
59               inc:include/exclude;
60      +print=tail:!twotail/onetail,
61             sig:!sig/nosig;
62      +format=fmt:!matrix/serial;
63      +matrix=custom;
64      +statistics[st_]=descriptives,xprod,all.
65 */
66 /* (declarations) */
67 /* (functions) */
68
69 int
70 internal_cmd_correlations (void)
71 {
72   struct cmd_correlations cmd;
73
74   cor_list = cor_last = NULL;
75   matrix_file = NULL;
76
77   lex_match_id ("PEARSON");
78   lex_match_id ("CORRELATIONS");
79
80   if (!parse_correlations (&cmd))
81     return CMD_FAILURE;
82   free_correlations (&cmd);
83
84   return CMD_SUCCESS;
85 }
86
87 static int
88 cor_custom_variables (struct cmd_correlations *cmd unused)
89 {
90   struct variable **v1, **v2;
91   int nv1, nv2;
92   struct cor_set *cor;
93
94   /* Ensure that this is a VARIABLES subcommand. */
95   if (!lex_match_id ("VARIABLES") && (token != T_ID || !is_varname (tokid))
96       && token != T_ALL)
97     return 2;
98   lex_match ('=');
99
100   if (!parse_variables (&default_dict, &v1, &nv1,
101                         PV_NO_DUPLICATE | PV_NUMERIC))
102     return 0;
103   
104   if (lex_match (T_WITH))
105     {
106       if (!parse_variables (&default_dict, &v2, &nv2,
107                             PV_NO_DUPLICATE | PV_NUMERIC))
108         {
109           free (v1);
110           return 0;
111         }
112     }
113   else
114     {
115       nv2 = nv1;
116       v2 = v1;
117     }
118
119   cor = xmalloc (sizeof *cor);
120   cor->next = NULL;
121   cor->v1 = v1;
122   cor->v2 = v2;
123   cor->nv1 = nv1;
124   cor->nv2 = nv2;
125   if (cor_list)
126     cor_last = cor_last->next = cor;
127   else
128     cor_list = cor_last = cor;
129   
130   return 1;
131 }
132
133 static int
134 cor_custom_matrix (struct cmd_correlations *cmd unused)
135 {
136   if (!lex_force_match ('('))
137     return 0;
138   
139   if (lex_match ('*'))
140     matrix_file = inline_file;
141   else
142     matrix_file = fh_parse_file_handle ();
143
144   if (!matrix_file)
145     return 0;
146
147   if (!lex_force_match (')'))
148     return 0;
149
150   return 1;
151 }
152
153 static void
154 free_correlations_state (void)
155 {
156   struct cor_set *cor, *next;
157
158   for (cor = cor_list; cor != NULL; cor = next)
159     {
160       next = cor->next;
161       if (cor->v1 != cor->v2)
162         free (cor->v2);
163       free (cor->v1);
164       free (cor);
165     }
166 }