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>
51 #define _(msgid) gettext (msgid)
53 /* FIXME: /N subcommand not implemented. It should be pretty simple,
56 /* Different types of variables for MATRIX DATA procedure. Order is
57 important: these are used for sort keys. */
60 MXD_SPLIT, /* SPLIT FILE variables. */
61 MXD_ROWTYPE, /* ROWTYPE_. */
62 MXD_FACTOR, /* Factor variables. */
63 MXD_VARNAME, /* VARNAME_. */
64 MXD_CONTINUOUS, /* Continuous variables. */
69 /* Format type enums. */
76 /* Matrix section enums. */
84 /* Diagonal inclusion enums. */
112 /* 0=vector, 1=matrix, 2=scalar. */
113 static const int content_type[PROX + 1] =
115 0, 2, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1,
118 /* Name of each content type. */
119 static const char *const content_names[PROX + 1] =
121 "N", "N", "N_MATRIX", "MEAN", "STDDEV", "COUNT", "MSE",
122 "DFE", "MAT", "COV", "CORR", "PROX",
125 /* A MATRIX DATA input program. */
126 struct matrix_data_pgm
128 struct pool *container; /* Arena used for all allocations. */
129 struct dfm_reader *reader; /* Data file to read. */
132 enum format_type fmt; /* LIST or FREE. */
133 enum matrix_section section;/* LOWER or UPPER or FULL. */
134 enum include_diagonal diag; /* DIAGONAL or NODIAGONAL. */
136 int explicit_rowtype; /* ROWTYPE_ specified explicitly in data? */
137 struct variable *rowtype_, *varname_; /* ROWTYPE_, VARNAME_ variables. */
139 struct variable *single_split; /* Single SPLIT FILE variable. */
141 /* Factor variables. */
142 size_t n_factors; /* Number of factor variables. */
143 struct variable **factors; /* Factor variables. */
144 int is_per_factor[PROX + 1]; /* Is there per-factor data? */
146 int cells; /* Number of cells, or -1 if none. */
148 int pop_n; /* Population N specified by user. */
150 /* CONTENTS subcommand. */
151 int contents[EOC * 3 + 1]; /* Contents. */
152 int n_contents; /* Number of entries. */
154 /* Continuous variables. */
155 int n_continuous; /* Number of continuous variables. */
156 int first_continuous; /* Index into dictionary of
157 first continuous variable. */
160 /* Auxiliary data attached to MATRIX DATA variables. */
163 int var_type; /* Variable type. */
164 int sub_type; /* Subtype. */
167 static const struct case_source_class matrix_data_with_rowtype_source_class;
168 static const struct case_source_class matrix_data_without_rowtype_source_class;
170 static int compare_variables_by_mxd_var_type (const void *pa,
172 static bool read_matrices_without_rowtype (struct dataset *ds, struct matrix_data_pgm *);
173 static bool read_matrices_with_rowtype (struct dataset *ds, struct matrix_data_pgm *);
174 static int string_to_content_type (const char *, int *);
175 static void attach_mxd_aux (struct variable *, int var_type, int sub_type);
178 cmd_matrix_data (struct lexer *lexer, struct dataset *ds)
181 struct matrix_data_pgm *mx;
182 struct file_handle *fh = fh_inline_file ();
187 discard_variables (ds);
189 pool = pool_create ();
190 mx = pool_alloc (pool, sizeof *mx);
191 mx->container = pool;
196 mx->explicit_rowtype = 0;
199 mx->single_split = NULL;
202 memset (mx->is_per_factor, 0, sizeof mx->is_per_factor);
206 mx->n_continuous = 0;
207 mx->first_continuous = 0;
208 while (lex_token (lexer) != '.')
210 lex_match (lexer, '/');
212 if (lex_match_id (lexer, "VARIABLES"))
219 msg (SE, _("VARIABLES subcommand multiply specified."));
224 lex_match (lexer, '=');
225 if (!parse_DATA_LIST_vars (lexer, &v, &nv, PV_NO_DUPLICATE))
231 for (i = 0; i < nv; i++)
232 if (!strcasecmp (v[i], "VARNAME_"))
234 msg (SE, _("VARNAME_ cannot be explicitly specified on "
236 for (i = 0; i < nv; i++)
246 for (i = 0; i < nv; i++)
248 struct variable *new_var;
250 if (strcasecmp (v[i], "ROWTYPE_"))
252 new_var = dict_create_var_assert (dataset_dict (ds), v[i], 0);
253 attach_mxd_aux (new_var, MXD_CONTINUOUS, i);
256 mx->explicit_rowtype = 1;
262 mx->rowtype_ = dict_create_var_assert (dataset_dict (ds),
264 attach_mxd_aux (mx->rowtype_, MXD_ROWTYPE, 0);
266 else if (lex_match_id (lexer, "FILE"))
268 lex_match (lexer, '=');
269 fh = fh_parse (lexer, FH_REF_FILE | FH_REF_INLINE);
273 else if (lex_match_id (lexer, "FORMAT"))
275 lex_match (lexer, '=');
277 while (lex_token (lexer) == T_ID)
279 if (lex_match_id (lexer, "LIST"))
281 else if (lex_match_id (lexer, "FREE"))
283 else if (lex_match_id (lexer, "LOWER"))
285 else if (lex_match_id (lexer, "UPPER"))
287 else if (lex_match_id (lexer, "FULL"))
289 else if (lex_match_id (lexer, "DIAGONAL"))
291 else if (lex_match_id (lexer, "NODIAGONAL"))
292 mx->diag = NODIAGONAL;
295 lex_error (lexer, _("in FORMAT subcommand"));
300 else if (lex_match_id (lexer, "SPLIT"))
302 lex_match (lexer, '=');
306 msg (SE, _("SPLIT subcommand multiply specified."));
311 if (lex_token (lexer) != T_ID)
313 lex_error (lexer, _("in SPLIT subcommand"));
317 if (dict_lookup_var (dataset_dict (ds), lex_tokid (lexer)) == NULL
318 && (lex_look_ahead (lexer) == '.' || lex_look_ahead (lexer) == '/'))
320 if (!strcasecmp (lex_tokid (lexer), "ROWTYPE_")
321 || !strcasecmp (lex_tokid (lexer), "VARNAME_"))
323 msg (SE, _("Split variable may not be named ROWTYPE_ "
328 mx->single_split = dict_create_var_assert (dataset_dict (ds),
329 lex_tokid (lexer), 0);
330 attach_mxd_aux (mx->single_split, MXD_CONTINUOUS, 0);
333 dict_set_split_vars (dataset_dict (ds), &mx->single_split, 1);
337 struct variable **split;
340 if (!parse_variables (lexer, dataset_dict (ds),
341 &split, &n, PV_NO_DUPLICATE))
344 dict_set_split_vars (dataset_dict (ds), split, n);
348 struct variable *const *split = dict_get_split_vars (dataset_dict (ds));
349 size_t split_cnt = dict_get_split_cnt (dataset_dict (ds));
352 for (i = 0; i < split_cnt; i++)
354 struct mxd_var *mv = var_get_aux (split[i]);
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 = var_get_aux (v);
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 (lexer, "CELLS"))
401 lex_match (lexer, '=');
405 msg (SE, _("CELLS subcommand multiply specified."));
409 if (!lex_is_integer (lexer) || lex_integer (lexer) < 1)
411 lex_error (lexer, _("expecting positive integer"));
415 mx->cells = lex_integer (lexer);
418 else if (lex_match_id (lexer, "N"))
420 lex_match (lexer, '=');
424 msg (SE, _("N subcommand multiply specified."));
428 if (!lex_is_integer (lexer) || lex_integer (lexer) < 1)
430 lex_error (lexer, _("expecting positive integer"));
434 mx->pop_n = lex_integer (lexer);
437 else if (lex_match_id (lexer, "CONTENTS"))
439 int inside_parens = 0;
440 unsigned collide = 0;
445 msg (SE, _("CONTENTS subcommand multiply specified."));
450 lex_match (lexer, '=');
455 for (i = 0; i <= PROX; i++)
456 mx->is_per_factor[i] = 0;
461 if (lex_match (lexer, '('))
465 msg (SE, _("Nested parentheses not allowed."));
471 else if (lex_match (lexer, ')'))
475 msg (SE, _("Mismatched right parenthesis (`(')."));
478 if (mx->contents[mx->n_contents - 1] == LPAREN)
480 msg (SE, _("Empty parentheses not allowed."));
491 if (lex_token (lexer) != T_ID)
493 lex_error (lexer, _("in CONTENTS subcommand"));
497 content_type = string_to_content_type (lex_tokid (lexer),
499 if (content_type == -1)
501 lex_error (lexer, _("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 (lex_token (lexer) == '/' || lex_token (lexer) == '.')
525 msg (SE, _("Missing right parenthesis."));
528 mx->contents[mx->n_contents] = EOC;
532 lex_error (lexer, NULL);
537 if (lex_token (lexer) != '.')
539 lex_error (lexer, _("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 (ds), "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 (ds), &v, &nv, 0);
585 qsort (v, nv, sizeof *v, compare_variables_by_mxd_var_type);
586 dict_reorder_vars (dataset_dict (ds), 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 (ds)); i++)
606 struct variable *v = dict_get_var (dataset_dict (ds), i);
607 struct mxd_var *mv = var_get_aux (v);
608 int type = mv->var_type;
610 assert (type >= 0 && type < MXD_COUNT);
611 var_set_both_formats (v, &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, lexer);
627 if (mx->reader == NULL)
630 if (mx->explicit_rowtype)
631 ok = read_matrices_with_rowtype (ds, mx);
633 ok = read_matrices_without_rowtype (ds, mx);
635 dfm_close_reader (mx->reader);
637 pool_destroy (mx->container);
639 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
642 discard_variables (ds);
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 (const 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 = var_get_aux (*pa);
701 const struct mxd_var *b = var_get_aux (*pb);
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 (var_get_aux (v) == 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);
867 data_in (s, FMT_F, 0,
868 dfm_get_column (reader, ss_data (s)),
869 (union value *) &token->number, 0);
873 token->string = ss_data (s);
874 token->length = ss_length (s);
876 dfm_reread_record (reader, dfm_get_column (reader, ss_end (s)));
881 /* Forcibly skip the end of a line for content type CONTENT in
884 force_eol (struct dfm_reader *reader, const char *content)
888 if (dfm_eof (reader))
891 p = dfm_get_record (reader);
892 if (ss_span (p, ss_cstr (CC_SPACES)) != ss_length (p))
894 msg (SE, _("End of line expected %s while reading %s."),
895 context (reader), content);
899 dfm_forward_record (reader);
903 /* Back end, omitting ROWTYPE_. */
907 const struct dictionary *dict; /* The dictionary */
908 struct matrix_data_pgm *mx; /* MATRIX DATA program. */
909 double ***data; /* MATRIX DATA data. */
910 double *factor_values; /* Factor values. */
911 int max_cell_idx; /* Max-numbered cell that we have
912 read so far, plus one. */
913 double *split_values; /* SPLIT FILE variable values. */
916 static bool nr_read_splits (struct nr_aux_data *, int compare);
917 static bool nr_read_factors (struct nr_aux_data *, int cell);
918 static bool nr_output_data (struct nr_aux_data *, struct ccase *,
919 write_case_func *, write_case_data);
920 static bool matrix_data_read_without_rowtype (struct case_source *source,
925 /* Read from the data file and write it to the active file.
926 Returns true if successful, false if an I/O error occurred. */
928 read_matrices_without_rowtype (struct dataset *ds, struct matrix_data_pgm *mx)
930 struct nr_aux_data nr;
937 nr.dict = dataset_dict (ds);
939 nr.factor_values = xnmalloc (mx->n_factors * mx->cells,
940 sizeof *nr.factor_values);
942 nr.split_values = xnmalloc (dict_get_split_cnt (dataset_dict (ds)),
943 sizeof *nr.split_values);
945 proc_set_source (ds, create_case_source (
946 &matrix_data_without_rowtype_source_class, &nr));
948 ok = procedure (ds, NULL, NULL);
950 free (nr.split_values);
951 free (nr.factor_values);
956 /* Mirror data across the diagonal of matrix CP which contains
957 CONTENT type data. */
959 fill_matrix (struct matrix_data_pgm *mx, int content, double *cp)
961 int type = content_type[content];
963 if (type == 1 && mx->section != FULL)
965 if (mx->diag == NODIAGONAL)
967 const double fill = content == CORR ? 1.0 : SYSMIS;
970 for (i = 0; i < mx->n_continuous; i++)
971 cp[i * (1 + mx->n_continuous)] = fill;
977 if (mx->section == LOWER)
979 int n_lines = mx->n_continuous;
980 if (mx->section != FULL && mx->diag == NODIAGONAL)
983 for (r = 1; r < n_lines; r++)
984 for (c = 0; c < r; c++)
985 cp[r + c * mx->n_continuous] = cp[c + r * mx->n_continuous];
989 assert (mx->section == UPPER);
990 for (r = 1; r < mx->n_continuous; r++)
991 for (c = 0; c < r; c++)
992 cp[c + r * mx->n_continuous] = cp[r + c * mx->n_continuous];
1000 for (c = 1; c < mx->n_continuous; c++)
1005 /* Read data lines for content type CONTENT from the data file.
1006 If PER_FACTOR is nonzero, then factor information is read from
1007 the data file. Data is for cell number CELL. */
1009 nr_read_data_lines (struct nr_aux_data *nr,
1010 int per_factor, int cell, int content, int compare)
1012 struct matrix_data_pgm *mx = nr->mx;
1013 const int type = content_type[content]; /* Content type. */
1014 int n_lines; /* Number of lines to parse from data file for this type. */
1015 double *cp; /* Current position in vector or matrix. */
1022 n_lines = mx->n_continuous;
1023 if (mx->section != FULL && mx->diag == NODIAGONAL)
1027 cp = nr->data[content][cell];
1028 if (type == 1 && mx->section == LOWER && mx->diag == NODIAGONAL)
1029 cp += mx->n_continuous;
1031 for (i = 0; i < n_lines; i++)
1035 if (!nr_read_splits (nr, 1))
1037 if (per_factor && !nr_read_factors (nr, cell))
1044 n_cols = mx->n_continuous;
1047 switch (mx->section)
1054 n_cols = mx->n_continuous - i;
1055 if (mx->diag == NODIAGONAL)
1062 n_cols = mx->n_continuous;
1078 for (j = 0; j < n_cols; j++)
1080 struct matrix_token token;
1081 if (!mget_token (&token, mx->reader))
1083 if (token.type != MNUM)
1085 msg (SE, _("expecting value for %s %s"),
1086 var_get_name (dict_get_var (nr->dict, j)),
1087 context (mx->reader));
1091 *cp++ = token.number;
1094 && !force_eol (mx->reader, content_names[content]))
1098 if (mx->section == LOWER)
1099 cp += mx->n_continuous - n_cols;
1102 fill_matrix (mx, content, nr->data[content][cell]);
1107 /* When ROWTYPE_ does not appear in the data, reads the matrices and
1108 writes them to the output file.
1109 Returns true if successful, false if an I/O error occurred. */
1111 matrix_data_read_without_rowtype (struct case_source *source,
1113 write_case_func *write_case,
1114 write_case_data wc_data)
1116 struct nr_aux_data *nr = source->aux;
1117 struct matrix_data_pgm *mx = nr->mx;
1122 nr->data = pool_nalloc (mx->container, PROX + 1, sizeof *nr->data);
1127 for (i = 0; i <= PROX; i++)
1131 for (cp = mx->contents; *cp != EOC; cp++)
1132 if (*cp != LPAREN && *cp != RPAREN)
1134 int per_factor = mx->is_per_factor[*cp];
1137 n_entries = mx->n_continuous;
1138 if (content_type[*cp] == 1)
1139 n_entries *= mx->n_continuous;
1142 int n_vectors = per_factor ? mx->cells : 1;
1145 nr->data[*cp] = pool_nalloc (mx->container,
1146 n_vectors, sizeof **nr->data);
1148 for (i = 0; i < n_vectors; i++)
1149 nr->data[*cp][i] = pool_nalloc (mx->container,
1150 n_entries, sizeof ***nr->data);
1159 if (!nr_read_splits (nr, 0))
1162 for (bp = mx->contents; *bp != EOC; bp = np)
1166 /* Trap the CONTENTS that we should parse in this pass
1167 between bp and ep. Set np to the starting bp for next
1172 while (*ep != RPAREN)
1180 while (*ep != EOC && *ep != LPAREN)
1189 for (i = 0; i < (per_factor ? mx->cells : 1); i++)
1193 for (cp = bp; cp < ep; cp++)
1194 if (!nr_read_data_lines (nr, per_factor, i, *cp, cp != bp))
1200 if (!nr_output_data (nr, c, write_case, wc_data))
1203 if (dict_get_split_cnt (nr->dict) == 0
1204 || !another_token (mx->reader))
1209 /* Read the split file variables. If COMPARE is 1, compares the
1210 values read to the last values read and returns true if they're equal,
1213 nr_read_splits (struct nr_aux_data *nr, int compare)
1215 struct matrix_data_pgm *mx = nr->mx;
1216 static int just_read = 0; /* FIXME: WTF? */
1220 if (compare && just_read)
1226 if (dict_get_split_vars (nr->dict) == NULL)
1229 if (mx->single_split)
1233 struct mxd_var *mv = var_get_aux (dict_get_split_vars (nr->dict)[0]);
1234 nr->split_values[0] = ++mv->sub_type;
1242 split_cnt = dict_get_split_cnt (nr->dict);
1243 for (i = 0; i < split_cnt; i++)
1245 struct matrix_token token;
1246 if (!mget_token (&token, mx->reader))
1248 if (token.type != MNUM)
1250 msg (SE, _("Syntax error expecting SPLIT FILE value %s."),
1251 context (mx->reader));
1256 nr->split_values[i] = token.number;
1257 else if (nr->split_values[i] != token.number)
1259 msg (SE, _("Expecting value %g for %s."),
1260 nr->split_values[i],
1261 var_get_name (dict_get_split_vars (nr->dict)[i]));
1269 /* Read the factors for cell CELL. If COMPARE is 1, compares the
1270 values read to the last values read and returns true if they're equal,
1273 nr_read_factors (struct nr_aux_data *nr, int cell)
1275 struct matrix_data_pgm *mx = nr->mx;
1278 if (mx->n_factors == 0)
1281 assert (nr->max_cell_idx >= cell);
1282 if (cell != nr->max_cell_idx)
1293 for (i = 0; i < mx->n_factors; i++)
1295 struct matrix_token token;
1296 if (!mget_token (&token, mx->reader))
1298 if (token.type != MNUM)
1300 msg (SE, _("Syntax error expecting factor value %s."),
1301 context (mx->reader));
1306 nr->factor_values[i + mx->n_factors * cell] = token.number;
1307 else if (nr->factor_values[i + mx->n_factors * cell] != token.number)
1309 msg (SE, _("Syntax error expecting value %g for %s %s."),
1310 nr->factor_values[i + mx->n_factors * cell],
1311 var_get_name (mx->factors[i]), context (mx->reader));
1320 /* Write the contents of a cell having content type CONTENT and data
1321 CP to the active file.
1322 Returns true if successful, false if an I/O error occurred. */
1324 dump_cell_content (const struct dictionary *dict,
1325 struct matrix_data_pgm *mx, int content, double *cp,
1327 write_case_func *write_case, write_case_data wc_data)
1329 int type = content_type[content];
1332 buf_copy_str_rpad (case_data_rw (c, mx->rowtype_)->s, 8,
1333 content_names[content]);
1336 memset (case_data_rw (c, mx->varname_)->s, ' ', 8);
1340 int n_lines = (type == 1) ? mx->n_continuous : 1;
1343 for (i = 0; i < n_lines; i++)
1347 for (j = 0; j < mx->n_continuous; j++)
1349 struct variable *v = dict_get_var (dict, mx->first_continuous + j);
1350 case_data_rw (c, v)->f = *cp;
1354 buf_copy_str_rpad (case_data_rw (c, mx->varname_)->s, 8,
1356 dict_get_var (dict, mx->first_continuous + i)));
1357 if (!write_case (wc_data))
1364 /* Finally dump out everything from nr_data[] to the output file. */
1366 nr_output_data (struct nr_aux_data *nr, struct ccase *c,
1367 write_case_func *write_case, write_case_data wc_data)
1369 struct matrix_data_pgm *mx = nr->mx;
1372 struct variable *const *split;
1376 split_cnt = dict_get_split_cnt (nr->dict);
1377 split = dict_get_split_vars (nr->dict);
1378 for (i = 0; i < split_cnt; i++)
1379 case_data_rw (c, split[i])->f = nr->split_values[i];
1386 for (cell = 0; cell < mx->cells; cell++)
1391 for (factor = 0; factor < mx->n_factors; factor++)
1392 case_data_rw (c, mx->factors[factor])->f
1393 = nr->factor_values[factor + cell * mx->n_factors];
1399 for (content = 0; content <= PROX; content++)
1400 if (mx->is_per_factor[content])
1402 assert (nr->data[content] != NULL
1403 && nr->data[content][cell] != NULL);
1405 if (!dump_cell_content (nr->dict, mx,
1406 content, nr->data[content][cell],
1407 c, write_case, wc_data))
1420 for (factor = 0; factor < mx->n_factors; factor++)
1421 case_data_rw (c, mx->factors[factor])->f = SYSMIS;
1424 for (content = 0; content <= PROX; content++)
1425 if (!mx->is_per_factor[content] && nr->data[content] != NULL)
1427 if (!dump_cell_content (nr->dict, mx, content, nr->data[content][0],
1428 c, write_case, wc_data))
1436 /* Back end, with ROWTYPE_. */
1438 /* All the data for one set of factor values. */
1442 int n_rows[PROX + 1];
1443 double *data[PROX + 1];
1444 struct factor_data *next;
1447 /* With ROWTYPE_ auxiliary data. */
1450 const struct dictionary *dict; /* The dictionary */
1451 struct matrix_data_pgm *mx; /* MATRIX DATA program. */
1452 int content; /* Type of current row. */
1453 double *split_values; /* SPLIT FILE variable values. */
1454 struct factor_data *data; /* All the data. */
1455 struct factor_data *current; /* Current factor. */
1458 static bool wr_read_splits (struct wr_aux_data *, struct ccase *,
1459 write_case_func *, write_case_data);
1460 static bool wr_output_data (struct wr_aux_data *, struct ccase *,
1461 write_case_func *, write_case_data);
1462 static bool wr_read_rowtype (struct wr_aux_data *,
1463 const struct matrix_token *, struct dfm_reader *);
1464 static bool wr_read_factors (struct wr_aux_data *);
1465 static bool wr_read_indeps (struct wr_aux_data *);
1466 static bool matrix_data_read_with_rowtype (struct case_source *,
1471 /* When ROWTYPE_ appears in the data, reads the matrices and writes
1472 them to the output file.
1473 Returns true if successful, false if an I/O error occurred. */
1475 read_matrices_with_rowtype (struct dataset *ds, struct matrix_data_pgm *mx)
1477 struct wr_aux_data wr;
1482 wr.split_values = NULL;
1485 wr.dict = dataset_dict (ds);
1488 proc_set_source (ds,
1489 create_case_source (&matrix_data_with_rowtype_source_class,
1491 ok = procedure (ds, NULL, NULL);
1493 free (wr.split_values);
1497 /* Read from the data file and write it to the active file.
1498 Returns true if successful, false if an I/O error occurred. */
1500 matrix_data_read_with_rowtype (struct case_source *source,
1502 write_case_func *write_case,
1503 write_case_data wc_data)
1505 struct wr_aux_data *wr = source->aux;
1506 struct matrix_data_pgm *mx = wr->mx;
1510 if (!wr_read_splits (wr, c, write_case, wc_data))
1513 if (!wr_read_factors (wr))
1516 if (!wr_read_indeps (wr))
1519 while (another_token (mx->reader));
1521 return wr_output_data (wr, c, write_case, wc_data);
1524 /* Read the split file variables. If they differ from the previous
1525 set of split variables then output the data. Returns success. */
1527 wr_read_splits (struct wr_aux_data *wr,
1529 write_case_func *write_case, write_case_data wc_data)
1531 struct matrix_data_pgm *mx = wr->mx;
1535 split_cnt = dict_get_split_cnt (wr->dict);
1539 if (wr->split_values)
1544 wr->split_values = xnmalloc (split_cnt, sizeof *wr->split_values);
1548 bool different = false;
1551 for (i = 0; i < split_cnt; i++)
1553 struct matrix_token token;
1554 if (!mget_token (&token, mx->reader))
1556 if (token.type != MNUM)
1558 msg (SE, _("Syntax error %s expecting SPLIT FILE value."),
1559 context (mx->reader));
1563 if (compare && wr->split_values[i] != token.number && !different)
1565 if (!wr_output_data (wr, c, write_case, wc_data))
1570 wr->split_values[i] = token.number;
1577 /* Compares doubles A and B, treating SYSMIS as greatest. */
1579 compare_doubles (const void *a_, const void *b_, const void *aux UNUSED)
1581 const double *a = a_;
1582 const double *b = b_;
1586 else if (*a == SYSMIS)
1588 else if (*b == SYSMIS)
1596 /* Return strcmp()-type comparison of the MX->n_factors factors at _A and
1597 _B. Sort missing values toward the end. */
1599 compare_factors (const void *a_, const void *b_, const void *mx_)
1601 const struct matrix_data_pgm *mx = mx_;
1602 struct factor_data *const *pa = a_;
1603 struct factor_data *const *pb = b_;
1604 const double *a = (*pa)->factors;
1605 const double *b = (*pb)->factors;
1607 return lexicographical_compare_3way (a, mx->n_factors,
1610 compare_doubles, NULL);
1613 /* Write out the data for the current split file to the active
1615 Returns true if successful, false if an I/O error occurred. */
1617 wr_output_data (struct wr_aux_data *wr,
1619 write_case_func *write_case, write_case_data wc_data)
1621 struct matrix_data_pgm *mx = wr->mx;
1625 struct variable *const *split;
1629 split_cnt = dict_get_split_cnt (wr->dict);
1630 split = dict_get_split_vars (wr->dict);
1631 for (i = 0; i < split_cnt; i++)
1632 case_data_rw (c, split[i])->f = wr->split_values[i];
1635 /* Sort the wr->data list. */
1637 struct factor_data **factors;
1638 struct factor_data *iter;
1641 factors = xnmalloc (mx->cells, sizeof *factors);
1643 for (i = 0, iter = wr->data; iter; iter = iter->next, i++)
1646 sort (factors, mx->cells, sizeof *factors, compare_factors, mx);
1648 wr->data = factors[0];
1649 for (i = 0; i < mx->cells - 1; i++)
1650 factors[i]->next = factors[i + 1];
1651 factors[mx->cells - 1]->next = NULL;
1656 /* Write out records for every set of factor values. */
1658 struct factor_data *iter;
1660 for (iter = wr->data; iter; iter = iter->next)
1665 for (factor = 0; factor < mx->n_factors; factor++)
1666 case_data_rw (c, mx->factors[factor])->f = iter->factors[factor];
1672 for (content = 0; content <= PROX; content++)
1674 if (!iter->n_rows[content])
1678 int type = content_type[content];
1679 int n_lines = (type == 1
1681 - (mx->section != FULL && mx->diag == NODIAGONAL))
1684 if (n_lines != iter->n_rows[content])
1686 msg (SE, _("Expected %d lines of data for %s content; "
1687 "actually saw %d lines. No data will be "
1688 "output for this content."),
1689 n_lines, content_names[content],
1690 iter->n_rows[content]);
1695 fill_matrix (mx, content, iter->data[content]);
1697 ok = dump_cell_content (wr->dict, mx, content,
1698 iter->data[content],
1699 c, write_case, wc_data);
1707 pool_destroy (mx->container);
1708 mx->container = pool_create ();
1710 wr->data = wr->current = NULL;
1715 /* Sets ROWTYPE_ based on the given TOKEN read from READER.
1718 wr_read_rowtype (struct wr_aux_data *wr,
1719 const struct matrix_token *token,
1720 struct dfm_reader *reader)
1722 if (wr->content != -1)
1724 msg (SE, _("Multiply specified ROWTYPE_ %s."), context (reader));
1727 if (token->type != MSTR)
1729 msg (SE, _("Syntax error %s expecting ROWTYPE_ string."),
1738 memcpy (s, token->string, MIN (15, token->length));
1739 s[MIN (15, token->length)] = 0;
1741 for (cp = s; *cp; cp++)
1742 *cp = toupper ((unsigned char) *cp);
1744 wr->content = string_to_content_type (s, NULL);
1747 if (wr->content == -1)
1749 msg (SE, _("Syntax error %s."), context (reader));
1756 /* Read the factors for the current row. Select a set of factors and
1757 point wr_current to it. */
1759 wr_read_factors (struct wr_aux_data *wr)
1761 struct matrix_data_pgm *mx = wr->mx;
1762 double *factor_values = local_alloc (sizeof *factor_values * mx->n_factors);
1768 for (i = 0; i < mx->n_factors; i++)
1770 struct matrix_token token;
1771 if (!mget_token (&token, mx->reader))
1773 if (token.type == MSTR)
1775 if (!wr_read_rowtype (wr, &token, mx->reader))
1777 if (!mget_token (&token, mx->reader))
1780 if (token.type != MNUM)
1782 msg (SE, _("Syntax error expecting factor value %s."),
1783 context (mx->reader));
1787 factor_values[i] = token.number;
1790 if (wr->content == -1)
1792 struct matrix_token token;
1793 if (!mget_token (&token, mx->reader))
1795 if (!wr_read_rowtype (wr, &token, mx->reader))
1799 /* Try the most recent factor first as a simple caching
1805 for (i = 0; i < mx->n_factors; i++)
1806 if (factor_values[i] != wr->current->factors[i])
1811 /* Linear search through the list. */
1814 struct factor_data *iter;
1816 for (iter = wr->data; iter; iter = iter->next)
1820 for (i = 0; i < mx->n_factors; i++)
1821 if (factor_values[i] != iter->factors[i])
1831 /* Not found. Make a new item. */
1833 struct factor_data *new = pool_alloc (mx->container, sizeof *new);
1835 new->factors = pool_nalloc (mx->container,
1836 mx->n_factors, sizeof *new->factors);
1841 for (i = 0; i < mx->n_factors; i++)
1842 new->factors[i] = factor_values[i];
1848 for (i = 0; i <= PROX; i++)
1851 new->data[i] = NULL;
1855 new->next = wr->data;
1856 wr->data = wr->current = new;
1861 local_free (factor_values);
1865 local_free (factor_values);
1869 /* Read the independent variables into wr->current. */
1871 wr_read_indeps (struct wr_aux_data *wr)
1873 struct matrix_data_pgm *mx = wr->mx;
1874 struct factor_data *c = wr->current;
1875 const int type = content_type[wr->content];
1876 const int n_rows = c->n_rows[wr->content];
1880 /* Allocate room for data if necessary. */
1881 if (c->data[wr->content] == NULL)
1883 int n_items = mx->n_continuous;
1885 n_items *= mx->n_continuous;
1887 c->data[wr->content] = pool_nalloc (mx->container,
1888 n_items, sizeof **c->data);
1891 cp = &c->data[wr->content][n_rows * mx->n_continuous];
1893 /* Figure out how much to read from this line. */
1900 msg (SE, _("Duplicate specification for %s."),
1901 content_names[wr->content]);
1905 n_cols = mx->n_continuous;
1910 if (n_rows >= mx->n_continuous - (mx->section != FULL && mx->diag == NODIAGONAL))
1912 msg (SE, _("Too many rows of matrix data for %s."),
1913 content_names[wr->content]);
1917 switch (mx->section)
1920 n_cols = n_rows + 1;
1921 if (mx->diag == NODIAGONAL)
1922 cp += mx->n_continuous;
1926 n_cols = mx->n_continuous - n_rows;
1927 if (mx->diag == NODIAGONAL)
1934 n_cols = mx->n_continuous;
1943 c->n_rows[wr->content]++;
1945 /* Read N_COLS items at CP. */
1949 for (j = 0; j < n_cols; j++)
1951 struct matrix_token token;
1952 if (!mget_token (&token, mx->reader))
1954 if (token.type != MNUM)
1956 msg (SE, _("Syntax error expecting value for %s %s."),
1957 var_get_name (dict_get_var (wr->dict,
1958 mx->first_continuous + j)),
1959 context (mx->reader));
1963 *cp++ = token.number;
1966 && !force_eol (mx->reader, content_names[wr->content]))
1973 /* Matrix source. */
1975 static const struct case_source_class matrix_data_with_rowtype_source_class =
1979 matrix_data_read_with_rowtype,
1983 static const struct case_source_class
1984 matrix_data_without_rowtype_source_class =
1988 matrix_data_read_without_rowtype,