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 const 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 /* A MATRIX DATA input program. */
101 struct matrix_data_pgm
103 struct pool *container; /* Arena used for all allocations. */
104 struct file_handle *data_file; /* The data file to be read. */
107 enum format_type fmt; /* LIST or FREE. */
108 enum matrix_section section;/* LOWER or UPPER or FULL. */
109 enum include_diagonal diag; /* DIAGONAL or NODIAGONAL. */
111 int explicit_rowtype; /* ROWTYPE_ specified explicitly in data? */
112 struct variable *rowtype_, *varname_; /* ROWTYPE_, VARNAME_ variables. */
114 struct variable *single_split; /* Single SPLIT FILE variable. */
116 /* Factor variables. */
117 int n_factors; /* Number of factor variables. */
118 struct variable **factors; /* Factor variables. */
119 int is_per_factor[PROX + 1]; /* Is there per-factor data? */
121 int cells; /* Number of cells, or -1 if none. */
123 int pop_n; /* Population N specified by user. */
125 /* CONTENTS subcommand. */
126 int contents[EOC * 3 + 1]; /* Contents. */
127 int n_contents; /* Number of entries. */
130 int n_continuous; /* Number of continuous variables. */
131 int first_continuous; /* Index into default_dict.var of
132 first continuous variable. */
135 static const struct case_source_class matrix_data_with_rowtype_source_class;
136 static const struct case_source_class matrix_data_without_rowtype_source_class;
138 static int compare_variables_by_mxd_vartype (const void *pa,
140 static void read_matrices_without_rowtype (struct matrix_data_pgm *);
141 static void read_matrices_with_rowtype (struct matrix_data_pgm *);
142 static int string_to_content_type (char *, int *);
145 static void debug_print (void);
149 cmd_matrix_data (void)
152 struct matrix_data_pgm *mx;
156 lex_match_id ("MATRIX");
157 lex_match_id ("DATA");
159 discard_variables ();
161 pool = pool_create ();
162 mx = pool_alloc (pool, sizeof *mx);
163 mx->container = pool;
164 mx->data_file = inline_file;
168 mx->explicit_rowtype = 0;
171 mx->single_split = NULL;
174 memset (mx->is_per_factor, 0, sizeof mx->is_per_factor);
178 mx->n_continuous = 0;
179 mx->first_continuous = 0;
184 if (lex_match_id ("VARIABLES"))
191 msg (SE, _("VARIABLES subcommand multiply specified."));
197 if (!parse_DATA_LIST_vars (&v, &nv, PV_NO_DUPLICATE))
203 for (i = 0; i < nv; i++)
204 if (!strcmp (v[i], "VARNAME_"))
206 msg (SE, _("VARNAME_ cannot be explicitly specified on "
208 for (i = 0; i < nv; i++)
218 for (i = 0; i < nv; i++)
220 struct variable *new_var;
222 if (strcmp (v[i], "ROWTYPE_"))
224 new_var = dict_create_var_assert (default_dict, v[i], 0);
225 new_var->p.mxd.vartype = MXD_CONTINUOUS;
226 new_var->p.mxd.subtype = i;
229 mx->explicit_rowtype = 1;
236 mx->rowtype_ = dict_create_var_assert (default_dict,
238 mx->rowtype_->p.mxd.vartype = MXD_ROWTYPE;
239 mx->rowtype_->p.mxd.subtype = 0;
242 else if (lex_match_id ("FILE"))
245 mx->data_file = fh_parse_file_handle ();
246 if (mx->data_file == NULL)
249 else if (lex_match_id ("FORMAT"))
253 while (token == T_ID)
255 if (lex_match_id ("LIST"))
257 else if (lex_match_id ("FREE"))
259 else if (lex_match_id ("LOWER"))
261 else if (lex_match_id ("UPPER"))
263 else if (lex_match_id ("FULL"))
265 else if (lex_match_id ("DIAGONAL"))
267 else if (lex_match_id ("NODIAGONAL"))
268 mx->diag = NODIAGONAL;
271 lex_error (_("in FORMAT subcommand"));
276 else if (lex_match_id ("SPLIT"))
282 msg (SE, _("SPLIT subcommand multiply specified."));
289 lex_error (_("in SPLIT subcommand"));
293 if (dict_lookup_var (default_dict, tokid) == NULL
294 && (lex_look_ahead () == '.' || lex_look_ahead () == '/'))
296 if (!strcmp (tokid, "ROWTYPE_") || !strcmp (tokid, "VARNAME_"))
298 msg (SE, _("Split variable may not be named ROWTYPE_ "
303 mx->single_split = dict_create_var_assert (default_dict,
307 mx->single_split->p.mxd.vartype = MXD_CONTINUOUS;
309 dict_set_split_vars (default_dict, &mx->single_split, 1);
313 struct variable **split;
316 if (!parse_variables (default_dict, &split, &n, PV_NO_DUPLICATE))
319 dict_set_split_vars (default_dict, split, n);
323 struct variable *const *split = dict_get_split_vars (default_dict);
324 size_t split_cnt = dict_get_split_cnt (default_dict);
327 for (i = 0; i < split_cnt; i++)
329 if (split[i]->p.mxd.vartype != MXD_CONTINUOUS)
331 msg (SE, _("Split variable %s is already another type."),
335 split[i]->p.mxd.vartype = MXD_SPLIT;
336 split[i]->p.mxd.subtype = i;
340 else if (lex_match_id ("FACTORS"))
346 msg (SE, _("FACTORS subcommand multiply specified."));
351 if (!parse_variables (default_dict, &mx->factors, &mx->n_factors, PV_NONE))
357 for (i = 0; i < mx->n_factors; i++)
359 if (mx->factors[i]->p.mxd.vartype != MXD_CONTINUOUS)
361 msg (SE, _("Factor variable %s is already another type."),
365 mx->factors[i]->p.mxd.vartype = MXD_FACTOR;
366 mx->factors[i]->p.mxd.subtype = i;
370 else if (lex_match_id ("CELLS"))
376 msg (SE, _("CELLS subcommand multiply specified."));
380 if (!lex_integer_p () || lex_integer () < 1)
382 lex_error (_("expecting positive integer"));
386 mx->cells = lex_integer ();
389 else if (lex_match_id ("N"))
395 msg (SE, _("N subcommand multiply specified."));
399 if (!lex_integer_p () || lex_integer () < 1)
401 lex_error (_("expecting positive integer"));
405 mx->pop_n = lex_integer ();
408 else if (lex_match_id ("CONTENTS"))
410 int inside_parens = 0;
411 unsigned collide = 0;
416 msg (SE, _("CONTENTS subcommand multiply specified."));
426 for (i = 0; i <= PROX; i++)
427 mx->is_per_factor[i] = 0;
436 msg (SE, _("Nested parentheses not allowed."));
442 else if (lex_match (')'))
446 msg (SE, _("Mismatched right parenthesis (`(')."));
449 if (mx->contents[mx->n_contents - 1] == LPAREN)
451 msg (SE, _("Empty parentheses not allowed."));
464 lex_error (_("in CONTENTS subcommand"));
468 content_type = string_to_content_type (tokid,
470 if (content_type == -1)
472 lex_error (_("in CONTENTS subcommand"));
477 if (collide & (1 << collide_index))
479 msg (SE, _("Content multiply specified for %s."),
480 content_names[content_type]);
483 collide |= (1 << collide_index);
486 mx->is_per_factor[item] = inside_parens;
488 mx->contents[mx->n_contents++] = item;
490 if (token == '/' || token == '.')
496 msg (SE, _("Missing right parenthesis."));
499 mx->contents[mx->n_contents] = EOC;
510 lex_error (_("expecting end of command"));
516 msg (SE, _("Missing VARIABLES subcommand."));
520 if (!mx->n_contents && !mx->explicit_rowtype)
522 msg (SW, _("CONTENTS subcommand not specified: assuming file "
523 "contains only CORR matrix."));
525 mx->contents[0] = CORR;
526 mx->contents[1] = EOC;
530 if (mx->n_factors && !mx->explicit_rowtype && mx->cells == -1)
532 msg (SE, _("Missing CELLS subcommand. CELLS is required "
533 "when ROWTYPE_ is not given in the data and "
534 "factors are present."));
538 if (mx->explicit_rowtype && mx->single_split)
540 msg (SE, _("Split file values must be present in the data when "
541 "ROWTYPE_ is present."));
545 /* Create VARNAME_. */
547 mx->varname_ = dict_create_var_assert (default_dict, "VARNAME_", 8);
548 mx->varname_->p.mxd.vartype = MXD_VARNAME;
549 mx->varname_->p.mxd.subtype = 0;
552 /* Sort the dictionary variables into the desired order for the
553 system file output. */
558 dict_get_vars (default_dict, &v, &nv, 0);
559 qsort (v, nv, sizeof *v, compare_variables_by_mxd_vartype);
560 dict_reorder_vars (default_dict, v, nv);
566 static const struct fmt_spec fmt_tab[MXD_COUNT] =
577 mx->first_continuous = -1;
578 for (i = 0; i < dict_get_var_cnt (default_dict); i++)
580 struct variable *v = dict_get_var (default_dict, i);
581 int type = v->p.mxd.vartype;
583 assert (type >= 0 && type < MXD_COUNT);
584 v->print = v->write = fmt_tab[type];
586 if (type == MXD_CONTINUOUS)
588 if (mx->first_continuous == -1 && type == MXD_CONTINUOUS)
589 mx->first_continuous = i;
593 if (mx->n_continuous == 0)
595 msg (SE, _("No continuous variables specified."));
603 if (mx->explicit_rowtype)
604 read_matrices_with_rowtype (mx);
606 read_matrices_without_rowtype (mx);
608 pool_destroy (mx->container);
613 discard_variables ();
615 pool_destroy (mx->container);
619 /* Look up string S as a content-type name and return the
620 corresponding enumerated value, or -1 if there is no match. If
621 COLLIDE is non-NULL then *COLLIDE returns a value (suitable for use
622 as a bit-index) which can be used for determining whether a related
623 statistic has already been used. */
625 string_to_content_type (char *s, int *collide)
636 {N_VECTOR, 0, "N_VECTOR"},
638 {N_SCALAR, 0, "N_SCALAR"},
639 {N_MATRIX, 1, "N_MATRIX"},
641 {STDDEV, 3, "STDDEV"},
653 for (tp = tab; tp->value != -1; tp++)
654 if (!strcmp (s, tp->string))
657 *collide = tp->collide;
664 /* Compare two variables using p.mxd.vartype and p.mxd.subtype
667 compare_variables_by_mxd_vartype (const void *a_, const void *b_)
669 struct variable *const *pa = a_;
670 struct variable *const *pb = b_;
671 const struct matrix_data_proc *a = &(*pa)->p.mxd;
672 const struct matrix_data_proc *b = &(*pb)->p.mxd;
674 if (a->vartype != b->vartype)
675 return a->vartype > b->vartype ? 1 : -1;
677 return a->subtype < b->subtype ? -1 : a->subtype > b->subtype;
681 /* Print out the command as input. */
685 printf ("MATRIX DATA\n\t/VARIABLES=");
690 for (i = 0; i < default_dict.nvar; i++)
691 printf ("%s ", default_dict.var[i]->name);
695 printf ("\t/FORMAT=");
698 else if (fmt == FREE)
702 if (section == LOWER)
704 else if (section == UPPER)
706 else if (section == FULL)
710 if (diag == DIAGONAL)
711 printf (" DIAGONAL\n");
712 else if (diag == NODIAGONAL)
713 printf (" NODIAGONAL\n");
717 if (dict_get_split_cnt (default_dict) != 0)
721 printf ("\t/SPLIT=");
722 for (i = 0; i < dict_get_split_cnt (default_dict); i++)
723 printf ("%s ", dict_get_split_vars (default_dict)[i]->name);
725 printf ("\t/* single split");
733 printf ("\t/FACTORS=");
734 for (i = 0; i < n_factors; i++)
735 printf ("%s ", factors[i]->name);
740 printf ("\t/CELLS=%d\n", cells);
743 printf ("\t/N=%d\n", mx->pop_n);
750 printf ("\t/CONTENTS=");
751 for (i = 0; i < mx->n_contents; i++)
753 if (mx->contents[i] == LPAREN)
760 else if (mx->contents[i] == RPAREN)
768 assert (mx->contents[i] >= 0 && mx->contents[i] <= PROX);
771 printf ("%s", content_names[mx->contents[i]]);
778 #endif /* DEBUGGING */
780 /* Matrix tokenizer. */
782 /* Matrix token types. */
783 enum matrix_token_type
791 enum matrix_token_type type;
792 double number; /* MNUM: token value. */
793 char *string; /* MSTR: token string; not null-terminated. */
794 int length; /* MSTR: tokstr length. */
797 static int mget_token (struct matrix_token *, struct file_handle *);
800 #define mget_token(TOKEN, HANDLE) mget_token_dump(TOKEN, HANDLE)
803 mdump_token (const struct matrix_token *token)
808 printf (" #%g", token->number);
811 printf (" '%.*s'", token->length, token->string);
820 mget_token_dump (struct matrix_token *token, struct file_handle *data_file)
822 int result = (mget_token) (token, data_file);
828 /* Return the current position in DATA_FILE. */
830 context (struct file_handle *data_file)
834 char *p = dfm_get_record (data_file, &len);
837 strcpy (buf, "at end of line");
841 int n_copy = min (10, len);
842 cp = stpcpy (buf, "before `");
843 while (n_copy && isspace ((unsigned char) *p))
845 while (n_copy && !isspace ((unsigned char) *p))
846 *cp++ = *p++, n_copy--;
854 /* Is there at least one token left in the data file? */
856 another_token (struct file_handle *data_file)
863 cp = dfm_get_record (data_file, &len);
868 while (isspace ((unsigned char) *cp) && cp < ep)
874 dfm_fwd_record (data_file);
877 dfm_set_record (data_file, cp);
882 /* Parse a MATRIX DATA token from mx->data_file into TOKEN. */
884 (mget_token) (struct matrix_token *token, struct file_handle *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);
907 first_column = dfm_get_cur_col (data_file) + 1;
909 /* Three types of fields: quoted with ', quoted with ", unquoted. */
910 if (*cp == '\'' || *cp == '"')
915 token->string = ++cp;
916 while (cp < ep && *cp != quote)
918 token->length = cp - token->string;
922 msg (SW, _("Scope of string exceeds line."));
926 int is_num = isdigit ((unsigned char) *cp) || *cp == '.';
928 token->string = cp++;
929 while (cp < ep && !isspace ((unsigned char) *cp) && *cp != ','
930 && *cp != '-' && *cp != '+')
932 if (isdigit ((unsigned char) *cp))
935 if ((tolower ((unsigned char) *cp) == 'd'
936 || tolower ((unsigned char) *cp) == 'e')
937 && (cp[1] == '+' || cp[1] == '-'))
943 token->length = cp - token->string;
944 assert (token->length);
950 di.s = token->string;
951 di.e = token->string + token->length;
952 di.v = (union value *) &token->number;
953 di.f1 = first_column;
954 di.format.type = FMT_F;
955 di.format.w = token->length;
965 dfm_set_record (data_file, cp);
970 /* Forcibly skip the end of a line for content type CONTENT in
973 force_eol (struct file_handle *data_file, const char *content)
978 cp = dfm_get_record (data_file, &len);
981 while (len && isspace (*cp))
986 msg (SE, _("End of line expected %s while reading %s."),
987 context (data_file), content);
991 dfm_fwd_record (data_file);
996 /* Back end, omitting ROWTYPE_. */
998 /* MATRIX DATA data. */
999 static double ***nr_data;
1001 /* Factor values. */
1002 static double *nr_factor_values;
1004 /* Largest-numbered cell that we have read in thus far, plus one. */
1005 static int max_cell_index;
1007 /* SPLIT FILE variable values. */
1008 static double *split_values;
1010 static int nr_read_splits (struct matrix_data_pgm *, int compare);
1011 static int nr_read_factors (struct matrix_data_pgm *, int cell);
1012 static void nr_output_data (struct matrix_data_pgm *,
1013 write_case_func *, write_case_data);
1014 static void matrix_data_read_without_rowtype (struct case_source *source,
1018 /* Read from the data file and write it to the active file. */
1020 read_matrices_without_rowtype (struct matrix_data_pgm *mx)
1022 if (mx->cells == -1)
1025 split_values = xmalloc (sizeof *split_values
1026 * dict_get_split_cnt (default_dict));
1027 nr_factor_values = xmalloc (sizeof *nr_factor_values * mx->n_factors * mx->cells);
1030 vfm_source = create_case_source (&matrix_data_without_rowtype_source_class,
1033 procedure (NULL, NULL, NULL, NULL);
1035 free (split_values);
1036 free (nr_factor_values);
1038 fh_close_handle (mx->data_file);
1041 /* Mirror data across the diagonal of matrix CP which contains
1042 CONTENT type data. */
1044 fill_matrix (struct matrix_data_pgm *mx, int content, double *cp)
1046 int type = content_type[content];
1048 if (type == 1 && mx->section != FULL)
1050 if (mx->diag == NODIAGONAL)
1052 const double fill = content == CORR ? 1.0 : SYSMIS;
1055 for (i = 0; i < mx->n_continuous; i++)
1056 cp[i * (1 + mx->n_continuous)] = fill;
1062 if (mx->section == LOWER)
1064 int n_lines = mx->n_continuous;
1065 if (mx->section != FULL && mx->diag == NODIAGONAL)
1068 for (r = 1; r < n_lines; r++)
1069 for (c = 0; c < r; c++)
1070 cp[r + c * mx->n_continuous] = cp[c + r * mx->n_continuous];
1074 assert (mx->section == UPPER);
1075 for (r = 1; r < mx->n_continuous; r++)
1076 for (c = 0; c < r; c++)
1077 cp[c + r * mx->n_continuous] = cp[r + c * mx->n_continuous];
1085 for (c = 1; c < mx->n_continuous; c++)
1090 /* Read data lines for content type CONTENT from the data file.
1091 If PER_FACTOR is nonzero, then factor information is read from
1092 the data file. Data is for cell number CELL. */
1094 nr_read_data_lines (struct matrix_data_pgm *mx,
1095 int per_factor, int cell, int content, int compare)
1098 const int type = content_type[content];
1100 /* Number of lines that must be parsed from the data file for this
1104 /* Current position in vector or matrix. */
1114 n_lines = mx->n_continuous;
1115 if (mx->section != FULL && mx->diag == NODIAGONAL)
1119 cp = nr_data[content][cell];
1120 if (type == 1 && mx->section == LOWER && mx->diag == NODIAGONAL)
1121 cp += mx->n_continuous;
1123 for (i = 0; i < n_lines; i++)
1127 if (!nr_read_splits (mx, 1))
1129 if (per_factor && !nr_read_factors (mx, cell))
1136 n_cols = mx->n_continuous;
1139 switch (mx->section)
1146 n_cols = mx->n_continuous - i;
1147 if (mx->diag == NODIAGONAL)
1154 n_cols = mx->n_continuous;
1170 for (j = 0; j < n_cols; j++)
1172 struct matrix_token token;
1173 if (!mget_token (&token, mx->data_file))
1175 if (token.type != MNUM)
1177 msg (SE, _("expecting value for %s %s"),
1178 dict_get_var (default_dict, j)->name,
1179 context (mx->data_file));
1183 *cp++ = token.number;
1186 && !force_eol (mx->data_file, content_names[content]))
1188 debug_printf (("\n"));
1191 if (mx->section == LOWER)
1192 cp += mx->n_continuous - n_cols;
1195 fill_matrix (mx, content, nr_data[content][cell]);
1200 /* When ROWTYPE_ does not appear in the data, reads the matrices and
1201 writes them to the output file. Returns success. */
1203 matrix_data_read_without_rowtype (struct case_source *source,
1204 write_case_func *write_case,
1205 write_case_data wc_data)
1207 struct matrix_data_pgm *mx = source->aux;
1212 nr_data = pool_alloc (mx->container, (PROX + 1) * sizeof *nr_data);
1217 for (i = 0; i <= PROX; i++)
1221 for (cp = mx->contents; *cp != EOC; cp++)
1222 if (*cp != LPAREN && *cp != RPAREN)
1224 int per_factor = mx->is_per_factor[*cp];
1227 n_entries = mx->n_continuous;
1228 if (content_type[*cp] == 1)
1229 n_entries *= mx->n_continuous;
1232 int n_vectors = per_factor ? mx->cells : 1;
1235 nr_data[*cp] = pool_alloc (mx->container,
1236 n_vectors * sizeof **nr_data);
1238 for (i = 0; i < n_vectors; i++)
1239 nr_data[*cp][i] = pool_alloc (mx->container,
1240 n_entries * sizeof ***nr_data);
1249 if (!nr_read_splits (mx, 0))
1252 for (bp = mx->contents; *bp != EOC; bp = np)
1256 /* Trap the CONTENTS that we should parse in this pass
1257 between bp and ep. Set np to the starting bp for next
1262 while (*ep != RPAREN)
1270 while (*ep != EOC && *ep != LPAREN)
1279 for (i = 0; i < (per_factor ? mx->cells : 1); i++)
1283 for (cp = bp; cp < ep; cp++)
1284 if (!nr_read_data_lines (mx, per_factor, i, *cp, cp != bp))
1290 nr_output_data (mx, write_case, wc_data);
1292 if (dict_get_split_cnt (default_dict) == 0
1293 || !another_token (mx->data_file))
1298 /* Read the split file variables. If COMPARE is 1, compares the
1299 values read to the last values read and returns 1 if they're equal,
1302 nr_read_splits (struct matrix_data_pgm *mx, int compare)
1304 static int just_read = 0;
1308 if (compare && just_read)
1314 if (dict_get_split_vars (default_dict) == NULL)
1317 if (mx->single_split)
1321 = ++dict_get_split_vars (default_dict)[0]->p.mxd.subtype;
1328 split_cnt = dict_get_split_cnt (default_dict);
1329 for (i = 0; i < split_cnt; i++)
1331 struct matrix_token token;
1332 if (!mget_token (&token, mx->data_file))
1334 if (token.type != MNUM)
1336 msg (SE, _("Syntax error expecting SPLIT FILE value %s."),
1337 context (mx->data_file));
1342 split_values[i] = token.number;
1343 else if (split_values[i] != token.number)
1345 msg (SE, _("Expecting value %g for %s."),
1346 split_values[i], dict_get_split_vars (default_dict)[i]->name);
1354 /* Read the factors for cell CELL. If COMPARE is 1, compares the
1355 values read to the last values read and returns 1 if they're equal,
1358 nr_read_factors (struct matrix_data_pgm *mx, int cell)
1362 if (mx->n_factors == 0)
1365 assert (max_cell_index >= cell);
1366 if (cell != max_cell_index)
1377 for (i = 0; i < mx->n_factors; i++)
1379 struct matrix_token token;
1380 if (!mget_token (&token, mx->data_file))
1382 if (token.type != MNUM)
1384 msg (SE, _("Syntax error expecting factor value %s."),
1385 context (mx->data_file));
1390 nr_factor_values[i + mx->n_factors * cell] = token.number;
1391 else if (nr_factor_values[i + mx->n_factors * cell] != token.number)
1393 msg (SE, _("Syntax error expecting value %g for %s %s."),
1394 nr_factor_values[i + mx->n_factors * cell],
1395 mx->factors[i]->name, context (mx->data_file));
1404 /* Write the contents of a cell having content type CONTENT and data
1405 CP to the active file. */
1407 dump_cell_content (struct matrix_data_pgm *mx, int content, double *cp,
1408 write_case_func *write_case, write_case_data wc_data)
1410 int type = content_type[content];
1413 st_bare_pad_copy (temp_case->data[mx->rowtype_->fv].s,
1414 content_names[content], 8);
1417 memset (&temp_case->data[mx->varname_->fv].s, ' ', 8);
1421 int n_lines = (type == 1) ? mx->n_continuous : 1;
1424 for (i = 0; i < n_lines; i++)
1428 for (j = 0; j < mx->n_continuous; j++)
1430 int fv = dict_get_var (default_dict, mx->first_continuous + j)->fv;
1431 temp_case->data[fv].f = *cp;
1435 st_bare_pad_copy (temp_case->data[mx->varname_->fv].s,
1436 dict_get_var (default_dict,
1437 mx->first_continuous + i)->name,
1439 write_case (wc_data);
1444 /* Finally dump out everything from nr_data[] to the output file. */
1446 nr_output_data (struct matrix_data_pgm *mx,
1447 write_case_func *write_case, write_case_data wc_data)
1450 struct variable *const *split;
1454 split_cnt = dict_get_split_cnt (default_dict);
1455 for (i = 0; i < split_cnt; i++)
1456 temp_case->data[split[i]->fv].f = split_values[i];
1463 for (cell = 0; cell < mx->cells; cell++)
1468 for (factor = 0; factor < mx->n_factors; factor++)
1470 temp_case->data[mx->factors[factor]->fv].f
1471 = nr_factor_values[factor + cell * mx->n_factors];
1472 debug_printf (("f:%s ", mx->factors[factor]->name));
1479 for (content = 0; content <= PROX; content++)
1480 if (mx->is_per_factor[content])
1482 assert (nr_data[content] != NULL
1483 && nr_data[content][cell] != NULL);
1485 dump_cell_content (mx, content, nr_data[content][cell],
1486 write_case, wc_data);
1498 for (factor = 0; factor < mx->n_factors; factor++)
1499 temp_case->data[mx->factors[factor]->fv].f = SYSMIS;
1502 for (content = 0; content <= PROX; content++)
1503 if (!mx->is_per_factor[content] && nr_data[content] != NULL)
1504 dump_cell_content (mx, content, nr_data[content][0],
1505 write_case, wc_data);
1509 /* Back end, with ROWTYPE_. */
1511 /* Type of current row. */
1512 static int wr_content;
1514 /* All the data for one set of factor values. */
1518 int n_rows[PROX + 1];
1519 double *data[PROX + 1];
1520 struct factor_data *next;
1523 /* All the data, period. */
1524 struct factor_data *wr_data;
1526 /* Current factor. */
1527 struct factor_data *wr_current;
1529 static int wr_read_splits (struct matrix_data_pgm *,
1530 write_case_func *, write_case_data);
1531 static int wr_output_data (struct matrix_data_pgm *, write_case_func *, write_case_data);
1532 static int wr_read_rowtype (const struct matrix_token *, struct file_handle *);
1533 static int wr_read_factors (struct matrix_data_pgm *);
1534 static int wr_read_indeps (struct matrix_data_pgm *);
1535 static void matrix_data_read_with_rowtype (struct case_source *,
1539 /* When ROWTYPE_ appears in the data, reads the matrices and writes
1540 them to the output file. */
1542 read_matrices_with_rowtype (struct matrix_data_pgm *mx)
1544 wr_data = wr_current = NULL;
1545 split_values = NULL;
1548 vfm_source = create_case_source (&matrix_data_with_rowtype_source_class, mx);
1550 procedure (NULL, NULL, NULL, NULL);
1552 free (split_values);
1553 fh_close_handle (mx->data_file);
1556 /* Read from the data file and write it to the active file. */
1558 matrix_data_read_with_rowtype (struct case_source *source,
1559 write_case_func *write_case,
1560 write_case_data wc_data)
1562 struct matrix_data_pgm *mx = source->aux;
1566 if (!wr_read_splits (mx, write_case, wc_data))
1569 if (!wr_read_factors (mx))
1572 if (!wr_read_indeps (mx))
1575 while (another_token (mx->data_file));
1577 wr_output_data (mx, write_case, wc_data);
1580 /* Read the split file variables. If they differ from the previous
1581 set of split variables then output the data. Returns success. */
1583 wr_read_splits (struct matrix_data_pgm *mx,
1584 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++)
1608 struct matrix_token token;
1609 if (!mget_token (&token, mx->data_file))
1611 if (token.type != MNUM)
1613 msg (SE, _("Syntax error %s expecting SPLIT FILE value."),
1614 context (mx->data_file));
1618 if (compare && split_values[i] != token.number && !different)
1620 if (!wr_output_data (mx, write_case, wc_data))
1625 split_values[i] = token.number;
1632 /* Compares doubles A and B, treating SYSMIS as greatest. */
1634 compare_doubles (const void *a_, const void *b_, void *aux UNUSED)
1636 const double *a = a_;
1637 const double *b = b_;
1641 else if (*a == SYSMIS)
1643 else if (*b == SYSMIS)
1651 /* Return strcmp()-type comparison of the MX->n_factors factors at _A and
1652 _B. Sort missing values toward the end. */
1654 compare_factors (const void *a_, const void *b_, void *mx_)
1656 struct matrix_data_pgm *mx = mx_;
1657 struct factor_data *const *pa = a_;
1658 struct factor_data *const *pb = b_;
1659 const double *a = (*pa)->factors;
1660 const double *b = (*pb)->factors;
1662 return lexicographical_compare_3way (a, mx->n_factors,
1665 compare_doubles, NULL);
1668 /* Write out the data for the current split file to the active
1671 wr_output_data (struct matrix_data_pgm *mx,
1672 write_case_func *write_case, write_case_data wc_data)
1675 struct variable *const *split;
1679 split_cnt = dict_get_split_cnt (default_dict);
1680 for (i = 0; i < split_cnt; i++)
1681 temp_case->data[split[i]->fv].f = split_values[i];
1684 /* Sort the wr_data list. */
1686 struct factor_data **factors;
1687 struct factor_data *iter;
1690 factors = xmalloc (sizeof *factors * mx->cells);
1692 for (i = 0, iter = wr_data; iter; iter = iter->next, i++)
1695 sort (factors, mx->cells, sizeof *factors, compare_factors, mx);
1697 wr_data = factors[0];
1698 for (i = 0; i < mx->cells - 1; i++)
1699 factors[i]->next = factors[i + 1];
1700 factors[mx->cells - 1]->next = NULL;
1705 /* Write out records for every set of factor values. */
1707 struct factor_data *iter;
1709 for (iter = wr_data; iter; iter = iter->next)
1714 for (factor = 0; factor < mx->n_factors; factor++)
1716 temp_case->data[mx->factors[factor]->fv].f
1717 = iter->factors[factor];
1718 debug_printf (("f:%s ", factors[factor]->name));
1725 for (content = 0; content <= PROX; content++)
1727 if (!iter->n_rows[content])
1731 int type = content_type[content];
1732 int n_lines = (type == 1
1734 - (mx->section != FULL && mx->diag == NODIAGONAL))
1737 if (n_lines != iter->n_rows[content])
1739 msg (SE, _("Expected %d lines of data for %s content; "
1740 "actually saw %d lines. No data will be "
1741 "output for this content."),
1742 n_lines, content_names[content],
1743 iter->n_rows[content]);
1748 fill_matrix (mx, content, iter->data[content]);
1750 dump_cell_content (mx, content, iter->data[content],
1751 write_case, wc_data);
1757 pool_destroy (mx->container);
1758 mx->container = pool_create ();
1760 wr_data = wr_current = NULL;
1765 /* Sets ROWTYPE_ based on the given TOKEN read from DATA_FILE.
1768 wr_read_rowtype (const struct matrix_token *token,
1769 struct file_handle *data_file)
1771 if (wr_content != -1)
1773 msg (SE, _("Multiply specified ROWTYPE_ %s."), context (data_file));
1776 if (token->type != MSTR)
1778 msg (SE, _("Syntax error %s expecting ROWTYPE_ string."),
1779 context (data_file));
1787 memcpy (s, token->string, min (15, token->length));
1788 s[min (15, token->length)] = 0;
1790 for (cp = s; *cp; cp++)
1791 *cp = toupper ((unsigned char) *cp);
1793 wr_content = string_to_content_type (s, NULL);
1796 if (wr_content == -1)
1798 msg (SE, _("Syntax error %s."), context (data_file));
1805 /* Read the factors for the current row. Select a set of factors and
1806 point wr_current to it. */
1808 wr_read_factors (struct matrix_data_pgm *mx)
1810 double *factor_values = local_alloc (sizeof *factor_values * mx->n_factors);
1816 for (i = 0; i < mx->n_factors; i++)
1818 struct matrix_token token;
1819 if (!mget_token (&token, mx->data_file))
1821 if (token.type == MSTR)
1823 if (!wr_read_rowtype (&token, mx->data_file))
1825 if (!mget_token (&token, mx->data_file))
1828 if (token.type != MNUM)
1830 msg (SE, _("Syntax error expecting factor value %s."),
1831 context (mx->data_file));
1835 factor_values[i] = token.number;
1838 if (wr_content == -1)
1840 struct matrix_token token;
1841 if (!mget_token (&token, mx->data_file))
1843 if (!wr_read_rowtype (&token, mx->data_file))
1847 /* Try the most recent factor first as a simple caching
1853 for (i = 0; i < mx->n_factors; i++)
1854 if (factor_values[i] != wr_current->factors[i])
1859 /* Linear search through the list. */
1862 struct factor_data *iter;
1864 for (iter = wr_data; iter; iter = iter->next)
1868 for (i = 0; i < mx->n_factors; i++)
1869 if (factor_values[i] != iter->factors[i])
1879 /* Not found. Make a new item. */
1881 struct factor_data *new = pool_alloc (mx->container, sizeof *new);
1883 new->factors = pool_alloc (mx->container, sizeof *new->factors * mx->n_factors);
1888 for (i = 0; i < mx->n_factors; i++)
1889 new->factors[i] = factor_values[i];
1895 for (i = 0; i <= PROX; i++)
1898 new->data[i] = NULL;
1902 new->next = wr_data;
1903 wr_data = wr_current = new;
1908 local_free (factor_values);
1912 local_free (factor_values);
1916 /* Read the independent variables into wr_current. */
1918 wr_read_indeps (struct matrix_data_pgm *mx)
1920 struct factor_data *c = wr_current;
1921 const int type = content_type[wr_content];
1922 const int n_rows = c->n_rows[wr_content];
1926 /* Allocate room for data if necessary. */
1927 if (c->data[wr_content] == NULL)
1929 int n_items = mx->n_continuous;
1931 n_items *= mx->n_continuous;
1933 c->data[wr_content] = pool_alloc (mx->container,
1934 sizeof **c->data * n_items);
1937 cp = &c->data[wr_content][n_rows * mx->n_continuous];
1939 /* Figure out how much to read from this line. */
1946 msg (SE, _("Duplicate specification for %s."),
1947 content_names[wr_content]);
1951 n_cols = mx->n_continuous;
1956 if (n_rows >= mx->n_continuous - (mx->section != FULL && mx->diag == NODIAGONAL))
1958 msg (SE, _("Too many rows of matrix data for %s."),
1959 content_names[wr_content]);
1963 switch (mx->section)
1966 n_cols = n_rows + 1;
1967 if (mx->diag == NODIAGONAL)
1968 cp += mx->n_continuous;
1972 n_cols = mx->n_continuous - n_rows;
1973 if (mx->diag == NODIAGONAL)
1980 n_cols = mx->n_continuous;
1989 c->n_rows[wr_content]++;
1991 debug_printf ((" (c=%p,r=%d,n=%d)", c, n_rows + 1, n_cols));
1993 /* Read N_COLS items at CP. */
1997 for (j = 0; j < n_cols; j++)
1999 struct matrix_token token;
2000 if (!mget_token (&token, mx->data_file))
2002 if (token.type != MNUM)
2004 msg (SE, _("Syntax error expecting value for %s %s."),
2005 dict_get_var (default_dict, mx->first_continuous + j)->name,
2006 context (mx->data_file));
2010 *cp++ = token.number;
2013 && !force_eol (mx->data_file, content_names[wr_content]))
2015 debug_printf (("\n"));
2021 /* Matrix source. */
2023 static const struct case_source_class matrix_data_with_rowtype_source_class =
2026 matrix_data_read_with_rowtype,
2030 static const struct case_source_class
2031 matrix_data_without_rowtype_source_class =
2034 matrix_data_read_without_rowtype,