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