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_assert (default_dict, v[i], 0);
220 new_var->p.mxd.vartype = MXD_CONTINUOUS;
221 new_var->p.mxd.subtype = i;
224 explicit_rowtype = 1;
231 rowtype_ = dict_create_var_assert (default_dict, "ROWTYPE_", 8);
232 rowtype_->p.mxd.vartype = MXD_ROWTYPE;
233 rowtype_->p.mxd.subtype = 0;
236 else if (lex_match_id ("FILE"))
239 data_file = fh_parse_file_handle ();
243 else if (lex_match_id ("FORMAT"))
247 while (token == T_ID)
249 if (lex_match_id ("LIST"))
251 else if (lex_match_id ("FREE"))
253 else if (lex_match_id ("LOWER"))
255 else if (lex_match_id ("UPPER"))
257 else if (lex_match_id ("FULL"))
259 else if (lex_match_id ("DIAGONAL"))
261 else if (lex_match_id ("NODIAGONAL"))
265 lex_error (_("in FORMAT subcommand"));
270 else if (lex_match_id ("SPLIT"))
276 msg (SE, _("SPLIT subcommand multiply specified."));
283 lex_error (_("in SPLIT subcommand"));
287 if (dict_lookup_var (default_dict, tokid) == NULL
288 && (lex_look_ahead () == '.' || lex_look_ahead () == '/'))
290 if (!strcmp (tokid, "ROWTYPE_") || !strcmp (tokid, "VARNAME_"))
292 msg (SE, _("Split variable may not be named ROWTYPE_ "
297 single_split = dict_create_var_assert (default_dict, tokid, 0);
300 single_split->p.mxd.vartype = MXD_CONTINUOUS;
302 dict_set_split_vars (default_dict, &single_split, 1);
306 struct variable **split;
309 if (!parse_variables (default_dict, &split, &n, PV_NO_DUPLICATE))
312 dict_set_split_vars (default_dict, split, n);
316 struct variable *const *split = dict_get_split_vars (default_dict);
317 size_t split_cnt = dict_get_split_cnt (default_dict);
320 for (i = 0; i < split_cnt; i++)
322 if (split[i]->p.mxd.vartype != MXD_CONTINUOUS)
324 msg (SE, _("Split variable %s is already another type."),
328 split[i]->p.mxd.vartype = MXD_SPLIT;
329 split[i]->p.mxd.subtype = i;
333 else if (lex_match_id ("FACTORS"))
339 msg (SE, _("FACTORS subcommand multiply specified."));
344 if (!parse_variables (default_dict, &factors, &n_factors, PV_NONE))
350 for (i = 0; i < n_factors; i++)
352 if (factors[i]->p.mxd.vartype != MXD_CONTINUOUS)
354 msg (SE, _("Factor variable %s is already another type."),
358 factors[i]->p.mxd.vartype = MXD_FACTOR;
359 factors[i]->p.mxd.subtype = i;
363 else if (lex_match_id ("CELLS"))
369 msg (SE, _("CELLS subcommand multiply specified."));
373 if (!lex_integer_p () || lex_integer () < 1)
375 lex_error (_("expecting positive integer"));
379 cells = lex_integer ();
382 else if (lex_match_id ("N"))
388 msg (SE, _("N subcommand multiply specified."));
392 if (!lex_integer_p () || lex_integer () < 1)
394 lex_error (_("expecting positive integer"));
398 pop_n = lex_integer ();
401 else if (lex_match_id ("CONTENTS"))
403 int inside_parens = 0;
404 unsigned collide = 0;
409 msg (SE, _("CONTENTS subcommand multiply specified."));
419 for (i = 0; i <= PROX; i++)
420 is_per_factor[i] = 0;
429 msg (SE, _("Nested parentheses not allowed."));
435 else if (lex_match (')'))
439 msg (SE, _("Mismatched right parenthesis (`(')."));
442 if (contents[n_contents - 1] == LPAREN)
444 msg (SE, _("Empty parentheses not allowed."));
457 lex_error (_("in CONTENTS subcommand"));
461 content_type = string_to_content_type (tokid,
463 if (content_type == -1)
465 lex_error (_("in CONTENTS subcommand"));
470 if (collide & (1 << collide_index))
472 msg (SE, _("Content multiply specified for %s."),
473 content_names[content_type]);
476 collide |= (1 << collide_index);
479 is_per_factor[item] = inside_parens;
481 contents[n_contents++] = item;
483 if (token == '/' || token == '.')
489 msg (SE, _("Missing right parenthesis."));
492 contents[n_contents] = EOC;
503 lex_error (_("expecting end of command"));
509 msg (SE, _("Missing VARIABLES subcommand."));
513 if (!n_contents && !explicit_rowtype)
515 msg (SW, _("CONTENTS subcommand not specified: assuming file "
516 "contains only CORR matrix."));
523 if (n_factors && !explicit_rowtype && cells == -1)
525 msg (SE, _("Missing CELLS subcommand. CELLS is required "
526 "when ROWTYPE_ is not given in the data and "
527 "factors are present."));
531 if (explicit_rowtype && single_split)
533 msg (SE, _("Split file values must be present in the data when "
534 "ROWTYPE_ is present."));
538 /* Create VARNAME_. */
540 varname_ = dict_create_var_assert (default_dict, "VARNAME_", 8);
541 varname_->p.mxd.vartype = MXD_VARNAME;
542 varname_->p.mxd.subtype = 0;
545 /* Sort the dictionary variables into the desired order for the
546 system file output. */
551 dict_get_vars (default_dict, &v, &nv, 0);
552 qsort (v, nv, sizeof *v, compare_variables_by_mxd_vartype);
553 dict_reorder_vars (default_dict, v, nv);
559 static const struct fmt_spec fmt_tab[MXD_COUNT] =
570 first_continuous = -1;
571 for (i = 0; i < dict_get_var_cnt (default_dict); i++)
573 struct variable *v = dict_get_var (default_dict, i);
574 int type = v->p.mxd.vartype;
576 assert (type >= 0 && type < MXD_COUNT);
577 v->print = v->write = fmt_tab[type];
579 if (type == MXD_CONTINUOUS)
581 if (first_continuous == -1 && type == MXD_CONTINUOUS)
582 first_continuous = i;
586 if (n_continuous == 0)
588 msg (SE, _("No continuous variables specified."));
596 if (explicit_rowtype)
597 read_matrices_with_rowtype ();
599 read_matrices_without_rowtype ();
601 pool_destroy (container);
606 discard_variables ();
608 pool_destroy (container);
612 /* Look up string S as a content-type name and return the
613 corresponding enumerated value, or -1 if there is no match. If
614 COLLIDE is non-NULL then *COLLIDE returns a value (suitable for use
615 as a bit-index) which can be used for determining whether a related
616 statistic has already been used. */
618 string_to_content_type (char *s, int *collide)
629 {N_VECTOR, 0, "N_VECTOR"},
631 {N_SCALAR, 0, "N_SCALAR"},
632 {N_MATRIX, 1, "N_MATRIX"},
634 {STDDEV, 3, "STDDEV"},
646 for (tp = tab; tp->value != -1; tp++)
647 if (!strcmp (s, tp->string))
650 *collide = tp->collide;
657 /* Compare two variables using p.mxd.vartype and p.mxd.subtype
660 compare_variables_by_mxd_vartype (const void *a_, const void *b_)
662 struct variable *const *pa = a_;
663 struct variable *const *pb = b_;
664 const struct matrix_data_proc *a = &(*pa)->p.mxd;
665 const struct matrix_data_proc *b = &(*pb)->p.mxd;
667 if (a->vartype != b->vartype)
668 return a->vartype > b->vartype ? 1 : -1;
670 return a->subtype < b->subtype ? -1 : a->subtype > b->subtype;
674 /* Print out the command as input. */
678 printf ("MATRIX DATA\n\t/VARIABLES=");
683 for (i = 0; i < default_dict.nvar; i++)
684 printf ("%s ", default_dict.var[i]->name);
688 printf ("\t/FORMAT=");
691 else if (fmt == FREE)
695 if (section == LOWER)
697 else if (section == UPPER)
699 else if (section == FULL)
703 if (diag == DIAGONAL)
704 printf (" DIAGONAL\n");
705 else if (diag == NODIAGONAL)
706 printf (" NODIAGONAL\n");
710 if (dict_get_split_cnt (default_dict) != 0)
714 printf ("\t/SPLIT=");
715 for (i = 0; i < dict_get_split_cnt (default_dict); i++)
716 printf ("%s ", dict_get_split_vars (default_dict)[i]->name);
718 printf ("\t/* single split");
726 printf ("\t/FACTORS=");
727 for (i = 0; i < n_factors; i++)
728 printf ("%s ", factors[i]->name);
733 printf ("\t/CELLS=%d\n", cells);
736 printf ("\t/N=%d\n", pop_n);
743 printf ("\t/CONTENTS=");
744 for (i = 0; i < n_contents; i++)
746 if (contents[i] == LPAREN)
753 else if (contents[i] == RPAREN)
761 assert (contents[i] >= 0 && contents[i] <= PROX);
764 printf ("%s", content_names[contents[i]]);
771 #endif /* DEBUGGING */
773 /* Matrix tokenizer. */
775 /* Matrix token types. */
778 MNULL, /* No token. */
781 MSTOP /* End of file. */
784 /* Current matrix token. */
787 /* Token string if applicable; not null-terminated. */
788 static char *mtokstr;
790 /* Length of mtokstr in characters. */
793 /* Token value if applicable. */
794 static double mtokval;
796 static int mget_token (void);
799 #define mget_token() mget_token_dump()
802 mget_token_dump (void)
804 int result = (mget_token) ();
815 printf (" <NULLTOK>");
818 printf (" #%g", mtokval);
821 printf (" #'%.*s'", mtoklen, mtokstr);
833 /* Return the current position in the data file. */
839 char *p = dfm_get_record (data_file, &len);
842 strcpy (buf, "at end of line");
846 int n_copy = min (10, len);
847 cp = stpcpy (buf, "before `");
848 while (n_copy && isspace ((unsigned char) *p))
850 while (n_copy && !isspace ((unsigned char) *p))
851 *cp++ = *p++, n_copy--;
859 /* Is there at least one token left in the data file? */
871 cp = dfm_get_record (data_file, &len);
876 while (isspace ((unsigned char) *cp) && cp < ep)
882 dfm_fwd_record (data_file);
885 dfm_set_record (data_file, cp);
890 /* Parse a MATRIX DATA token from data_file into mtok*. */
900 cp = dfm_get_record (data_file, &len);
910 while (isspace ((unsigned char) *cp) && cp < ep)
916 dfm_fwd_record (data_file);
919 dfm_set_record (data_file, cp);
920 first_column = dfm_get_cur_col (data_file) + 1;
922 /* Three types of fields: quoted with ', quoted with ", unquoted. */
923 if (*cp == '\'' || *cp == '"')
929 while (cp < ep && *cp != quote)
931 mtoklen = cp - mtokstr;
935 msg (SW, _("Scope of string exceeds line."));
939 int is_num = isdigit ((unsigned char) *cp) || *cp == '.';
942 while (cp < ep && !isspace ((unsigned char) *cp) && *cp != ','
943 && *cp != '-' && *cp != '+')
945 if (isdigit ((unsigned char) *cp))
948 if ((tolower ((unsigned char) *cp) == 'd'
949 || tolower ((unsigned char) *cp) == 'e')
950 && (cp[1] == '+' || cp[1] == '-'))
956 mtoklen = cp - mtokstr;
964 di.e = mtokstr + mtoklen;
965 di.v = (union value *) &mtokval;
966 di.f1 = first_column;
967 di.format.type = FMT_F;
968 di.format.w = mtoklen;
978 dfm_set_record (data_file, cp);
983 /* Forcibly skip the end of a line for content type CONTENT in
986 force_eol (const char *content)
994 cp = dfm_get_record (data_file, &len);
997 while (len && isspace (*cp))
1002 msg (SE, _("End of line expected %s while reading %s."),
1003 context (), content);
1007 dfm_fwd_record (data_file);
1012 /* Back end, omitting ROWTYPE_. */
1014 /* MATRIX DATA data. */
1015 static double ***nr_data;
1017 /* Factor values. */
1018 static double *nr_factor_values;
1020 /* Largest-numbered cell that we have read in thus far, plus one. */
1021 static int max_cell_index;
1023 /* SPLIT FILE variable values. */
1024 static double *split_values;
1026 static int nr_read_splits (int compare);
1027 static int nr_read_factors (int cell);
1028 static void nr_output_data (write_case_func *, write_case_data);
1029 static void matrix_data_read_without_rowtype (write_case_func *,
1032 /* Read from the data file and write it to the active file. */
1034 read_matrices_without_rowtype (void)
1040 split_values = xmalloc (sizeof *split_values
1041 * dict_get_split_cnt (default_dict));
1042 nr_factor_values = xmalloc (sizeof *nr_factor_values * n_factors * cells);
1045 matrix_data_source.read = matrix_data_read_without_rowtype;
1046 vfm_source = &matrix_data_source;
1048 procedure (NULL, NULL, NULL, NULL);
1050 free (split_values);
1051 free (nr_factor_values);
1053 fh_close_handle (data_file);
1056 /* Mirror data across the diagonal of matrix CP which contains
1057 CONTENT type data. */
1059 fill_matrix (int content, double *cp)
1061 int type = content_type[content];
1063 if (type == 1 && section != FULL)
1065 if (diag == NODIAGONAL)
1067 const double fill = content == CORR ? 1.0 : SYSMIS;
1070 for (i = 0; i < n_continuous; i++)
1071 cp[i * (1 + n_continuous)] = fill;
1077 if (section == LOWER)
1079 int n_lines = n_continuous;
1080 if (section != FULL && diag == NODIAGONAL)
1083 for (r = 1; r < n_lines; r++)
1084 for (c = 0; c < r; c++)
1085 cp[r + c * n_continuous] = cp[c + r * n_continuous];
1089 assert (section == UPPER);
1090 for (r = 1; r < n_continuous; r++)
1091 for (c = 0; c < r; c++)
1092 cp[c + r * n_continuous] = cp[r + c * n_continuous];
1100 for (c = 1; c < n_continuous; c++)
1105 /* Read data lines for content type CONTENT from the data file. If
1106 PER_FACTOR is nonzero, then factor information is read from the
1107 data file. Data is for cell number CELL. */
1109 nr_read_data_lines (int per_factor, int cell, int content, int compare)
1112 const int type = content_type[content];
1114 /* Number of lines that must be parsed from the data file for this
1118 /* Current position in vector or matrix. */
1128 n_lines = n_continuous;
1129 if (section != FULL && diag == NODIAGONAL)
1133 cp = nr_data[content][cell];
1134 if (type == 1 && section == LOWER && diag == NODIAGONAL)
1137 for (i = 0; i < n_lines; i++)
1141 if (!nr_read_splits (1))
1143 if (per_factor && !nr_read_factors (cell))
1150 n_cols = n_continuous;
1160 n_cols = n_continuous - i;
1161 if (diag == NODIAGONAL)
1168 n_cols = n_continuous;
1184 for (j = 0; j < n_cols; j++)
1190 msg (SE, _("expecting value for %s %s"),
1191 dict_get_var (default_dict, j)->name, context ());
1197 if (!force_eol (content_names[content]))
1199 debug_printf (("\n"));
1202 if (section == LOWER)
1203 cp += n_continuous - n_cols;
1206 fill_matrix (content, nr_data[content][cell]);
1211 /* When ROWTYPE_ does not appear in the data, reads the matrices and
1212 writes them to the output file. Returns success. */
1214 matrix_data_read_without_rowtype (write_case_func *write_case,
1215 write_case_data wc_data)
1220 nr_data = pool_alloc (container, (PROX + 1) * sizeof *nr_data);
1225 for (i = 0; i <= PROX; i++)
1229 for (cp = contents; *cp != EOC; cp++)
1230 if (*cp != LPAREN && *cp != RPAREN)
1232 int per_factor = is_per_factor[*cp];
1235 n_entries = n_continuous;
1236 if (content_type[*cp] == 1)
1237 n_entries *= n_continuous;
1240 int n_vectors = per_factor ? cells : 1;
1243 nr_data[*cp] = pool_alloc (container,
1244 n_vectors * sizeof **nr_data);
1246 for (i = 0; i < n_vectors; i++)
1247 nr_data[*cp][i] = pool_alloc (container,
1248 n_entries * sizeof ***nr_data);
1257 if (!nr_read_splits (0))
1260 for (bp = contents; *bp != EOC; bp = np)
1264 /* Trap the CONTENTS that we should parse in this pass
1265 between bp and ep. Set np to the starting bp for next
1270 while (*ep != RPAREN)
1278 while (*ep != EOC && *ep != LPAREN)
1287 for (i = 0; i < (per_factor ? cells : 1); i++)
1291 for (cp = bp; cp < ep; cp++)
1292 if (!nr_read_data_lines (per_factor, i, *cp, cp != bp))
1298 nr_output_data (write_case, wc_data);
1300 if (dict_get_split_cnt (default_dict) == 0 || !another_token ())
1305 /* Read the split file variables. If COMPARE is 1, compares the
1306 values read to the last values read and returns 1 if they're equal,
1309 nr_read_splits (int compare)
1311 static int just_read = 0;
1315 if (compare && just_read)
1321 if (dict_get_split_vars (default_dict) == NULL)
1328 = ++dict_get_split_vars (default_dict)[0]->p.mxd.subtype;
1335 split_cnt = dict_get_split_cnt (default_dict);
1336 for (i = 0; i < split_cnt; i++)
1342 msg (SE, _("Syntax error expecting SPLIT FILE value %s."),
1348 split_values[i] = mtokval;
1349 else if (split_values[i] != mtokval)
1351 msg (SE, _("Expecting value %g for %s."),
1352 split_values[i], dict_get_split_vars (default_dict)[i]->name);
1360 /* Read the factors for cell CELL. If COMPARE is 1, compares the
1361 values read to the last values read and returns 1 if they're equal,
1364 nr_read_factors (int cell)
1371 assert (max_cell_index >= cell);
1372 if (cell != max_cell_index)
1383 for (i = 0; i < n_factors; i++)
1389 msg (SE, _("Syntax error expecting factor value %s."),
1395 nr_factor_values[i + n_factors * cell] = mtokval;
1396 else if (nr_factor_values[i + n_factors * cell] != mtokval)
1398 msg (SE, _("Syntax error expecting value %g for %s %s."),
1399 nr_factor_values[i + n_factors * cell],
1400 factors[i]->name, context ());
1409 /* Write the contents of a cell having content type CONTENT and data
1410 CP to the active file. */
1412 dump_cell_content (int content, double *cp,
1413 write_case_func *write_case, write_case_data wc_data)
1415 int type = content_type[content];
1418 st_bare_pad_copy (temp_case->data[rowtype_->fv].s,
1419 content_names[content], 8);
1422 memset (&temp_case->data[varname_->fv].s, ' ', 8);
1426 int n_lines = (type == 1) ? n_continuous : 1;
1429 for (i = 0; i < n_lines; i++)
1433 for (j = 0; j < n_continuous; j++)
1435 int fv = dict_get_var (default_dict, first_continuous + j)->fv;
1436 temp_case->data[fv].f = *cp;
1440 st_bare_pad_copy (temp_case->data[varname_->fv].s,
1441 dict_get_var (default_dict,
1442 first_continuous + i)->name,
1444 write_case (wc_data);
1449 /* Finally dump out everything from nr_data[] to the output file. */
1451 nr_output_data (write_case_func *write_case, write_case_data wc_data)
1454 struct variable *const *split;
1458 split_cnt = dict_get_split_cnt (default_dict);
1459 for (i = 0; i < split_cnt; i++)
1460 temp_case->data[split[i]->fv].f = split_values[i];
1467 for (cell = 0; cell < cells; cell++)
1472 for (factor = 0; factor < n_factors; factor++)
1474 temp_case->data[factors[factor]->fv].f
1475 = nr_factor_values[factor + cell * n_factors];
1476 debug_printf (("f:%s ", factors[factor]->name));
1483 for (content = 0; content <= PROX; content++)
1484 if (is_per_factor[content])
1486 assert (nr_data[content] != NULL
1487 && nr_data[content][cell] != NULL);
1489 dump_cell_content (content, nr_data[content][cell],
1490 write_case, wc_data);
1502 for (factor = 0; factor < n_factors; factor++)
1503 temp_case->data[factors[factor]->fv].f = SYSMIS;
1506 for (content = 0; content <= PROX; content++)
1507 if (!is_per_factor[content] && nr_data[content] != NULL)
1508 dump_cell_content (content, nr_data[content][0],
1509 write_case, wc_data);
1513 /* Back end, with ROWTYPE_. */
1515 /* Type of current row. */
1516 static int wr_content;
1518 /* All the data for one set of factor values. */
1522 int n_rows[PROX + 1];
1523 double *data[PROX + 1];
1524 struct factor_data *next;
1527 /* All the data, period. */
1528 struct factor_data *wr_data;
1530 /* Current factor. */
1531 struct factor_data *wr_current;
1533 static int wr_read_splits (write_case_func *, write_case_data);
1534 static int wr_output_data (write_case_func *, write_case_data);
1535 static int wr_read_rowtype (void);
1536 static int wr_read_factors (void);
1537 static int wr_read_indeps (void);
1538 static void matrix_data_read_with_rowtype (write_case_func *,
1541 /* When ROWTYPE_ appears in the data, reads the matrices and writes
1542 them to the output file. */
1544 read_matrices_with_rowtype (void)
1547 wr_data = wr_current = NULL;
1548 split_values = NULL;
1551 matrix_data_source.read = matrix_data_read_with_rowtype;
1552 vfm_source = &matrix_data_source;
1554 procedure (NULL, NULL, NULL, NULL);
1556 free (split_values);
1557 fh_close_handle (data_file);
1560 /* Read from the data file and write it to the active file. */
1562 matrix_data_read_with_rowtype (write_case_func *write_case,
1563 write_case_data wc_data)
1567 if (!wr_read_splits (write_case, wc_data))
1570 if (!wr_read_factors ())
1573 if (!wr_read_indeps ())
1576 while (another_token ());
1578 wr_output_data (write_case, wc_data);
1581 /* Read the split file variables. If they differ from the previous
1582 set of split variables then output the data. Returns success. */
1584 wr_read_splits (write_case_func *write_case, write_case_data wc_data)
1589 split_cnt = dict_get_split_cnt (default_dict);
1598 split_values = xmalloc (split_cnt * sizeof *split_values);
1606 for (i = 0; i < split_cnt; i++)
1612 msg (SE, _("Syntax error %s expecting SPLIT FILE value."),
1617 if (compare && split_values[i] != mtokval && !different)
1619 if (!wr_output_data (write_case, wc_data))
1624 split_values[i] = mtokval;
1631 /* Compares doubles A and B, treating SYSMIS as greatest. */
1633 compare_doubles (const void *a_, const void *b_, void *aux UNUSED)
1635 const double *a = a_;
1636 const double *b = b_;
1640 else if (*a == SYSMIS)
1642 else if (*b == SYSMIS)
1650 /* Return strcmp()-type comparison of the n_factors factors at _A and
1651 _B. Sort missing values toward the end. */
1653 compare_factors (const void *a_, const void *b_)
1655 struct factor_data *const *pa = a_;
1656 struct factor_data *const *pb = b_;
1657 const double *a = (*pa)->factors;
1658 const double *b = (*pb)->factors;
1660 return lexicographical_compare (a, n_factors,
1663 compare_doubles, NULL);
1666 /* Write out the data for the current split file to the active
1669 wr_output_data (write_case_func *write_case, write_case_data wc_data)
1672 struct variable *const *split;
1676 split_cnt = dict_get_split_cnt (default_dict);
1677 for (i = 0; i < split_cnt; i++)
1678 temp_case->data[split[i]->fv].f = split_values[i];
1681 /* Sort the wr_data list. */
1683 struct factor_data **factors;
1684 struct factor_data *iter;
1687 factors = xmalloc (sizeof *factors * cells);
1689 for (i = 0, iter = wr_data; iter; iter = iter->next, i++)
1692 qsort (factors, cells, sizeof *factors, compare_factors);
1694 wr_data = factors[0];
1695 for (i = 0; i < cells - 1; i++)
1696 factors[i]->next = factors[i + 1];
1697 factors[cells - 1]->next = NULL;
1702 /* Write out records for every set of factor values. */
1704 struct factor_data *iter;
1706 for (iter = wr_data; iter; iter = iter->next)
1711 for (factor = 0; factor < n_factors; factor++)
1713 temp_case->data[factors[factor]->fv].f
1714 = iter->factors[factor];
1715 debug_printf (("f:%s ", factors[factor]->name));
1722 for (content = 0; content <= PROX; content++)
1724 if (!iter->n_rows[content])
1728 int type = content_type[content];
1729 int n_lines = (type == 1
1731 - (section != FULL && diag == NODIAGONAL))
1734 if (n_lines != iter->n_rows[content])
1736 msg (SE, _("Expected %d lines of data for %s content; "
1737 "actually saw %d lines. No data will be "
1738 "output for this content."),
1739 n_lines, content_names[content],
1740 iter->n_rows[content]);
1745 fill_matrix (content, iter->data[content]);
1747 dump_cell_content (content, iter->data[content],
1748 write_case, wc_data);
1754 pool_destroy (container);
1755 container = pool_create ();
1757 wr_data = wr_current = NULL;
1762 /* Read ROWTYPE_ from the data file. Return success. */
1764 wr_read_rowtype (void)
1766 if (wr_content != -1)
1768 msg (SE, _("Multiply specified ROWTYPE_ %s."), context ());
1773 msg (SE, _("Syntax error %s expecting ROWTYPE_ string."), context ());
1781 memcpy (s, mtokstr, min (15, mtoklen));
1782 s[min (15, mtoklen)] = 0;
1784 for (cp = s; *cp; cp++)
1785 *cp = toupper ((unsigned char) *cp);
1787 wr_content = string_to_content_type (s, NULL);
1790 if (wr_content == -1)
1792 msg (SE, _("Syntax error %s."), context ());
1799 /* Read the factors for the current row. Select a set of factors and
1800 point wr_current to it. */
1802 wr_read_factors (void)
1804 double *factor_values = local_alloc (sizeof *factor_values * n_factors);
1810 for (i = 0; i < n_factors; i++)
1816 if (!wr_read_rowtype ())
1823 msg (SE, _("Syntax error expecting factor value %s."),
1828 factor_values[i] = mtokval;
1831 if (wr_content == -1)
1835 if (!wr_read_rowtype ())
1839 /* Try the most recent factor first as a simple caching
1845 for (i = 0; i < n_factors; i++)
1846 if (factor_values[i] != wr_current->factors[i])
1851 /* Linear search through the list. */
1854 struct factor_data *iter;
1856 for (iter = wr_data; iter; iter = iter->next)
1860 for (i = 0; i < n_factors; i++)
1861 if (factor_values[i] != iter->factors[i])
1871 /* Not found. Make a new item. */
1873 struct factor_data *new = pool_alloc (container, sizeof *new);
1875 new->factors = pool_alloc (container, sizeof *new->factors * n_factors);
1880 for (i = 0; i < n_factors; i++)
1881 new->factors[i] = factor_values[i];
1887 for (i = 0; i <= PROX; i++)
1890 new->data[i] = NULL;
1894 new->next = wr_data;
1895 wr_data = wr_current = new;
1900 local_free (factor_values);
1904 local_free (factor_values);
1908 /* Read the independent variables into wr_current. */
1910 wr_read_indeps (void)
1912 struct factor_data *c = wr_current;
1913 const int type = content_type[wr_content];
1914 const int n_rows = c->n_rows[wr_content];
1918 /* Allocate room for data if necessary. */
1919 if (c->data[wr_content] == NULL)
1921 int n_items = n_continuous;
1923 n_items *= n_continuous;
1925 c->data[wr_content] = pool_alloc (container,
1926 sizeof **c->data * n_items);
1929 cp = &c->data[wr_content][n_rows * n_continuous];
1931 /* Figure out how much to read from this line. */
1938 msg (SE, _("Duplicate specification for %s."),
1939 content_names[wr_content]);
1943 n_cols = n_continuous;
1948 if (n_rows >= n_continuous - (section != FULL && diag == NODIAGONAL))
1950 msg (SE, _("Too many rows of matrix data for %s."),
1951 content_names[wr_content]);
1958 n_cols = n_rows + 1;
1959 if (diag == NODIAGONAL)
1964 n_cols = n_continuous - n_rows;
1965 if (diag == NODIAGONAL)
1972 n_cols = n_continuous;
1981 c->n_rows[wr_content]++;
1983 debug_printf ((" (c=%p,r=%d,n=%d)", c, n_rows + 1, n_cols));
1985 /* Read N_COLS items at CP. */
1989 for (j = 0; j < n_cols; j++)
1995 msg (SE, _("Syntax error expecting value for %s %s."),
1996 dict_get_var (default_dict, first_continuous + j)->name,
2003 if (!force_eol (content_names[wr_content]))
2005 debug_printf (("\n"));
2011 /* Matrix source. */
2013 struct case_stream matrix_data_source =