Sat Dec 27 16:16:49 2003 Ben Pfaff <blp@gnu.org>
[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")
94       && (token != T_ID || dict_lookup_var (default_dict, tokid) != NULL)
95       && token != T_ALL)
96     return 2;
97   lex_match ('=');
98
99   if (!parse_variables (default_dict, &v1, &nv1,
100                         PV_NO_DUPLICATE | PV_NUMERIC))
101     return 0;
102   
103   if (lex_match (T_WITH))
104     {
105       if (!parse_variables (default_dict, &v2, &nv2,
106                             PV_NO_DUPLICATE | PV_NUMERIC))
107         {
108           free (v1);
109           return 0;
110         }
111     }
112   else
113     {
114       nv2 = nv1;
115       v2 = v1;
116     }
117
118   cor = xmalloc (sizeof *cor);
119   cor->next = NULL;
120   cor->v1 = v1;
121   cor->v2 = v2;
122   cor->nv1 = nv1;
123   cor->nv2 = nv2;
124   if (cor_list)
125     cor_last = cor_last->next = cor;
126   else
127     cor_list = cor_last = cor;
128   
129   return 1;
130 }
131
132 static int
133 cor_custom_matrix (struct cmd_correlations *cmd unused)
134 {
135   if (!lex_force_match ('('))
136     return 0;
137   
138   if (lex_match ('*'))
139     matrix_file = inline_file;
140   else
141     matrix_file = fh_parse_file_handle ();
142
143   if (!matrix_file)
144     return 0;
145
146   if (!lex_force_match (')'))
147     return 0;
148
149   return 1;
150 }
151
152 static void
153 free_correlations_state (void)
154 {
155   struct cor_set *cor, *next;
156
157   for (cor = cor_list; cor != NULL; cor = next)
158     {
159       next = cor->next;
160       if (cor->v1 != cor->v2)
161         free (cor->v2);
162       free (cor->v1);
163       free (cor);
164     }
165 }
166
167 /*
168   Local Variables:
169   mode: c
170   End:
171 */