1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000, 2006 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 dictionary 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 dataset *ds, struct matrix_data_pgm *);
172 static bool read_matrices_with_rowtype (struct dataset *ds, struct matrix_data_pgm *);
173 static int string_to_content_type (const char *, int *);
174 static void attach_mxd_aux (struct variable *, int var_type, int sub_type);
177 cmd_matrix_data (struct lexer *lexer, struct dataset *ds)
180 struct matrix_data_pgm *mx;
181 struct file_handle *fh = fh_inline_file ();
186 discard_variables (ds);
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;
207 while (lex_token (lexer) != '.')
209 lex_match (lexer, '/');
211 if (lex_match_id (lexer, "VARIABLES"))
218 msg (SE, _("VARIABLES subcommand multiply specified."));
223 lex_match (lexer, '=');
224 if (!parse_DATA_LIST_vars (lexer, &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 (ds), 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 (ds),
263 attach_mxd_aux (mx->rowtype_, MXD_ROWTYPE, 0);
265 else if (lex_match_id (lexer, "FILE"))
267 lex_match (lexer, '=');
268 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE);
272 else if (lex_match_id (lexer, "FORMAT"))
274 lex_match (lexer, '=');
276 while (lex_token (lexer) == T_ID)
278 if (lex_match_id (lexer, "LIST"))
280 else if (lex_match_id (lexer, "FREE"))
282 else if (lex_match_id (lexer, "LOWER"))
284 else if (lex_match_id (lexer, "UPPER"))
286 else if (lex_match_id (lexer, "FULL"))
288 else if (lex_match_id (lexer, "DIAGONAL"))
290 else if (lex_match_id (lexer, "NODIAGONAL"))
291 mx->diag = NODIAGONAL;
294 lex_error (lexer, _("in FORMAT subcommand"));
299 else if (lex_match_id (lexer, "SPLIT"))
301 lex_match (lexer, '=');
305 msg (SE, _("SPLIT subcommand multiply specified."));
310 if (lex_token (lexer) != T_ID)
312 lex_error (lexer, _("in SPLIT subcommand"));
316 if (dict_lookup_var (dataset_dict (ds), lex_tokid (lexer)) == NULL
317 && (lex_look_ahead (lexer) == '.' || lex_look_ahead (lexer) == '/'))
319 if (!strcasecmp (lex_tokid (lexer), "ROWTYPE_")
320 || !strcasecmp (lex_tokid (lexer), "VARNAME_"))
322 msg (SE, _("Split variable may not be named ROWTYPE_ "
327 mx->single_split = dict_create_var_assert (dataset_dict (ds),
328 lex_tokid (lexer), 0);
329 attach_mxd_aux (mx->single_split, MXD_CONTINUOUS, 0);
332 dict_set_split_vars (dataset_dict (ds), &mx->single_split, 1);
336 struct variable **split;
339 if (!parse_variables (lexer, dataset_dict (ds),
340 &split, &n, PV_NO_DUPLICATE))
343 dict_set_split_vars (dataset_dict (ds), split, n);
347 struct variable *const *split = dict_get_split_vars (dataset_dict (ds));
348 size_t split_cnt = dict_get_split_cnt (dataset_dict (ds));
351 for (i = 0; i < split_cnt; i++)
353 struct mxd_var *mv = split[i]->aux;
355 if (mv->var_type != MXD_CONTINUOUS)
357 msg (SE, _("Split variable %s is already another type."),
361 var_clear_aux (split[i]);
362 attach_mxd_aux (split[i], MXD_SPLIT, i);
366 else if (lex_match_id (lexer, "FACTORS"))
368 lex_match (lexer, '=');
372 msg (SE, _("FACTORS subcommand multiply specified."));
377 if (!parse_variables (lexer, dataset_dict (ds), &mx->factors, &mx->n_factors,
384 for (i = 0; i < mx->n_factors; i++)
386 struct variable *v = mx->factors[i];
387 struct mxd_var *mv = v->aux;
389 if (mv->var_type != MXD_CONTINUOUS)
391 msg (SE, _("Factor variable %s is already another type."),
396 attach_mxd_aux (v, MXD_FACTOR, i);
400 else if (lex_match_id (lexer, "CELLS"))
402 lex_match (lexer, '=');
406 msg (SE, _("CELLS subcommand multiply specified."));
410 if (!lex_is_integer (lexer) || lex_integer (lexer) < 1)
412 lex_error (lexer, _("expecting positive integer"));
416 mx->cells = lex_integer (lexer);
419 else if (lex_match_id (lexer, "N"))
421 lex_match (lexer, '=');
425 msg (SE, _("N subcommand multiply specified."));
429 if (!lex_is_integer (lexer) || lex_integer (lexer) < 1)
431 lex_error (lexer, _("expecting positive integer"));
435 mx->pop_n = lex_integer (lexer);
438 else if (lex_match_id (lexer, "CONTENTS"))
440 int inside_parens = 0;
441 unsigned collide = 0;
446 msg (SE, _("CONTENTS subcommand multiply specified."));
451 lex_match (lexer, '=');
456 for (i = 0; i <= PROX; i++)
457 mx->is_per_factor[i] = 0;
462 if (lex_match (lexer, '('))
466 msg (SE, _("Nested parentheses not allowed."));
472 else if (lex_match (lexer, ')'))
476 msg (SE, _("Mismatched right parenthesis (`(')."));
479 if (mx->contents[mx->n_contents - 1] == LPAREN)
481 msg (SE, _("Empty parentheses not allowed."));
492 if (lex_token (lexer) != T_ID)
494 lex_error (lexer, _("in CONTENTS subcommand"));
498 content_type = string_to_content_type (lex_tokid (lexer),
500 if (content_type == -1)
502 lex_error (lexer, _("in CONTENTS subcommand"));
507 if (collide & (1 << collide_index))
509 msg (SE, _("Content multiply specified for %s."),
510 content_names[content_type]);
513 collide |= (1 << collide_index);
516 mx->is_per_factor[item] = inside_parens;
518 mx->contents[mx->n_contents++] = item;
520 if (lex_token (lexer) == '/' || lex_token (lexer) == '.')
526 msg (SE, _("Missing right parenthesis."));
529 mx->contents[mx->n_contents] = EOC;
533 lex_error (lexer, NULL);
538 if (lex_token (lexer) != '.')
540 lex_error (lexer, _("expecting end of command"));
546 msg (SE, _("Missing VARIABLES subcommand."));
550 if (!mx->n_contents && !mx->explicit_rowtype)
552 msg (SW, _("CONTENTS subcommand not specified: assuming file "
553 "contains only CORR matrix."));
555 mx->contents[0] = CORR;
556 mx->contents[1] = EOC;
560 if (mx->n_factors && !mx->explicit_rowtype && mx->cells == -1)
562 msg (SE, _("Missing CELLS subcommand. CELLS is required "
563 "when ROWTYPE_ is not given in the data and "
564 "factors are present."));
568 if (mx->explicit_rowtype && mx->single_split)
570 msg (SE, _("Split file values must be present in the data when "
571 "ROWTYPE_ is present."));
575 /* Create VARNAME_. */
576 mx->varname_ = dict_create_var_assert (dataset_dict (ds), "VARNAME_", 8);
577 attach_mxd_aux (mx->varname_, MXD_VARNAME, 0);
579 /* Sort the dictionary variables into the desired order for the
580 system file output. */
585 dict_get_vars (dataset_dict (ds), &v, &nv, 0);
586 qsort (v, nv, sizeof *v, compare_variables_by_mxd_var_type);
587 dict_reorder_vars (dataset_dict (ds), v, nv);
593 static const struct fmt_spec fmt_tab[MXD_COUNT] =
604 mx->first_continuous = -1;
605 for (i = 0; i < dict_get_var_cnt (dataset_dict (ds)); i++)
607 struct variable *v = dict_get_var (dataset_dict (ds), i);
608 struct mxd_var *mv = v->aux;
609 int type = mv->var_type;
611 assert (type >= 0 && type < MXD_COUNT);
612 v->print = v->write = fmt_tab[type];
614 if (type == MXD_CONTINUOUS)
616 if (mx->first_continuous == -1 && type == MXD_CONTINUOUS)
617 mx->first_continuous = i;
621 if (mx->n_continuous == 0)
623 msg (SE, _("No continuous variables specified."));
627 mx->reader = dfm_open_reader (fh, lexer);
628 if (mx->reader == NULL)
631 if (mx->explicit_rowtype)
632 ok = read_matrices_with_rowtype (ds, mx);
634 ok = read_matrices_without_rowtype (ds, mx);
636 dfm_close_reader (mx->reader);
638 pool_destroy (mx->container);
640 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
643 discard_variables (ds);
645 pool_destroy (mx->container);
646 return CMD_CASCADING_FAILURE;
649 /* Look up string S as a content-type name and return the
650 corresponding enumerated value, or -1 if there is no match. If
651 COLLIDE is non-NULL then *COLLIDE returns a value (suitable for use
652 as a bit-index) which can be used for determining whether a related
653 statistic has already been used. */
655 string_to_content_type (const char *s, int *collide)
666 {N_VECTOR, 0, "N_VECTOR"},
668 {N_SCALAR, 0, "N_SCALAR"},
669 {N_MATRIX, 1, "N_MATRIX"},
671 {STDDEV, 3, "STDDEV"},
683 for (tp = tab; tp->value != -1; tp++)
684 if (!strcasecmp (s, tp->string))
687 *collide = tp->collide;
694 /* Compare two variables using p.mxd.var_type and p.mxd.sub_type
697 compare_variables_by_mxd_var_type (const void *a_, const void *b_)
699 struct variable *const *pa = a_;
700 struct variable *const *pb = b_;
701 const struct mxd_var *a = (*pa)->aux;
702 const struct mxd_var *b = (*pb)->aux;
704 if (a->var_type != b->var_type)
705 return a->var_type > b->var_type ? 1 : -1;
707 return a->sub_type < b->sub_type ? -1 : a->sub_type > b->sub_type;
710 /* Attaches a struct mxd_var with the specific member values to
713 attach_mxd_aux (struct variable *v, int var_type, int sub_type)
717 assert (v->aux == NULL);
718 mv = xmalloc (sizeof *mv);
719 mv->var_type = var_type;
720 mv->sub_type = sub_type;
721 var_attach_aux (v, mv, var_dtor_free);
724 /* Matrix tokenizer. */
726 /* Matrix token types. */
727 enum matrix_token_type
733 /* A MATRIX DATA parsing token. */
736 enum matrix_token_type type;
737 double number; /* MNUM: token value. */
738 char *string; /* MSTR: token string; not null-terminated. */
739 int length; /* MSTR: tokstr length. */
742 static int mget_token (struct matrix_token *, struct dfm_reader *);
745 #define mget_token(TOKEN, READER) mget_token_dump(TOKEN, READER)
748 mdump_token (const struct matrix_token *token)
753 printf (" #%g", token->number);
756 printf (" '%.*s'", token->length, token->string);
765 mget_token_dump (struct matrix_token *token, struct dfm_reader *reader)
767 int result = (mget_token) (token, reader);
773 /* Return the current position in READER. */
775 context (struct dfm_reader *reader)
777 static struct string buf = DS_EMPTY_INITIALIZER;
780 if (dfm_eof (reader))
781 ds_assign_cstr (&buf, "at end of file");
786 p = dfm_get_record (reader);
787 ss_ltrim (&p, ss_cstr (CC_SPACES));
789 ds_assign_cstr (&buf, "at end of line");
791 ds_put_format (&buf, "before `%.*s'",
792 (int) ss_cspan (p, ss_cstr (CC_SPACES)), ss_data (p));
795 return ds_cstr (&buf);
798 /* Is there at least one token left in the data file? */
800 another_token (struct dfm_reader *reader)
807 if (dfm_eof (reader))
810 p = dfm_get_record (reader);
811 space_cnt = ss_span (p, ss_cstr (CC_SPACES));
812 if (space_cnt < ss_length (p))
814 dfm_forward_columns (reader, space_cnt);
818 dfm_forward_record (reader);
823 /* Parse a MATRIX DATA token from READER into TOKEN. */
825 (mget_token) (struct matrix_token *token, struct dfm_reader *reader)
827 struct substring line, p;
831 if (!another_token (reader))
834 line = p = dfm_get_record (reader);
836 /* Three types of fields: quoted with ', quoted with ", unquoted. */
838 if (c == '\'' || c == '"')
841 if (!ss_get_until (&p, c, &s))
842 msg (SW, _("Scope of string exceeds line."));
846 bool is_num = isdigit (c) || c == '.';
847 const char *start = ss_data (p);
852 if (strchr (CC_SPACES ",-+", c) != NULL)
857 if (strchr ("deDE", c) && strchr ("+-", ss_at (p, 1)))
865 s = ss_buffer (start, ss_data (p) - start);
868 data_in (s, FMT_F, 0,
869 dfm_get_column (reader, ss_data (s)),
870 (union value *) &token->number, 0);
874 token->string = ss_data (s);
875 token->length = ss_length (s);
877 dfm_reread_record (reader, dfm_get_column (reader, ss_end (s)));
882 /* Forcibly skip the end of a line for content type CONTENT in
885 force_eol (struct dfm_reader *reader, const char *content)
889 if (dfm_eof (reader))
892 p = dfm_get_record (reader);
893 if (ss_span (p, ss_cstr (CC_SPACES)) != ss_length (p))
895 msg (SE, _("End of line expected %s while reading %s."),
896 context (reader), content);
900 dfm_forward_record (reader);
904 /* Back end, omitting ROWTYPE_. */
908 const struct dictionary *dict; /* The dictionary */
909 struct matrix_data_pgm *mx; /* MATRIX DATA program. */
910 double ***data; /* MATRIX DATA data. */
911 double *factor_values; /* Factor values. */
912 int max_cell_idx; /* Max-numbered cell that we have
913 read so far, plus one. */
914 double *split_values; /* SPLIT FILE variable values. */
917 static bool nr_read_splits (struct nr_aux_data *, int compare);
918 static bool nr_read_factors (struct nr_aux_data *, int cell);
919 static bool nr_output_data (struct nr_aux_data *, struct ccase *,
920 write_case_func *, write_case_data);
921 static bool matrix_data_read_without_rowtype (struct case_source *source,
926 /* Read from the data file and write it to the active file.
927 Returns true if successful, false if an I/O error occurred. */
929 read_matrices_without_rowtype (struct dataset *ds, struct matrix_data_pgm *mx)
931 struct nr_aux_data nr;
938 nr.dict = dataset_dict (ds);
940 nr.factor_values = xnmalloc (mx->n_factors * mx->cells,
941 sizeof *nr.factor_values);
943 nr.split_values = xnmalloc (dict_get_split_cnt (dataset_dict (ds)),
944 sizeof *nr.split_values);
946 proc_set_source (ds, create_case_source (
947 &matrix_data_without_rowtype_source_class, &nr));
949 ok = procedure (ds, NULL, NULL);
951 free (nr.split_values);
952 free (nr.factor_values);
957 /* Mirror data across the diagonal of matrix CP which contains
958 CONTENT type data. */
960 fill_matrix (struct matrix_data_pgm *mx, int content, double *cp)
962 int type = content_type[content];
964 if (type == 1 && mx->section != FULL)
966 if (mx->diag == NODIAGONAL)
968 const double fill = content == CORR ? 1.0 : SYSMIS;
971 for (i = 0; i < mx->n_continuous; i++)
972 cp[i * (1 + mx->n_continuous)] = fill;
978 if (mx->section == LOWER)
980 int n_lines = mx->n_continuous;
981 if (mx->section != FULL && mx->diag == NODIAGONAL)
984 for (r = 1; r < n_lines; r++)
985 for (c = 0; c < r; c++)
986 cp[r + c * mx->n_continuous] = cp[c + r * mx->n_continuous];
990 assert (mx->section == UPPER);
991 for (r = 1; r < mx->n_continuous; r++)
992 for (c = 0; c < r; c++)
993 cp[c + r * mx->n_continuous] = cp[r + c * mx->n_continuous];
1001 for (c = 1; c < mx->n_continuous; c++)
1006 /* Read data lines for content type CONTENT from the data file.
1007 If PER_FACTOR is nonzero, then factor information is read from
1008 the data file. Data is for cell number CELL. */
1010 nr_read_data_lines (struct nr_aux_data *nr,
1011 int per_factor, int cell, int content, int compare)
1013 struct matrix_data_pgm *mx = nr->mx;
1014 const int type = content_type[content]; /* Content type. */
1015 int n_lines; /* Number of lines to parse from data file for this type. */
1016 double *cp; /* Current position in vector or matrix. */
1023 n_lines = mx->n_continuous;
1024 if (mx->section != FULL && mx->diag == NODIAGONAL)
1028 cp = nr->data[content][cell];
1029 if (type == 1 && mx->section == LOWER && mx->diag == NODIAGONAL)
1030 cp += mx->n_continuous;
1032 for (i = 0; i < n_lines; i++)
1036 if (!nr_read_splits (nr, 1))
1038 if (per_factor && !nr_read_factors (nr, cell))
1045 n_cols = mx->n_continuous;
1048 switch (mx->section)
1055 n_cols = mx->n_continuous - i;
1056 if (mx->diag == NODIAGONAL)
1063 n_cols = mx->n_continuous;
1079 for (j = 0; j < n_cols; j++)
1081 struct matrix_token token;
1082 if (!mget_token (&token, mx->reader))
1084 if (token.type != MNUM)
1086 msg (SE, _("expecting value for %s %s"),
1087 dict_get_var (nr->dict, j)->name,
1088 context (mx->reader));
1092 *cp++ = token.number;
1095 && !force_eol (mx->reader, content_names[content]))
1099 if (mx->section == LOWER)
1100 cp += mx->n_continuous - n_cols;
1103 fill_matrix (mx, content, nr->data[content][cell]);
1108 /* When ROWTYPE_ does not appear in the data, reads the matrices and
1109 writes them to the output file.
1110 Returns true if successful, false if an I/O error occurred. */
1112 matrix_data_read_without_rowtype (struct case_source *source,
1114 write_case_func *write_case,
1115 write_case_data wc_data)
1117 struct nr_aux_data *nr = source->aux;
1118 struct matrix_data_pgm *mx = nr->mx;
1123 nr->data = pool_nalloc (mx->container, PROX + 1, sizeof *nr->data);
1128 for (i = 0; i <= PROX; i++)
1132 for (cp = mx->contents; *cp != EOC; cp++)
1133 if (*cp != LPAREN && *cp != RPAREN)
1135 int per_factor = mx->is_per_factor[*cp];
1138 n_entries = mx->n_continuous;
1139 if (content_type[*cp] == 1)
1140 n_entries *= mx->n_continuous;
1143 int n_vectors = per_factor ? mx->cells : 1;
1146 nr->data[*cp] = pool_nalloc (mx->container,
1147 n_vectors, sizeof **nr->data);
1149 for (i = 0; i < n_vectors; i++)
1150 nr->data[*cp][i] = pool_nalloc (mx->container,
1151 n_entries, sizeof ***nr->data);
1160 if (!nr_read_splits (nr, 0))
1163 for (bp = mx->contents; *bp != EOC; bp = np)
1167 /* Trap the CONTENTS that we should parse in this pass
1168 between bp and ep. Set np to the starting bp for next
1173 while (*ep != RPAREN)
1181 while (*ep != EOC && *ep != LPAREN)
1190 for (i = 0; i < (per_factor ? mx->cells : 1); i++)
1194 for (cp = bp; cp < ep; cp++)
1195 if (!nr_read_data_lines (nr, per_factor, i, *cp, cp != bp))
1201 if (!nr_output_data (nr, c, write_case, wc_data))
1204 if (dict_get_split_cnt (nr->dict) == 0
1205 || !another_token (mx->reader))
1210 /* Read the split file variables. If COMPARE is 1, compares the
1211 values read to the last values read and returns true if they're equal,
1214 nr_read_splits (struct nr_aux_data *nr, int compare)
1216 struct matrix_data_pgm *mx = nr->mx;
1217 static int just_read = 0; /* FIXME: WTF? */
1221 if (compare && just_read)
1227 if (dict_get_split_vars (nr->dict) == NULL)
1230 if (mx->single_split)
1234 struct mxd_var *mv = dict_get_split_vars (nr->dict)[0]->aux;
1235 nr->split_values[0] = ++mv->sub_type;
1243 split_cnt = dict_get_split_cnt (nr->dict);
1244 for (i = 0; i < split_cnt; i++)
1246 struct matrix_token token;
1247 if (!mget_token (&token, mx->reader))
1249 if (token.type != MNUM)
1251 msg (SE, _("Syntax error expecting SPLIT FILE value %s."),
1252 context (mx->reader));
1257 nr->split_values[i] = token.number;
1258 else if (nr->split_values[i] != token.number)
1260 msg (SE, _("Expecting value %g for %s."),
1261 nr->split_values[i],
1262 dict_get_split_vars (nr->dict)[i]->name);
1270 /* Read the factors for cell CELL. If COMPARE is 1, compares the
1271 values read to the last values read and returns true if they're equal,
1274 nr_read_factors (struct nr_aux_data *nr, int cell)
1276 struct matrix_data_pgm *mx = nr->mx;
1279 if (mx->n_factors == 0)
1282 assert (nr->max_cell_idx >= cell);
1283 if (cell != nr->max_cell_idx)
1294 for (i = 0; i < mx->n_factors; i++)
1296 struct matrix_token token;
1297 if (!mget_token (&token, mx->reader))
1299 if (token.type != MNUM)
1301 msg (SE, _("Syntax error expecting factor value %s."),
1302 context (mx->reader));
1307 nr->factor_values[i + mx->n_factors * cell] = token.number;
1308 else if (nr->factor_values[i + mx->n_factors * cell] != token.number)
1310 msg (SE, _("Syntax error expecting value %g for %s %s."),
1311 nr->factor_values[i + mx->n_factors * cell],
1312 mx->factors[i]->name, context (mx->reader));
1321 /* Write the contents of a cell having content type CONTENT and data
1322 CP to the active file.
1323 Returns true if successful, false if an I/O error occurred. */
1325 dump_cell_content (const struct dictionary *dict,
1326 struct matrix_data_pgm *mx, int content, double *cp,
1328 write_case_func *write_case, write_case_data wc_data)
1330 int type = content_type[content];
1333 buf_copy_str_rpad (case_data_rw (c, mx->rowtype_->fv)->s, 8,
1334 content_names[content]);
1337 memset (case_data_rw (c, mx->varname_->fv)->s, ' ', 8);
1341 int n_lines = (type == 1) ? mx->n_continuous : 1;
1344 for (i = 0; i < n_lines; i++)
1348 for (j = 0; j < mx->n_continuous; j++)
1350 int fv = dict_get_var (dict, mx->first_continuous + j)->fv;
1351 case_data_rw (c, fv)->f = *cp;
1355 buf_copy_str_rpad (case_data_rw (c, mx->varname_->fv)->s, 8,
1357 mx->first_continuous + i)->name);
1358 if (!write_case (wc_data))
1365 /* Finally dump out everything from nr_data[] to the output file. */
1367 nr_output_data (struct nr_aux_data *nr, struct ccase *c,
1368 write_case_func *write_case, write_case_data wc_data)
1370 struct matrix_data_pgm *mx = nr->mx;
1373 struct variable *const *split;
1377 split_cnt = dict_get_split_cnt (nr->dict);
1378 split = dict_get_split_vars (nr->dict);
1379 for (i = 0; i < split_cnt; i++)
1380 case_data_rw (c, split[i]->fv)->f = nr->split_values[i];
1387 for (cell = 0; cell < mx->cells; cell++)
1392 for (factor = 0; factor < mx->n_factors; factor++)
1393 case_data_rw (c, mx->factors[factor]->fv)->f
1394 = nr->factor_values[factor + cell * mx->n_factors];
1400 for (content = 0; content <= PROX; content++)
1401 if (mx->is_per_factor[content])
1403 assert (nr->data[content] != NULL
1404 && nr->data[content][cell] != NULL);
1406 if (!dump_cell_content (nr->dict, mx,
1407 content, nr->data[content][cell],
1408 c, write_case, wc_data))
1421 for (factor = 0; factor < mx->n_factors; factor++)
1422 case_data_rw (c, mx->factors[factor]->fv)->f = SYSMIS;
1425 for (content = 0; content <= PROX; content++)
1426 if (!mx->is_per_factor[content] && nr->data[content] != NULL)
1428 if (!dump_cell_content (nr->dict, mx, content, nr->data[content][0],
1429 c, write_case, wc_data))
1437 /* Back end, with ROWTYPE_. */
1439 /* All the data for one set of factor values. */
1443 int n_rows[PROX + 1];
1444 double *data[PROX + 1];
1445 struct factor_data *next;
1448 /* With ROWTYPE_ auxiliary data. */
1451 const struct dictionary *dict; /* The dictionary */
1452 struct matrix_data_pgm *mx; /* MATRIX DATA program. */
1453 int content; /* Type of current row. */
1454 double *split_values; /* SPLIT FILE variable values. */
1455 struct factor_data *data; /* All the data. */
1456 struct factor_data *current; /* Current factor. */
1459 static bool wr_read_splits (struct wr_aux_data *, struct ccase *,
1460 write_case_func *, write_case_data);
1461 static bool wr_output_data (struct wr_aux_data *, struct ccase *,
1462 write_case_func *, write_case_data);
1463 static bool wr_read_rowtype (struct wr_aux_data *,
1464 const struct matrix_token *, struct dfm_reader *);
1465 static bool wr_read_factors (struct wr_aux_data *);
1466 static bool wr_read_indeps (struct wr_aux_data *);
1467 static bool matrix_data_read_with_rowtype (struct case_source *,
1472 /* When ROWTYPE_ appears in the data, reads the matrices and writes
1473 them to the output file.
1474 Returns true if successful, false if an I/O error occurred. */
1476 read_matrices_with_rowtype (struct dataset *ds, struct matrix_data_pgm *mx)
1478 struct wr_aux_data wr;
1483 wr.split_values = NULL;
1486 wr.dict = dataset_dict (ds);
1489 proc_set_source (ds,
1490 create_case_source (&matrix_data_with_rowtype_source_class,
1492 ok = procedure (ds, NULL, NULL);
1494 free (wr.split_values);
1498 /* Read from the data file and write it to the active file.
1499 Returns true if successful, false if an I/O error occurred. */
1501 matrix_data_read_with_rowtype (struct case_source *source,
1503 write_case_func *write_case,
1504 write_case_data wc_data)
1506 struct wr_aux_data *wr = source->aux;
1507 struct matrix_data_pgm *mx = wr->mx;
1511 if (!wr_read_splits (wr, c, write_case, wc_data))
1514 if (!wr_read_factors (wr))
1517 if (!wr_read_indeps (wr))
1520 while (another_token (mx->reader));
1522 return wr_output_data (wr, c, write_case, wc_data);
1525 /* Read the split file variables. If they differ from the previous
1526 set of split variables then output the data. Returns success. */
1528 wr_read_splits (struct wr_aux_data *wr,
1530 write_case_func *write_case, write_case_data wc_data)
1532 struct matrix_data_pgm *mx = wr->mx;
1536 split_cnt = dict_get_split_cnt (wr->dict);
1540 if (wr->split_values)
1545 wr->split_values = xnmalloc (split_cnt, sizeof *wr->split_values);
1549 bool different = false;
1552 for (i = 0; i < split_cnt; i++)
1554 struct matrix_token token;
1555 if (!mget_token (&token, mx->reader))
1557 if (token.type != MNUM)
1559 msg (SE, _("Syntax error %s expecting SPLIT FILE value."),
1560 context (mx->reader));
1564 if (compare && wr->split_values[i] != token.number && !different)
1566 if (!wr_output_data (wr, c, write_case, wc_data))
1571 wr->split_values[i] = token.number;
1578 /* Compares doubles A and B, treating SYSMIS as greatest. */
1580 compare_doubles (const void *a_, const void *b_, const void *aux UNUSED)
1582 const double *a = a_;
1583 const double *b = b_;
1587 else if (*a == SYSMIS)
1589 else if (*b == SYSMIS)
1597 /* Return strcmp()-type comparison of the MX->n_factors factors at _A and
1598 _B. Sort missing values toward the end. */
1600 compare_factors (const void *a_, const void *b_, const void *mx_)
1602 const struct matrix_data_pgm *mx = mx_;
1603 struct factor_data *const *pa = a_;
1604 struct factor_data *const *pb = b_;
1605 const double *a = (*pa)->factors;
1606 const double *b = (*pb)->factors;
1608 return lexicographical_compare_3way (a, mx->n_factors,
1611 compare_doubles, NULL);
1614 /* Write out the data for the current split file to the active
1616 Returns true if successful, false if an I/O error occurred. */
1618 wr_output_data (struct wr_aux_data *wr,
1620 write_case_func *write_case, write_case_data wc_data)
1622 struct matrix_data_pgm *mx = wr->mx;
1626 struct variable *const *split;
1630 split_cnt = dict_get_split_cnt (wr->dict);
1631 split = dict_get_split_vars (wr->dict);
1632 for (i = 0; i < split_cnt; i++)
1633 case_data_rw (c, split[i]->fv)->f = wr->split_values[i];
1636 /* Sort the wr->data list. */
1638 struct factor_data **factors;
1639 struct factor_data *iter;
1642 factors = xnmalloc (mx->cells, sizeof *factors);
1644 for (i = 0, iter = wr->data; iter; iter = iter->next, i++)
1647 sort (factors, mx->cells, sizeof *factors, compare_factors, mx);
1649 wr->data = factors[0];
1650 for (i = 0; i < mx->cells - 1; i++)
1651 factors[i]->next = factors[i + 1];
1652 factors[mx->cells - 1]->next = NULL;
1657 /* Write out records for every set of factor values. */
1659 struct factor_data *iter;
1661 for (iter = wr->data; iter; iter = iter->next)
1666 for (factor = 0; factor < mx->n_factors; factor++)
1667 case_data_rw (c, mx->factors[factor]->fv)->f
1668 = iter->factors[factor];
1674 for (content = 0; content <= PROX; content++)
1676 if (!iter->n_rows[content])
1680 int type = content_type[content];
1681 int n_lines = (type == 1
1683 - (mx->section != FULL && mx->diag == NODIAGONAL))
1686 if (n_lines != iter->n_rows[content])
1688 msg (SE, _("Expected %d lines of data for %s content; "
1689 "actually saw %d lines. No data will be "
1690 "output for this content."),
1691 n_lines, content_names[content],
1692 iter->n_rows[content]);
1697 fill_matrix (mx, content, iter->data[content]);
1699 ok = dump_cell_content (wr->dict, mx, content,
1700 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 (wr->dict, 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,