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., 51 Franklin Street, Fifth Floor, Boston, MA
26 #include <data/case-source.h>
27 #include <data/case.h>
28 #include <data/data-in.h>
29 #include <data/dictionary.h>
30 #include <data/procedure.h>
31 #include <data/variable.h>
32 #include <language/command.h>
33 #include <language/data-io/data-reader.h>
34 #include <language/data-io/file-handle.h>
35 #include <language/lexer/lexer.h>
36 #include <language/lexer/variable-parser.h>
37 #include <libpspp/alloc.h>
38 #include <libpspp/array.h>
39 #include <libpspp/assertion.h>
40 #include <libpspp/compiler.h>
41 #include <libpspp/message.h>
42 #include <libpspp/message.h>
43 #include <libpspp/misc.h>
44 #include <libpspp/pool.h>
45 #include <libpspp/str.h>
50 #define _(msgid) gettext (msgid)
52 /* FIXME: /N subcommand not implemented. It should be pretty simple,
55 /* Different types of variables for MATRIX DATA procedure. Order is
56 important: these are used for sort keys. */
59 MXD_SPLIT, /* SPLIT FILE variables. */
60 MXD_ROWTYPE, /* ROWTYPE_. */
61 MXD_FACTOR, /* Factor variables. */
62 MXD_VARNAME, /* VARNAME_. */
63 MXD_CONTINUOUS, /* Continuous variables. */
68 /* Format type enums. */
75 /* Matrix section enums. */
83 /* Diagonal inclusion enums. */
111 /* 0=vector, 1=matrix, 2=scalar. */
112 static const int content_type[PROX + 1] =
114 0, 2, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1,
117 /* Name of each content type. */
118 static const char *const content_names[PROX + 1] =
120 "N", "N", "N_MATRIX", "MEAN", "STDDEV", "COUNT", "MSE",
121 "DFE", "MAT", "COV", "CORR", "PROX",
124 /* A MATRIX DATA input program. */
125 struct matrix_data_pgm
127 struct pool *container; /* Arena used for all allocations. */
128 struct dfm_reader *reader; /* Data file to read. */
131 enum format_type fmt; /* LIST or FREE. */
132 enum matrix_section section;/* LOWER or UPPER or FULL. */
133 enum include_diagonal diag; /* DIAGONAL or NODIAGONAL. */
135 int explicit_rowtype; /* ROWTYPE_ specified explicitly in data? */
136 struct variable *rowtype_, *varname_; /* ROWTYPE_, VARNAME_ variables. */
138 struct variable *single_split; /* Single SPLIT FILE variable. */
140 /* Factor variables. */
141 size_t n_factors; /* Number of factor variables. */
142 struct variable **factors; /* Factor variables. */
143 int is_per_factor[PROX + 1]; /* Is there per-factor data? */
145 int cells; /* Number of cells, or -1 if none. */
147 int pop_n; /* Population N specified by user. */
149 /* CONTENTS subcommand. */
150 int contents[EOC * 3 + 1]; /* Contents. */
151 int n_contents; /* Number of entries. */
153 /* Continuous variables. */
154 int n_continuous; /* Number of continuous variables. */
155 int first_continuous; /* Index into dataset_dict (current_dataset).var of
156 first continuous variable. */
159 /* Auxiliary data attached to MATRIX DATA variables. */
162 int var_type; /* Variable type. */
163 int sub_type; /* Subtype. */
166 static const struct case_source_class matrix_data_with_rowtype_source_class;
167 static const struct case_source_class matrix_data_without_rowtype_source_class;
169 static int compare_variables_by_mxd_var_type (const void *pa,
171 static bool read_matrices_without_rowtype (struct matrix_data_pgm *);
172 static bool read_matrices_with_rowtype (struct matrix_data_pgm *);
173 static int string_to_content_type (char *, int *);
174 static void attach_mxd_aux (struct variable *, int var_type, int sub_type);
177 cmd_matrix_data (void)
180 struct matrix_data_pgm *mx;
181 struct file_handle *fh = fh_inline_file ();
186 discard_variables (current_dataset);
188 pool = pool_create ();
189 mx = pool_alloc (pool, sizeof *mx);
190 mx->container = pool;
195 mx->explicit_rowtype = 0;
198 mx->single_split = NULL;
201 memset (mx->is_per_factor, 0, sizeof mx->is_per_factor);
205 mx->n_continuous = 0;
206 mx->first_continuous = 0;
211 if (lex_match_id ("VARIABLES"))
218 msg (SE, _("VARIABLES subcommand multiply specified."));
224 if (!parse_DATA_LIST_vars (&v, &nv, PV_NO_DUPLICATE))
230 for (i = 0; i < nv; i++)
231 if (!strcasecmp (v[i], "VARNAME_"))
233 msg (SE, _("VARNAME_ cannot be explicitly specified on "
235 for (i = 0; i < nv; i++)
245 for (i = 0; i < nv; i++)
247 struct variable *new_var;
249 if (strcasecmp (v[i], "ROWTYPE_"))
251 new_var = dict_create_var_assert (dataset_dict (current_dataset), v[i], 0);
252 attach_mxd_aux (new_var, MXD_CONTINUOUS, i);
255 mx->explicit_rowtype = 1;
261 mx->rowtype_ = dict_create_var_assert (dataset_dict (current_dataset),
263 attach_mxd_aux (mx->rowtype_, MXD_ROWTYPE, 0);
265 else if (lex_match_id ("FILE"))
268 fh = fh_parse (FH_REF_FILE | FH_REF_INLINE);
272 else if (lex_match_id ("FORMAT"))
276 while (token == T_ID)
278 if (lex_match_id ("LIST"))
280 else if (lex_match_id ("FREE"))
282 else if (lex_match_id ("LOWER"))
284 else if (lex_match_id ("UPPER"))
286 else if (lex_match_id ("FULL"))
288 else if (lex_match_id ("DIAGONAL"))
290 else if (lex_match_id ("NODIAGONAL"))
291 mx->diag = NODIAGONAL;
294 lex_error (_("in FORMAT subcommand"));
299 else if (lex_match_id ("SPLIT"))
305 msg (SE, _("SPLIT subcommand multiply specified."));
312 lex_error (_("in SPLIT subcommand"));
316 if (dict_lookup_var (dataset_dict (current_dataset), tokid) == NULL
317 && (lex_look_ahead () == '.' || lex_look_ahead () == '/'))
319 if (!strcasecmp (tokid, "ROWTYPE_")
320 || !strcasecmp (tokid, "VARNAME_"))
322 msg (SE, _("Split variable may not be named ROWTYPE_ "
327 mx->single_split = dict_create_var_assert (dataset_dict (current_dataset),
329 attach_mxd_aux (mx->single_split, MXD_CONTINUOUS, 0);
332 dict_set_split_vars (dataset_dict (current_dataset), &mx->single_split, 1);
336 struct variable **split;
339 if (!parse_variables (dataset_dict (current_dataset), &split, &n, PV_NO_DUPLICATE))
342 dict_set_split_vars (dataset_dict (current_dataset), split, n);
346 struct variable *const *split = dict_get_split_vars (dataset_dict (current_dataset));
347 size_t split_cnt = dict_get_split_cnt (dataset_dict (current_dataset));
350 for (i = 0; i < split_cnt; i++)
352 struct mxd_var *mv = split[i]->aux;
354 if (mv->var_type != MXD_CONTINUOUS)
356 msg (SE, _("Split variable %s is already another type."),
360 var_clear_aux (split[i]);
361 attach_mxd_aux (split[i], MXD_SPLIT, i);
365 else if (lex_match_id ("FACTORS"))
371 msg (SE, _("FACTORS subcommand multiply specified."));
376 if (!parse_variables (dataset_dict (current_dataset), &mx->factors, &mx->n_factors,
383 for (i = 0; i < mx->n_factors; i++)
385 struct variable *v = mx->factors[i];
386 struct mxd_var *mv = v->aux;
388 if (mv->var_type != MXD_CONTINUOUS)
390 msg (SE, _("Factor variable %s is already another type."),
395 attach_mxd_aux (v, MXD_FACTOR, i);
399 else if (lex_match_id ("CELLS"))
405 msg (SE, _("CELLS subcommand multiply specified."));
409 if (!lex_is_integer () || lex_integer () < 1)
411 lex_error (_("expecting positive integer"));
415 mx->cells = lex_integer ();
418 else if (lex_match_id ("N"))
424 msg (SE, _("N subcommand multiply specified."));
428 if (!lex_is_integer () || lex_integer () < 1)
430 lex_error (_("expecting positive integer"));
434 mx->pop_n = lex_integer ();
437 else if (lex_match_id ("CONTENTS"))
439 int inside_parens = 0;
440 unsigned collide = 0;
445 msg (SE, _("CONTENTS subcommand multiply specified."));
455 for (i = 0; i <= PROX; i++)
456 mx->is_per_factor[i] = 0;
465 msg (SE, _("Nested parentheses not allowed."));
471 else if (lex_match (')'))
475 msg (SE, _("Mismatched right parenthesis (`(')."));
478 if (mx->contents[mx->n_contents - 1] == LPAREN)
480 msg (SE, _("Empty parentheses not allowed."));
493 lex_error (_("in CONTENTS subcommand"));
497 content_type = string_to_content_type (tokid,
499 if (content_type == -1)
501 lex_error (_("in CONTENTS subcommand"));
506 if (collide & (1 << collide_index))
508 msg (SE, _("Content multiply specified for %s."),
509 content_names[content_type]);
512 collide |= (1 << collide_index);
515 mx->is_per_factor[item] = inside_parens;
517 mx->contents[mx->n_contents++] = item;
519 if (token == '/' || token == '.')
525 msg (SE, _("Missing right parenthesis."));
528 mx->contents[mx->n_contents] = EOC;
539 lex_error (_("expecting end of command"));
545 msg (SE, _("Missing VARIABLES subcommand."));
549 if (!mx->n_contents && !mx->explicit_rowtype)
551 msg (SW, _("CONTENTS subcommand not specified: assuming file "
552 "contains only CORR matrix."));
554 mx->contents[0] = CORR;
555 mx->contents[1] = EOC;
559 if (mx->n_factors && !mx->explicit_rowtype && mx->cells == -1)
561 msg (SE, _("Missing CELLS subcommand. CELLS is required "
562 "when ROWTYPE_ is not given in the data and "
563 "factors are present."));
567 if (mx->explicit_rowtype && mx->single_split)
569 msg (SE, _("Split file values must be present in the data when "
570 "ROWTYPE_ is present."));
574 /* Create VARNAME_. */
575 mx->varname_ = dict_create_var_assert (dataset_dict (current_dataset), "VARNAME_", 8);
576 attach_mxd_aux (mx->varname_, MXD_VARNAME, 0);
578 /* Sort the dictionary variables into the desired order for the
579 system file output. */
584 dict_get_vars (dataset_dict (current_dataset), &v, &nv, 0);
585 qsort (v, nv, sizeof *v, compare_variables_by_mxd_var_type);
586 dict_reorder_vars (dataset_dict (current_dataset), v, nv);
592 static const struct fmt_spec fmt_tab[MXD_COUNT] =
603 mx->first_continuous = -1;
604 for (i = 0; i < dict_get_var_cnt (dataset_dict (current_dataset)); i++)
606 struct variable *v = dict_get_var (dataset_dict (current_dataset), i);
607 struct mxd_var *mv = v->aux;
608 int type = mv->var_type;
610 assert (type >= 0 && type < MXD_COUNT);
611 v->print = v->write = fmt_tab[type];
613 if (type == MXD_CONTINUOUS)
615 if (mx->first_continuous == -1 && type == MXD_CONTINUOUS)
616 mx->first_continuous = i;
620 if (mx->n_continuous == 0)
622 msg (SE, _("No continuous variables specified."));
626 mx->reader = dfm_open_reader (fh);
627 if (mx->reader == NULL)
630 if (mx->explicit_rowtype)
631 ok = read_matrices_with_rowtype (mx);
633 ok = read_matrices_without_rowtype (mx);
635 dfm_close_reader (mx->reader);
637 pool_destroy (mx->container);
639 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
642 discard_variables (current_dataset);
644 pool_destroy (mx->container);
645 return CMD_CASCADING_FAILURE;
648 /* Look up string S as a content-type name and return the
649 corresponding enumerated value, or -1 if there is no match. If
650 COLLIDE is non-NULL then *COLLIDE returns a value (suitable for use
651 as a bit-index) which can be used for determining whether a related
652 statistic has already been used. */
654 string_to_content_type (char *s, int *collide)
665 {N_VECTOR, 0, "N_VECTOR"},
667 {N_SCALAR, 0, "N_SCALAR"},
668 {N_MATRIX, 1, "N_MATRIX"},
670 {STDDEV, 3, "STDDEV"},
682 for (tp = tab; tp->value != -1; tp++)
683 if (!strcasecmp (s, tp->string))
686 *collide = tp->collide;
693 /* Compare two variables using p.mxd.var_type and p.mxd.sub_type
696 compare_variables_by_mxd_var_type (const void *a_, const void *b_)
698 struct variable *const *pa = a_;
699 struct variable *const *pb = b_;
700 const struct mxd_var *a = (*pa)->aux;
701 const struct mxd_var *b = (*pb)->aux;
703 if (a->var_type != b->var_type)
704 return a->var_type > b->var_type ? 1 : -1;
706 return a->sub_type < b->sub_type ? -1 : a->sub_type > b->sub_type;
709 /* Attaches a struct mxd_var with the specific member values to
712 attach_mxd_aux (struct variable *v, int var_type, int sub_type)
716 assert (v->aux == NULL);
717 mv = xmalloc (sizeof *mv);
718 mv->var_type = var_type;
719 mv->sub_type = sub_type;
720 var_attach_aux (v, mv, var_dtor_free);
723 /* Matrix tokenizer. */
725 /* Matrix token types. */
726 enum matrix_token_type
732 /* A MATRIX DATA parsing token. */
735 enum matrix_token_type type;
736 double number; /* MNUM: token value. */
737 char *string; /* MSTR: token string; not null-terminated. */
738 int length; /* MSTR: tokstr length. */
741 static int mget_token (struct matrix_token *, struct dfm_reader *);
744 #define mget_token(TOKEN, READER) mget_token_dump(TOKEN, READER)
747 mdump_token (const struct matrix_token *token)
752 printf (" #%g", token->number);
755 printf (" '%.*s'", token->length, token->string);
764 mget_token_dump (struct matrix_token *token, struct dfm_reader *reader)
766 int result = (mget_token) (token, reader);
772 /* Return the current position in READER. */
774 context (struct dfm_reader *reader)
776 static struct string buf = DS_EMPTY_INITIALIZER;
779 if (dfm_eof (reader))
780 ds_assign_cstr (&buf, "at end of file");
785 p = dfm_get_record (reader);
786 ss_ltrim (&p, ss_cstr (CC_SPACES));
788 ds_assign_cstr (&buf, "at end of line");
790 ds_put_format (&buf, "before `%.*s'",
791 (int) ss_cspan (p, ss_cstr (CC_SPACES)), ss_data (p));
794 return ds_cstr (&buf);
797 /* Is there at least one token left in the data file? */
799 another_token (struct dfm_reader *reader)
806 if (dfm_eof (reader))
809 p = dfm_get_record (reader);
810 space_cnt = ss_span (p, ss_cstr (CC_SPACES));
811 if (space_cnt < ss_length (p))
813 dfm_forward_columns (reader, space_cnt);
817 dfm_forward_record (reader);
822 /* Parse a MATRIX DATA token from READER into TOKEN. */
824 (mget_token) (struct matrix_token *token, struct dfm_reader *reader)
826 struct substring line, p;
830 if (!another_token (reader))
833 line = p = dfm_get_record (reader);
835 /* Three types of fields: quoted with ', quoted with ", unquoted. */
837 if (c == '\'' || c == '"')
840 if (!ss_get_until (&p, c, &s))
841 msg (SW, _("Scope of string exceeds line."));
845 bool is_num = isdigit (c) || c == '.';
846 const char *start = ss_data (p);
851 if (strchr (CC_SPACES ",-+", c) != NULL)
856 if (strchr ("deDE", c) && strchr ("+-", ss_at (p, 1)))
864 s = ss_buffer (start, ss_data (p) - start);
872 di.v = (union value *) &token->number;
873 di.f1 = dfm_get_column (reader, di.s);
874 di.format = make_output_format (FMT_F, token->length, 0);
881 token->string = ss_data (s);
882 token->length = ss_length (s);
884 dfm_reread_record (reader, dfm_get_column (reader, ss_end (s)));
889 /* Forcibly skip the end of a line for content type CONTENT in
892 force_eol (struct dfm_reader *reader, const char *content)
896 if (dfm_eof (reader))
899 p = dfm_get_record (reader);
900 if (ss_span (p, ss_cstr (CC_SPACES)) != ss_length (p))
902 msg (SE, _("End of line expected %s while reading %s."),
903 context (reader), content);
907 dfm_forward_record (reader);
911 /* Back end, omitting ROWTYPE_. */
915 struct matrix_data_pgm *mx; /* MATRIX DATA program. */
916 double ***data; /* MATRIX DATA data. */
917 double *factor_values; /* Factor values. */
918 int max_cell_idx; /* Max-numbered cell that we have
919 read so far, plus one. */
920 double *split_values; /* SPLIT FILE variable values. */
923 static bool nr_read_splits (struct nr_aux_data *, int compare);
924 static bool nr_read_factors (struct nr_aux_data *, int cell);
925 static bool nr_output_data (struct nr_aux_data *, struct ccase *,
926 write_case_func *, write_case_data);
927 static bool matrix_data_read_without_rowtype (struct case_source *source,
932 /* Read from the data file and write it to the active file.
933 Returns true if successful, false if an I/O error occurred. */
935 read_matrices_without_rowtype (struct matrix_data_pgm *mx)
937 struct nr_aux_data nr;
945 nr.factor_values = xnmalloc (mx->n_factors * mx->cells,
946 sizeof *nr.factor_values);
948 nr.split_values = xnmalloc (dict_get_split_cnt (dataset_dict (current_dataset)),
949 sizeof *nr.split_values);
951 proc_set_source (current_dataset, create_case_source (
952 &matrix_data_without_rowtype_source_class, &nr));
954 ok = procedure (current_dataset,NULL, NULL);
956 free (nr.split_values);
957 free (nr.factor_values);
962 /* Mirror data across the diagonal of matrix CP which contains
963 CONTENT type data. */
965 fill_matrix (struct matrix_data_pgm *mx, int content, double *cp)
967 int type = content_type[content];
969 if (type == 1 && mx->section != FULL)
971 if (mx->diag == NODIAGONAL)
973 const double fill = content == CORR ? 1.0 : SYSMIS;
976 for (i = 0; i < mx->n_continuous; i++)
977 cp[i * (1 + mx->n_continuous)] = fill;
983 if (mx->section == LOWER)
985 int n_lines = mx->n_continuous;
986 if (mx->section != FULL && mx->diag == NODIAGONAL)
989 for (r = 1; r < n_lines; r++)
990 for (c = 0; c < r; c++)
991 cp[r + c * mx->n_continuous] = cp[c + r * mx->n_continuous];
995 assert (mx->section == UPPER);
996 for (r = 1; r < mx->n_continuous; r++)
997 for (c = 0; c < r; c++)
998 cp[c + r * mx->n_continuous] = cp[r + c * mx->n_continuous];
1006 for (c = 1; c < mx->n_continuous; c++)
1011 /* Read data lines for content type CONTENT from the data file.
1012 If PER_FACTOR is nonzero, then factor information is read from
1013 the data file. Data is for cell number CELL. */
1015 nr_read_data_lines (struct nr_aux_data *nr,
1016 int per_factor, int cell, int content, int compare)
1018 struct matrix_data_pgm *mx = nr->mx;
1019 const int type = content_type[content]; /* Content type. */
1020 int n_lines; /* Number of lines to parse from data file for this type. */
1021 double *cp; /* Current position in vector or matrix. */
1028 n_lines = mx->n_continuous;
1029 if (mx->section != FULL && mx->diag == NODIAGONAL)
1033 cp = nr->data[content][cell];
1034 if (type == 1 && mx->section == LOWER && mx->diag == NODIAGONAL)
1035 cp += mx->n_continuous;
1037 for (i = 0; i < n_lines; i++)
1041 if (!nr_read_splits (nr, 1))
1043 if (per_factor && !nr_read_factors (nr, cell))
1050 n_cols = mx->n_continuous;
1053 switch (mx->section)
1060 n_cols = mx->n_continuous - i;
1061 if (mx->diag == NODIAGONAL)
1068 n_cols = mx->n_continuous;
1084 for (j = 0; j < n_cols; j++)
1086 struct matrix_token token;
1087 if (!mget_token (&token, mx->reader))
1089 if (token.type != MNUM)
1091 msg (SE, _("expecting value for %s %s"),
1092 dict_get_var (dataset_dict (current_dataset), j)->name,
1093 context (mx->reader));
1097 *cp++ = token.number;
1100 && !force_eol (mx->reader, content_names[content]))
1104 if (mx->section == LOWER)
1105 cp += mx->n_continuous - n_cols;
1108 fill_matrix (mx, content, nr->data[content][cell]);
1113 /* When ROWTYPE_ does not appear in the data, reads the matrices and
1114 writes them to the output file.
1115 Returns true if successful, false if an I/O error occurred. */
1117 matrix_data_read_without_rowtype (struct case_source *source,
1119 write_case_func *write_case,
1120 write_case_data wc_data)
1122 struct nr_aux_data *nr = source->aux;
1123 struct matrix_data_pgm *mx = nr->mx;
1128 nr->data = pool_nalloc (mx->container, PROX + 1, sizeof *nr->data);
1133 for (i = 0; i <= PROX; i++)
1137 for (cp = mx->contents; *cp != EOC; cp++)
1138 if (*cp != LPAREN && *cp != RPAREN)
1140 int per_factor = mx->is_per_factor[*cp];
1143 n_entries = mx->n_continuous;
1144 if (content_type[*cp] == 1)
1145 n_entries *= mx->n_continuous;
1148 int n_vectors = per_factor ? mx->cells : 1;
1151 nr->data[*cp] = pool_nalloc (mx->container,
1152 n_vectors, sizeof **nr->data);
1154 for (i = 0; i < n_vectors; i++)
1155 nr->data[*cp][i] = pool_nalloc (mx->container,
1156 n_entries, sizeof ***nr->data);
1165 if (!nr_read_splits (nr, 0))
1168 for (bp = mx->contents; *bp != EOC; bp = np)
1172 /* Trap the CONTENTS that we should parse in this pass
1173 between bp and ep. Set np to the starting bp for next
1178 while (*ep != RPAREN)
1186 while (*ep != EOC && *ep != LPAREN)
1195 for (i = 0; i < (per_factor ? mx->cells : 1); i++)
1199 for (cp = bp; cp < ep; cp++)
1200 if (!nr_read_data_lines (nr, per_factor, i, *cp, cp != bp))
1206 if (!nr_output_data (nr, c, write_case, wc_data))
1209 if (dict_get_split_cnt (dataset_dict (current_dataset)) == 0
1210 || !another_token (mx->reader))
1215 /* Read the split file variables. If COMPARE is 1, compares the
1216 values read to the last values read and returns true if they're equal,
1219 nr_read_splits (struct nr_aux_data *nr, int compare)
1221 struct matrix_data_pgm *mx = nr->mx;
1222 static int just_read = 0; /* FIXME: WTF? */
1226 if (compare && just_read)
1232 if (dict_get_split_vars (dataset_dict (current_dataset)) == NULL)
1235 if (mx->single_split)
1239 struct mxd_var *mv = dict_get_split_vars (dataset_dict (current_dataset))[0]->aux;
1240 nr->split_values[0] = ++mv->sub_type;
1248 split_cnt = dict_get_split_cnt (dataset_dict (current_dataset));
1249 for (i = 0; i < split_cnt; i++)
1251 struct matrix_token token;
1252 if (!mget_token (&token, mx->reader))
1254 if (token.type != MNUM)
1256 msg (SE, _("Syntax error expecting SPLIT FILE value %s."),
1257 context (mx->reader));
1262 nr->split_values[i] = token.number;
1263 else if (nr->split_values[i] != token.number)
1265 msg (SE, _("Expecting value %g for %s."),
1266 nr->split_values[i],
1267 dict_get_split_vars (dataset_dict (current_dataset))[i]->name);
1275 /* Read the factors for cell CELL. If COMPARE is 1, compares the
1276 values read to the last values read and returns true if they're equal,
1279 nr_read_factors (struct nr_aux_data *nr, int cell)
1281 struct matrix_data_pgm *mx = nr->mx;
1284 if (mx->n_factors == 0)
1287 assert (nr->max_cell_idx >= cell);
1288 if (cell != nr->max_cell_idx)
1299 for (i = 0; i < mx->n_factors; i++)
1301 struct matrix_token token;
1302 if (!mget_token (&token, mx->reader))
1304 if (token.type != MNUM)
1306 msg (SE, _("Syntax error expecting factor value %s."),
1307 context (mx->reader));
1312 nr->factor_values[i + mx->n_factors * cell] = token.number;
1313 else if (nr->factor_values[i + mx->n_factors * cell] != token.number)
1315 msg (SE, _("Syntax error expecting value %g for %s %s."),
1316 nr->factor_values[i + mx->n_factors * cell],
1317 mx->factors[i]->name, context (mx->reader));
1326 /* Write the contents of a cell having content type CONTENT and data
1327 CP to the active file.
1328 Returns true if successful, false if an I/O error occurred. */
1330 dump_cell_content (struct matrix_data_pgm *mx, int content, double *cp,
1332 write_case_func *write_case, write_case_data wc_data)
1334 int type = content_type[content];
1337 buf_copy_str_rpad (case_data_rw (c, mx->rowtype_->fv)->s, 8,
1338 content_names[content]);
1341 memset (case_data_rw (c, mx->varname_->fv)->s, ' ', 8);
1345 int n_lines = (type == 1) ? mx->n_continuous : 1;
1348 for (i = 0; i < n_lines; i++)
1352 for (j = 0; j < mx->n_continuous; j++)
1354 int fv = dict_get_var (dataset_dict (current_dataset), mx->first_continuous + j)->fv;
1355 case_data_rw (c, fv)->f = *cp;
1359 buf_copy_str_rpad (case_data_rw (c, mx->varname_->fv)->s, 8,
1360 dict_get_var (dataset_dict (current_dataset),
1361 mx->first_continuous + i)->name);
1362 if (!write_case (wc_data))
1369 /* Finally dump out everything from nr_data[] to the output file. */
1371 nr_output_data (struct nr_aux_data *nr, struct ccase *c,
1372 write_case_func *write_case, write_case_data wc_data)
1374 struct matrix_data_pgm *mx = nr->mx;
1377 struct variable *const *split;
1381 split_cnt = dict_get_split_cnt (dataset_dict (current_dataset));
1382 split = dict_get_split_vars (dataset_dict (current_dataset));
1383 for (i = 0; i < split_cnt; i++)
1384 case_data_rw (c, split[i]->fv)->f = nr->split_values[i];
1391 for (cell = 0; cell < mx->cells; cell++)
1396 for (factor = 0; factor < mx->n_factors; factor++)
1397 case_data_rw (c, mx->factors[factor]->fv)->f
1398 = nr->factor_values[factor + cell * mx->n_factors];
1404 for (content = 0; content <= PROX; content++)
1405 if (mx->is_per_factor[content])
1407 assert (nr->data[content] != NULL
1408 && nr->data[content][cell] != NULL);
1410 if (!dump_cell_content (mx, content, nr->data[content][cell],
1411 c, write_case, wc_data))
1424 for (factor = 0; factor < mx->n_factors; factor++)
1425 case_data_rw (c, mx->factors[factor]->fv)->f = SYSMIS;
1428 for (content = 0; content <= PROX; content++)
1429 if (!mx->is_per_factor[content] && nr->data[content] != NULL)
1431 if (!dump_cell_content (mx, content, nr->data[content][0],
1432 c, write_case, wc_data))
1440 /* Back end, with ROWTYPE_. */
1442 /* All the data for one set of factor values. */
1446 int n_rows[PROX + 1];
1447 double *data[PROX + 1];
1448 struct factor_data *next;
1451 /* With ROWTYPE_ auxiliary data. */
1454 struct matrix_data_pgm *mx; /* MATRIX DATA program. */
1455 int content; /* Type of current row. */
1456 double *split_values; /* SPLIT FILE variable values. */
1457 struct factor_data *data; /* All the data. */
1458 struct factor_data *current; /* Current factor. */
1461 static bool wr_read_splits (struct wr_aux_data *, struct ccase *,
1462 write_case_func *, write_case_data);
1463 static bool wr_output_data (struct wr_aux_data *, struct ccase *,
1464 write_case_func *, write_case_data);
1465 static bool wr_read_rowtype (struct wr_aux_data *,
1466 const struct matrix_token *, struct dfm_reader *);
1467 static bool wr_read_factors (struct wr_aux_data *);
1468 static bool wr_read_indeps (struct wr_aux_data *);
1469 static bool matrix_data_read_with_rowtype (struct case_source *,
1474 /* When ROWTYPE_ appears in the data, reads the matrices and writes
1475 them to the output file.
1476 Returns true if successful, false if an I/O error occurred. */
1478 read_matrices_with_rowtype (struct matrix_data_pgm *mx)
1480 struct wr_aux_data wr;
1485 wr.split_values = NULL;
1490 proc_set_source (current_dataset,
1491 create_case_source (&matrix_data_with_rowtype_source_class,
1493 ok = procedure (current_dataset,NULL, NULL);
1495 free (wr.split_values);
1499 /* Read from the data file and write it to the active file.
1500 Returns true if successful, false if an I/O error occurred. */
1502 matrix_data_read_with_rowtype (struct case_source *source,
1504 write_case_func *write_case,
1505 write_case_data wc_data)
1507 struct wr_aux_data *wr = source->aux;
1508 struct matrix_data_pgm *mx = wr->mx;
1512 if (!wr_read_splits (wr, c, write_case, wc_data))
1515 if (!wr_read_factors (wr))
1518 if (!wr_read_indeps (wr))
1521 while (another_token (mx->reader));
1523 return wr_output_data (wr, c, write_case, wc_data);
1526 /* Read the split file variables. If they differ from the previous
1527 set of split variables then output the data. Returns success. */
1529 wr_read_splits (struct wr_aux_data *wr,
1531 write_case_func *write_case, write_case_data wc_data)
1533 struct matrix_data_pgm *mx = wr->mx;
1537 split_cnt = dict_get_split_cnt (dataset_dict (current_dataset));
1541 if (wr->split_values)
1546 wr->split_values = xnmalloc (split_cnt, sizeof *wr->split_values);
1550 bool different = false;
1553 for (i = 0; i < split_cnt; i++)
1555 struct matrix_token token;
1556 if (!mget_token (&token, mx->reader))
1558 if (token.type != MNUM)
1560 msg (SE, _("Syntax error %s expecting SPLIT FILE value."),
1561 context (mx->reader));
1565 if (compare && wr->split_values[i] != token.number && !different)
1567 if (!wr_output_data (wr, c, write_case, wc_data))
1572 wr->split_values[i] = token.number;
1579 /* Compares doubles A and B, treating SYSMIS as greatest. */
1581 compare_doubles (const void *a_, const void *b_, void *aux UNUSED)
1583 const double *a = a_;
1584 const double *b = b_;
1588 else if (*a == SYSMIS)
1590 else if (*b == SYSMIS)
1598 /* Return strcmp()-type comparison of the MX->n_factors factors at _A and
1599 _B. Sort missing values toward the end. */
1601 compare_factors (const void *a_, const void *b_, void *mx_)
1603 struct matrix_data_pgm *mx = mx_;
1604 struct factor_data *const *pa = a_;
1605 struct factor_data *const *pb = b_;
1606 const double *a = (*pa)->factors;
1607 const double *b = (*pb)->factors;
1609 return lexicographical_compare_3way (a, mx->n_factors,
1612 compare_doubles, NULL);
1615 /* Write out the data for the current split file to the active
1617 Returns true if successful, false if an I/O error occurred. */
1619 wr_output_data (struct wr_aux_data *wr,
1621 write_case_func *write_case, write_case_data wc_data)
1623 struct matrix_data_pgm *mx = wr->mx;
1627 struct variable *const *split;
1631 split_cnt = dict_get_split_cnt (dataset_dict (current_dataset));
1632 split = dict_get_split_vars (dataset_dict (current_dataset));
1633 for (i = 0; i < split_cnt; i++)
1634 case_data_rw (c, split[i]->fv)->f = wr->split_values[i];
1637 /* Sort the wr->data list. */
1639 struct factor_data **factors;
1640 struct factor_data *iter;
1643 factors = xnmalloc (mx->cells, sizeof *factors);
1645 for (i = 0, iter = wr->data; iter; iter = iter->next, i++)
1648 sort (factors, mx->cells, sizeof *factors, compare_factors, mx);
1650 wr->data = factors[0];
1651 for (i = 0; i < mx->cells - 1; i++)
1652 factors[i]->next = factors[i + 1];
1653 factors[mx->cells - 1]->next = NULL;
1658 /* Write out records for every set of factor values. */
1660 struct factor_data *iter;
1662 for (iter = wr->data; iter; iter = iter->next)
1667 for (factor = 0; factor < mx->n_factors; factor++)
1668 case_data_rw (c, mx->factors[factor]->fv)->f
1669 = iter->factors[factor];
1675 for (content = 0; content <= PROX; content++)
1677 if (!iter->n_rows[content])
1681 int type = content_type[content];
1682 int n_lines = (type == 1
1684 - (mx->section != FULL && mx->diag == NODIAGONAL))
1687 if (n_lines != iter->n_rows[content])
1689 msg (SE, _("Expected %d lines of data for %s content; "
1690 "actually saw %d lines. No data will be "
1691 "output for this content."),
1692 n_lines, content_names[content],
1693 iter->n_rows[content]);
1698 fill_matrix (mx, content, iter->data[content]);
1700 ok = dump_cell_content (mx, content, iter->data[content],
1701 c, write_case, wc_data);
1709 pool_destroy (mx->container);
1710 mx->container = pool_create ();
1712 wr->data = wr->current = NULL;
1717 /* Sets ROWTYPE_ based on the given TOKEN read from READER.
1720 wr_read_rowtype (struct wr_aux_data *wr,
1721 const struct matrix_token *token,
1722 struct dfm_reader *reader)
1724 if (wr->content != -1)
1726 msg (SE, _("Multiply specified ROWTYPE_ %s."), context (reader));
1729 if (token->type != MSTR)
1731 msg (SE, _("Syntax error %s expecting ROWTYPE_ string."),
1740 memcpy (s, token->string, min (15, token->length));
1741 s[min (15, token->length)] = 0;
1743 for (cp = s; *cp; cp++)
1744 *cp = toupper ((unsigned char) *cp);
1746 wr->content = string_to_content_type (s, NULL);
1749 if (wr->content == -1)
1751 msg (SE, _("Syntax error %s."), context (reader));
1758 /* Read the factors for the current row. Select a set of factors and
1759 point wr_current to it. */
1761 wr_read_factors (struct wr_aux_data *wr)
1763 struct matrix_data_pgm *mx = wr->mx;
1764 double *factor_values = local_alloc (sizeof *factor_values * mx->n_factors);
1770 for (i = 0; i < mx->n_factors; i++)
1772 struct matrix_token token;
1773 if (!mget_token (&token, mx->reader))
1775 if (token.type == MSTR)
1777 if (!wr_read_rowtype (wr, &token, mx->reader))
1779 if (!mget_token (&token, mx->reader))
1782 if (token.type != MNUM)
1784 msg (SE, _("Syntax error expecting factor value %s."),
1785 context (mx->reader));
1789 factor_values[i] = token.number;
1792 if (wr->content == -1)
1794 struct matrix_token token;
1795 if (!mget_token (&token, mx->reader))
1797 if (!wr_read_rowtype (wr, &token, mx->reader))
1801 /* Try the most recent factor first as a simple caching
1807 for (i = 0; i < mx->n_factors; i++)
1808 if (factor_values[i] != wr->current->factors[i])
1813 /* Linear search through the list. */
1816 struct factor_data *iter;
1818 for (iter = wr->data; iter; iter = iter->next)
1822 for (i = 0; i < mx->n_factors; i++)
1823 if (factor_values[i] != iter->factors[i])
1833 /* Not found. Make a new item. */
1835 struct factor_data *new = pool_alloc (mx->container, sizeof *new);
1837 new->factors = pool_nalloc (mx->container,
1838 mx->n_factors, sizeof *new->factors);
1843 for (i = 0; i < mx->n_factors; i++)
1844 new->factors[i] = factor_values[i];
1850 for (i = 0; i <= PROX; i++)
1853 new->data[i] = NULL;
1857 new->next = wr->data;
1858 wr->data = wr->current = new;
1863 local_free (factor_values);
1867 local_free (factor_values);
1871 /* Read the independent variables into wr->current. */
1873 wr_read_indeps (struct wr_aux_data *wr)
1875 struct matrix_data_pgm *mx = wr->mx;
1876 struct factor_data *c = wr->current;
1877 const int type = content_type[wr->content];
1878 const int n_rows = c->n_rows[wr->content];
1882 /* Allocate room for data if necessary. */
1883 if (c->data[wr->content] == NULL)
1885 int n_items = mx->n_continuous;
1887 n_items *= mx->n_continuous;
1889 c->data[wr->content] = pool_nalloc (mx->container,
1890 n_items, sizeof **c->data);
1893 cp = &c->data[wr->content][n_rows * mx->n_continuous];
1895 /* Figure out how much to read from this line. */
1902 msg (SE, _("Duplicate specification for %s."),
1903 content_names[wr->content]);
1907 n_cols = mx->n_continuous;
1912 if (n_rows >= mx->n_continuous - (mx->section != FULL && mx->diag == NODIAGONAL))
1914 msg (SE, _("Too many rows of matrix data for %s."),
1915 content_names[wr->content]);
1919 switch (mx->section)
1922 n_cols = n_rows + 1;
1923 if (mx->diag == NODIAGONAL)
1924 cp += mx->n_continuous;
1928 n_cols = mx->n_continuous - n_rows;
1929 if (mx->diag == NODIAGONAL)
1936 n_cols = mx->n_continuous;
1945 c->n_rows[wr->content]++;
1947 /* Read N_COLS items at CP. */
1951 for (j = 0; j < n_cols; j++)
1953 struct matrix_token token;
1954 if (!mget_token (&token, mx->reader))
1956 if (token.type != MNUM)
1958 msg (SE, _("Syntax error expecting value for %s %s."),
1959 dict_get_var (dataset_dict (current_dataset), mx->first_continuous + j)->name,
1960 context (mx->reader));
1964 *cp++ = token.number;
1967 && !force_eol (mx->reader, content_names[wr->content]))
1974 /* Matrix source. */
1976 static const struct case_source_class matrix_data_with_rowtype_source_class =
1980 matrix_data_read_with_rowtype,
1984 static const struct case_source_class
1985 matrix_data_without_rowtype_source_class =
1989 matrix_data_read_without_rowtype,