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
20 /* AIX requires this to be the first thing in the file. */
23 #define alloca __builtin_alloca
31 #ifndef alloca /* predefined by HP cc +Olibcalls */
42 #include "algorithm.h"
48 #include "file-handle.h"
56 #include "debug-print.h"
58 /* FIXME: /N subcommand not implemented. It should be pretty simple,
61 /* Format type enums. */
68 /* Matrix section enums. */
76 /* Diagonal inclusion enums. */
104 /* 0=vector, 1=matrix, 2=scalar. */
105 static int content_type[PROX + 1] =
107 0, 2, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1,
110 /* Name of each content type. */
111 static const char *content_names[PROX + 1] =
113 "N", "N", "N_MATRIX", "MEAN", "STDDEV", "COUNT", "MSE",
114 "DFE", "MAT", "COV", "CORR", "PROX",
117 /* The data file to be read. */
118 static struct file_handle *data_file;
121 static int fmt; /* LIST or FREE. */
122 static int section; /* LOWER or UPPER or FULL. */
123 static int diag; /* DIAGONAL or NODIAGONAL. */
125 /* Arena used for all the MATRIX DATA allocations. */
126 static struct pool *container;
128 /* ROWTYPE_ specified explicitly in data? */
129 static int explicit_rowtype;
131 /* ROWTYPE_, VARNAME_ variables. */
132 static struct variable *rowtype_, *varname_;
134 /* Is is per-factor data? */
135 int is_per_factor[PROX + 1];
137 /* Single SPLIT FILE variable. */
138 static struct variable *single_split;
140 /* Factor variables. */
141 static int n_factors;
142 static struct variable **factors;
144 /* Number of cells, or -1 if none. */
147 /* Population N specified by user. */
150 /* CONTENTS subcommand. */
151 static int contents[EOC * 3 + 1];
152 static int n_contents;
154 /* Number of continuous variables. */
155 static int n_continuous;
157 /* Index into default_dict.var of first continuous variables. */
158 static int first_continuous;
160 static int compare_variables_by_mxd_vartype (const void *pa,
162 static void read_matrices_without_rowtype (void);
163 static void read_matrices_with_rowtype (void);
164 static int string_to_content_type (char *, int *);
167 static void debug_print (void);
171 cmd_matrix_data (void)
175 lex_match_id ("MATRIX");
176 lex_match_id ("DATA");
178 container = pool_create ();
180 discard_variables ();
182 data_file = inline_file;
196 if (lex_match_id ("VARIABLES"))
203 msg (SE, _("VARIABLES subcommand multiply specified."));
209 if (!parse_DATA_LIST_vars (&v, &nv, PV_NO_DUPLICATE))
215 for (i = 0; i < nv; i++)
216 if (!strcmp (v[i], "VARNAME_"))
218 msg (SE, _("VARNAME_ cannot be explicitly specified on "
220 for (i = 0; i < nv; i++)
230 for (i = 0; i < nv; i++)
232 struct variable *new_var;
234 if (strcmp (v[i], "ROWTYPE_"))
236 new_var = dict_create_var (default_dict, v[i], 0);
237 assert (new_var != NULL);
238 new_var->p.mxd.vartype = MXD_CONTINUOUS;
239 new_var->p.mxd.subtype = i;
242 explicit_rowtype = 1;
249 rowtype_ = dict_create_var (default_dict, "ROWTYPE_", 8);
250 assert (rowtype_ != NULL);
251 rowtype_->p.mxd.vartype = MXD_ROWTYPE;
252 rowtype_->p.mxd.subtype = 0;
255 else if (lex_match_id ("FILE"))
258 data_file = fh_parse_file_handle ();
262 else if (lex_match_id ("FORMAT"))
266 while (token == T_ID)
268 if (lex_match_id ("LIST"))
270 else if (lex_match_id ("FREE"))
272 else if (lex_match_id ("LOWER"))
274 else if (lex_match_id ("UPPER"))
276 else if (lex_match_id ("FULL"))
278 else if (lex_match_id ("DIAGONAL"))
280 else if (lex_match_id ("NODIAGONAL"))
284 lex_error (_("in FORMAT subcommand"));
289 else if (lex_match_id ("SPLIT"))
295 msg (SE, _("SPLIT subcommand multiply specified."));
302 lex_error (_("in SPLIT subcommand"));
306 if (dict_lookup_var (default_dict, tokid) == NULL
307 && (lex_look_ahead () == '.' || lex_look_ahead () == '/'))
309 if (!strcmp (tokid, "ROWTYPE_") || !strcmp (tokid, "VARNAME_"))
311 msg (SE, _("Split variable may not be named ROWTYPE_ "
316 single_split = dict_create_var (default_dict, tokid, 0);
317 assert (single_split != NULL);
320 single_split->p.mxd.vartype = MXD_CONTINUOUS;
322 dict_set_split_vars (default_dict, &single_split, 1);
326 struct variable **split;
329 if (!parse_variables (default_dict, &split, &n, PV_NO_DUPLICATE))
332 dict_set_split_vars (default_dict, split, n);
336 struct variable *const *split = dict_get_split_vars (default_dict);
337 size_t split_cnt = dict_get_split_cnt (default_dict);
340 for (i = 0; i < split_cnt; i++)
342 if (split[i]->p.mxd.vartype != MXD_CONTINUOUS)
344 msg (SE, _("Split variable %s is already another type."),
348 split[i]->p.mxd.vartype = MXD_SPLIT;
349 split[i]->p.mxd.subtype = i;
353 else if (lex_match_id ("FACTORS"))
359 msg (SE, _("FACTORS subcommand multiply specified."));
364 if (!parse_variables (default_dict, &factors, &n_factors, PV_NONE))
370 for (i = 0; i < n_factors; i++)
372 if (factors[i]->p.mxd.vartype != MXD_CONTINUOUS)
374 msg (SE, _("Factor variable %s is already another type."),
378 factors[i]->p.mxd.vartype = MXD_FACTOR;
379 factors[i]->p.mxd.subtype = i;
383 else if (lex_match_id ("CELLS"))
389 msg (SE, _("CELLS subcommand multiply specified."));
393 if (!lex_integer_p () || lex_integer () < 1)
395 lex_error (_("expecting positive integer"));
399 cells = lex_integer ();
402 else if (lex_match_id ("N"))
408 msg (SE, _("N subcommand multiply specified."));
412 if (!lex_integer_p () || lex_integer () < 1)
414 lex_error (_("expecting positive integer"));
418 pop_n = lex_integer ();
421 else if (lex_match_id ("CONTENTS"))
423 int inside_parens = 0;
424 unsigned collide = 0;
429 msg (SE, _("CONTENTS subcommand multiply specified."));
439 for (i = 0; i <= PROX; i++)
440 is_per_factor[i] = 0;
449 msg (SE, _("Nested parentheses not allowed."));
455 else if (lex_match (')'))
459 msg (SE, _("Mismatched right parenthesis (`(')."));
462 if (contents[n_contents - 1] == LPAREN)
464 msg (SE, _("Empty parentheses not allowed."));
477 lex_error (_("in CONTENTS subcommand"));
481 content_type = string_to_content_type (tokid,
483 if (content_type == -1)
485 lex_error (_("in CONTENTS subcommand"));
490 if (collide & (1 << collide_index))
492 msg (SE, _("Content multiply specified for %s."),
493 content_names[content_type]);
496 collide |= (1 << collide_index);
499 is_per_factor[item] = inside_parens;
501 contents[n_contents++] = item;
503 if (token == '/' || token == '.')
509 msg (SE, _("Missing right parenthesis."));
512 contents[n_contents] = EOC;
523 lex_error (_("expecting end of command"));
529 msg (SE, _("Missing VARIABLES subcommand."));
533 if (!n_contents && !explicit_rowtype)
535 msg (SW, _("CONTENTS subcommand not specified: assuming file "
536 "contains only CORR matrix."));
543 if (n_factors && !explicit_rowtype && cells == -1)
545 msg (SE, _("Missing CELLS subcommand. CELLS is required "
546 "when ROWTYPE_ is not given in the data and "
547 "factors are present."));
551 if (explicit_rowtype && single_split)
553 msg (SE, _("Split file values must be present in the data when "
554 "ROWTYPE_ is present."));
558 /* Create VARNAME_. */
560 varname_ = dict_create_var (default_dict, "VARNAME_", 8);
561 assert (varname_ != NULL);
562 varname_->p.mxd.vartype = MXD_VARNAME;
563 varname_->p.mxd.subtype = 0;
566 /* Sort the dictionary variables into the desired order for the
567 system file output. */
572 dict_get_vars (default_dict, &v, &nv, 0);
573 qsort (v, nv, sizeof *v, compare_variables_by_mxd_vartype);
574 dict_reorder_vars (default_dict, v, nv);
580 static const struct fmt_spec fmt_tab[MXD_COUNT] =
591 first_continuous = -1;
592 for (i = 0; i < dict_get_var_cnt (default_dict); i++)
594 struct variable *v = dict_get_var (default_dict, i);
595 int type = v->p.mxd.vartype;
597 assert (type >= 0 && type < MXD_COUNT);
598 v->print = v->write = fmt_tab[type];
600 if (type == MXD_CONTINUOUS)
602 if (first_continuous == -1 && type == MXD_CONTINUOUS)
603 first_continuous = i;
607 if (n_continuous == 0)
609 msg (SE, _("No continuous variables specified."));
617 if (explicit_rowtype)
618 read_matrices_with_rowtype ();
620 read_matrices_without_rowtype ();
622 pool_destroy (container);
627 discard_variables ();
629 pool_destroy (container);
633 /* Look up string S as a content-type name and return the
634 corresponding enumerated value, or -1 if there is no match. If
635 COLLIDE is non-NULL then *COLLIDE returns a value (suitable for use
636 as a bit-index) which can be used for determining whether a related
637 statistic has already been used. */
639 string_to_content_type (char *s, int *collide)
650 {N_VECTOR, 0, "N_VECTOR"},
652 {N_SCALAR, 0, "N_SCALAR"},
653 {N_MATRIX, 1, "N_MATRIX"},
655 {STDDEV, 3, "STDDEV"},
667 for (tp = tab; tp->value != -1; tp++)
668 if (!strcmp (s, tp->string))
671 *collide = tp->collide;
678 /* Compare two variables using p.mxd.vartype and p.mxd.subtype
681 compare_variables_by_mxd_vartype (const void *a_, const void *b_)
683 struct variable *const *pa = a_;
684 struct variable *const *pb = b_;
685 const struct matrix_data_proc *a = &(*pa)->p.mxd;
686 const struct matrix_data_proc *b = &(*pb)->p.mxd;
688 if (a->vartype != b->vartype)
689 return a->vartype > b->vartype ? 1 : -1;
691 return a->subtype < b->subtype ? -1 : a->subtype > b->subtype;
695 /* Print out the command as input. */
699 printf ("MATRIX DATA\n\t/VARIABLES=");
704 for (i = 0; i < default_dict.nvar; i++)
705 printf ("%s ", default_dict.var[i]->name);
709 printf ("\t/FORMAT=");
712 else if (fmt == FREE)
716 if (section == LOWER)
718 else if (section == UPPER)
720 else if (section == FULL)
724 if (diag == DIAGONAL)
725 printf (" DIAGONAL\n");
726 else if (diag == NODIAGONAL)
727 printf (" NODIAGONAL\n");
731 if (dict_get_split_cnt (default_dict) != 0)
735 printf ("\t/SPLIT=");
736 for (i = 0; i < dict_get_split_cnt (default_dict); i++)
737 printf ("%s ", dict_get_split_vars (default_dict)[i]->name);
739 printf ("\t/* single split");
747 printf ("\t/FACTORS=");
748 for (i = 0; i < n_factors; i++)
749 printf ("%s ", factors[i]->name);
754 printf ("\t/CELLS=%d\n", cells);
757 printf ("\t/N=%d\n", pop_n);
764 printf ("\t/CONTENTS=");
765 for (i = 0; i < n_contents; i++)
767 if (contents[i] == LPAREN)
774 else if (contents[i] == RPAREN)
782 assert (contents[i] >= 0 && contents[i] <= PROX);
785 printf ("%s", content_names[contents[i]]);
792 #endif /* DEBUGGING */
794 /* Matrix tokenizer. */
796 /* Matrix token types. */
799 MNULL, /* No token. */
802 MSTOP /* End of file. */
805 /* Current matrix token. */
808 /* Token string if applicable; not null-terminated. */
809 static char *mtokstr;
811 /* Length of mtokstr in characters. */
814 /* Token value if applicable. */
815 static double mtokval;
817 static int mget_token (void);
820 #define mget_token() mget_token_dump()
823 mget_token_dump (void)
825 int result = (mget_token) ();
836 printf (" <NULLTOK>");
839 printf (" #%g", mtokval);
842 printf (" #'%.*s'", mtoklen, mtokstr);
854 /* Return the current position in the data file. */
860 char *p = dfm_get_record (data_file, &len);
863 strcpy (buf, "at end of line");
867 int n_copy = min (10, len);
868 cp = stpcpy (buf, "before `");
869 while (n_copy && isspace ((unsigned char) *p))
871 while (n_copy && !isspace ((unsigned char) *p))
872 *cp++ = *p++, n_copy--;
880 /* Is there at least one token left in the data file? */
892 cp = dfm_get_record (data_file, &len);
897 while (isspace ((unsigned char) *cp) && cp < ep)
903 dfm_fwd_record (data_file);
906 dfm_set_record (data_file, cp);
911 /* Parse a MATRIX DATA token from data_file into mtok*. */
921 cp = dfm_get_record (data_file, &len);
931 while (isspace ((unsigned char) *cp) && cp < ep)
937 dfm_fwd_record (data_file);
940 dfm_set_record (data_file, cp);
941 first_column = dfm_get_cur_col (data_file) + 1;
943 /* Three types of fields: quoted with ', quoted with ", unquoted. */
944 if (*cp == '\'' || *cp == '"')
950 while (cp < ep && *cp != quote)
952 mtoklen = cp - mtokstr;
956 msg (SW, _("Scope of string exceeds line."));
960 int is_num = isdigit ((unsigned char) *cp) || *cp == '.';
963 while (cp < ep && !isspace ((unsigned char) *cp) && *cp != ','
964 && *cp != '-' && *cp != '+')
966 if (isdigit ((unsigned char) *cp))
969 if ((tolower ((unsigned char) *cp) == 'd'
970 || tolower ((unsigned char) *cp) == 'e')
971 && (cp[1] == '+' || cp[1] == '-'))
977 mtoklen = cp - mtokstr;
985 di.e = mtokstr + mtoklen;
986 di.v = (union value *) &mtokval;
987 di.f1 = first_column;
988 di.format.type = FMT_F;
989 di.format.w = mtoklen;
999 dfm_set_record (data_file, cp);
1004 /* Forcibly skip the end of a line for content type CONTENT in
1007 force_eol (const char *content)
1015 cp = dfm_get_record (data_file, &len);
1018 while (len && isspace (*cp))
1023 msg (SE, _("End of line expected %s while reading %s."),
1024 context (), content);
1028 dfm_fwd_record (data_file);
1033 /* Back end, omitting ROWTYPE_. */
1035 /* MATRIX DATA data. */
1036 static double ***nr_data;
1038 /* Factor values. */
1039 static double *nr_factor_values;
1041 /* Largest-numbered cell that we have read in thus far, plus one. */
1042 static int max_cell_index;
1044 /* SPLIT FILE variable values. */
1045 static double *split_values;
1047 static int nr_read_splits (int compare);
1048 static int nr_read_factors (int cell);
1049 static void nr_output_data (void);
1050 static int matrix_data_read_without_rowtype (void);
1052 /* Read from the data file and write it to the active file. */
1054 read_matrices_without_rowtype (void)
1060 split_values = xmalloc (sizeof *split_values
1061 * dict_get_split_cnt (default_dict));
1062 nr_factor_values = xmalloc (sizeof *nr_factor_values * n_factors * cells);
1065 matrix_data_source.read = (void (*)(void)) matrix_data_read_without_rowtype;
1066 vfm_source = &matrix_data_source;
1068 procedure (NULL, NULL, NULL);
1070 free (split_values);
1071 free (nr_factor_values);
1073 fh_close_handle (data_file);
1076 /* Mirror data across the diagonal of matrix CP which contains
1077 CONTENT type data. */
1079 fill_matrix (int content, double *cp)
1081 int type = content_type[content];
1083 if (type == 1 && section != FULL)
1085 if (diag == NODIAGONAL)
1087 const double fill = content == CORR ? 1.0 : SYSMIS;
1090 for (i = 0; i < n_continuous; i++)
1091 cp[i * (1 + n_continuous)] = fill;
1097 if (section == LOWER)
1099 int n_lines = n_continuous;
1100 if (section != FULL && diag == NODIAGONAL)
1103 for (r = 1; r < n_lines; r++)
1104 for (c = 0; c < r; c++)
1105 cp[r + c * n_continuous] = cp[c + r * n_continuous];
1109 assert (section == UPPER);
1110 for (r = 1; r < n_continuous; r++)
1111 for (c = 0; c < r; c++)
1112 cp[c + r * n_continuous] = cp[r + c * n_continuous];
1120 for (c = 1; c < n_continuous; c++)
1125 /* Read data lines for content type CONTENT from the data file. If
1126 PER_FACTOR is nonzero, then factor information is read from the
1127 data file. Data is for cell number CELL. */
1129 nr_read_data_lines (int per_factor, int cell, int content, int compare)
1132 const int type = content_type[content];
1134 /* Number of lines that must be parsed from the data file for this
1138 /* Current position in vector or matrix. */
1148 n_lines = n_continuous;
1149 if (section != FULL && diag == NODIAGONAL)
1153 cp = nr_data[content][cell];
1154 if (type == 1 && section == LOWER && diag == NODIAGONAL)
1157 for (i = 0; i < n_lines; i++)
1161 if (!nr_read_splits (1))
1163 if (per_factor && !nr_read_factors (cell))
1170 n_cols = n_continuous;
1180 n_cols = n_continuous - i;
1181 if (diag == NODIAGONAL)
1188 n_cols = n_continuous;
1204 for (j = 0; j < n_cols; j++)
1210 msg (SE, _("expecting value for %s %s"),
1211 dict_get_var (default_dict, j)->name, context ());
1217 if (!force_eol (content_names[content]))
1219 debug_printf (("\n"));
1222 if (section == LOWER)
1223 cp += n_continuous - n_cols;
1226 fill_matrix (content, nr_data[content][cell]);
1231 /* When ROWTYPE_ does not appear in the data, reads the matrices and
1232 writes them to the output file. Returns success. */
1234 matrix_data_read_without_rowtype (void)
1239 nr_data = pool_alloc (container, (PROX + 1) * sizeof *nr_data);
1244 for (i = 0; i <= PROX; i++)
1248 for (cp = contents; *cp != EOC; cp++)
1249 if (*cp != LPAREN && *cp != RPAREN)
1251 int per_factor = is_per_factor[*cp];
1254 n_entries = n_continuous;
1255 if (content_type[*cp] == 1)
1256 n_entries *= n_continuous;
1259 int n_vectors = per_factor ? cells : 1;
1262 nr_data[*cp] = pool_alloc (container,
1263 n_vectors * sizeof **nr_data);
1265 for (i = 0; i < n_vectors; i++)
1266 nr_data[*cp][i] = pool_alloc (container,
1267 n_entries * sizeof ***nr_data);
1276 if (!nr_read_splits (0))
1279 for (bp = contents; *bp != EOC; bp = np)
1283 /* Trap the CONTENTS that we should parse in this pass
1284 between bp and ep. Set np to the starting bp for next
1289 while (*ep != RPAREN)
1297 while (*ep != EOC && *ep != LPAREN)
1306 for (i = 0; i < (per_factor ? cells : 1); i++)
1310 for (cp = bp; cp < ep; cp++)
1311 if (!nr_read_data_lines (per_factor, i, *cp, cp != bp))
1319 if (dict_get_split_cnt (default_dict) == 0 || !another_token ())
1324 /* Read the split file variables. If COMPARE is 1, compares the
1325 values read to the last values read and returns 1 if they're equal,
1328 nr_read_splits (int compare)
1330 static int just_read = 0;
1334 if (compare && just_read)
1340 if (dict_get_split_vars (default_dict) == NULL)
1347 = ++dict_get_split_vars (default_dict)[0]->p.mxd.subtype;
1354 split_cnt = dict_get_split_cnt (default_dict);
1355 for (i = 0; i < split_cnt; i++)
1361 msg (SE, _("Syntax error expecting SPLIT FILE value %s."),
1367 split_values[i] = mtokval;
1368 else if (split_values[i] != mtokval)
1370 msg (SE, _("Expecting value %g for %s."),
1371 split_values[i], dict_get_split_vars (default_dict)[i]->name);
1379 /* Read the factors for cell CELL. If COMPARE is 1, compares the
1380 values read to the last values read and returns 1 if they're equal,
1383 nr_read_factors (int cell)
1390 assert (max_cell_index >= cell);
1391 if (cell != max_cell_index)
1402 for (i = 0; i < n_factors; i++)
1408 msg (SE, _("Syntax error expecting factor value %s."),
1414 nr_factor_values[i + n_factors * cell] = mtokval;
1415 else if (nr_factor_values[i + n_factors * cell] != mtokval)
1417 msg (SE, _("Syntax error expecting value %g for %s %s."),
1418 nr_factor_values[i + n_factors * cell],
1419 factors[i]->name, context ());
1428 /* Write the contents of a cell having content type CONTENT and data
1429 CP to the active file. */
1431 dump_cell_content (int content, double *cp)
1433 int type = content_type[content];
1436 st_bare_pad_copy (temp_case->data[rowtype_->fv].s,
1437 content_names[content], 8);
1440 memset (&temp_case->data[varname_->fv].s, ' ', 8);
1444 int n_lines = (type == 1) ? n_continuous : 1;
1447 for (i = 0; i < n_lines; i++)
1451 for (j = 0; j < n_continuous; j++)
1453 int fv = dict_get_var (default_dict, first_continuous + j)->fv;
1454 temp_case->data[fv].f = *cp;
1458 st_bare_pad_copy (temp_case->data[varname_->fv].s,
1459 dict_get_var (default_dict,
1460 first_continuous + i)->name,
1467 /* Finally dump out everything from nr_data[] to the output file. */
1469 nr_output_data (void)
1472 struct variable *const *split;
1476 split_cnt = dict_get_split_cnt (default_dict);
1477 for (i = 0; i < split_cnt; i++)
1478 temp_case->data[split[i]->fv].f = split_values[i];
1485 for (cell = 0; cell < cells; cell++)
1490 for (factor = 0; factor < n_factors; factor++)
1492 temp_case->data[factors[factor]->fv].f
1493 = nr_factor_values[factor + cell * n_factors];
1494 debug_printf (("f:%s ", factors[factor]->name));
1501 for (content = 0; content <= PROX; content++)
1502 if (is_per_factor[content])
1504 assert (nr_data[content] != NULL
1505 && nr_data[content][cell] != NULL);
1507 dump_cell_content (content, nr_data[content][cell]);
1519 for (factor = 0; factor < n_factors; factor++)
1520 temp_case->data[factors[factor]->fv].f = SYSMIS;
1523 for (content = 0; content <= PROX; content++)
1524 if (!is_per_factor[content] && nr_data[content] != NULL)
1525 dump_cell_content (content, nr_data[content][0]);
1529 /* Back end, with ROWTYPE_. */
1531 /* Type of current row. */
1532 static int wr_content;
1534 /* All the data for one set of factor values. */
1538 int n_rows[PROX + 1];
1539 double *data[PROX + 1];
1540 struct factor_data *next;
1543 /* All the data, period. */
1544 struct factor_data *wr_data;
1546 /* Current factor. */
1547 struct factor_data *wr_current;
1549 static int wr_read_splits (void);
1550 static int wr_output_data (void);
1551 static int wr_read_rowtype (void);
1552 static int wr_read_factors (void);
1553 static int wr_read_indeps (void);
1554 static int matrix_data_read_with_rowtype (void);
1556 /* When ROWTYPE_ appears in the data, reads the matrices and writes
1557 them to the output file. */
1559 read_matrices_with_rowtype (void)
1562 wr_data = wr_current = NULL;
1563 split_values = NULL;
1566 matrix_data_source.read = (void (*)(void)) matrix_data_read_with_rowtype;
1567 vfm_source = &matrix_data_source;
1569 procedure (NULL, NULL, NULL);
1571 free (split_values);
1572 fh_close_handle (data_file);
1575 /* Read from the data file and write it to the active file. */
1577 matrix_data_read_with_rowtype (void)
1581 if (!wr_read_splits ())
1584 if (!wr_read_factors ())
1587 if (!wr_read_indeps ())
1590 while (another_token ());
1596 /* Read the split file variables. If they differ from the previous
1597 set of split variables then output the data. Returns success. */
1599 wr_read_splits (void)
1604 split_cnt = dict_get_split_cnt (default_dict);
1613 split_values = xmalloc (split_cnt * sizeof *split_values);
1621 for (i = 0; i < split_cnt; i++)
1627 msg (SE, _("Syntax error %s expecting SPLIT FILE value."),
1632 if (compare && split_values[i] != mtokval && !different)
1634 if (!wr_output_data ())
1639 split_values[i] = mtokval;
1646 /* Compares doubles A and B, treating SYSMIS as greatest. */
1648 compare_doubles (const void *a_, const void *b_, void *aux unused)
1650 const double *a = a_;
1651 const double *b = b_;
1655 else if (*a == SYSMIS)
1657 else if (*b == SYSMIS)
1665 /* Return strcmp()-type comparison of the n_factors factors at _A and
1666 _B. Sort missing values toward the end. */
1668 compare_factors (const void *a_, const void *b_)
1670 struct factor_data *const *pa = a_;
1671 struct factor_data *const *pb = b_;
1672 const double *a = (*pa)->factors;
1673 const double *b = (*pb)->factors;
1675 return lexicographical_compare (a, n_factors,
1678 compare_doubles, NULL);
1681 /* Write out the data for the current split file to the active
1684 wr_output_data (void)
1687 struct variable *const *split;
1691 split_cnt = dict_get_split_cnt (default_dict);
1692 for (i = 0; i < split_cnt; i++)
1693 temp_case->data[split[i]->fv].f = split_values[i];
1696 /* Sort the wr_data list. */
1698 struct factor_data **factors;
1699 struct factor_data *iter;
1702 factors = xmalloc (sizeof *factors * cells);
1704 for (i = 0, iter = wr_data; iter; iter = iter->next, i++)
1707 qsort (factors, cells, sizeof *factors, compare_factors);
1709 wr_data = factors[0];
1710 for (i = 0; i < cells - 1; i++)
1711 factors[i]->next = factors[i + 1];
1712 factors[cells - 1]->next = NULL;
1717 /* Write out records for every set of factor values. */
1719 struct factor_data *iter;
1721 for (iter = wr_data; iter; iter = iter->next)
1726 for (factor = 0; factor < n_factors; factor++)
1728 temp_case->data[factors[factor]->fv].f
1729 = iter->factors[factor];
1730 debug_printf (("f:%s ", factors[factor]->name));
1737 for (content = 0; content <= PROX; content++)
1739 if (!iter->n_rows[content])
1743 int type = content_type[content];
1744 int n_lines = (type == 1
1746 - (section != FULL && diag == NODIAGONAL))
1749 if (n_lines != iter->n_rows[content])
1751 msg (SE, _("Expected %d lines of data for %s content; "
1752 "actually saw %d lines. No data will be "
1753 "output for this content."),
1754 n_lines, content_names[content],
1755 iter->n_rows[content]);
1760 fill_matrix (content, iter->data[content]);
1762 dump_cell_content (content, iter->data[content]);
1768 pool_destroy (container);
1769 container = pool_create ();
1771 wr_data = wr_current = NULL;
1776 /* Read ROWTYPE_ from the data file. Return success. */
1778 wr_read_rowtype (void)
1780 if (wr_content != -1)
1782 msg (SE, _("Multiply specified ROWTYPE_ %s."), context ());
1787 msg (SE, _("Syntax error %s expecting ROWTYPE_ string."), context ());
1795 memcpy (s, mtokstr, min (15, mtoklen));
1796 s[min (15, mtoklen)] = 0;
1798 for (cp = s; *cp; cp++)
1799 *cp = toupper ((unsigned char) *cp);
1801 wr_content = string_to_content_type (s, NULL);
1804 if (wr_content == -1)
1806 msg (SE, _("Syntax error %s."), context ());
1813 /* Read the factors for the current row. Select a set of factors and
1814 point wr_current to it. */
1816 wr_read_factors (void)
1818 double *factor_values = local_alloc (sizeof *factor_values * n_factors);
1824 for (i = 0; i < n_factors; i++)
1830 if (!wr_read_rowtype ())
1837 msg (SE, _("Syntax error expecting factor value %s."),
1842 factor_values[i] = mtokval;
1845 if (wr_content == -1)
1849 if (!wr_read_rowtype ())
1853 /* Try the most recent factor first as a simple caching
1859 for (i = 0; i < n_factors; i++)
1860 if (factor_values[i] != wr_current->factors[i])
1865 /* Linear search through the list. */
1868 struct factor_data *iter;
1870 for (iter = wr_data; iter; iter = iter->next)
1874 for (i = 0; i < n_factors; i++)
1875 if (factor_values[i] != iter->factors[i])
1885 /* Not found. Make a new item. */
1887 struct factor_data *new = pool_alloc (container, sizeof *new);
1889 new->factors = pool_alloc (container, sizeof *new->factors * n_factors);
1894 for (i = 0; i < n_factors; i++)
1895 new->factors[i] = factor_values[i];
1901 for (i = 0; i <= PROX; i++)
1904 new->data[i] = NULL;
1908 new->next = wr_data;
1909 wr_data = wr_current = new;
1914 local_free (factor_values);
1918 local_free (factor_values);
1922 /* Read the independent variables into wr_current. */
1924 wr_read_indeps (void)
1926 struct factor_data *c = wr_current;
1927 const int type = content_type[wr_content];
1928 const int n_rows = c->n_rows[wr_content];
1932 /* Allocate room for data if necessary. */
1933 if (c->data[wr_content] == NULL)
1935 int n_items = n_continuous;
1937 n_items *= n_continuous;
1939 c->data[wr_content] = pool_alloc (container,
1940 sizeof **c->data * n_items);
1943 cp = &c->data[wr_content][n_rows * n_continuous];
1945 /* Figure out how much to read from this line. */
1952 msg (SE, _("Duplicate specification for %s."),
1953 content_names[wr_content]);
1957 n_cols = n_continuous;
1962 if (n_rows >= n_continuous - (section != FULL && diag == NODIAGONAL))
1964 msg (SE, _("Too many rows of matrix data for %s."),
1965 content_names[wr_content]);
1972 n_cols = n_rows + 1;
1973 if (diag == NODIAGONAL)
1978 n_cols = n_continuous - n_rows;
1979 if (diag == NODIAGONAL)
1986 n_cols = n_continuous;
1995 c->n_rows[wr_content]++;
1997 debug_printf ((" (c=%p,r=%d,n=%d)", c, n_rows + 1, n_cols));
1999 /* Read N_COLS items at CP. */
2003 for (j = 0; j < n_cols; j++)
2009 msg (SE, _("Syntax error expecting value for %s %s."),
2010 dict_get_var (default_dict, first_continuous + j)->name,
2017 if (!force_eol (content_names[wr_content]))
2019 debug_printf (("\n"));
2025 /* Matrix source. */
2027 struct case_stream matrix_data_source =