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. */
129 /* Continuous variables. */
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 discard_variables ();
158 pool = pool_create ();
159 mx = pool_alloc (pool, sizeof *mx);
160 mx->container = pool;
161 mx->data_file = inline_file;
165 mx->explicit_rowtype = 0;
168 mx->single_split = NULL;
171 memset (mx->is_per_factor, 0, sizeof mx->is_per_factor);
175 mx->n_continuous = 0;
176 mx->first_continuous = 0;
181 if (lex_match_id ("VARIABLES"))
188 msg (SE, _("VARIABLES subcommand multiply specified."));
194 if (!parse_DATA_LIST_vars (&v, &nv, PV_NO_DUPLICATE))
200 for (i = 0; i < nv; i++)
201 if (!strcmp (v[i], "VARNAME_"))
203 msg (SE, _("VARNAME_ cannot be explicitly specified on "
205 for (i = 0; i < nv; i++)
215 for (i = 0; i < nv; i++)
217 struct variable *new_var;
219 if (strcmp (v[i], "ROWTYPE_"))
221 new_var = dict_create_var_assert (default_dict, v[i], 0);
222 new_var->p.mxd.vartype = MXD_CONTINUOUS;
223 new_var->p.mxd.subtype = i;
226 mx->explicit_rowtype = 1;
233 mx->rowtype_ = dict_create_var_assert (default_dict,
235 mx->rowtype_->p.mxd.vartype = MXD_ROWTYPE;
236 mx->rowtype_->p.mxd.subtype = 0;
239 else if (lex_match_id ("FILE"))
242 mx->data_file = fh_parse_file_handle ();
243 if (mx->data_file == NULL)
246 else if (lex_match_id ("FORMAT"))
250 while (token == T_ID)
252 if (lex_match_id ("LIST"))
254 else if (lex_match_id ("FREE"))
256 else if (lex_match_id ("LOWER"))
258 else if (lex_match_id ("UPPER"))
260 else if (lex_match_id ("FULL"))
262 else if (lex_match_id ("DIAGONAL"))
264 else if (lex_match_id ("NODIAGONAL"))
265 mx->diag = NODIAGONAL;
268 lex_error (_("in FORMAT subcommand"));
273 else if (lex_match_id ("SPLIT"))
279 msg (SE, _("SPLIT subcommand multiply specified."));
286 lex_error (_("in SPLIT subcommand"));
290 if (dict_lookup_var (default_dict, tokid) == NULL
291 && (lex_look_ahead () == '.' || lex_look_ahead () == '/'))
293 if (!strcmp (tokid, "ROWTYPE_") || !strcmp (tokid, "VARNAME_"))
295 msg (SE, _("Split variable may not be named ROWTYPE_ "
300 mx->single_split = dict_create_var_assert (default_dict,
304 mx->single_split->p.mxd.vartype = MXD_CONTINUOUS;
306 dict_set_split_vars (default_dict, &mx->single_split, 1);
310 struct variable **split;
313 if (!parse_variables (default_dict, &split, &n, PV_NO_DUPLICATE))
316 dict_set_split_vars (default_dict, split, n);
320 struct variable *const *split = dict_get_split_vars (default_dict);
321 size_t split_cnt = dict_get_split_cnt (default_dict);
324 for (i = 0; i < split_cnt; i++)
326 if (split[i]->p.mxd.vartype != MXD_CONTINUOUS)
328 msg (SE, _("Split variable %s is already another type."),
332 split[i]->p.mxd.vartype = MXD_SPLIT;
333 split[i]->p.mxd.subtype = i;
337 else if (lex_match_id ("FACTORS"))
343 msg (SE, _("FACTORS subcommand multiply specified."));
348 if (!parse_variables (default_dict, &mx->factors, &mx->n_factors, PV_NONE))
354 for (i = 0; i < mx->n_factors; i++)
356 if (mx->factors[i]->p.mxd.vartype != MXD_CONTINUOUS)
358 msg (SE, _("Factor variable %s is already another type."),
362 mx->factors[i]->p.mxd.vartype = MXD_FACTOR;
363 mx->factors[i]->p.mxd.subtype = i;
367 else if (lex_match_id ("CELLS"))
373 msg (SE, _("CELLS subcommand multiply specified."));
377 if (!lex_integer_p () || lex_integer () < 1)
379 lex_error (_("expecting positive integer"));
383 mx->cells = lex_integer ();
386 else if (lex_match_id ("N"))
392 msg (SE, _("N subcommand multiply specified."));
396 if (!lex_integer_p () || lex_integer () < 1)
398 lex_error (_("expecting positive integer"));
402 mx->pop_n = lex_integer ();
405 else if (lex_match_id ("CONTENTS"))
407 int inside_parens = 0;
408 unsigned collide = 0;
413 msg (SE, _("CONTENTS subcommand multiply specified."));
423 for (i = 0; i <= PROX; i++)
424 mx->is_per_factor[i] = 0;
433 msg (SE, _("Nested parentheses not allowed."));
439 else if (lex_match (')'))
443 msg (SE, _("Mismatched right parenthesis (`(')."));
446 if (mx->contents[mx->n_contents - 1] == LPAREN)
448 msg (SE, _("Empty parentheses not allowed."));
461 lex_error (_("in CONTENTS subcommand"));
465 content_type = string_to_content_type (tokid,
467 if (content_type == -1)
469 lex_error (_("in CONTENTS subcommand"));
474 if (collide & (1 << collide_index))
476 msg (SE, _("Content multiply specified for %s."),
477 content_names[content_type]);
480 collide |= (1 << collide_index);
483 mx->is_per_factor[item] = inside_parens;
485 mx->contents[mx->n_contents++] = item;
487 if (token == '/' || token == '.')
493 msg (SE, _("Missing right parenthesis."));
496 mx->contents[mx->n_contents] = EOC;
507 lex_error (_("expecting end of command"));
513 msg (SE, _("Missing VARIABLES subcommand."));
517 if (!mx->n_contents && !mx->explicit_rowtype)
519 msg (SW, _("CONTENTS subcommand not specified: assuming file "
520 "contains only CORR matrix."));
522 mx->contents[0] = CORR;
523 mx->contents[1] = EOC;
527 if (mx->n_factors && !mx->explicit_rowtype && mx->cells == -1)
529 msg (SE, _("Missing CELLS subcommand. CELLS is required "
530 "when ROWTYPE_ is not given in the data and "
531 "factors are present."));
535 if (mx->explicit_rowtype && mx->single_split)
537 msg (SE, _("Split file values must be present in the data when "
538 "ROWTYPE_ is present."));
542 /* Create VARNAME_. */
544 mx->varname_ = dict_create_var_assert (default_dict, "VARNAME_", 8);
545 mx->varname_->p.mxd.vartype = MXD_VARNAME;
546 mx->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 mx->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 (mx->first_continuous == -1 && type == MXD_CONTINUOUS)
586 mx->first_continuous = i;
590 if (mx->n_continuous == 0)
592 msg (SE, _("No continuous variables specified."));
600 if (!dfm_open_for_reading (mx->data_file))
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
789 /* A MATRIX DATA parsing token. */
792 enum matrix_token_type type;
793 double number; /* MNUM: token value. */
794 char *string; /* MSTR: token string; not null-terminated. */
795 int length; /* MSTR: tokstr length. */
798 static int mget_token (struct matrix_token *, struct file_handle *);
801 #define mget_token(TOKEN, HANDLE) mget_token_dump(TOKEN, HANDLE)
804 mdump_token (const struct matrix_token *token)
809 printf (" #%g", token->number);
812 printf (" '%.*s'", token->length, token->string);
821 mget_token_dump (struct matrix_token *token, struct file_handle *data_file)
823 int result = (mget_token) (token, data_file);
829 /* Return the current position in DATA_FILE. */
831 context (struct file_handle *data_file)
835 char *p = dfm_get_record (data_file, &len);
838 strcpy (buf, "at end of line");
842 int n_copy = min (10, len);
843 cp = stpcpy (buf, "before `");
844 while (n_copy && isspace ((unsigned char) *p))
846 while (n_copy && !isspace ((unsigned char) *p))
847 *cp++ = *p++, n_copy--;
855 /* Is there at least one token left in the data file? */
857 another_token (struct file_handle *data_file)
864 cp = dfm_get_record (data_file, &len);
869 while (isspace ((unsigned char) *cp) && cp < ep)
875 dfm_fwd_record (data_file);
878 dfm_set_record (data_file, cp);
883 /* Parse a MATRIX DATA token from mx->data_file into TOKEN. */
885 (mget_token) (struct matrix_token *token, struct file_handle *data_file)
893 cp = dfm_get_record (data_file, &len);
898 while (isspace ((unsigned char) *cp) && cp < ep)
904 dfm_fwd_record (data_file);
907 dfm_set_record (data_file, cp);
908 first_column = dfm_get_cur_col (data_file) + 1;
910 /* Three types of fields: quoted with ', quoted with ", unquoted. */
911 if (*cp == '\'' || *cp == '"')
916 token->string = ++cp;
917 while (cp < ep && *cp != quote)
919 token->length = cp - token->string;
923 msg (SW, _("Scope of string exceeds line."));
927 int is_num = isdigit ((unsigned char) *cp) || *cp == '.';
929 token->string = cp++;
930 while (cp < ep && !isspace ((unsigned char) *cp) && *cp != ','
931 && *cp != '-' && *cp != '+')
933 if (isdigit ((unsigned char) *cp))
936 if ((tolower ((unsigned char) *cp) == 'd'
937 || tolower ((unsigned char) *cp) == 'e')
938 && (cp[1] == '+' || cp[1] == '-'))
944 token->length = cp - token->string;
945 assert (token->length);
951 di.s = token->string;
952 di.e = token->string + token->length;
953 di.v = (union value *) &token->number;
954 di.f1 = first_column;
955 di.format.type = FMT_F;
956 di.format.w = token->length;
966 dfm_set_record (data_file, cp);
971 /* Forcibly skip the end of a line for content type CONTENT in
974 force_eol (struct file_handle *data_file, const char *content)
979 cp = dfm_get_record (data_file, &len);
982 while (len && isspace (*cp))
987 msg (SE, _("End of line expected %s while reading %s."),
988 context (data_file), content);
992 dfm_fwd_record (data_file);
997 /* Back end, omitting ROWTYPE_. */
1001 struct matrix_data_pgm *mx; /* MATRIX DATA program. */
1002 double ***data; /* MATRIX DATA data. */
1003 double *factor_values; /* Factor values. */
1004 int max_cell_idx; /* Max-numbered cell that we have
1005 read so far, plus one. */
1006 double *split_values; /* SPLIT FILE variable values. */
1009 static int nr_read_splits (struct nr_aux_data *, int compare);
1010 static int nr_read_factors (struct nr_aux_data *, int cell);
1011 static void nr_output_data (struct nr_aux_data *, struct ccase *,
1012 write_case_func *, write_case_data);
1013 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 struct nr_aux_data nr;
1024 if (mx->cells == -1)
1029 nr.factor_values = xmalloc (sizeof *nr.factor_values * mx->n_factors * mx->cells);
1030 nr.max_cell_idx = 0;
1031 nr.split_values = xmalloc (sizeof *nr.split_values
1032 * dict_get_split_cnt (default_dict));
1034 vfm_source = create_case_source (&matrix_data_without_rowtype_source_class,
1037 procedure (NULL, &nr);
1039 free (nr.split_values);
1040 free (nr.factor_values);
1042 fh_close_handle (mx->data_file);
1045 /* Mirror data across the diagonal of matrix CP which contains
1046 CONTENT type data. */
1048 fill_matrix (struct matrix_data_pgm *mx, int content, double *cp)
1050 int type = content_type[content];
1052 if (type == 1 && mx->section != FULL)
1054 if (mx->diag == NODIAGONAL)
1056 const double fill = content == CORR ? 1.0 : SYSMIS;
1059 for (i = 0; i < mx->n_continuous; i++)
1060 cp[i * (1 + mx->n_continuous)] = fill;
1066 if (mx->section == LOWER)
1068 int n_lines = mx->n_continuous;
1069 if (mx->section != FULL && mx->diag == NODIAGONAL)
1072 for (r = 1; r < n_lines; r++)
1073 for (c = 0; c < r; c++)
1074 cp[r + c * mx->n_continuous] = cp[c + r * mx->n_continuous];
1078 assert (mx->section == UPPER);
1079 for (r = 1; r < mx->n_continuous; r++)
1080 for (c = 0; c < r; c++)
1081 cp[c + r * mx->n_continuous] = cp[r + c * mx->n_continuous];
1089 for (c = 1; c < mx->n_continuous; c++)
1094 /* Read data lines for content type CONTENT from the data file.
1095 If PER_FACTOR is nonzero, then factor information is read from
1096 the data file. Data is for cell number CELL. */
1098 nr_read_data_lines (struct nr_aux_data *nr,
1099 int per_factor, int cell, int content, int compare)
1101 struct matrix_data_pgm *mx = nr->mx;
1102 const int type = content_type[content]; /* Content type. */
1103 int n_lines; /* Number of lines to parse from data file for this type. */
1104 double *cp; /* Current position in vector or matrix. */
1111 n_lines = mx->n_continuous;
1112 if (mx->section != FULL && mx->diag == NODIAGONAL)
1116 cp = nr->data[content][cell];
1117 if (type == 1 && mx->section == LOWER && mx->diag == NODIAGONAL)
1118 cp += mx->n_continuous;
1120 for (i = 0; i < n_lines; i++)
1124 if (!nr_read_splits (nr, 1))
1126 if (per_factor && !nr_read_factors (nr, cell))
1133 n_cols = mx->n_continuous;
1136 switch (mx->section)
1143 n_cols = mx->n_continuous - i;
1144 if (mx->diag == NODIAGONAL)
1151 n_cols = mx->n_continuous;
1169 for (j = 0; j < n_cols; j++)
1171 struct matrix_token token;
1172 if (!mget_token (&token, mx->data_file))
1174 if (token.type != MNUM)
1176 msg (SE, _("expecting value for %s %s"),
1177 dict_get_var (default_dict, j)->name,
1178 context (mx->data_file));
1182 *cp++ = token.number;
1185 && !force_eol (mx->data_file, content_names[content]))
1187 debug_printf (("\n"));
1190 if (mx->section == LOWER)
1191 cp += mx->n_continuous - n_cols;
1194 fill_matrix (mx, content, nr->data[content][cell]);
1199 /* When ROWTYPE_ does not appear in the data, reads the matrices and
1200 writes them to the output file. Returns success. */
1202 matrix_data_read_without_rowtype (struct case_source *source,
1204 write_case_func *write_case,
1205 write_case_data wc_data)
1207 struct nr_aux_data *nr = source->aux;
1208 struct matrix_data_pgm *mx = nr->mx;
1213 nr->data = pool_alloc (mx->container, (PROX + 1) * sizeof *nr->data);
1218 for (i = 0; i <= PROX; i++)
1222 for (cp = mx->contents; *cp != EOC; cp++)
1223 if (*cp != LPAREN && *cp != RPAREN)
1225 int per_factor = mx->is_per_factor[*cp];
1228 n_entries = mx->n_continuous;
1229 if (content_type[*cp] == 1)
1230 n_entries *= mx->n_continuous;
1233 int n_vectors = per_factor ? mx->cells : 1;
1236 nr->data[*cp] = pool_alloc (mx->container,
1237 n_vectors * sizeof **nr->data);
1239 for (i = 0; i < n_vectors; i++)
1240 nr->data[*cp][i] = pool_alloc (mx->container,
1241 n_entries * sizeof ***nr->data);
1250 if (!nr_read_splits (nr, 0))
1253 for (bp = mx->contents; *bp != EOC; bp = np)
1257 /* Trap the CONTENTS that we should parse in this pass
1258 between bp and ep. Set np to the starting bp for next
1263 while (*ep != RPAREN)
1271 while (*ep != EOC && *ep != LPAREN)
1280 for (i = 0; i < (per_factor ? mx->cells : 1); i++)
1284 for (cp = bp; cp < ep; cp++)
1285 if (!nr_read_data_lines (nr, per_factor, i, *cp, cp != bp))
1291 nr_output_data (nr, c, write_case, wc_data);
1293 if (dict_get_split_cnt (default_dict) == 0
1294 || !another_token (mx->data_file))
1299 /* Read the split file variables. If COMPARE is 1, compares the
1300 values read to the last values read and returns 1 if they're equal,
1303 nr_read_splits (struct nr_aux_data *nr, int compare)
1305 struct matrix_data_pgm *mx = nr->mx;
1306 static int just_read = 0; /* FIXME: WTF? */
1310 if (compare && just_read)
1316 if (dict_get_split_vars (default_dict) == NULL)
1319 if (mx->single_split)
1323 = ++dict_get_split_vars (default_dict)[0]->p.mxd.subtype;
1330 split_cnt = dict_get_split_cnt (default_dict);
1331 for (i = 0; i < split_cnt; i++)
1333 struct matrix_token token;
1334 if (!mget_token (&token, mx->data_file))
1336 if (token.type != MNUM)
1338 msg (SE, _("Syntax error expecting SPLIT FILE value %s."),
1339 context (mx->data_file));
1344 nr->split_values[i] = token.number;
1345 else if (nr->split_values[i] != token.number)
1347 msg (SE, _("Expecting value %g for %s."),
1348 nr->split_values[i],
1349 dict_get_split_vars (default_dict)[i]->name);
1357 /* Read the factors for cell CELL. If COMPARE is 1, compares the
1358 values read to the last values read and returns 1 if they're equal,
1361 nr_read_factors (struct nr_aux_data *nr, int cell)
1363 struct matrix_data_pgm *mx = nr->mx;
1366 if (mx->n_factors == 0)
1369 assert (nr->max_cell_idx >= cell);
1370 if (cell != nr->max_cell_idx)
1381 for (i = 0; i < mx->n_factors; i++)
1383 struct matrix_token token;
1384 if (!mget_token (&token, mx->data_file))
1386 if (token.type != MNUM)
1388 msg (SE, _("Syntax error expecting factor value %s."),
1389 context (mx->data_file));
1394 nr->factor_values[i + mx->n_factors * cell] = token.number;
1395 else if (nr->factor_values[i + mx->n_factors * cell] != token.number)
1397 msg (SE, _("Syntax error expecting value %g for %s %s."),
1398 nr->factor_values[i + mx->n_factors * cell],
1399 mx->factors[i]->name, context (mx->data_file));
1408 /* Write the contents of a cell having content type CONTENT and data
1409 CP to the active file. */
1411 dump_cell_content (struct matrix_data_pgm *mx, int content, double *cp,
1413 write_case_func *write_case, write_case_data wc_data)
1415 int type = content_type[content];
1418 st_bare_pad_copy (c->data[mx->rowtype_->fv].s,
1419 content_names[content], 8);
1422 memset (&c->data[mx->varname_->fv].s, ' ', 8);
1426 int n_lines = (type == 1) ? mx->n_continuous : 1;
1429 for (i = 0; i < n_lines; i++)
1433 for (j = 0; j < mx->n_continuous; j++)
1435 int fv = dict_get_var (default_dict, mx->first_continuous + j)->fv;
1436 c->data[fv].f = *cp;
1440 st_bare_pad_copy (c->data[mx->varname_->fv].s,
1441 dict_get_var (default_dict,
1442 mx->first_continuous + i)->name,
1444 write_case (wc_data);
1449 /* Finally dump out everything from nr_data[] to the output file. */
1451 nr_output_data (struct nr_aux_data *nr, struct ccase *c,
1452 write_case_func *write_case, write_case_data wc_data)
1454 struct matrix_data_pgm *mx = nr->mx;
1457 struct variable *const *split;
1461 split_cnt = dict_get_split_cnt (default_dict);
1462 split = dict_get_split_vars (default_dict);
1463 for (i = 0; i < split_cnt; i++)
1464 c->data[split[i]->fv].f = nr->split_values[i];
1471 for (cell = 0; cell < mx->cells; cell++)
1476 for (factor = 0; factor < mx->n_factors; factor++)
1478 c->data[mx->factors[factor]->fv].f
1479 = nr->factor_values[factor + cell * mx->n_factors];
1480 debug_printf (("f:%s ", mx->factors[factor]->name));
1487 for (content = 0; content <= PROX; content++)
1488 if (mx->is_per_factor[content])
1490 assert (nr->data[content] != NULL
1491 && nr->data[content][cell] != NULL);
1493 dump_cell_content (mx, content, nr->data[content][cell],
1494 c, write_case, wc_data);
1506 for (factor = 0; factor < mx->n_factors; factor++)
1507 c->data[mx->factors[factor]->fv].f = SYSMIS;
1510 for (content = 0; content <= PROX; content++)
1511 if (!mx->is_per_factor[content] && nr->data[content] != NULL)
1512 dump_cell_content (mx, content, nr->data[content][0],
1513 c, write_case, wc_data);
1517 /* Back end, with ROWTYPE_. */
1519 /* All the data for one set of factor values. */
1523 int n_rows[PROX + 1];
1524 double *data[PROX + 1];
1525 struct factor_data *next;
1528 /* With ROWTYPE_ auxiliary data. */
1531 struct matrix_data_pgm *mx; /* MATRIX DATA program. */
1532 int content; /* Type of current row. */
1533 double *split_values; /* SPLIT FILE variable values. */
1534 struct factor_data *data; /* All the data. */
1535 struct factor_data *current; /* Current factor. */
1538 static int wr_read_splits (struct wr_aux_data *, struct ccase *,
1539 write_case_func *, write_case_data);
1540 static int wr_output_data (struct wr_aux_data *, struct ccase *,
1541 write_case_func *, write_case_data);
1542 static int wr_read_rowtype (struct wr_aux_data *,
1543 const struct matrix_token *, struct file_handle *);
1544 static int wr_read_factors (struct wr_aux_data *);
1545 static int wr_read_indeps (struct wr_aux_data *);
1546 static void matrix_data_read_with_rowtype (struct case_source *,
1551 /* When ROWTYPE_ appears in the data, reads the matrices and writes
1552 them to the output file. */
1554 read_matrices_with_rowtype (struct matrix_data_pgm *mx)
1556 struct wr_aux_data wr;
1560 wr.split_values = NULL;
1565 vfm_source = create_case_source (&matrix_data_with_rowtype_source_class,
1567 procedure (NULL, &wr);
1569 free (wr.split_values);
1570 fh_close_handle (mx->data_file);
1573 /* Read from the data file and write it to the active file. */
1575 matrix_data_read_with_rowtype (struct case_source *source,
1577 write_case_func *write_case,
1578 write_case_data wc_data)
1580 struct wr_aux_data *wr = source->aux;
1581 struct matrix_data_pgm *mx = wr->mx;
1585 if (!wr_read_splits (wr, c, write_case, wc_data))
1588 if (!wr_read_factors (wr))
1591 if (!wr_read_indeps (wr))
1594 while (another_token (mx->data_file));
1596 wr_output_data (wr, c, write_case, wc_data);
1599 /* Read the split file variables. If they differ from the previous
1600 set of split variables then output the data. Returns success. */
1602 wr_read_splits (struct wr_aux_data *wr,
1604 write_case_func *write_case, write_case_data wc_data)
1606 struct matrix_data_pgm *mx = wr->mx;
1610 split_cnt = dict_get_split_cnt (default_dict);
1614 if (wr->split_values)
1619 wr->split_values = xmalloc (split_cnt * sizeof *wr->split_values);
1626 for (i = 0; i < split_cnt; i++)
1628 struct matrix_token token;
1629 if (!mget_token (&token, mx->data_file))
1631 if (token.type != MNUM)
1633 msg (SE, _("Syntax error %s expecting SPLIT FILE value."),
1634 context (mx->data_file));
1638 if (compare && wr->split_values[i] != token.number && !different)
1640 if (!wr_output_data (wr, c, write_case, wc_data))
1645 wr->split_values[i] = token.number;
1652 /* Compares doubles A and B, treating SYSMIS as greatest. */
1654 compare_doubles (const void *a_, const void *b_, void *aux UNUSED)
1656 const double *a = a_;
1657 const double *b = b_;
1661 else if (*a == SYSMIS)
1663 else if (*b == SYSMIS)
1671 /* Return strcmp()-type comparison of the MX->n_factors factors at _A and
1672 _B. Sort missing values toward the end. */
1674 compare_factors (const void *a_, const void *b_, void *mx_)
1676 struct matrix_data_pgm *mx = mx_;
1677 struct factor_data *const *pa = a_;
1678 struct factor_data *const *pb = b_;
1679 const double *a = (*pa)->factors;
1680 const double *b = (*pb)->factors;
1682 return lexicographical_compare_3way (a, mx->n_factors,
1685 compare_doubles, NULL);
1688 /* Write out the data for the current split file to the active
1691 wr_output_data (struct wr_aux_data *wr,
1693 write_case_func *write_case, write_case_data wc_data)
1695 struct matrix_data_pgm *mx = wr->mx;
1698 struct variable *const *split;
1702 split_cnt = dict_get_split_cnt (default_dict);
1703 split = dict_get_split_vars (default_dict);
1704 for (i = 0; i < split_cnt; i++)
1705 c->data[split[i]->fv].f = wr->split_values[i];
1708 /* Sort the wr->data list. */
1710 struct factor_data **factors;
1711 struct factor_data *iter;
1714 factors = xmalloc (sizeof *factors * mx->cells);
1716 for (i = 0, iter = wr->data; iter; iter = iter->next, i++)
1719 sort (factors, mx->cells, sizeof *factors, compare_factors, mx);
1721 wr->data = factors[0];
1722 for (i = 0; i < mx->cells - 1; i++)
1723 factors[i]->next = factors[i + 1];
1724 factors[mx->cells - 1]->next = NULL;
1729 /* Write out records for every set of factor values. */
1731 struct factor_data *iter;
1733 for (iter = wr->data; iter; iter = iter->next)
1738 for (factor = 0; factor < mx->n_factors; factor++)
1740 c->data[mx->factors[factor]->fv].f
1741 = iter->factors[factor];
1742 debug_printf (("f:%s ", factors[factor]->name));
1749 for (content = 0; content <= PROX; content++)
1751 if (!iter->n_rows[content])
1755 int type = content_type[content];
1756 int n_lines = (type == 1
1758 - (mx->section != FULL && mx->diag == NODIAGONAL))
1761 if (n_lines != iter->n_rows[content])
1763 msg (SE, _("Expected %d lines of data for %s content; "
1764 "actually saw %d lines. No data will be "
1765 "output for this content."),
1766 n_lines, content_names[content],
1767 iter->n_rows[content]);
1772 fill_matrix (mx, content, iter->data[content]);
1774 dump_cell_content (mx, content, iter->data[content],
1775 c, write_case, wc_data);
1781 pool_destroy (mx->container);
1782 mx->container = pool_create ();
1784 wr->data = wr->current = NULL;
1789 /* Sets ROWTYPE_ based on the given TOKEN read from DATA_FILE.
1792 wr_read_rowtype (struct wr_aux_data *wr,
1793 const struct matrix_token *token,
1794 struct file_handle *data_file)
1796 if (wr->content != -1)
1798 msg (SE, _("Multiply specified ROWTYPE_ %s."), context (data_file));
1801 if (token->type != MSTR)
1803 msg (SE, _("Syntax error %s expecting ROWTYPE_ string."),
1804 context (data_file));
1812 memcpy (s, token->string, min (15, token->length));
1813 s[min (15, token->length)] = 0;
1815 for (cp = s; *cp; cp++)
1816 *cp = toupper ((unsigned char) *cp);
1818 wr->content = string_to_content_type (s, NULL);
1821 if (wr->content == -1)
1823 msg (SE, _("Syntax error %s."), context (data_file));
1830 /* Read the factors for the current row. Select a set of factors and
1831 point wr_current to it. */
1833 wr_read_factors (struct wr_aux_data *wr)
1835 struct matrix_data_pgm *mx = wr->mx;
1836 double *factor_values = local_alloc (sizeof *factor_values * mx->n_factors);
1842 for (i = 0; i < mx->n_factors; i++)
1844 struct matrix_token token;
1845 if (!mget_token (&token, mx->data_file))
1847 if (token.type == MSTR)
1849 if (!wr_read_rowtype (wr, &token, mx->data_file))
1851 if (!mget_token (&token, mx->data_file))
1854 if (token.type != MNUM)
1856 msg (SE, _("Syntax error expecting factor value %s."),
1857 context (mx->data_file));
1861 factor_values[i] = token.number;
1864 if (wr->content == -1)
1866 struct matrix_token token;
1867 if (!mget_token (&token, mx->data_file))
1869 if (!wr_read_rowtype (wr, &token, mx->data_file))
1873 /* Try the most recent factor first as a simple caching
1879 for (i = 0; i < mx->n_factors; i++)
1880 if (factor_values[i] != wr->current->factors[i])
1885 /* Linear search through the list. */
1888 struct factor_data *iter;
1890 for (iter = wr->data; iter; iter = iter->next)
1894 for (i = 0; i < mx->n_factors; i++)
1895 if (factor_values[i] != iter->factors[i])
1905 /* Not found. Make a new item. */
1907 struct factor_data *new = pool_alloc (mx->container, sizeof *new);
1909 new->factors = pool_alloc (mx->container, sizeof *new->factors * mx->n_factors);
1914 for (i = 0; i < mx->n_factors; i++)
1915 new->factors[i] = factor_values[i];
1921 for (i = 0; i <= PROX; i++)
1924 new->data[i] = NULL;
1928 new->next = wr->data;
1929 wr->data = wr->current = new;
1934 local_free (factor_values);
1938 local_free (factor_values);
1942 /* Read the independent variables into wr->current. */
1944 wr_read_indeps (struct wr_aux_data *wr)
1946 struct matrix_data_pgm *mx = wr->mx;
1947 struct factor_data *c = wr->current;
1948 const int type = content_type[wr->content];
1949 const int n_rows = c->n_rows[wr->content];
1953 /* Allocate room for data if necessary. */
1954 if (c->data[wr->content] == NULL)
1956 int n_items = mx->n_continuous;
1958 n_items *= mx->n_continuous;
1960 c->data[wr->content] = pool_alloc (mx->container,
1961 sizeof **c->data * n_items);
1964 cp = &c->data[wr->content][n_rows * mx->n_continuous];
1966 /* Figure out how much to read from this line. */
1973 msg (SE, _("Duplicate specification for %s."),
1974 content_names[wr->content]);
1978 n_cols = mx->n_continuous;
1983 if (n_rows >= mx->n_continuous - (mx->section != FULL && mx->diag == NODIAGONAL))
1985 msg (SE, _("Too many rows of matrix data for %s."),
1986 content_names[wr->content]);
1990 switch (mx->section)
1993 n_cols = n_rows + 1;
1994 if (mx->diag == NODIAGONAL)
1995 cp += mx->n_continuous;
1999 n_cols = mx->n_continuous - n_rows;
2000 if (mx->diag == NODIAGONAL)
2007 n_cols = mx->n_continuous;
2018 c->n_rows[wr->content]++;
2020 debug_printf ((" (c=%p,r=%d,n=%d)", c, n_rows + 1, n_cols));
2022 /* Read N_COLS items at CP. */
2026 for (j = 0; j < n_cols; j++)
2028 struct matrix_token token;
2029 if (!mget_token (&token, mx->data_file))
2031 if (token.type != MNUM)
2033 msg (SE, _("Syntax error expecting value for %s %s."),
2034 dict_get_var (default_dict, mx->first_continuous + j)->name,
2035 context (mx->data_file));
2039 *cp++ = token.number;
2042 && !force_eol (mx->data_file, content_names[wr->content]))
2044 debug_printf (("\n"));
2050 /* Matrix source. */
2052 static const struct case_source_class matrix_data_with_rowtype_source_class =
2056 matrix_data_read_with_rowtype,
2060 static const struct case_source_class
2061 matrix_data_without_rowtype_source_class =
2065 matrix_data_read_without_rowtype,