1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include <data/case-source.h>
26 #include <data/case.h>
27 #include <data/data-in.h>
28 #include <data/dictionary.h>
29 #include <data/procedure.h>
30 #include <data/variable.h>
31 #include <language/command.h>
32 #include <language/data-io/data-reader.h>
33 #include <language/data-io/file-handle.h>
34 #include <language/lexer/lexer.h>
35 #include <language/lexer/variable-parser.h>
36 #include <libpspp/alloc.h>
37 #include <libpspp/array.h>
38 #include <libpspp/assertion.h>
39 #include <libpspp/compiler.h>
40 #include <libpspp/message.h>
41 #include <libpspp/message.h>
42 #include <libpspp/misc.h>
43 #include <libpspp/pool.h>
44 #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 = var_get_aux (split[i]);
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 (lexer, "FACTORS"))
367 lex_match (lexer, '=');
371 msg (SE, _("FACTORS subcommand multiply specified."));
376 if (!parse_variables (lexer, dataset_dict (ds), &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 = var_get_aux (v);
387 if (mv->var_type != MXD_CONTINUOUS)
389 msg (SE, _("Factor variable %s is already another type."),
394 attach_mxd_aux (v, MXD_FACTOR, i);
398 else if (lex_match_id (lexer, "CELLS"))
400 lex_match (lexer, '=');
404 msg (SE, _("CELLS subcommand multiply specified."));
408 if (!lex_is_integer (lexer) || lex_integer (lexer) < 1)
410 lex_error (lexer, _("expecting positive integer"));
414 mx->cells = lex_integer (lexer);
417 else if (lex_match_id (lexer, "N"))
419 lex_match (lexer, '=');
423 msg (SE, _("N subcommand multiply specified."));
427 if (!lex_is_integer (lexer) || lex_integer (lexer) < 1)
429 lex_error (lexer, _("expecting positive integer"));
433 mx->pop_n = lex_integer (lexer);
436 else if (lex_match_id (lexer, "CONTENTS"))
438 int inside_parens = 0;
439 unsigned collide = 0;
444 msg (SE, _("CONTENTS subcommand multiply specified."));
449 lex_match (lexer, '=');
454 for (i = 0; i <= PROX; i++)
455 mx->is_per_factor[i] = 0;
460 if (lex_match (lexer, '('))
464 msg (SE, _("Nested parentheses not allowed."));
470 else if (lex_match (lexer, ')'))
474 msg (SE, _("Mismatched right parenthesis (`(')."));
477 if (mx->contents[mx->n_contents - 1] == LPAREN)
479 msg (SE, _("Empty parentheses not allowed."));
490 if (lex_token (lexer) != T_ID)
492 lex_error (lexer, _("in CONTENTS subcommand"));
496 content_type = string_to_content_type (lex_tokid (lexer),
498 if (content_type == -1)
500 lex_error (lexer, _("in CONTENTS subcommand"));
505 if (collide & (1 << collide_index))
507 msg (SE, _("Content multiply specified for %s."),
508 content_names[content_type]);
511 collide |= (1 << collide_index);
514 mx->is_per_factor[item] = inside_parens;
516 mx->contents[mx->n_contents++] = item;
518 if (lex_token (lexer) == '/' || lex_token (lexer) == '.')
524 msg (SE, _("Missing right parenthesis."));
527 mx->contents[mx->n_contents] = EOC;
531 lex_error (lexer, NULL);
536 if (lex_token (lexer) != '.')
538 lex_error (lexer, _("expecting end of command"));
544 msg (SE, _("Missing VARIABLES subcommand."));
548 if (!mx->n_contents && !mx->explicit_rowtype)
550 msg (SW, _("CONTENTS subcommand not specified: assuming file "
551 "contains only CORR matrix."));
553 mx->contents[0] = CORR;
554 mx->contents[1] = EOC;
558 if (mx->n_factors && !mx->explicit_rowtype && mx->cells == -1)
560 msg (SE, _("Missing CELLS subcommand. CELLS is required "
561 "when ROWTYPE_ is not given in the data and "
562 "factors are present."));
566 if (mx->explicit_rowtype && mx->single_split)
568 msg (SE, _("Split file values must be present in the data when "
569 "ROWTYPE_ is present."));
573 /* Create VARNAME_. */
574 mx->varname_ = dict_create_var_assert (dataset_dict (ds), "VARNAME_", 8);
575 attach_mxd_aux (mx->varname_, MXD_VARNAME, 0);
577 /* Sort the dictionary variables into the desired order for the
578 system file output. */
583 dict_get_vars (dataset_dict (ds), &v, &nv, 0);
584 qsort (v, nv, sizeof *v, compare_variables_by_mxd_var_type);
585 dict_reorder_vars (dataset_dict (ds), v, nv);
591 static const struct fmt_spec fmt_tab[MXD_COUNT] =
602 mx->first_continuous = -1;
603 for (i = 0; i < dict_get_var_cnt (dataset_dict (ds)); i++)
605 struct variable *v = dict_get_var (dataset_dict (ds), i);
606 struct mxd_var *mv = var_get_aux (v);
607 int type = mv->var_type;
609 assert (type >= 0 && type < MXD_COUNT);
610 var_set_both_formats (v, &fmt_tab[type]);
612 if (type == MXD_CONTINUOUS)
614 if (mx->first_continuous == -1 && type == MXD_CONTINUOUS)
615 mx->first_continuous = i;
619 if (mx->n_continuous == 0)
621 msg (SE, _("No continuous variables specified."));
625 mx->reader = dfm_open_reader (fh, lexer);
626 if (mx->reader == NULL)
629 if (mx->explicit_rowtype)
630 ok = read_matrices_with_rowtype (ds, mx);
632 ok = read_matrices_without_rowtype (ds, mx);
634 dfm_close_reader (mx->reader);
636 pool_destroy (mx->container);
638 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
641 discard_variables (ds);
643 pool_destroy (mx->container);
644 return CMD_CASCADING_FAILURE;
647 /* Look up string S as a content-type name and return the
648 corresponding enumerated value, or -1 if there is no match. If
649 COLLIDE is non-NULL then *COLLIDE returns a value (suitable for use
650 as a bit-index) which can be used for determining whether a related
651 statistic has already been used. */
653 string_to_content_type (const char *s, int *collide)
664 {N_VECTOR, 0, "N_VECTOR"},
666 {N_SCALAR, 0, "N_SCALAR"},
667 {N_MATRIX, 1, "N_MATRIX"},
669 {STDDEV, 3, "STDDEV"},
681 for (tp = tab; tp->value != -1; tp++)
682 if (!strcasecmp (s, tp->string))
685 *collide = tp->collide;
692 /* Compare two variables using p.mxd.var_type and p.mxd.sub_type
695 compare_variables_by_mxd_var_type (const void *a_, const void *b_)
697 struct variable *const *pa = a_;
698 struct variable *const *pb = b_;
699 const struct mxd_var *a = var_get_aux (*pa);
700 const struct mxd_var *b = var_get_aux (*pb);
702 if (a->var_type != b->var_type)
703 return a->var_type > b->var_type ? 1 : -1;
705 return a->sub_type < b->sub_type ? -1 : a->sub_type > b->sub_type;
708 /* Attaches a struct mxd_var with the specific member values to
711 attach_mxd_aux (struct variable *v, int var_type, int sub_type)
715 assert (var_get_aux (v) == NULL);
716 mv = xmalloc (sizeof *mv);
717 mv->var_type = var_type;
718 mv->sub_type = sub_type;
719 var_attach_aux (v, mv, var_dtor_free);
722 /* Matrix tokenizer. */
724 /* Matrix token types. */
725 enum matrix_token_type
731 /* A MATRIX DATA parsing token. */
734 enum matrix_token_type type;
735 double number; /* MNUM: token value. */
736 char *string; /* MSTR: token string; not null-terminated. */
737 int length; /* MSTR: tokstr length. */
740 static int mget_token (struct matrix_token *, struct dfm_reader *);
743 #define mget_token(TOKEN, READER) mget_token_dump(TOKEN, READER)
746 mdump_token (const struct matrix_token *token)
751 printf (" #%g", token->number);
754 printf (" '%.*s'", token->length, token->string);
763 mget_token_dump (struct matrix_token *token, struct dfm_reader *reader)
765 int result = (mget_token) (token, reader);
771 /* Return the current position in READER. */
773 context (struct dfm_reader *reader)
775 static struct string buf = DS_EMPTY_INITIALIZER;
778 if (dfm_eof (reader))
779 ds_assign_cstr (&buf, "at end of file");
784 p = dfm_get_record (reader);
785 ss_ltrim (&p, ss_cstr (CC_SPACES));
787 ds_assign_cstr (&buf, "at end of line");
789 ds_put_format (&buf, "before `%.*s'",
790 (int) ss_cspan (p, ss_cstr (CC_SPACES)), ss_data (p));
793 return ds_cstr (&buf);
796 /* Is there at least one token left in the data file? */
798 another_token (struct dfm_reader *reader)
805 if (dfm_eof (reader))
808 p = dfm_get_record (reader);
809 space_cnt = ss_span (p, ss_cstr (CC_SPACES));
810 if (space_cnt < ss_length (p))
812 dfm_forward_columns (reader, space_cnt);
816 dfm_forward_record (reader);
821 /* Parse a MATRIX DATA token from READER into TOKEN. */
823 (mget_token) (struct matrix_token *token, struct dfm_reader *reader)
825 struct substring line, p;
829 if (!another_token (reader))
832 line = p = dfm_get_record (reader);
834 /* Three types of fields: quoted with ', quoted with ", unquoted. */
836 if (c == '\'' || c == '"')
839 if (!ss_get_until (&p, c, &s))
840 msg (SW, _("Scope of string exceeds line."));
844 bool is_num = isdigit (c) || c == '.';
845 const char *start = ss_data (p);
850 if (strchr (CC_SPACES ",-+", c) != NULL)
855 if (strchr ("deDE", c) && strchr ("+-", ss_at (p, 1)))
863 s = ss_buffer (start, ss_data (p) - start);
866 data_in (s, FMT_F, 0,
867 dfm_get_column (reader, ss_data (s)),
868 (union value *) &token->number, 0);
872 token->string = ss_data (s);
873 token->length = ss_length (s);
875 dfm_reread_record (reader, dfm_get_column (reader, ss_end (s)));
880 /* Forcibly skip the end of a line for content type CONTENT in
883 force_eol (struct dfm_reader *reader, const char *content)
887 if (dfm_eof (reader))
890 p = dfm_get_record (reader);
891 if (ss_span (p, ss_cstr (CC_SPACES)) != ss_length (p))
893 msg (SE, _("End of line expected %s while reading %s."),
894 context (reader), content);
898 dfm_forward_record (reader);
902 /* Back end, omitting ROWTYPE_. */
906 const struct dictionary *dict; /* The dictionary */
907 struct matrix_data_pgm *mx; /* MATRIX DATA program. */
908 double ***data; /* MATRIX DATA data. */
909 double *factor_values; /* Factor values. */
910 int max_cell_idx; /* Max-numbered cell that we have
911 read so far, plus one. */
912 double *split_values; /* SPLIT FILE variable values. */
915 static bool nr_read_splits (struct nr_aux_data *, int compare);
916 static bool nr_read_factors (struct nr_aux_data *, int cell);
917 static bool nr_output_data (struct nr_aux_data *, struct ccase *,
918 write_case_func *, write_case_data);
919 static bool matrix_data_read_without_rowtype (struct case_source *source,
924 /* Read from the data file and write it to the active file.
925 Returns true if successful, false if an I/O error occurred. */
927 read_matrices_without_rowtype (struct dataset *ds, struct matrix_data_pgm *mx)
929 struct nr_aux_data nr;
936 nr.dict = dataset_dict (ds);
938 nr.factor_values = xnmalloc (mx->n_factors * mx->cells,
939 sizeof *nr.factor_values);
941 nr.split_values = xnmalloc (dict_get_split_cnt (dataset_dict (ds)),
942 sizeof *nr.split_values);
944 proc_set_source (ds, create_case_source (
945 &matrix_data_without_rowtype_source_class, &nr));
947 ok = procedure (ds, NULL, NULL);
949 free (nr.split_values);
950 free (nr.factor_values);
955 /* Mirror data across the diagonal of matrix CP which contains
956 CONTENT type data. */
958 fill_matrix (struct matrix_data_pgm *mx, int content, double *cp)
960 int type = content_type[content];
962 if (type == 1 && mx->section != FULL)
964 if (mx->diag == NODIAGONAL)
966 const double fill = content == CORR ? 1.0 : SYSMIS;
969 for (i = 0; i < mx->n_continuous; i++)
970 cp[i * (1 + mx->n_continuous)] = fill;
976 if (mx->section == LOWER)
978 int n_lines = mx->n_continuous;
979 if (mx->section != FULL && mx->diag == NODIAGONAL)
982 for (r = 1; r < n_lines; r++)
983 for (c = 0; c < r; c++)
984 cp[r + c * mx->n_continuous] = cp[c + r * mx->n_continuous];
988 assert (mx->section == UPPER);
989 for (r = 1; r < mx->n_continuous; r++)
990 for (c = 0; c < r; c++)
991 cp[c + r * mx->n_continuous] = cp[r + c * mx->n_continuous];
999 for (c = 1; c < mx->n_continuous; c++)
1004 /* Read data lines for content type CONTENT from the data file.
1005 If PER_FACTOR is nonzero, then factor information is read from
1006 the data file. Data is for cell number CELL. */
1008 nr_read_data_lines (struct nr_aux_data *nr,
1009 int per_factor, int cell, int content, int compare)
1011 struct matrix_data_pgm *mx = nr->mx;
1012 const int type = content_type[content]; /* Content type. */
1013 int n_lines; /* Number of lines to parse from data file for this type. */
1014 double *cp; /* Current position in vector or matrix. */
1021 n_lines = mx->n_continuous;
1022 if (mx->section != FULL && mx->diag == NODIAGONAL)
1026 cp = nr->data[content][cell];
1027 if (type == 1 && mx->section == LOWER && mx->diag == NODIAGONAL)
1028 cp += mx->n_continuous;
1030 for (i = 0; i < n_lines; i++)
1034 if (!nr_read_splits (nr, 1))
1036 if (per_factor && !nr_read_factors (nr, cell))
1043 n_cols = mx->n_continuous;
1046 switch (mx->section)
1053 n_cols = mx->n_continuous - i;
1054 if (mx->diag == NODIAGONAL)
1061 n_cols = mx->n_continuous;
1077 for (j = 0; j < n_cols; j++)
1079 struct matrix_token token;
1080 if (!mget_token (&token, mx->reader))
1082 if (token.type != MNUM)
1084 msg (SE, _("expecting value for %s %s"),
1085 var_get_name (dict_get_var (nr->dict, j)),
1086 context (mx->reader));
1090 *cp++ = token.number;
1093 && !force_eol (mx->reader, content_names[content]))
1097 if (mx->section == LOWER)
1098 cp += mx->n_continuous - n_cols;
1101 fill_matrix (mx, content, nr->data[content][cell]);
1106 /* When ROWTYPE_ does not appear in the data, reads the matrices and
1107 writes them to the output file.
1108 Returns true if successful, false if an I/O error occurred. */
1110 matrix_data_read_without_rowtype (struct case_source *source,
1112 write_case_func *write_case,
1113 write_case_data wc_data)
1115 struct nr_aux_data *nr = source->aux;
1116 struct matrix_data_pgm *mx = nr->mx;
1121 nr->data = pool_nalloc (mx->container, PROX + 1, sizeof *nr->data);
1126 for (i = 0; i <= PROX; i++)
1130 for (cp = mx->contents; *cp != EOC; cp++)
1131 if (*cp != LPAREN && *cp != RPAREN)
1133 int per_factor = mx->is_per_factor[*cp];
1136 n_entries = mx->n_continuous;
1137 if (content_type[*cp] == 1)
1138 n_entries *= mx->n_continuous;
1141 int n_vectors = per_factor ? mx->cells : 1;
1144 nr->data[*cp] = pool_nalloc (mx->container,
1145 n_vectors, sizeof **nr->data);
1147 for (i = 0; i < n_vectors; i++)
1148 nr->data[*cp][i] = pool_nalloc (mx->container,
1149 n_entries, sizeof ***nr->data);
1158 if (!nr_read_splits (nr, 0))
1161 for (bp = mx->contents; *bp != EOC; bp = np)
1165 /* Trap the CONTENTS that we should parse in this pass
1166 between bp and ep. Set np to the starting bp for next
1171 while (*ep != RPAREN)
1179 while (*ep != EOC && *ep != LPAREN)
1188 for (i = 0; i < (per_factor ? mx->cells : 1); i++)
1192 for (cp = bp; cp < ep; cp++)
1193 if (!nr_read_data_lines (nr, per_factor, i, *cp, cp != bp))
1199 if (!nr_output_data (nr, c, write_case, wc_data))
1202 if (dict_get_split_cnt (nr->dict) == 0
1203 || !another_token (mx->reader))
1208 /* Read the split file variables. If COMPARE is 1, compares the
1209 values read to the last values read and returns true if they're equal,
1212 nr_read_splits (struct nr_aux_data *nr, int compare)
1214 struct matrix_data_pgm *mx = nr->mx;
1215 static int just_read = 0; /* FIXME: WTF? */
1219 if (compare && just_read)
1225 if (dict_get_split_vars (nr->dict) == NULL)
1228 if (mx->single_split)
1232 struct mxd_var *mv = var_get_aux (dict_get_split_vars (nr->dict)[0]);
1233 nr->split_values[0] = ++mv->sub_type;
1241 split_cnt = dict_get_split_cnt (nr->dict);
1242 for (i = 0; i < split_cnt; i++)
1244 struct matrix_token token;
1245 if (!mget_token (&token, mx->reader))
1247 if (token.type != MNUM)
1249 msg (SE, _("Syntax error expecting SPLIT FILE value %s."),
1250 context (mx->reader));
1255 nr->split_values[i] = token.number;
1256 else if (nr->split_values[i] != token.number)
1258 msg (SE, _("Expecting value %g for %s."),
1259 nr->split_values[i],
1260 var_get_name (dict_get_split_vars (nr->dict)[i]));
1268 /* Read the factors for cell CELL. If COMPARE is 1, compares the
1269 values read to the last values read and returns true if they're equal,
1272 nr_read_factors (struct nr_aux_data *nr, int cell)
1274 struct matrix_data_pgm *mx = nr->mx;
1277 if (mx->n_factors == 0)
1280 assert (nr->max_cell_idx >= cell);
1281 if (cell != nr->max_cell_idx)
1292 for (i = 0; i < mx->n_factors; i++)
1294 struct matrix_token token;
1295 if (!mget_token (&token, mx->reader))
1297 if (token.type != MNUM)
1299 msg (SE, _("Syntax error expecting factor value %s."),
1300 context (mx->reader));
1305 nr->factor_values[i + mx->n_factors * cell] = token.number;
1306 else if (nr->factor_values[i + mx->n_factors * cell] != token.number)
1308 msg (SE, _("Syntax error expecting value %g for %s %s."),
1309 nr->factor_values[i + mx->n_factors * cell],
1310 var_get_name (mx->factors[i]), context (mx->reader));
1319 /* Write the contents of a cell having content type CONTENT and data
1320 CP to the active file.
1321 Returns true if successful, false if an I/O error occurred. */
1323 dump_cell_content (const struct dictionary *dict,
1324 struct matrix_data_pgm *mx, int content, double *cp,
1326 write_case_func *write_case, write_case_data wc_data)
1328 int type = content_type[content];
1331 buf_copy_str_rpad (case_data_rw (c, mx->rowtype_)->s, 8,
1332 content_names[content]);
1335 memset (case_data_rw (c, mx->varname_)->s, ' ', 8);
1339 int n_lines = (type == 1) ? mx->n_continuous : 1;
1342 for (i = 0; i < n_lines; i++)
1346 for (j = 0; j < mx->n_continuous; j++)
1348 struct variable *v = dict_get_var (dict, mx->first_continuous + j);
1349 case_data_rw (c, v)->f = *cp;
1353 buf_copy_str_rpad (case_data_rw (c, mx->varname_)->s, 8,
1355 dict_get_var (dict, mx->first_continuous + i)));
1356 if (!write_case (wc_data))
1363 /* Finally dump out everything from nr_data[] to the output file. */
1365 nr_output_data (struct nr_aux_data *nr, struct ccase *c,
1366 write_case_func *write_case, write_case_data wc_data)
1368 struct matrix_data_pgm *mx = nr->mx;
1371 struct variable *const *split;
1375 split_cnt = dict_get_split_cnt (nr->dict);
1376 split = dict_get_split_vars (nr->dict);
1377 for (i = 0; i < split_cnt; i++)
1378 case_data_rw (c, split[i])->f = nr->split_values[i];
1385 for (cell = 0; cell < mx->cells; cell++)
1390 for (factor = 0; factor < mx->n_factors; factor++)
1391 case_data_rw (c, mx->factors[factor])->f
1392 = nr->factor_values[factor + cell * mx->n_factors];
1398 for (content = 0; content <= PROX; content++)
1399 if (mx->is_per_factor[content])
1401 assert (nr->data[content] != NULL
1402 && nr->data[content][cell] != NULL);
1404 if (!dump_cell_content (nr->dict, mx,
1405 content, nr->data[content][cell],
1406 c, write_case, wc_data))
1419 for (factor = 0; factor < mx->n_factors; factor++)
1420 case_data_rw (c, mx->factors[factor])->f = SYSMIS;
1423 for (content = 0; content <= PROX; content++)
1424 if (!mx->is_per_factor[content] && nr->data[content] != NULL)
1426 if (!dump_cell_content (nr->dict, mx, content, nr->data[content][0],
1427 c, write_case, wc_data))
1435 /* Back end, with ROWTYPE_. */
1437 /* All the data for one set of factor values. */
1441 int n_rows[PROX + 1];
1442 double *data[PROX + 1];
1443 struct factor_data *next;
1446 /* With ROWTYPE_ auxiliary data. */
1449 const struct dictionary *dict; /* The dictionary */
1450 struct matrix_data_pgm *mx; /* MATRIX DATA program. */
1451 int content; /* Type of current row. */
1452 double *split_values; /* SPLIT FILE variable values. */
1453 struct factor_data *data; /* All the data. */
1454 struct factor_data *current; /* Current factor. */
1457 static bool wr_read_splits (struct wr_aux_data *, struct ccase *,
1458 write_case_func *, write_case_data);
1459 static bool wr_output_data (struct wr_aux_data *, struct ccase *,
1460 write_case_func *, write_case_data);
1461 static bool wr_read_rowtype (struct wr_aux_data *,
1462 const struct matrix_token *, struct dfm_reader *);
1463 static bool wr_read_factors (struct wr_aux_data *);
1464 static bool wr_read_indeps (struct wr_aux_data *);
1465 static bool matrix_data_read_with_rowtype (struct case_source *,
1470 /* When ROWTYPE_ appears in the data, reads the matrices and writes
1471 them to the output file.
1472 Returns true if successful, false if an I/O error occurred. */
1474 read_matrices_with_rowtype (struct dataset *ds, struct matrix_data_pgm *mx)
1476 struct wr_aux_data wr;
1481 wr.split_values = NULL;
1484 wr.dict = dataset_dict (ds);
1487 proc_set_source (ds,
1488 create_case_source (&matrix_data_with_rowtype_source_class,
1490 ok = procedure (ds, NULL, NULL);
1492 free (wr.split_values);
1496 /* Read from the data file and write it to the active file.
1497 Returns true if successful, false if an I/O error occurred. */
1499 matrix_data_read_with_rowtype (struct case_source *source,
1501 write_case_func *write_case,
1502 write_case_data wc_data)
1504 struct wr_aux_data *wr = source->aux;
1505 struct matrix_data_pgm *mx = wr->mx;
1509 if (!wr_read_splits (wr, c, write_case, wc_data))
1512 if (!wr_read_factors (wr))
1515 if (!wr_read_indeps (wr))
1518 while (another_token (mx->reader));
1520 return wr_output_data (wr, c, write_case, wc_data);
1523 /* Read the split file variables. If they differ from the previous
1524 set of split variables then output the data. Returns success. */
1526 wr_read_splits (struct wr_aux_data *wr,
1528 write_case_func *write_case, write_case_data wc_data)
1530 struct matrix_data_pgm *mx = wr->mx;
1534 split_cnt = dict_get_split_cnt (wr->dict);
1538 if (wr->split_values)
1543 wr->split_values = xnmalloc (split_cnt, sizeof *wr->split_values);
1547 bool different = false;
1550 for (i = 0; i < split_cnt; i++)
1552 struct matrix_token token;
1553 if (!mget_token (&token, mx->reader))
1555 if (token.type != MNUM)
1557 msg (SE, _("Syntax error %s expecting SPLIT FILE value."),
1558 context (mx->reader));
1562 if (compare && wr->split_values[i] != token.number && !different)
1564 if (!wr_output_data (wr, c, write_case, wc_data))
1569 wr->split_values[i] = token.number;
1576 /* Compares doubles A and B, treating SYSMIS as greatest. */
1578 compare_doubles (const void *a_, const void *b_, const void *aux UNUSED)
1580 const double *a = a_;
1581 const double *b = b_;
1585 else if (*a == SYSMIS)
1587 else if (*b == SYSMIS)
1595 /* Return strcmp()-type comparison of the MX->n_factors factors at _A and
1596 _B. Sort missing values toward the end. */
1598 compare_factors (const void *a_, const void *b_, const void *mx_)
1600 const struct matrix_data_pgm *mx = mx_;
1601 struct factor_data *const *pa = a_;
1602 struct factor_data *const *pb = b_;
1603 const double *a = (*pa)->factors;
1604 const double *b = (*pb)->factors;
1606 return lexicographical_compare_3way (a, mx->n_factors,
1609 compare_doubles, NULL);
1612 /* Write out the data for the current split file to the active
1614 Returns true if successful, false if an I/O error occurred. */
1616 wr_output_data (struct wr_aux_data *wr,
1618 write_case_func *write_case, write_case_data wc_data)
1620 struct matrix_data_pgm *mx = wr->mx;
1624 struct variable *const *split;
1628 split_cnt = dict_get_split_cnt (wr->dict);
1629 split = dict_get_split_vars (wr->dict);
1630 for (i = 0; i < split_cnt; i++)
1631 case_data_rw (c, split[i])->f = wr->split_values[i];
1634 /* Sort the wr->data list. */
1636 struct factor_data **factors;
1637 struct factor_data *iter;
1640 factors = xnmalloc (mx->cells, sizeof *factors);
1642 for (i = 0, iter = wr->data; iter; iter = iter->next, i++)
1645 sort (factors, mx->cells, sizeof *factors, compare_factors, mx);
1647 wr->data = factors[0];
1648 for (i = 0; i < mx->cells - 1; i++)
1649 factors[i]->next = factors[i + 1];
1650 factors[mx->cells - 1]->next = NULL;
1655 /* Write out records for every set of factor values. */
1657 struct factor_data *iter;
1659 for (iter = wr->data; iter; iter = iter->next)
1664 for (factor = 0; factor < mx->n_factors; factor++)
1665 case_data_rw (c, mx->factors[factor])->f = iter->factors[factor];
1671 for (content = 0; content <= PROX; content++)
1673 if (!iter->n_rows[content])
1677 int type = content_type[content];
1678 int n_lines = (type == 1
1680 - (mx->section != FULL && mx->diag == NODIAGONAL))
1683 if (n_lines != iter->n_rows[content])
1685 msg (SE, _("Expected %d lines of data for %s content; "
1686 "actually saw %d lines. No data will be "
1687 "output for this content."),
1688 n_lines, content_names[content],
1689 iter->n_rows[content]);
1694 fill_matrix (mx, content, iter->data[content]);
1696 ok = dump_cell_content (wr->dict, mx, content,
1697 iter->data[content],
1698 c, write_case, wc_data);
1706 pool_destroy (mx->container);
1707 mx->container = pool_create ();
1709 wr->data = wr->current = NULL;
1714 /* Sets ROWTYPE_ based on the given TOKEN read from READER.
1717 wr_read_rowtype (struct wr_aux_data *wr,
1718 const struct matrix_token *token,
1719 struct dfm_reader *reader)
1721 if (wr->content != -1)
1723 msg (SE, _("Multiply specified ROWTYPE_ %s."), context (reader));
1726 if (token->type != MSTR)
1728 msg (SE, _("Syntax error %s expecting ROWTYPE_ string."),
1737 memcpy (s, token->string, MIN (15, token->length));
1738 s[MIN (15, token->length)] = 0;
1740 for (cp = s; *cp; cp++)
1741 *cp = toupper ((unsigned char) *cp);
1743 wr->content = string_to_content_type (s, NULL);
1746 if (wr->content == -1)
1748 msg (SE, _("Syntax error %s."), context (reader));
1755 /* Read the factors for the current row. Select a set of factors and
1756 point wr_current to it. */
1758 wr_read_factors (struct wr_aux_data *wr)
1760 struct matrix_data_pgm *mx = wr->mx;
1761 double *factor_values = local_alloc (sizeof *factor_values * mx->n_factors);
1767 for (i = 0; i < mx->n_factors; i++)
1769 struct matrix_token token;
1770 if (!mget_token (&token, mx->reader))
1772 if (token.type == MSTR)
1774 if (!wr_read_rowtype (wr, &token, mx->reader))
1776 if (!mget_token (&token, mx->reader))
1779 if (token.type != MNUM)
1781 msg (SE, _("Syntax error expecting factor value %s."),
1782 context (mx->reader));
1786 factor_values[i] = token.number;
1789 if (wr->content == -1)
1791 struct matrix_token token;
1792 if (!mget_token (&token, mx->reader))
1794 if (!wr_read_rowtype (wr, &token, mx->reader))
1798 /* Try the most recent factor first as a simple caching
1804 for (i = 0; i < mx->n_factors; i++)
1805 if (factor_values[i] != wr->current->factors[i])
1810 /* Linear search through the list. */
1813 struct factor_data *iter;
1815 for (iter = wr->data; iter; iter = iter->next)
1819 for (i = 0; i < mx->n_factors; i++)
1820 if (factor_values[i] != iter->factors[i])
1830 /* Not found. Make a new item. */
1832 struct factor_data *new = pool_alloc (mx->container, sizeof *new);
1834 new->factors = pool_nalloc (mx->container,
1835 mx->n_factors, sizeof *new->factors);
1840 for (i = 0; i < mx->n_factors; i++)
1841 new->factors[i] = factor_values[i];
1847 for (i = 0; i <= PROX; i++)
1850 new->data[i] = NULL;
1854 new->next = wr->data;
1855 wr->data = wr->current = new;
1860 local_free (factor_values);
1864 local_free (factor_values);
1868 /* Read the independent variables into wr->current. */
1870 wr_read_indeps (struct wr_aux_data *wr)
1872 struct matrix_data_pgm *mx = wr->mx;
1873 struct factor_data *c = wr->current;
1874 const int type = content_type[wr->content];
1875 const int n_rows = c->n_rows[wr->content];
1879 /* Allocate room for data if necessary. */
1880 if (c->data[wr->content] == NULL)
1882 int n_items = mx->n_continuous;
1884 n_items *= mx->n_continuous;
1886 c->data[wr->content] = pool_nalloc (mx->container,
1887 n_items, sizeof **c->data);
1890 cp = &c->data[wr->content][n_rows * mx->n_continuous];
1892 /* Figure out how much to read from this line. */
1899 msg (SE, _("Duplicate specification for %s."),
1900 content_names[wr->content]);
1904 n_cols = mx->n_continuous;
1909 if (n_rows >= mx->n_continuous - (mx->section != FULL && mx->diag == NODIAGONAL))
1911 msg (SE, _("Too many rows of matrix data for %s."),
1912 content_names[wr->content]);
1916 switch (mx->section)
1919 n_cols = n_rows + 1;
1920 if (mx->diag == NODIAGONAL)
1921 cp += mx->n_continuous;
1925 n_cols = mx->n_continuous - n_rows;
1926 if (mx->diag == NODIAGONAL)
1933 n_cols = mx->n_continuous;
1942 c->n_rows[wr->content]++;
1944 /* Read N_COLS items at CP. */
1948 for (j = 0; j < n_cols; j++)
1950 struct matrix_token token;
1951 if (!mget_token (&token, mx->reader))
1953 if (token.type != MNUM)
1955 msg (SE, _("Syntax error expecting value for %s %s."),
1956 var_get_name (dict_get_var (wr->dict,
1957 mx->first_continuous + j)),
1958 context (mx->reader));
1962 *cp++ = token.number;
1965 && !force_eol (mx->reader, content_names[wr->content]))
1972 /* Matrix source. */
1974 static const struct case_source_class matrix_data_with_rowtype_source_class =
1978 matrix_data_read_with_rowtype,
1982 static const struct case_source_class
1983 matrix_data_without_rowtype_source_class =
1987 matrix_data_read_without_rowtype,