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 (void);
1033 static int matrix_data_read_without_rowtype (void);
1035 /* Read from the data file and write it to the active file. */
1037 read_matrices_without_rowtype (void)
1043 split_values = xmalloc (sizeof *split_values
1044 * dict_get_split_cnt (default_dict));
1045 nr_factor_values = xmalloc (sizeof *nr_factor_values * n_factors * cells);
1048 matrix_data_source.read = (void (*)(void)) matrix_data_read_without_rowtype;
1049 vfm_source = &matrix_data_source;
1051 procedure (NULL, NULL, NULL);
1053 free (split_values);
1054 free (nr_factor_values);
1056 fh_close_handle (data_file);
1059 /* Mirror data across the diagonal of matrix CP which contains
1060 CONTENT type data. */
1062 fill_matrix (int content, double *cp)
1064 int type = content_type[content];
1066 if (type == 1 && section != FULL)
1068 if (diag == NODIAGONAL)
1070 const double fill = content == CORR ? 1.0 : SYSMIS;
1073 for (i = 0; i < n_continuous; i++)
1074 cp[i * (1 + n_continuous)] = fill;
1080 if (section == LOWER)
1082 int n_lines = n_continuous;
1083 if (section != FULL && diag == NODIAGONAL)
1086 for (r = 1; r < n_lines; r++)
1087 for (c = 0; c < r; c++)
1088 cp[r + c * n_continuous] = cp[c + r * n_continuous];
1092 assert (section == UPPER);
1093 for (r = 1; r < n_continuous; r++)
1094 for (c = 0; c < r; c++)
1095 cp[c + r * n_continuous] = cp[r + c * n_continuous];
1103 for (c = 1; c < n_continuous; c++)
1108 /* Read data lines for content type CONTENT from the data file. If
1109 PER_FACTOR is nonzero, then factor information is read from the
1110 data file. Data is for cell number CELL. */
1112 nr_read_data_lines (int per_factor, int cell, int content, int compare)
1115 const int type = content_type[content];
1117 /* Number of lines that must be parsed from the data file for this
1121 /* Current position in vector or matrix. */
1131 n_lines = n_continuous;
1132 if (section != FULL && diag == NODIAGONAL)
1136 cp = nr_data[content][cell];
1137 if (type == 1 && section == LOWER && diag == NODIAGONAL)
1140 for (i = 0; i < n_lines; i++)
1144 if (!nr_read_splits (1))
1146 if (per_factor && !nr_read_factors (cell))
1153 n_cols = n_continuous;
1163 n_cols = n_continuous - i;
1164 if (diag == NODIAGONAL)
1171 n_cols = n_continuous;
1187 for (j = 0; j < n_cols; j++)
1193 msg (SE, _("expecting value for %s %s"),
1194 dict_get_var (default_dict, j)->name, context ());
1200 if (!force_eol (content_names[content]))
1202 debug_printf (("\n"));
1205 if (section == LOWER)
1206 cp += n_continuous - n_cols;
1209 fill_matrix (content, nr_data[content][cell]);
1214 /* When ROWTYPE_ does not appear in the data, reads the matrices and
1215 writes them to the output file. Returns success. */
1217 matrix_data_read_without_rowtype (void)
1222 nr_data = pool_alloc (container, (PROX + 1) * sizeof *nr_data);
1227 for (i = 0; i <= PROX; i++)
1231 for (cp = contents; *cp != EOC; cp++)
1232 if (*cp != LPAREN && *cp != RPAREN)
1234 int per_factor = is_per_factor[*cp];
1237 n_entries = n_continuous;
1238 if (content_type[*cp] == 1)
1239 n_entries *= n_continuous;
1242 int n_vectors = per_factor ? cells : 1;
1245 nr_data[*cp] = pool_alloc (container,
1246 n_vectors * sizeof **nr_data);
1248 for (i = 0; i < n_vectors; i++)
1249 nr_data[*cp][i] = pool_alloc (container,
1250 n_entries * sizeof ***nr_data);
1259 if (!nr_read_splits (0))
1262 for (bp = contents; *bp != EOC; bp = np)
1266 /* Trap the CONTENTS that we should parse in this pass
1267 between bp and ep. Set np to the starting bp for next
1272 while (*ep != RPAREN)
1280 while (*ep != EOC && *ep != LPAREN)
1289 for (i = 0; i < (per_factor ? cells : 1); i++)
1293 for (cp = bp; cp < ep; cp++)
1294 if (!nr_read_data_lines (per_factor, i, *cp, cp != bp))
1302 if (dict_get_split_cnt (default_dict) == 0 || !another_token ())
1307 /* Read the split file variables. If COMPARE is 1, compares the
1308 values read to the last values read and returns 1 if they're equal,
1311 nr_read_splits (int compare)
1313 static int just_read = 0;
1317 if (compare && just_read)
1323 if (dict_get_split_vars (default_dict) == NULL)
1330 = ++dict_get_split_vars (default_dict)[0]->p.mxd.subtype;
1337 split_cnt = dict_get_split_cnt (default_dict);
1338 for (i = 0; i < split_cnt; i++)
1344 msg (SE, _("Syntax error expecting SPLIT FILE value %s."),
1350 split_values[i] = mtokval;
1351 else if (split_values[i] != mtokval)
1353 msg (SE, _("Expecting value %g for %s."),
1354 split_values[i], dict_get_split_vars (default_dict)[i]->name);
1362 /* Read the factors for cell CELL. If COMPARE is 1, compares the
1363 values read to the last values read and returns 1 if they're equal,
1366 nr_read_factors (int cell)
1373 assert (max_cell_index >= cell);
1374 if (cell != max_cell_index)
1385 for (i = 0; i < n_factors; i++)
1391 msg (SE, _("Syntax error expecting factor value %s."),
1397 nr_factor_values[i + n_factors * cell] = mtokval;
1398 else if (nr_factor_values[i + n_factors * cell] != mtokval)
1400 msg (SE, _("Syntax error expecting value %g for %s %s."),
1401 nr_factor_values[i + n_factors * cell],
1402 factors[i]->name, context ());
1411 /* Write the contents of a cell having content type CONTENT and data
1412 CP to the active file. */
1414 dump_cell_content (int content, double *cp)
1416 int type = content_type[content];
1419 st_bare_pad_copy (temp_case->data[rowtype_->fv].s,
1420 content_names[content], 8);
1423 memset (&temp_case->data[varname_->fv].s, ' ', 8);
1427 int n_lines = (type == 1) ? n_continuous : 1;
1430 for (i = 0; i < n_lines; i++)
1434 for (j = 0; j < n_continuous; j++)
1436 int fv = dict_get_var (default_dict, first_continuous + j)->fv;
1437 temp_case->data[fv].f = *cp;
1441 st_bare_pad_copy (temp_case->data[varname_->fv].s,
1442 dict_get_var (default_dict,
1443 first_continuous + i)->name,
1450 /* Finally dump out everything from nr_data[] to the output file. */
1452 nr_output_data (void)
1455 struct variable *const *split;
1459 split_cnt = dict_get_split_cnt (default_dict);
1460 for (i = 0; i < split_cnt; i++)
1461 temp_case->data[split[i]->fv].f = split_values[i];
1468 for (cell = 0; cell < cells; cell++)
1473 for (factor = 0; factor < n_factors; factor++)
1475 temp_case->data[factors[factor]->fv].f
1476 = nr_factor_values[factor + cell * n_factors];
1477 debug_printf (("f:%s ", factors[factor]->name));
1484 for (content = 0; content <= PROX; content++)
1485 if (is_per_factor[content])
1487 assert (nr_data[content] != NULL
1488 && nr_data[content][cell] != NULL);
1490 dump_cell_content (content, nr_data[content][cell]);
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]);
1512 /* Back end, with ROWTYPE_. */
1514 /* Type of current row. */
1515 static int wr_content;
1517 /* All the data for one set of factor values. */
1521 int n_rows[PROX + 1];
1522 double *data[PROX + 1];
1523 struct factor_data *next;
1526 /* All the data, period. */
1527 struct factor_data *wr_data;
1529 /* Current factor. */
1530 struct factor_data *wr_current;
1532 static int wr_read_splits (void);
1533 static int wr_output_data (void);
1534 static int wr_read_rowtype (void);
1535 static int wr_read_factors (void);
1536 static int wr_read_indeps (void);
1537 static int matrix_data_read_with_rowtype (void);
1539 /* When ROWTYPE_ appears in the data, reads the matrices and writes
1540 them to the output file. */
1542 read_matrices_with_rowtype (void)
1545 wr_data = wr_current = NULL;
1546 split_values = NULL;
1549 matrix_data_source.read = (void (*)(void)) matrix_data_read_with_rowtype;
1550 vfm_source = &matrix_data_source;
1552 procedure (NULL, NULL, NULL);
1554 free (split_values);
1555 fh_close_handle (data_file);
1558 /* Read from the data file and write it to the active file. */
1560 matrix_data_read_with_rowtype (void)
1564 if (!wr_read_splits ())
1567 if (!wr_read_factors ())
1570 if (!wr_read_indeps ())
1573 while (another_token ());
1579 /* Read the split file variables. If they differ from the previous
1580 set of split variables then output the data. Returns success. */
1582 wr_read_splits (void)
1587 split_cnt = dict_get_split_cnt (default_dict);
1596 split_values = xmalloc (split_cnt * sizeof *split_values);
1604 for (i = 0; i < split_cnt; i++)
1610 msg (SE, _("Syntax error %s expecting SPLIT FILE value."),
1615 if (compare && split_values[i] != mtokval && !different)
1617 if (!wr_output_data ())
1622 split_values[i] = mtokval;
1629 /* Compares doubles A and B, treating SYSMIS as greatest. */
1631 compare_doubles (const void *a_, const void *b_, void *aux UNUSED)
1633 const double *a = a_;
1634 const double *b = b_;
1638 else if (*a == SYSMIS)
1640 else if (*b == SYSMIS)
1648 /* Return strcmp()-type comparison of the n_factors factors at _A and
1649 _B. Sort missing values toward the end. */
1651 compare_factors (const void *a_, const void *b_)
1653 struct factor_data *const *pa = a_;
1654 struct factor_data *const *pb = b_;
1655 const double *a = (*pa)->factors;
1656 const double *b = (*pb)->factors;
1658 return lexicographical_compare (a, n_factors,
1661 compare_doubles, NULL);
1664 /* Write out the data for the current split file to the active
1667 wr_output_data (void)
1670 struct variable *const *split;
1674 split_cnt = dict_get_split_cnt (default_dict);
1675 for (i = 0; i < split_cnt; i++)
1676 temp_case->data[split[i]->fv].f = split_values[i];
1679 /* Sort the wr_data list. */
1681 struct factor_data **factors;
1682 struct factor_data *iter;
1685 factors = xmalloc (sizeof *factors * cells);
1687 for (i = 0, iter = wr_data; iter; iter = iter->next, i++)
1690 qsort (factors, cells, sizeof *factors, compare_factors);
1692 wr_data = factors[0];
1693 for (i = 0; i < cells - 1; i++)
1694 factors[i]->next = factors[i + 1];
1695 factors[cells - 1]->next = NULL;
1700 /* Write out records for every set of factor values. */
1702 struct factor_data *iter;
1704 for (iter = wr_data; iter; iter = iter->next)
1709 for (factor = 0; factor < n_factors; factor++)
1711 temp_case->data[factors[factor]->fv].f
1712 = iter->factors[factor];
1713 debug_printf (("f:%s ", factors[factor]->name));
1720 for (content = 0; content <= PROX; content++)
1722 if (!iter->n_rows[content])
1726 int type = content_type[content];
1727 int n_lines = (type == 1
1729 - (section != FULL && diag == NODIAGONAL))
1732 if (n_lines != iter->n_rows[content])
1734 msg (SE, _("Expected %d lines of data for %s content; "
1735 "actually saw %d lines. No data will be "
1736 "output for this content."),
1737 n_lines, content_names[content],
1738 iter->n_rows[content]);
1743 fill_matrix (content, iter->data[content]);
1745 dump_cell_content (content, iter->data[content]);
1751 pool_destroy (container);
1752 container = pool_create ();
1754 wr_data = wr_current = NULL;
1759 /* Read ROWTYPE_ from the data file. Return success. */
1761 wr_read_rowtype (void)
1763 if (wr_content != -1)
1765 msg (SE, _("Multiply specified ROWTYPE_ %s."), context ());
1770 msg (SE, _("Syntax error %s expecting ROWTYPE_ string."), context ());
1778 memcpy (s, mtokstr, min (15, mtoklen));
1779 s[min (15, mtoklen)] = 0;
1781 for (cp = s; *cp; cp++)
1782 *cp = toupper ((unsigned char) *cp);
1784 wr_content = string_to_content_type (s, NULL);
1787 if (wr_content == -1)
1789 msg (SE, _("Syntax error %s."), context ());
1796 /* Read the factors for the current row. Select a set of factors and
1797 point wr_current to it. */
1799 wr_read_factors (void)
1801 double *factor_values = local_alloc (sizeof *factor_values * n_factors);
1807 for (i = 0; i < n_factors; i++)
1813 if (!wr_read_rowtype ())
1820 msg (SE, _("Syntax error expecting factor value %s."),
1825 factor_values[i] = mtokval;
1828 if (wr_content == -1)
1832 if (!wr_read_rowtype ())
1836 /* Try the most recent factor first as a simple caching
1842 for (i = 0; i < n_factors; i++)
1843 if (factor_values[i] != wr_current->factors[i])
1848 /* Linear search through the list. */
1851 struct factor_data *iter;
1853 for (iter = wr_data; iter; iter = iter->next)
1857 for (i = 0; i < n_factors; i++)
1858 if (factor_values[i] != iter->factors[i])
1868 /* Not found. Make a new item. */
1870 struct factor_data *new = pool_alloc (container, sizeof *new);
1872 new->factors = pool_alloc (container, sizeof *new->factors * n_factors);
1877 for (i = 0; i < n_factors; i++)
1878 new->factors[i] = factor_values[i];
1884 for (i = 0; i <= PROX; i++)
1887 new->data[i] = NULL;
1891 new->next = wr_data;
1892 wr_data = wr_current = new;
1897 local_free (factor_values);
1901 local_free (factor_values);
1905 /* Read the independent variables into wr_current. */
1907 wr_read_indeps (void)
1909 struct factor_data *c = wr_current;
1910 const int type = content_type[wr_content];
1911 const int n_rows = c->n_rows[wr_content];
1915 /* Allocate room for data if necessary. */
1916 if (c->data[wr_content] == NULL)
1918 int n_items = n_continuous;
1920 n_items *= n_continuous;
1922 c->data[wr_content] = pool_alloc (container,
1923 sizeof **c->data * n_items);
1926 cp = &c->data[wr_content][n_rows * n_continuous];
1928 /* Figure out how much to read from this line. */
1935 msg (SE, _("Duplicate specification for %s."),
1936 content_names[wr_content]);
1940 n_cols = n_continuous;
1945 if (n_rows >= n_continuous - (section != FULL && diag == NODIAGONAL))
1947 msg (SE, _("Too many rows of matrix data for %s."),
1948 content_names[wr_content]);
1955 n_cols = n_rows + 1;
1956 if (diag == NODIAGONAL)
1961 n_cols = n_continuous - n_rows;
1962 if (diag == NODIAGONAL)
1969 n_cols = n_continuous;
1978 c->n_rows[wr_content]++;
1980 debug_printf ((" (c=%p,r=%d,n=%d)", c, n_rows + 1, n_cols));
1982 /* Read N_COLS items at CP. */
1986 for (j = 0; j < n_cols; j++)
1992 msg (SE, _("Syntax error expecting value for %s %s."),
1993 dict_get_var (default_dict, first_continuous + j)->name,
2000 if (!force_eol (content_names[wr_content]))
2002 debug_printf (("\n"));
2008 /* Matrix source. */
2010 struct case_stream matrix_data_source =