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 = force_create_variable (&default_dict, v[i],
238 new_var->p.mxd.vartype = MXD_CONTINUOUS;
239 new_var->p.mxd.subtype = i;
242 explicit_rowtype = 1;
249 rowtype_ = force_create_variable (&default_dict, "ROWTYPE_",
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 (!is_varname (tokid)
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 = force_create_variable (&default_dict, tokid,
320 single_split->p.mxd.vartype = MXD_CONTINUOUS;
322 default_dict.n_splits = 1;
323 default_dict.splits = xmalloc (2 * sizeof *default_dict.splits);
324 default_dict.splits[0] = single_split;
325 default_dict.splits[1] = NULL;
332 if (!parse_variables (NULL, &v, &n, PV_NO_DUPLICATE))
335 default_dict.n_splits = n;
336 default_dict.splits = v = xrealloc (v, sizeof *v * (n + 1));
343 for (i = 0; i < default_dict.n_splits; i++)
345 if (default_dict.splits[i]->p.mxd.vartype != MXD_CONTINUOUS)
347 msg (SE, _("Split variable %s is already another type."),
351 default_dict.splits[i]->p.mxd.vartype = MXD_SPLIT;
352 default_dict.splits[i]->p.mxd.subtype = i;
356 else if (lex_match_id ("FACTORS"))
362 msg (SE, _("FACTORS subcommand multiply specified."));
367 if (!parse_variables (NULL, &factors, &n_factors, PV_NONE))
373 for (i = 0; i < n_factors; i++)
375 if (factors[i]->p.mxd.vartype != MXD_CONTINUOUS)
377 msg (SE, _("Factor variable %s is already another type."),
381 factors[i]->p.mxd.vartype = MXD_FACTOR;
382 factors[i]->p.mxd.subtype = i;
386 else if (lex_match_id ("CELLS"))
392 msg (SE, _("CELLS subcommand multiply specified."));
396 if (!lex_integer_p () || lex_integer () < 1)
398 lex_error (_("expecting positive integer"));
402 cells = lex_integer ();
405 else if (lex_match_id ("N"))
411 msg (SE, _("N subcommand multiply specified."));
415 if (!lex_integer_p () || lex_integer () < 1)
417 lex_error (_("expecting positive integer"));
421 pop_n = lex_integer ();
424 else if (lex_match_id ("CONTENTS"))
426 int inside_parens = 0;
427 unsigned collide = 0;
432 msg (SE, _("CONTENTS subcommand multiply specified."));
442 for (i = 0; i <= PROX; i++)
443 is_per_factor[i] = 0;
452 msg (SE, _("Nested parentheses not allowed."));
458 else if (lex_match (')'))
462 msg (SE, _("Mismatched right parenthesis (`(')."));
465 if (contents[n_contents - 1] == LPAREN)
467 msg (SE, _("Empty parentheses not allowed."));
480 lex_error (_("in CONTENTS subcommand"));
484 content_type = string_to_content_type (tokid,
486 if (content_type == -1)
488 lex_error (_("in CONTENTS subcommand"));
493 if (collide & (1 << collide_index))
495 msg (SE, _("Content multiply specified for %s."),
496 content_names[content_type]);
499 collide |= (1 << collide_index);
502 is_per_factor[item] = inside_parens;
504 contents[n_contents++] = item;
506 if (token == '/' || token == '.')
512 msg (SE, _("Missing right parenthesis."));
515 contents[n_contents] = EOC;
526 lex_error (_("expecting end of command"));
532 msg (SE, _("Missing VARIABLES subcommand."));
536 if (!n_contents && !explicit_rowtype)
538 msg (SW, _("CONTENTS subcommand not specified: assuming file "
539 "contains only CORR matrix."));
546 if (n_factors && !explicit_rowtype && cells == -1)
548 msg (SE, _("Missing CELLS subcommand. CELLS is required "
549 "when ROWTYPE_ is not given in the data and "
550 "factors are present."));
554 if (explicit_rowtype && single_split)
556 msg (SE, _("Split file values must be present in the data when "
557 "ROWTYPE_ is present."));
561 /* Create VARNAME_. */
563 varname_ = force_create_variable (&default_dict, "VARNAME_",
565 varname_->p.mxd.vartype = MXD_VARNAME;
566 varname_->p.mxd.subtype = 0;
569 /* Sort the dictionary variables into the desired order for the
570 system file output. */
574 qsort (default_dict.var, default_dict.nvar, sizeof *default_dict.var,
575 compare_variables_by_mxd_vartype);
577 for (i = 0; i < default_dict.nvar; i++)
578 default_dict.var[i]->index = i;
583 static const struct fmt_spec fmt_tab[MXD_COUNT] =
594 first_continuous = -1;
595 for (i = 0; i < default_dict.nvar; i++)
597 struct variable *v = default_dict.var[i];
598 int type = v->p.mxd.vartype;
600 assert (type >= 0 && type < MXD_COUNT);
601 v->print = v->write = fmt_tab[type];
603 if (type == MXD_CONTINUOUS)
605 if (first_continuous == -1 && type == MXD_CONTINUOUS)
606 first_continuous = i;
610 if (n_continuous == 0)
612 msg (SE, _("No continuous variables specified."));
620 if (explicit_rowtype)
621 read_matrices_with_rowtype ();
623 read_matrices_without_rowtype ();
625 pool_destroy (container);
630 discard_variables ();
632 pool_destroy (container);
636 /* Look up string S as a content-type name and return the
637 corresponding enumerated value, or -1 if there is no match. If
638 COLLIDE is non-NULL then *COLLIDE returns a value (suitable for use
639 as a bit-index) which can be used for determining whether a related
640 statistic has already been used. */
642 string_to_content_type (char *s, int *collide)
653 {N_VECTOR, 0, "N_VECTOR"},
655 {N_SCALAR, 0, "N_SCALAR"},
656 {N_MATRIX, 1, "N_MATRIX"},
658 {STDDEV, 3, "STDDEV"},
670 for (tp = tab; tp->value != -1; tp++)
671 if (!strcmp (s, tp->string))
674 *collide = tp->collide;
681 /* Compare two variables using p.mxd.vartype and p.mxd.subtype
684 compare_variables_by_mxd_vartype (const void *a_, const void *b_)
686 struct variable *const *pa = a_;
687 struct variable *const *pb = b_;
688 const struct matrix_data_proc *a = &(*pa)->p.mxd;
689 const struct matrix_data_proc *b = &(*pb)->p.mxd;
691 if (a->vartype != b->vartype)
692 return a->vartype > b->vartype ? 1 : -1;
694 return a->subtype < b->subtype ? -1 : a->subtype > b->subtype;
698 /* Print out the command as input. */
702 printf ("MATRIX DATA\n\t/VARIABLES=");
707 for (i = 0; i < default_dict.nvar; i++)
708 printf ("%s ", default_dict.var[i]->name);
712 printf ("\t/FORMAT=");
715 else if (fmt == FREE)
719 if (section == LOWER)
721 else if (section == UPPER)
723 else if (section == FULL)
727 if (diag == DIAGONAL)
728 printf (" DIAGONAL\n");
729 else if (diag == NODIAGONAL)
730 printf (" NODIAGONAL\n");
734 if (default_dict.n_splits)
738 printf ("\t/SPLIT=");
739 for (i = 0; i < default_dict.n_splits; i++)
740 printf ("%s ", default_dict.splits[i]->name);
742 printf ("\t/* single split");
750 printf ("\t/FACTORS=");
751 for (i = 0; i < n_factors; i++)
752 printf ("%s ", factors[i]->name);
757 printf ("\t/CELLS=%d\n", cells);
760 printf ("\t/N=%d\n", pop_n);
767 printf ("\t/CONTENTS=");
768 for (i = 0; i < n_contents; i++)
770 if (contents[i] == LPAREN)
777 else if (contents[i] == RPAREN)
785 assert (contents[i] >= 0 && contents[i] <= PROX);
788 printf ("%s", content_names[contents[i]]);
795 #endif /* DEBUGGING */
797 /* Matrix tokenizer. */
799 /* Matrix token types. */
802 MNULL, /* No token. */
805 MSTOP /* End of file. */
808 /* Current matrix token. */
811 /* Token string if applicable; not null-terminated. */
812 static char *mtokstr;
814 /* Length of mtokstr in characters. */
817 /* Token value if applicable. */
818 static double mtokval;
820 static int mget_token (void);
823 #define mget_token() mget_token_dump()
826 mget_token_dump (void)
828 int result = (mget_token) ();
839 printf (" <NULLTOK>");
842 printf (" #%g", mtokval);
845 printf (" #'%.*s'", mtoklen, mtokstr);
857 /* Return the current position in the data file. */
863 char *p = dfm_get_record (data_file, &len);
866 strcpy (buf, "at end of line");
870 int n_copy = min (10, len);
871 cp = stpcpy (buf, "before `");
872 while (n_copy && isspace ((unsigned char) *p))
874 while (n_copy && !isspace ((unsigned char) *p))
875 *cp++ = *p++, n_copy--;
883 /* Is there at least one token left in the data file? */
895 cp = dfm_get_record (data_file, &len);
900 while (isspace ((unsigned char) *cp) && cp < ep)
906 dfm_fwd_record (data_file);
909 dfm_set_record (data_file, cp);
914 /* Parse a MATRIX DATA token from data_file into mtok*. */
924 cp = dfm_get_record (data_file, &len);
934 while (isspace ((unsigned char) *cp) && cp < ep)
940 dfm_fwd_record (data_file);
943 dfm_set_record (data_file, cp);
944 first_column = dfm_get_cur_col (data_file) + 1;
946 /* Three types of fields: quoted with ', quoted with ", unquoted. */
947 if (*cp == '\'' || *cp == '"')
953 while (cp < ep && *cp != quote)
955 mtoklen = cp - mtokstr;
959 msg (SW, _("Scope of string exceeds line."));
963 int is_num = isdigit ((unsigned char) *cp) || *cp == '.';
966 while (cp < ep && !isspace ((unsigned char) *cp) && *cp != ','
967 && *cp != '-' && *cp != '+')
969 if (isdigit ((unsigned char) *cp))
972 if ((tolower ((unsigned char) *cp) == 'd'
973 || tolower ((unsigned char) *cp) == 'e')
974 && (cp[1] == '+' || cp[1] == '-'))
980 mtoklen = cp - mtokstr;
988 di.e = mtokstr + mtoklen;
989 di.v = (union value *) &mtokval;
990 di.f1 = first_column;
991 di.format.type = FMT_F;
992 di.format.w = mtoklen;
1002 dfm_set_record (data_file, cp);
1007 /* Forcibly skip the end of a line for content type CONTENT in
1010 force_eol (const char *content)
1018 cp = dfm_get_record (data_file, &len);
1021 while (len && isspace (*cp))
1026 msg (SE, _("End of line expected %s while reading %s."),
1027 context (), content);
1031 dfm_fwd_record (data_file);
1036 /* Back end, omitting ROWTYPE_. */
1038 /* MATRIX DATA data. */
1039 static double ***nr_data;
1041 /* Factor values. */
1042 static double *nr_factor_values;
1044 /* Largest-numbered cell that we have read in thus far, plus one. */
1045 static int max_cell_index;
1047 /* SPLIT FILE variable values. */
1048 static double *split_values;
1050 static int nr_read_splits (int compare);
1051 static int nr_read_factors (int cell);
1052 static void nr_output_data (void);
1053 static int matrix_data_read_without_rowtype (void);
1055 /* Read from the data file and write it to the active file. */
1057 read_matrices_without_rowtype (void)
1063 split_values = xmalloc (sizeof *split_values * default_dict.n_splits);
1064 nr_factor_values = xmalloc (sizeof *nr_factor_values * n_factors * cells);
1067 matrix_data_source.read = (void (*)(void)) matrix_data_read_without_rowtype;
1068 vfm_source = &matrix_data_source;
1070 procedure (NULL, NULL, NULL);
1072 free (split_values);
1073 free (nr_factor_values);
1075 fh_close_handle (data_file);
1078 /* Mirror data across the diagonal of matrix CP which contains
1079 CONTENT type data. */
1081 fill_matrix (int content, double *cp)
1083 int type = content_type[content];
1085 if (type == 1 && section != FULL)
1087 if (diag == NODIAGONAL)
1089 const double fill = content == CORR ? 1.0 : SYSMIS;
1092 for (i = 0; i < n_continuous; i++)
1093 cp[i * (1 + n_continuous)] = fill;
1099 if (section == LOWER)
1101 int n_lines = n_continuous;
1102 if (section != FULL && diag == NODIAGONAL)
1105 for (r = 1; r < n_lines; r++)
1106 for (c = 0; c < r; c++)
1107 cp[r + c * n_continuous] = cp[c + r * n_continuous];
1111 assert (section == UPPER);
1112 for (r = 1; r < n_continuous; r++)
1113 for (c = 0; c < r; c++)
1114 cp[c + r * n_continuous] = cp[r + c * n_continuous];
1122 for (c = 1; c < n_continuous; c++)
1127 /* Read data lines for content type CONTENT from the data file. If
1128 PER_FACTOR is nonzero, then factor information is read from the
1129 data file. Data is for cell number CELL. */
1131 nr_read_data_lines (int per_factor, int cell, int content, int compare)
1134 const int type = content_type[content];
1136 /* Number of lines that must be parsed from the data file for this
1140 /* Current position in vector or matrix. */
1150 n_lines = n_continuous;
1151 if (section != FULL && diag == NODIAGONAL)
1155 cp = nr_data[content][cell];
1156 if (type == 1 && section == LOWER && diag == NODIAGONAL)
1159 for (i = 0; i < n_lines; i++)
1163 if (!nr_read_splits (1))
1165 if (per_factor && !nr_read_factors (cell))
1172 n_cols = n_continuous;
1182 n_cols = n_continuous - i;
1183 if (diag == NODIAGONAL)
1190 n_cols = n_continuous;
1206 for (j = 0; j < n_cols; j++)
1212 msg (SE, _("expecting value for %s %s"),
1213 default_dict.var[j]->name, context ());
1219 if (!force_eol (content_names[content]))
1221 debug_printf (("\n"));
1224 if (section == LOWER)
1225 cp += n_continuous - n_cols;
1228 fill_matrix (content, nr_data[content][cell]);
1233 /* When ROWTYPE_ does not appear in the data, reads the matrices and
1234 writes them to the output file. Returns success. */
1236 matrix_data_read_without_rowtype (void)
1241 nr_data = pool_alloc (container, (PROX + 1) * sizeof *nr_data);
1246 for (i = 0; i <= PROX; i++)
1250 for (cp = contents; *cp != EOC; cp++)
1251 if (*cp != LPAREN && *cp != RPAREN)
1253 int per_factor = is_per_factor[*cp];
1256 n_entries = n_continuous;
1257 if (content_type[*cp] == 1)
1258 n_entries *= n_continuous;
1261 int n_vectors = per_factor ? cells : 1;
1264 nr_data[*cp] = pool_alloc (container,
1265 n_vectors * sizeof **nr_data);
1267 for (i = 0; i < n_vectors; i++)
1268 nr_data[*cp][i] = pool_alloc (container,
1269 n_entries * sizeof ***nr_data);
1278 if (!nr_read_splits (0))
1281 for (bp = contents; *bp != EOC; bp = np)
1285 /* Trap the CONTENTS that we should parse in this pass
1286 between bp and ep. Set np to the starting bp for next
1291 while (*ep != RPAREN)
1299 while (*ep != EOC && *ep != LPAREN)
1308 for (i = 0; i < (per_factor ? cells : 1); i++)
1312 for (cp = bp; cp < ep; cp++)
1313 if (!nr_read_data_lines (per_factor, i, *cp, cp != bp))
1321 if (default_dict.n_splits == 0 || !another_token ())
1326 /* Read the split file variables. If COMPARE is 1, compares the
1327 values read to the last values read and returns 1 if they're equal,
1330 nr_read_splits (int compare)
1332 static int just_read = 0;
1334 if (compare && just_read)
1340 if (default_dict.n_splits == 0)
1346 split_values[0] = ++default_dict.splits[0]->p.mxd.subtype;
1356 for (i = 0; i < default_dict.n_splits; i++)
1362 msg (SE, _("Syntax error expecting SPLIT FILE value %s."),
1368 split_values[i] = mtokval;
1369 else if (split_values[i] != mtokval)
1371 msg (SE, _("Expecting value %g for %s."),
1372 split_values[i], default_dict.splits[i]->name);
1381 /* Read the factors for cell CELL. If COMPARE is 1, compares the
1382 values read to the last values read and returns 1 if they're equal,
1385 nr_read_factors (int cell)
1392 assert (max_cell_index >= cell);
1393 if (cell != max_cell_index)
1404 for (i = 0; i < n_factors; i++)
1410 msg (SE, _("Syntax error expecting factor value %s."),
1416 nr_factor_values[i + n_factors * cell] = mtokval;
1417 else if (nr_factor_values[i + n_factors * cell] != mtokval)
1419 msg (SE, _("Syntax error expecting value %g for %s %s."),
1420 nr_factor_values[i + n_factors * cell],
1421 factors[i]->name, context ());
1430 /* Write the contents of a cell having content type CONTENT and data
1431 CP to the active file. */
1433 dump_cell_content (int content, double *cp)
1435 int type = content_type[content];
1438 st_bare_pad_copy (temp_case->data[rowtype_->fv].s,
1439 content_names[content], 8);
1442 memset (&temp_case->data[varname_->fv].s, ' ', 8);
1446 int n_lines = (type == 1) ? n_continuous : 1;
1449 for (i = 0; i < n_lines; i++)
1453 for (j = 0; j < n_continuous; j++)
1455 temp_case->data[(default_dict.var
1456 [first_continuous + j]->fv)].f = *cp;
1457 debug_printf (("c:%s(%g) ",
1458 default_dict.var[first_continuous + j]->name,
1463 st_bare_pad_copy (temp_case->data[varname_->fv].s,
1464 default_dict.var[first_continuous + i]->name,
1466 debug_printf (("\n"));
1472 /* Finally dump out everything from nr_data[] to the output file. */
1474 nr_output_data (void)
1479 for (i = 0; i < default_dict.n_splits; i++)
1480 temp_case->data[default_dict.splits[i]->fv].f = split_values[i];
1487 for (cell = 0; cell < cells; cell++)
1492 for (factor = 0; factor < n_factors; factor++)
1494 temp_case->data[factors[factor]->fv].f
1495 = nr_factor_values[factor + cell * n_factors];
1496 debug_printf (("f:%s ", factors[factor]->name));
1503 for (content = 0; content <= PROX; content++)
1504 if (is_per_factor[content])
1506 assert (nr_data[content] != NULL
1507 && nr_data[content][cell] != NULL);
1509 dump_cell_content (content, nr_data[content][cell]);
1521 for (factor = 0; factor < n_factors; factor++)
1522 temp_case->data[factors[factor]->fv].f = SYSMIS;
1525 for (content = 0; content <= PROX; content++)
1526 if (!is_per_factor[content] && nr_data[content] != NULL)
1527 dump_cell_content (content, nr_data[content][0]);
1531 /* Back end, with ROWTYPE_. */
1533 /* Type of current row. */
1534 static int wr_content;
1536 /* All the data for one set of factor values. */
1540 int n_rows[PROX + 1];
1541 double *data[PROX + 1];
1542 struct factor_data *next;
1545 /* All the data, period. */
1546 struct factor_data *wr_data;
1548 /* Current factor. */
1549 struct factor_data *wr_current;
1551 static int wr_read_splits (void);
1552 static int wr_output_data (void);
1553 static int wr_read_rowtype (void);
1554 static int wr_read_factors (void);
1555 static int wr_read_indeps (void);
1556 static int matrix_data_read_with_rowtype (void);
1558 /* When ROWTYPE_ appears in the data, reads the matrices and writes
1559 them to the output file. */
1561 read_matrices_with_rowtype (void)
1564 wr_data = wr_current = NULL;
1565 split_values = NULL;
1568 matrix_data_source.read = (void (*)(void)) matrix_data_read_with_rowtype;
1569 vfm_source = &matrix_data_source;
1571 procedure (NULL, NULL, NULL);
1573 free (split_values);
1574 fh_close_handle (data_file);
1577 /* Read from the data file and write it to the active file. */
1579 matrix_data_read_with_rowtype (void)
1583 if (!wr_read_splits ())
1586 if (!wr_read_factors ())
1589 if (!wr_read_indeps ())
1592 while (another_token ());
1598 /* Read the split file variables. If they differ from the previous
1599 set of split variables then output the data. Returns success. */
1601 wr_read_splits (void)
1605 if (default_dict.n_splits == 0)
1613 split_values = xmalloc (sizeof *split_values * default_dict.n_splits);
1620 for (i = 0; i < default_dict.n_splits; i++)
1626 msg (SE, _("Syntax error %s expecting SPLIT FILE value."),
1631 if (compare && split_values[i] != mtokval && !different)
1633 if (!wr_output_data ())
1638 split_values[i] = mtokval;
1645 /* Compares doubles A and B, treating SYSMIS as greatest. */
1647 compare_doubles (const void *a_, const void *b_, void *aux unused)
1649 const double *a = a_;
1650 const double *b = b_;
1654 else if (*a == SYSMIS)
1656 else if (*b == SYSMIS)
1664 /* Return strcmp()-type comparison of the n_factors factors at _A and
1665 _B. Sort missing values toward the end. */
1667 compare_factors (const void *a_, const void *b_)
1669 struct factor_data *const *pa = a_;
1670 struct factor_data *const *pb = b_;
1671 const double *a = (*pa)->factors;
1672 const double *b = (*pb)->factors;
1674 return lexicographical_compare (a, n_factors,
1677 compare_doubles, NULL);
1680 /* Write out the data for the current split file to the active
1683 wr_output_data (void)
1688 for (i = 0; i < default_dict.n_splits; i++)
1689 temp_case->data[default_dict.splits[i]->fv].f = split_values[i];
1692 /* Sort the wr_data list. */
1694 struct factor_data **factors;
1695 struct factor_data *iter;
1698 factors = xmalloc (sizeof *factors * cells);
1700 for (i = 0, iter = wr_data; iter; iter = iter->next, i++)
1703 qsort (factors, cells, sizeof *factors, compare_factors);
1705 wr_data = factors[0];
1706 for (i = 0; i < cells - 1; i++)
1707 factors[i]->next = factors[i + 1];
1708 factors[cells - 1]->next = NULL;
1713 /* Write out records for every set of factor values. */
1715 struct factor_data *iter;
1717 for (iter = wr_data; iter; iter = iter->next)
1722 for (factor = 0; factor < n_factors; factor++)
1724 temp_case->data[factors[factor]->fv].f
1725 = iter->factors[factor];
1726 debug_printf (("f:%s ", factors[factor]->name));
1733 for (content = 0; content <= PROX; content++)
1735 if (!iter->n_rows[content])
1739 int type = content_type[content];
1740 int n_lines = (type == 1
1742 - (section != FULL && diag == NODIAGONAL))
1745 if (n_lines != iter->n_rows[content])
1747 msg (SE, _("Expected %d lines of data for %s content; "
1748 "actually saw %d lines. No data will be "
1749 "output for this content."),
1750 n_lines, content_names[content],
1751 iter->n_rows[content]);
1756 fill_matrix (content, iter->data[content]);
1758 dump_cell_content (content, iter->data[content]);
1764 pool_destroy (container);
1765 container = pool_create ();
1767 wr_data = wr_current = NULL;
1772 /* Read ROWTYPE_ from the data file. Return success. */
1774 wr_read_rowtype (void)
1776 if (wr_content != -1)
1778 msg (SE, _("Multiply specified ROWTYPE_ %s."), context ());
1783 msg (SE, _("Syntax error %s expecting ROWTYPE_ string."), context ());
1791 memcpy (s, mtokstr, min (15, mtoklen));
1792 s[min (15, mtoklen)] = 0;
1794 for (cp = s; *cp; cp++)
1795 *cp = toupper ((unsigned char) *cp);
1797 wr_content = string_to_content_type (s, NULL);
1800 if (wr_content == -1)
1802 msg (SE, _("Syntax error %s."), context ());
1809 /* Read the factors for the current row. Select a set of factors and
1810 point wr_current to it. */
1812 wr_read_factors (void)
1814 double *factor_values = local_alloc (sizeof *factor_values * n_factors);
1820 for (i = 0; i < n_factors; i++)
1826 if (!wr_read_rowtype ())
1833 msg (SE, _("Syntax error expecting factor value %s."),
1838 factor_values[i] = mtokval;
1841 if (wr_content == -1)
1845 if (!wr_read_rowtype ())
1849 /* Try the most recent factor first as a simple caching
1855 for (i = 0; i < n_factors; i++)
1856 if (factor_values[i] != wr_current->factors[i])
1861 /* Linear search through the list. */
1864 struct factor_data *iter;
1866 for (iter = wr_data; iter; iter = iter->next)
1870 for (i = 0; i < n_factors; i++)
1871 if (factor_values[i] != iter->factors[i])
1881 /* Not found. Make a new item. */
1883 struct factor_data *new = pool_alloc (container, sizeof *new);
1885 new->factors = pool_alloc (container, sizeof *new->factors * n_factors);
1890 for (i = 0; i < n_factors; i++)
1891 new->factors[i] = factor_values[i];
1897 for (i = 0; i <= PROX; i++)
1900 new->data[i] = NULL;
1904 new->next = wr_data;
1905 wr_data = wr_current = new;
1910 local_free (factor_values);
1914 local_free (factor_values);
1918 /* Read the independent variables into wr_current. */
1920 wr_read_indeps (void)
1922 struct factor_data *c = wr_current;
1923 const int type = content_type[wr_content];
1924 const int n_rows = c->n_rows[wr_content];
1928 /* Allocate room for data if necessary. */
1929 if (c->data[wr_content] == NULL)
1931 int n_items = n_continuous;
1933 n_items *= n_continuous;
1935 c->data[wr_content] = pool_alloc (container,
1936 sizeof **c->data * n_items);
1939 cp = &c->data[wr_content][n_rows * n_continuous];
1941 /* Figure out how much to read from this line. */
1948 msg (SE, _("Duplicate specification for %s."),
1949 content_names[wr_content]);
1953 n_cols = n_continuous;
1958 if (n_rows >= n_continuous - (section != FULL && diag == NODIAGONAL))
1960 msg (SE, _("Too many rows of matrix data for %s."),
1961 content_names[wr_content]);
1968 n_cols = n_rows + 1;
1969 if (diag == NODIAGONAL)
1974 n_cols = n_continuous - n_rows;
1975 if (diag == NODIAGONAL)
1982 n_cols = n_continuous;
1991 c->n_rows[wr_content]++;
1993 debug_printf ((" (c=%p,r=%d,n=%d)", c, n_rows + 1, n_cols));
1995 /* Read N_COLS items at CP. */
1999 for (j = 0; j < n_cols; j++)
2005 msg (SE, _("Syntax error expecting value for %s %s."),
2006 default_dict.var[first_continuous + j]->name, context ());
2012 if (!force_eol (content_names[wr_content]))
2014 debug_printf (("\n"));
2020 /* Matrix source. */
2022 struct case_stream matrix_data_source =