1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
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.
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.
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
25 #include "algorithm.h"
31 #include "file-handle.h"
39 #include "debug-print.h"
41 /* FIXME: /N subcommand not implemented. It should be pretty simple,
44 /* Format type enums. */
51 /* Matrix section enums. */
59 /* Diagonal inclusion enums. */
87 /* 0=vector, 1=matrix, 2=scalar. */
88 static int content_type[PROX + 1] =
90 0, 2, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1,
93 /* Name of each content type. */
94 static const char *content_names[PROX + 1] =
96 "N", "N", "N_MATRIX", "MEAN", "STDDEV", "COUNT", "MSE",
97 "DFE", "MAT", "COV", "CORR", "PROX",
100 /* The data file to be read. */
101 static struct file_handle *data_file;
104 static int fmt; /* LIST or FREE. */
105 static int section; /* LOWER or UPPER or FULL. */
106 static int diag; /* DIAGONAL or NODIAGONAL. */
108 /* Arena used for all the MATRIX DATA allocations. */
109 static struct pool *container;
111 /* ROWTYPE_ specified explicitly in data? */
112 static int explicit_rowtype;
114 /* ROWTYPE_, VARNAME_ variables. */
115 static struct variable *rowtype_, *varname_;
117 /* Is is per-factor data? */
118 int is_per_factor[PROX + 1];
120 /* Single SPLIT FILE variable. */
121 static struct variable *single_split;
123 /* Factor variables. */
124 static int n_factors;
125 static struct variable **factors;
127 /* Number of cells, or -1 if none. */
130 /* Population N specified by user. */
133 /* CONTENTS subcommand. */
134 static int contents[EOC * 3 + 1];
135 static int n_contents;
137 /* Number of continuous variables. */
138 static int n_continuous;
140 /* Index into default_dict.var of first continuous variables. */
141 static int first_continuous;
143 static int compare_variables_by_mxd_vartype (const void *pa,
145 static void read_matrices_without_rowtype (void);
146 static void read_matrices_with_rowtype (void);
147 static int string_to_content_type (char *, int *);
150 static void debug_print (void);
154 cmd_matrix_data (void)
158 lex_match_id ("MATRIX");
159 lex_match_id ("DATA");
161 container = pool_create ();
163 discard_variables ();
165 data_file = inline_file;
179 if (lex_match_id ("VARIABLES"))
186 msg (SE, _("VARIABLES subcommand multiply specified."));
192 if (!parse_DATA_LIST_vars (&v, &nv, PV_NO_DUPLICATE))
198 for (i = 0; i < nv; i++)
199 if (!strcmp (v[i], "VARNAME_"))
201 msg (SE, _("VARNAME_ cannot be explicitly specified on "
203 for (i = 0; i < nv; i++)
213 for (i = 0; i < nv; i++)
215 struct variable *new_var;
217 if (strcmp (v[i], "ROWTYPE_"))
219 new_var = dict_create_var (default_dict, v[i], 0);
220 assert (new_var != NULL);
221 new_var->p.mxd.vartype = MXD_CONTINUOUS;
222 new_var->p.mxd.subtype = i;
225 explicit_rowtype = 1;
232 rowtype_ = dict_create_var (default_dict, "ROWTYPE_", 8);
233 assert (rowtype_ != NULL);
234 rowtype_->p.mxd.vartype = MXD_ROWTYPE;
235 rowtype_->p.mxd.subtype = 0;
238 else if (lex_match_id ("FILE"))
241 data_file = fh_parse_file_handle ();
245 else if (lex_match_id ("FORMAT"))
249 while (token == T_ID)
251 if (lex_match_id ("LIST"))
253 else if (lex_match_id ("FREE"))
255 else if (lex_match_id ("LOWER"))
257 else if (lex_match_id ("UPPER"))
259 else if (lex_match_id ("FULL"))
261 else if (lex_match_id ("DIAGONAL"))
263 else if (lex_match_id ("NODIAGONAL"))
267 lex_error (_("in FORMAT subcommand"));
272 else if (lex_match_id ("SPLIT"))
278 msg (SE, _("SPLIT subcommand multiply specified."));
285 lex_error (_("in SPLIT subcommand"));
289 if (dict_lookup_var (default_dict, tokid) == NULL
290 && (lex_look_ahead () == '.' || lex_look_ahead () == '/'))
292 if (!strcmp (tokid, "ROWTYPE_") || !strcmp (tokid, "VARNAME_"))
294 msg (SE, _("Split variable may not be named ROWTYPE_ "
299 single_split = dict_create_var (default_dict, tokid, 0);
300 assert (single_split != NULL);
303 single_split->p.mxd.vartype = MXD_CONTINUOUS;
305 dict_set_split_vars (default_dict, &single_split, 1);
309 struct variable **split;
312 if (!parse_variables (default_dict, &split, &n, PV_NO_DUPLICATE))
315 dict_set_split_vars (default_dict, split, n);
319 struct variable *const *split = dict_get_split_vars (default_dict);
320 size_t split_cnt = dict_get_split_cnt (default_dict);
323 for (i = 0; i < split_cnt; i++)
325 if (split[i]->p.mxd.vartype != MXD_CONTINUOUS)
327 msg (SE, _("Split variable %s is already another type."),
331 split[i]->p.mxd.vartype = MXD_SPLIT;
332 split[i]->p.mxd.subtype = i;
336 else if (lex_match_id ("FACTORS"))
342 msg (SE, _("FACTORS subcommand multiply specified."));
347 if (!parse_variables (default_dict, &factors, &n_factors, PV_NONE))
353 for (i = 0; i < n_factors; i++)
355 if (factors[i]->p.mxd.vartype != MXD_CONTINUOUS)
357 msg (SE, _("Factor variable %s is already another type."),
361 factors[i]->p.mxd.vartype = MXD_FACTOR;
362 factors[i]->p.mxd.subtype = i;
366 else if (lex_match_id ("CELLS"))
372 msg (SE, _("CELLS subcommand multiply specified."));
376 if (!lex_integer_p () || lex_integer () < 1)
378 lex_error (_("expecting positive integer"));
382 cells = lex_integer ();
385 else if (lex_match_id ("N"))
391 msg (SE, _("N subcommand multiply specified."));
395 if (!lex_integer_p () || lex_integer () < 1)
397 lex_error (_("expecting positive integer"));
401 pop_n = lex_integer ();
404 else if (lex_match_id ("CONTENTS"))
406 int inside_parens = 0;
407 unsigned collide = 0;
412 msg (SE, _("CONTENTS subcommand multiply specified."));
422 for (i = 0; i <= PROX; i++)
423 is_per_factor[i] = 0;
432 msg (SE, _("Nested parentheses not allowed."));
438 else if (lex_match (')'))
442 msg (SE, _("Mismatched right parenthesis (`(')."));
445 if (contents[n_contents - 1] == LPAREN)
447 msg (SE, _("Empty parentheses not allowed."));
460 lex_error (_("in CONTENTS subcommand"));
464 content_type = string_to_content_type (tokid,
466 if (content_type == -1)
468 lex_error (_("in CONTENTS subcommand"));
473 if (collide & (1 << collide_index))
475 msg (SE, _("Content multiply specified for %s."),
476 content_names[content_type]);
479 collide |= (1 << collide_index);
482 is_per_factor[item] = inside_parens;
484 contents[n_contents++] = item;
486 if (token == '/' || token == '.')
492 msg (SE, _("Missing right parenthesis."));
495 contents[n_contents] = EOC;
506 lex_error (_("expecting end of command"));
512 msg (SE, _("Missing VARIABLES subcommand."));
516 if (!n_contents && !explicit_rowtype)
518 msg (SW, _("CONTENTS subcommand not specified: assuming file "
519 "contains only CORR matrix."));
526 if (n_factors && !explicit_rowtype && cells == -1)
528 msg (SE, _("Missing CELLS subcommand. CELLS is required "
529 "when ROWTYPE_ is not given in the data and "
530 "factors are present."));
534 if (explicit_rowtype && single_split)
536 msg (SE, _("Split file values must be present in the data when "
537 "ROWTYPE_ is present."));
541 /* Create VARNAME_. */
543 varname_ = dict_create_var (default_dict, "VARNAME_", 8);
544 assert (varname_ != NULL);
545 varname_->p.mxd.vartype = MXD_VARNAME;
546 varname_->p.mxd.subtype = 0;
549 /* Sort the dictionary variables into the desired order for the
550 system file output. */
555 dict_get_vars (default_dict, &v, &nv, 0);
556 qsort (v, nv, sizeof *v, compare_variables_by_mxd_vartype);
557 dict_reorder_vars (default_dict, v, nv);
563 static const struct fmt_spec fmt_tab[MXD_COUNT] =
574 first_continuous = -1;
575 for (i = 0; i < dict_get_var_cnt (default_dict); i++)
577 struct variable *v = dict_get_var (default_dict, i);
578 int type = v->p.mxd.vartype;
580 assert (type >= 0 && type < MXD_COUNT);
581 v->print = v->write = fmt_tab[type];
583 if (type == MXD_CONTINUOUS)
585 if (first_continuous == -1 && type == MXD_CONTINUOUS)
586 first_continuous = i;
590 if (n_continuous == 0)
592 msg (SE, _("No continuous variables specified."));
600 if (explicit_rowtype)
601 read_matrices_with_rowtype ();
603 read_matrices_without_rowtype ();
605 pool_destroy (container);
610 discard_variables ();
612 pool_destroy (container);
616 /* Look up string S as a content-type name and return the
617 corresponding enumerated value, or -1 if there is no match. If
618 COLLIDE is non-NULL then *COLLIDE returns a value (suitable for use
619 as a bit-index) which can be used for determining whether a related
620 statistic has already been used. */
622 string_to_content_type (char *s, int *collide)
633 {N_VECTOR, 0, "N_VECTOR"},
635 {N_SCALAR, 0, "N_SCALAR"},
636 {N_MATRIX, 1, "N_MATRIX"},
638 {STDDEV, 3, "STDDEV"},
650 for (tp = tab; tp->value != -1; tp++)
651 if (!strcmp (s, tp->string))
654 *collide = tp->collide;
661 /* Compare two variables using p.mxd.vartype and p.mxd.subtype
664 compare_variables_by_mxd_vartype (const void *a_, const void *b_)
666 struct variable *const *pa = a_;
667 struct variable *const *pb = b_;
668 const struct matrix_data_proc *a = &(*pa)->p.mxd;
669 const struct matrix_data_proc *b = &(*pb)->p.mxd;
671 if (a->vartype != b->vartype)
672 return a->vartype > b->vartype ? 1 : -1;
674 return a->subtype < b->subtype ? -1 : a->subtype > b->subtype;
678 /* Print out the command as input. */
682 printf ("MATRIX DATA\n\t/VARIABLES=");
687 for (i = 0; i < default_dict.nvar; i++)
688 printf ("%s ", default_dict.var[i]->name);
692 printf ("\t/FORMAT=");
695 else if (fmt == FREE)
699 if (section == LOWER)
701 else if (section == UPPER)
703 else if (section == FULL)
707 if (diag == DIAGONAL)
708 printf (" DIAGONAL\n");
709 else if (diag == NODIAGONAL)
710 printf (" NODIAGONAL\n");
714 if (dict_get_split_cnt (default_dict) != 0)
718 printf ("\t/SPLIT=");
719 for (i = 0; i < dict_get_split_cnt (default_dict); i++)
720 printf ("%s ", dict_get_split_vars (default_dict)[i]->name);
722 printf ("\t/* single split");
730 printf ("\t/FACTORS=");
731 for (i = 0; i < n_factors; i++)
732 printf ("%s ", factors[i]->name);
737 printf ("\t/CELLS=%d\n", cells);
740 printf ("\t/N=%d\n", pop_n);
747 printf ("\t/CONTENTS=");
748 for (i = 0; i < n_contents; i++)
750 if (contents[i] == LPAREN)
757 else if (contents[i] == RPAREN)
765 assert (contents[i] >= 0 && contents[i] <= PROX);
768 printf ("%s", content_names[contents[i]]);
775 #endif /* DEBUGGING */
777 /* Matrix tokenizer. */
779 /* Matrix token types. */
782 MNULL, /* No token. */
785 MSTOP /* End of file. */
788 /* Current matrix token. */
791 /* Token string if applicable; not null-terminated. */
792 static char *mtokstr;
794 /* Length of mtokstr in characters. */
797 /* Token value if applicable. */
798 static double mtokval;
800 static int mget_token (void);
803 #define mget_token() mget_token_dump()
806 mget_token_dump (void)
808 int result = (mget_token) ();
819 printf (" <NULLTOK>");
822 printf (" #%g", mtokval);
825 printf (" #'%.*s'", mtoklen, mtokstr);
837 /* Return the current position in the data file. */
843 char *p = dfm_get_record (data_file, &len);
846 strcpy (buf, "at end of line");
850 int n_copy = min (10, len);
851 cp = stpcpy (buf, "before `");
852 while (n_copy && isspace ((unsigned char) *p))
854 while (n_copy && !isspace ((unsigned char) *p))
855 *cp++ = *p++, n_copy--;
863 /* Is there at least one token left in the data file? */
875 cp = dfm_get_record (data_file, &len);
880 while (isspace ((unsigned char) *cp) && cp < ep)
886 dfm_fwd_record (data_file);
889 dfm_set_record (data_file, cp);
894 /* Parse a MATRIX DATA token from data_file into mtok*. */
904 cp = dfm_get_record (data_file, &len);
914 while (isspace ((unsigned char) *cp) && cp < ep)
920 dfm_fwd_record (data_file);
923 dfm_set_record (data_file, cp);
924 first_column = dfm_get_cur_col (data_file) + 1;
926 /* Three types of fields: quoted with ', quoted with ", unquoted. */
927 if (*cp == '\'' || *cp == '"')
933 while (cp < ep && *cp != quote)
935 mtoklen = cp - mtokstr;
939 msg (SW, _("Scope of string exceeds line."));
943 int is_num = isdigit ((unsigned char) *cp) || *cp == '.';
946 while (cp < ep && !isspace ((unsigned char) *cp) && *cp != ','
947 && *cp != '-' && *cp != '+')
949 if (isdigit ((unsigned char) *cp))
952 if ((tolower ((unsigned char) *cp) == 'd'
953 || tolower ((unsigned char) *cp) == 'e')
954 && (cp[1] == '+' || cp[1] == '-'))
960 mtoklen = cp - mtokstr;
968 di.e = mtokstr + mtoklen;
969 di.v = (union value *) &mtokval;
970 di.f1 = first_column;
971 di.format.type = FMT_F;
972 di.format.w = mtoklen;
982 dfm_set_record (data_file, cp);
987 /* Forcibly skip the end of a line for content type CONTENT in
990 force_eol (const char *content)
998 cp = dfm_get_record (data_file, &len);
1001 while (len && isspace (*cp))
1006 msg (SE, _("End of line expected %s while reading %s."),
1007 context (), content);
1011 dfm_fwd_record (data_file);
1016 /* Back end, omitting ROWTYPE_. */
1018 /* MATRIX DATA data. */
1019 static double ***nr_data;
1021 /* Factor values. */
1022 static double *nr_factor_values;
1024 /* Largest-numbered cell that we have read in thus far, plus one. */
1025 static int max_cell_index;
1027 /* SPLIT FILE variable values. */
1028 static double *split_values;
1030 static int nr_read_splits (int compare);
1031 static int nr_read_factors (int cell);
1032 static void nr_output_data (write_case_func *, write_case_data);
1033 static void matrix_data_read_without_rowtype (write_case_func *,
1036 /* Read from the data file and write it to the active file. */
1038 read_matrices_without_rowtype (void)
1044 split_values = xmalloc (sizeof *split_values
1045 * dict_get_split_cnt (default_dict));
1046 nr_factor_values = xmalloc (sizeof *nr_factor_values * n_factors * cells);
1049 matrix_data_source.read = matrix_data_read_without_rowtype;
1050 vfm_source = &matrix_data_source;
1052 procedure (NULL, NULL, NULL, NULL);
1054 free (split_values);
1055 free (nr_factor_values);
1057 fh_close_handle (data_file);
1060 /* Mirror data across the diagonal of matrix CP which contains
1061 CONTENT type data. */
1063 fill_matrix (int content, double *cp)
1065 int type = content_type[content];
1067 if (type == 1 && section != FULL)
1069 if (diag == NODIAGONAL)
1071 const double fill = content == CORR ? 1.0 : SYSMIS;
1074 for (i = 0; i < n_continuous; i++)
1075 cp[i * (1 + n_continuous)] = fill;
1081 if (section == LOWER)
1083 int n_lines = n_continuous;
1084 if (section != FULL && diag == NODIAGONAL)
1087 for (r = 1; r < n_lines; r++)
1088 for (c = 0; c < r; c++)
1089 cp[r + c * n_continuous] = cp[c + r * n_continuous];
1093 assert (section == UPPER);
1094 for (r = 1; r < n_continuous; r++)
1095 for (c = 0; c < r; c++)
1096 cp[c + r * n_continuous] = cp[r + c * n_continuous];
1104 for (c = 1; c < n_continuous; c++)
1109 /* Read data lines for content type CONTENT from the data file. If
1110 PER_FACTOR is nonzero, then factor information is read from the
1111 data file. Data is for cell number CELL. */
1113 nr_read_data_lines (int per_factor, int cell, int content, int compare)
1116 const int type = content_type[content];
1118 /* Number of lines that must be parsed from the data file for this
1122 /* Current position in vector or matrix. */
1132 n_lines = n_continuous;
1133 if (section != FULL && diag == NODIAGONAL)
1137 cp = nr_data[content][cell];
1138 if (type == 1 && section == LOWER && diag == NODIAGONAL)
1141 for (i = 0; i < n_lines; i++)
1145 if (!nr_read_splits (1))
1147 if (per_factor && !nr_read_factors (cell))
1154 n_cols = n_continuous;
1164 n_cols = n_continuous - i;
1165 if (diag == NODIAGONAL)
1172 n_cols = n_continuous;
1188 for (j = 0; j < n_cols; j++)
1194 msg (SE, _("expecting value for %s %s"),
1195 dict_get_var (default_dict, j)->name, context ());
1201 if (!force_eol (content_names[content]))
1203 debug_printf (("\n"));
1206 if (section == LOWER)
1207 cp += n_continuous - n_cols;
1210 fill_matrix (content, nr_data[content][cell]);
1215 /* When ROWTYPE_ does not appear in the data, reads the matrices and
1216 writes them to the output file. Returns success. */
1218 matrix_data_read_without_rowtype (write_case_func *write_case,
1219 write_case_data wc_data)
1224 nr_data = pool_alloc (container, (PROX + 1) * sizeof *nr_data);
1229 for (i = 0; i <= PROX; i++)
1233 for (cp = contents; *cp != EOC; cp++)
1234 if (*cp != LPAREN && *cp != RPAREN)
1236 int per_factor = is_per_factor[*cp];
1239 n_entries = n_continuous;
1240 if (content_type[*cp] == 1)
1241 n_entries *= n_continuous;
1244 int n_vectors = per_factor ? cells : 1;
1247 nr_data[*cp] = pool_alloc (container,
1248 n_vectors * sizeof **nr_data);
1250 for (i = 0; i < n_vectors; i++)
1251 nr_data[*cp][i] = pool_alloc (container,
1252 n_entries * sizeof ***nr_data);
1261 if (!nr_read_splits (0))
1264 for (bp = contents; *bp != EOC; bp = np)
1268 /* Trap the CONTENTS that we should parse in this pass
1269 between bp and ep. Set np to the starting bp for next
1274 while (*ep != RPAREN)
1282 while (*ep != EOC && *ep != LPAREN)
1291 for (i = 0; i < (per_factor ? cells : 1); i++)
1295 for (cp = bp; cp < ep; cp++)
1296 if (!nr_read_data_lines (per_factor, i, *cp, cp != bp))
1302 nr_output_data (write_case, wc_data);
1304 if (dict_get_split_cnt (default_dict) == 0 || !another_token ())
1309 /* Read the split file variables. If COMPARE is 1, compares the
1310 values read to the last values read and returns 1 if they're equal,
1313 nr_read_splits (int compare)
1315 static int just_read = 0;
1319 if (compare && just_read)
1325 if (dict_get_split_vars (default_dict) == NULL)
1332 = ++dict_get_split_vars (default_dict)[0]->p.mxd.subtype;
1339 split_cnt = dict_get_split_cnt (default_dict);
1340 for (i = 0; i < split_cnt; i++)
1346 msg (SE, _("Syntax error expecting SPLIT FILE value %s."),
1352 split_values[i] = mtokval;
1353 else if (split_values[i] != mtokval)
1355 msg (SE, _("Expecting value %g for %s."),
1356 split_values[i], dict_get_split_vars (default_dict)[i]->name);
1364 /* Read the factors for cell CELL. If COMPARE is 1, compares the
1365 values read to the last values read and returns 1 if they're equal,
1368 nr_read_factors (int cell)
1375 assert (max_cell_index >= cell);
1376 if (cell != max_cell_index)
1387 for (i = 0; i < n_factors; i++)
1393 msg (SE, _("Syntax error expecting factor value %s."),
1399 nr_factor_values[i + n_factors * cell] = mtokval;
1400 else if (nr_factor_values[i + n_factors * cell] != mtokval)
1402 msg (SE, _("Syntax error expecting value %g for %s %s."),
1403 nr_factor_values[i + n_factors * cell],
1404 factors[i]->name, context ());
1413 /* Write the contents of a cell having content type CONTENT and data
1414 CP to the active file. */
1416 dump_cell_content (int content, double *cp,
1417 write_case_func *write_case, write_case_data wc_data)
1419 int type = content_type[content];
1422 st_bare_pad_copy (temp_case->data[rowtype_->fv].s,
1423 content_names[content], 8);
1426 memset (&temp_case->data[varname_->fv].s, ' ', 8);
1430 int n_lines = (type == 1) ? n_continuous : 1;
1433 for (i = 0; i < n_lines; i++)
1437 for (j = 0; j < n_continuous; j++)
1439 int fv = dict_get_var (default_dict, first_continuous + j)->fv;
1440 temp_case->data[fv].f = *cp;
1444 st_bare_pad_copy (temp_case->data[varname_->fv].s,
1445 dict_get_var (default_dict,
1446 first_continuous + i)->name,
1448 write_case (wc_data);
1453 /* Finally dump out everything from nr_data[] to the output file. */
1455 nr_output_data (write_case_func *write_case, write_case_data wc_data)
1458 struct variable *const *split;
1462 split_cnt = dict_get_split_cnt (default_dict);
1463 for (i = 0; i < split_cnt; i++)
1464 temp_case->data[split[i]->fv].f = split_values[i];
1471 for (cell = 0; cell < cells; cell++)
1476 for (factor = 0; factor < n_factors; factor++)
1478 temp_case->data[factors[factor]->fv].f
1479 = nr_factor_values[factor + cell * n_factors];
1480 debug_printf (("f:%s ", factors[factor]->name));
1487 for (content = 0; content <= PROX; content++)
1488 if (is_per_factor[content])
1490 assert (nr_data[content] != NULL
1491 && nr_data[content][cell] != NULL);
1493 dump_cell_content (content, nr_data[content][cell],
1494 write_case, wc_data);
1506 for (factor = 0; factor < n_factors; factor++)
1507 temp_case->data[factors[factor]->fv].f = SYSMIS;
1510 for (content = 0; content <= PROX; content++)
1511 if (!is_per_factor[content] && nr_data[content] != NULL)
1512 dump_cell_content (content, nr_data[content][0],
1513 write_case, wc_data);
1517 /* Back end, with ROWTYPE_. */
1519 /* Type of current row. */
1520 static int wr_content;
1522 /* All the data for one set of factor values. */
1526 int n_rows[PROX + 1];
1527 double *data[PROX + 1];
1528 struct factor_data *next;
1531 /* All the data, period. */
1532 struct factor_data *wr_data;
1534 /* Current factor. */
1535 struct factor_data *wr_current;
1537 static int wr_read_splits (write_case_func *, write_case_data);
1538 static int wr_output_data (write_case_func *, write_case_data);
1539 static int wr_read_rowtype (void);
1540 static int wr_read_factors (void);
1541 static int wr_read_indeps (void);
1542 static void matrix_data_read_with_rowtype (write_case_func *,
1545 /* When ROWTYPE_ appears in the data, reads the matrices and writes
1546 them to the output file. */
1548 read_matrices_with_rowtype (void)
1551 wr_data = wr_current = NULL;
1552 split_values = NULL;
1555 matrix_data_source.read = matrix_data_read_with_rowtype;
1556 vfm_source = &matrix_data_source;
1558 procedure (NULL, NULL, NULL, NULL);
1560 free (split_values);
1561 fh_close_handle (data_file);
1564 /* Read from the data file and write it to the active file. */
1566 matrix_data_read_with_rowtype (write_case_func *write_case,
1567 write_case_data wc_data)
1571 if (!wr_read_splits (write_case, wc_data))
1574 if (!wr_read_factors ())
1577 if (!wr_read_indeps ())
1580 while (another_token ());
1582 wr_output_data (write_case, wc_data);
1585 /* Read the split file variables. If they differ from the previous
1586 set of split variables then output the data. Returns success. */
1588 wr_read_splits (write_case_func *write_case, write_case_data wc_data)
1593 split_cnt = dict_get_split_cnt (default_dict);
1602 split_values = xmalloc (split_cnt * sizeof *split_values);
1610 for (i = 0; i < split_cnt; i++)
1616 msg (SE, _("Syntax error %s expecting SPLIT FILE value."),
1621 if (compare && split_values[i] != mtokval && !different)
1623 if (!wr_output_data (write_case, wc_data))
1628 split_values[i] = mtokval;
1635 /* Compares doubles A and B, treating SYSMIS as greatest. */
1637 compare_doubles (const void *a_, const void *b_, void *aux UNUSED)
1639 const double *a = a_;
1640 const double *b = b_;
1644 else if (*a == SYSMIS)
1646 else if (*b == SYSMIS)
1654 /* Return strcmp()-type comparison of the n_factors factors at _A and
1655 _B. Sort missing values toward the end. */
1657 compare_factors (const void *a_, const void *b_)
1659 struct factor_data *const *pa = a_;
1660 struct factor_data *const *pb = b_;
1661 const double *a = (*pa)->factors;
1662 const double *b = (*pb)->factors;
1664 return lexicographical_compare (a, n_factors,
1667 compare_doubles, NULL);
1670 /* Write out the data for the current split file to the active
1673 wr_output_data (write_case_func *write_case, write_case_data wc_data)
1676 struct variable *const *split;
1680 split_cnt = dict_get_split_cnt (default_dict);
1681 for (i = 0; i < split_cnt; i++)
1682 temp_case->data[split[i]->fv].f = split_values[i];
1685 /* Sort the wr_data list. */
1687 struct factor_data **factors;
1688 struct factor_data *iter;
1691 factors = xmalloc (sizeof *factors * cells);
1693 for (i = 0, iter = wr_data; iter; iter = iter->next, i++)
1696 qsort (factors, cells, sizeof *factors, compare_factors);
1698 wr_data = factors[0];
1699 for (i = 0; i < cells - 1; i++)
1700 factors[i]->next = factors[i + 1];
1701 factors[cells - 1]->next = NULL;
1706 /* Write out records for every set of factor values. */
1708 struct factor_data *iter;
1710 for (iter = wr_data; iter; iter = iter->next)
1715 for (factor = 0; factor < n_factors; factor++)
1717 temp_case->data[factors[factor]->fv].f
1718 = iter->factors[factor];
1719 debug_printf (("f:%s ", factors[factor]->name));
1726 for (content = 0; content <= PROX; content++)
1728 if (!iter->n_rows[content])
1732 int type = content_type[content];
1733 int n_lines = (type == 1
1735 - (section != FULL && diag == NODIAGONAL))
1738 if (n_lines != iter->n_rows[content])
1740 msg (SE, _("Expected %d lines of data for %s content; "
1741 "actually saw %d lines. No data will be "
1742 "output for this content."),
1743 n_lines, content_names[content],
1744 iter->n_rows[content]);
1749 fill_matrix (content, iter->data[content]);
1751 dump_cell_content (content, iter->data[content],
1752 write_case, wc_data);
1758 pool_destroy (container);
1759 container = pool_create ();
1761 wr_data = wr_current = NULL;
1766 /* Read ROWTYPE_ from the data file. Return success. */
1768 wr_read_rowtype (void)
1770 if (wr_content != -1)
1772 msg (SE, _("Multiply specified ROWTYPE_ %s."), context ());
1777 msg (SE, _("Syntax error %s expecting ROWTYPE_ string."), context ());
1785 memcpy (s, mtokstr, min (15, mtoklen));
1786 s[min (15, mtoklen)] = 0;
1788 for (cp = s; *cp; cp++)
1789 *cp = toupper ((unsigned char) *cp);
1791 wr_content = string_to_content_type (s, NULL);
1794 if (wr_content == -1)
1796 msg (SE, _("Syntax error %s."), context ());
1803 /* Read the factors for the current row. Select a set of factors and
1804 point wr_current to it. */
1806 wr_read_factors (void)
1808 double *factor_values = local_alloc (sizeof *factor_values * n_factors);
1814 for (i = 0; i < n_factors; i++)
1820 if (!wr_read_rowtype ())
1827 msg (SE, _("Syntax error expecting factor value %s."),
1832 factor_values[i] = mtokval;
1835 if (wr_content == -1)
1839 if (!wr_read_rowtype ())
1843 /* Try the most recent factor first as a simple caching
1849 for (i = 0; i < n_factors; i++)
1850 if (factor_values[i] != wr_current->factors[i])
1855 /* Linear search through the list. */
1858 struct factor_data *iter;
1860 for (iter = wr_data; iter; iter = iter->next)
1864 for (i = 0; i < n_factors; i++)
1865 if (factor_values[i] != iter->factors[i])
1875 /* Not found. Make a new item. */
1877 struct factor_data *new = pool_alloc (container, sizeof *new);
1879 new->factors = pool_alloc (container, sizeof *new->factors * n_factors);
1884 for (i = 0; i < n_factors; i++)
1885 new->factors[i] = factor_values[i];
1891 for (i = 0; i <= PROX; i++)
1894 new->data[i] = NULL;
1898 new->next = wr_data;
1899 wr_data = wr_current = new;
1904 local_free (factor_values);
1908 local_free (factor_values);
1912 /* Read the independent variables into wr_current. */
1914 wr_read_indeps (void)
1916 struct factor_data *c = wr_current;
1917 const int type = content_type[wr_content];
1918 const int n_rows = c->n_rows[wr_content];
1922 /* Allocate room for data if necessary. */
1923 if (c->data[wr_content] == NULL)
1925 int n_items = n_continuous;
1927 n_items *= n_continuous;
1929 c->data[wr_content] = pool_alloc (container,
1930 sizeof **c->data * n_items);
1933 cp = &c->data[wr_content][n_rows * n_continuous];
1935 /* Figure out how much to read from this line. */
1942 msg (SE, _("Duplicate specification for %s."),
1943 content_names[wr_content]);
1947 n_cols = n_continuous;
1952 if (n_rows >= n_continuous - (section != FULL && diag == NODIAGONAL))
1954 msg (SE, _("Too many rows of matrix data for %s."),
1955 content_names[wr_content]);
1962 n_cols = n_rows + 1;
1963 if (diag == NODIAGONAL)
1968 n_cols = n_continuous - n_rows;
1969 if (diag == NODIAGONAL)
1976 n_cols = n_continuous;
1985 c->n_rows[wr_content]++;
1987 debug_printf ((" (c=%p,r=%d,n=%d)", c, n_rows + 1, n_cols));
1989 /* Read N_COLS items at CP. */
1993 for (j = 0; j < n_cols; j++)
1999 msg (SE, _("Syntax error expecting value for %s %s."),
2000 dict_get_var (default_dict, first_continuous + j)->name,
2007 if (!force_eol (content_names[wr_content]))
2009 debug_printf (("\n"));
2015 /* Matrix source. */
2017 struct case_stream matrix_data_source =