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 = split[i]->aux;
356 if (mv->var_type != MXD_CONTINUOUS)
358 msg (SE, _("Split variable %s is already another type."),
362 var_clear_aux (split[i]);
363 attach_mxd_aux (split[i], MXD_SPLIT, i);
367 else if (lex_match_id (lexer, "FACTORS"))
369 lex_match (lexer, '=');
373 msg (SE, _("FACTORS subcommand multiply specified."));
378 if (!parse_variables (lexer, dataset_dict (ds), &mx->factors, &mx->n_factors,
385 for (i = 0; i < mx->n_factors; i++)
387 struct variable *v = mx->factors[i];
388 struct mxd_var *mv = v->aux;
390 if (mv->var_type != MXD_CONTINUOUS)
392 msg (SE, _("Factor variable %s is already another type."),
397 attach_mxd_aux (v, MXD_FACTOR, i);
401 else if (lex_match_id (lexer, "CELLS"))
403 lex_match (lexer, '=');
407 msg (SE, _("CELLS subcommand multiply specified."));
411 if (!lex_is_integer (lexer) || lex_integer (lexer) < 1)
413 lex_error (lexer, _("expecting positive integer"));
417 mx->cells = lex_integer (lexer);
420 else if (lex_match_id (lexer, "N"))
422 lex_match (lexer, '=');
426 msg (SE, _("N subcommand multiply specified."));
430 if (!lex_is_integer (lexer) || lex_integer (lexer) < 1)
432 lex_error (lexer, _("expecting positive integer"));
436 mx->pop_n = lex_integer (lexer);
439 else if (lex_match_id (lexer, "CONTENTS"))
441 int inside_parens = 0;
442 unsigned collide = 0;
447 msg (SE, _("CONTENTS subcommand multiply specified."));
452 lex_match (lexer, '=');
457 for (i = 0; i <= PROX; i++)
458 mx->is_per_factor[i] = 0;
463 if (lex_match (lexer, '('))
467 msg (SE, _("Nested parentheses not allowed."));
473 else if (lex_match (lexer, ')'))
477 msg (SE, _("Mismatched right parenthesis (`(')."));
480 if (mx->contents[mx->n_contents - 1] == LPAREN)
482 msg (SE, _("Empty parentheses not allowed."));
493 if (lex_token (lexer) != T_ID)
495 lex_error (lexer, _("in CONTENTS subcommand"));
499 content_type = string_to_content_type (lex_tokid (lexer),
501 if (content_type == -1)
503 lex_error (lexer, _("in CONTENTS subcommand"));
508 if (collide & (1 << collide_index))
510 msg (SE, _("Content multiply specified for %s."),
511 content_names[content_type]);
514 collide |= (1 << collide_index);
517 mx->is_per_factor[item] = inside_parens;
519 mx->contents[mx->n_contents++] = item;
521 if (lex_token (lexer) == '/' || lex_token (lexer) == '.')
527 msg (SE, _("Missing right parenthesis."));
530 mx->contents[mx->n_contents] = EOC;
534 lex_error (lexer, NULL);
539 if (lex_token (lexer) != '.')
541 lex_error (lexer, _("expecting end of command"));
547 msg (SE, _("Missing VARIABLES subcommand."));
551 if (!mx->n_contents && !mx->explicit_rowtype)
553 msg (SW, _("CONTENTS subcommand not specified: assuming file "
554 "contains only CORR matrix."));
556 mx->contents[0] = CORR;
557 mx->contents[1] = EOC;
561 if (mx->n_factors && !mx->explicit_rowtype && mx->cells == -1)
563 msg (SE, _("Missing CELLS subcommand. CELLS is required "
564 "when ROWTYPE_ is not given in the data and "
565 "factors are present."));
569 if (mx->explicit_rowtype && mx->single_split)
571 msg (SE, _("Split file values must be present in the data when "
572 "ROWTYPE_ is present."));
576 /* Create VARNAME_. */
577 mx->varname_ = dict_create_var_assert (dataset_dict (ds), "VARNAME_", 8);
578 attach_mxd_aux (mx->varname_, MXD_VARNAME, 0);
580 /* Sort the dictionary variables into the desired order for the
581 system file output. */
586 dict_get_vars (dataset_dict (ds), &v, &nv, 0);
587 qsort (v, nv, sizeof *v, compare_variables_by_mxd_var_type);
588 dict_reorder_vars (dataset_dict (ds), v, nv);
594 static const struct fmt_spec fmt_tab[MXD_COUNT] =
605 mx->first_continuous = -1;
606 for (i = 0; i < dict_get_var_cnt (dataset_dict (ds)); i++)
608 struct variable *v = dict_get_var (dataset_dict (ds), i);
609 struct mxd_var *mv = v->aux;
610 int type = mv->var_type;
612 assert (type >= 0 && type < MXD_COUNT);
613 v->print = v->write = fmt_tab[type];
615 if (type == MXD_CONTINUOUS)
617 if (mx->first_continuous == -1 && type == MXD_CONTINUOUS)
618 mx->first_continuous = i;
622 if (mx->n_continuous == 0)
624 msg (SE, _("No continuous variables specified."));
628 mx->reader = dfm_open_reader (fh, lexer);
629 if (mx->reader == NULL)
632 if (mx->explicit_rowtype)
633 ok = read_matrices_with_rowtype (ds, mx);
635 ok = read_matrices_without_rowtype (ds, mx);
637 dfm_close_reader (mx->reader);
639 pool_destroy (mx->container);
641 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
644 discard_variables (ds);
646 pool_destroy (mx->container);
647 return CMD_CASCADING_FAILURE;
650 /* Look up string S as a content-type name and return the
651 corresponding enumerated value, or -1 if there is no match. If
652 COLLIDE is non-NULL then *COLLIDE returns a value (suitable for use
653 as a bit-index) which can be used for determining whether a related
654 statistic has already been used. */
656 string_to_content_type (const char *s, int *collide)
667 {N_VECTOR, 0, "N_VECTOR"},
669 {N_SCALAR, 0, "N_SCALAR"},
670 {N_MATRIX, 1, "N_MATRIX"},
672 {STDDEV, 3, "STDDEV"},
684 for (tp = tab; tp->value != -1; tp++)
685 if (!strcasecmp (s, tp->string))
688 *collide = tp->collide;
695 /* Compare two variables using p.mxd.var_type and p.mxd.sub_type
698 compare_variables_by_mxd_var_type (const void *a_, const void *b_)
700 struct variable *const *pa = a_;
701 struct variable *const *pb = b_;
702 const struct mxd_var *a = (*pa)->aux;
703 const struct mxd_var *b = (*pb)->aux;
705 if (a->var_type != b->var_type)
706 return a->var_type > b->var_type ? 1 : -1;
708 return a->sub_type < b->sub_type ? -1 : a->sub_type > b->sub_type;
711 /* Attaches a struct mxd_var with the specific member values to
714 attach_mxd_aux (struct variable *v, int var_type, int sub_type)
718 assert (v->aux == NULL);
719 mv = xmalloc (sizeof *mv);
720 mv->var_type = var_type;
721 mv->sub_type = sub_type;
722 var_attach_aux (v, mv, var_dtor_free);
725 /* Matrix tokenizer. */
727 /* Matrix token types. */
728 enum matrix_token_type
734 /* A MATRIX DATA parsing token. */
737 enum matrix_token_type type;
738 double number; /* MNUM: token value. */
739 char *string; /* MSTR: token string; not null-terminated. */
740 int length; /* MSTR: tokstr length. */
743 static int mget_token (struct matrix_token *, struct dfm_reader *);
746 #define mget_token(TOKEN, READER) mget_token_dump(TOKEN, READER)
749 mdump_token (const struct matrix_token *token)
754 printf (" #%g", token->number);
757 printf (" '%.*s'", token->length, token->string);
766 mget_token_dump (struct matrix_token *token, struct dfm_reader *reader)
768 int result = (mget_token) (token, reader);
774 /* Return the current position in READER. */
776 context (struct dfm_reader *reader)
778 static struct string buf = DS_EMPTY_INITIALIZER;
781 if (dfm_eof (reader))
782 ds_assign_cstr (&buf, "at end of file");
787 p = dfm_get_record (reader);
788 ss_ltrim (&p, ss_cstr (CC_SPACES));
790 ds_assign_cstr (&buf, "at end of line");
792 ds_put_format (&buf, "before `%.*s'",
793 (int) ss_cspan (p, ss_cstr (CC_SPACES)), ss_data (p));
796 return ds_cstr (&buf);
799 /* Is there at least one token left in the data file? */
801 another_token (struct dfm_reader *reader)
808 if (dfm_eof (reader))
811 p = dfm_get_record (reader);
812 space_cnt = ss_span (p, ss_cstr (CC_SPACES));
813 if (space_cnt < ss_length (p))
815 dfm_forward_columns (reader, space_cnt);
819 dfm_forward_record (reader);
824 /* Parse a MATRIX DATA token from READER into TOKEN. */
826 (mget_token) (struct matrix_token *token, struct dfm_reader *reader)
828 struct substring line, p;
832 if (!another_token (reader))
835 line = p = dfm_get_record (reader);
837 /* Three types of fields: quoted with ', quoted with ", unquoted. */
839 if (c == '\'' || c == '"')
842 if (!ss_get_until (&p, c, &s))
843 msg (SW, _("Scope of string exceeds line."));
847 bool is_num = isdigit (c) || c == '.';
848 const char *start = ss_data (p);
853 if (strchr (CC_SPACES ",-+", c) != NULL)
858 if (strchr ("deDE", c) && strchr ("+-", ss_at (p, 1)))
866 s = ss_buffer (start, ss_data (p) - start);
869 data_in (s, FMT_F, 0,
870 dfm_get_column (reader, ss_data (s)),
871 (union value *) &token->number, 0);
875 token->string = ss_data (s);
876 token->length = ss_length (s);
878 dfm_reread_record (reader, dfm_get_column (reader, ss_end (s)));
883 /* Forcibly skip the end of a line for content type CONTENT in
886 force_eol (struct dfm_reader *reader, const char *content)
890 if (dfm_eof (reader))
893 p = dfm_get_record (reader);
894 if (ss_span (p, ss_cstr (CC_SPACES)) != ss_length (p))
896 msg (SE, _("End of line expected %s while reading %s."),
897 context (reader), content);
901 dfm_forward_record (reader);
905 /* Back end, omitting ROWTYPE_. */
909 const struct dictionary *dict; /* The dictionary */
910 struct matrix_data_pgm *mx; /* MATRIX DATA program. */
911 double ***data; /* MATRIX DATA data. */
912 double *factor_values; /* Factor values. */
913 int max_cell_idx; /* Max-numbered cell that we have
914 read so far, plus one. */
915 double *split_values; /* SPLIT FILE variable values. */
918 static bool nr_read_splits (struct nr_aux_data *, int compare);
919 static bool nr_read_factors (struct nr_aux_data *, int cell);
920 static bool nr_output_data (struct nr_aux_data *, struct ccase *,
921 write_case_func *, write_case_data);
922 static bool matrix_data_read_without_rowtype (struct case_source *source,
927 /* Read from the data file and write it to the active file.
928 Returns true if successful, false if an I/O error occurred. */
930 read_matrices_without_rowtype (struct dataset *ds, struct matrix_data_pgm *mx)
932 struct nr_aux_data nr;
939 nr.dict = dataset_dict (ds);
941 nr.factor_values = xnmalloc (mx->n_factors * mx->cells,
942 sizeof *nr.factor_values);
944 nr.split_values = xnmalloc (dict_get_split_cnt (dataset_dict (ds)),
945 sizeof *nr.split_values);
947 proc_set_source (ds, create_case_source (
948 &matrix_data_without_rowtype_source_class, &nr));
950 ok = procedure (ds, NULL, NULL);
952 free (nr.split_values);
953 free (nr.factor_values);
958 /* Mirror data across the diagonal of matrix CP which contains
959 CONTENT type data. */
961 fill_matrix (struct matrix_data_pgm *mx, int content, double *cp)
963 int type = content_type[content];
965 if (type == 1 && mx->section != FULL)
967 if (mx->diag == NODIAGONAL)
969 const double fill = content == CORR ? 1.0 : SYSMIS;
972 for (i = 0; i < mx->n_continuous; i++)
973 cp[i * (1 + mx->n_continuous)] = fill;
979 if (mx->section == LOWER)
981 int n_lines = mx->n_continuous;
982 if (mx->section != FULL && mx->diag == NODIAGONAL)
985 for (r = 1; r < n_lines; r++)
986 for (c = 0; c < r; c++)
987 cp[r + c * mx->n_continuous] = cp[c + r * mx->n_continuous];
991 assert (mx->section == UPPER);
992 for (r = 1; r < mx->n_continuous; r++)
993 for (c = 0; c < r; c++)
994 cp[c + r * mx->n_continuous] = cp[r + c * mx->n_continuous];
1002 for (c = 1; c < mx->n_continuous; c++)
1007 /* Read data lines for content type CONTENT from the data file.
1008 If PER_FACTOR is nonzero, then factor information is read from
1009 the data file. Data is for cell number CELL. */
1011 nr_read_data_lines (struct nr_aux_data *nr,
1012 int per_factor, int cell, int content, int compare)
1014 struct matrix_data_pgm *mx = nr->mx;
1015 const int type = content_type[content]; /* Content type. */
1016 int n_lines; /* Number of lines to parse from data file for this type. */
1017 double *cp; /* Current position in vector or matrix. */
1024 n_lines = mx->n_continuous;
1025 if (mx->section != FULL && mx->diag == NODIAGONAL)
1029 cp = nr->data[content][cell];
1030 if (type == 1 && mx->section == LOWER && mx->diag == NODIAGONAL)
1031 cp += mx->n_continuous;
1033 for (i = 0; i < n_lines; i++)
1037 if (!nr_read_splits (nr, 1))
1039 if (per_factor && !nr_read_factors (nr, cell))
1046 n_cols = mx->n_continuous;
1049 switch (mx->section)
1056 n_cols = mx->n_continuous - i;
1057 if (mx->diag == NODIAGONAL)
1064 n_cols = mx->n_continuous;
1080 for (j = 0; j < n_cols; j++)
1082 struct matrix_token token;
1083 if (!mget_token (&token, mx->reader))
1085 if (token.type != MNUM)
1087 msg (SE, _("expecting value for %s %s"),
1088 dict_get_var (nr->dict, j)->name,
1089 context (mx->reader));
1093 *cp++ = token.number;
1096 && !force_eol (mx->reader, content_names[content]))
1100 if (mx->section == LOWER)
1101 cp += mx->n_continuous - n_cols;
1104 fill_matrix (mx, content, nr->data[content][cell]);
1109 /* When ROWTYPE_ does not appear in the data, reads the matrices and
1110 writes them to the output file.
1111 Returns true if successful, false if an I/O error occurred. */
1113 matrix_data_read_without_rowtype (struct case_source *source,
1115 write_case_func *write_case,
1116 write_case_data wc_data)
1118 struct nr_aux_data *nr = source->aux;
1119 struct matrix_data_pgm *mx = nr->mx;
1124 nr->data = pool_nalloc (mx->container, PROX + 1, sizeof *nr->data);
1129 for (i = 0; i <= PROX; i++)
1133 for (cp = mx->contents; *cp != EOC; cp++)
1134 if (*cp != LPAREN && *cp != RPAREN)
1136 int per_factor = mx->is_per_factor[*cp];
1139 n_entries = mx->n_continuous;
1140 if (content_type[*cp] == 1)
1141 n_entries *= mx->n_continuous;
1144 int n_vectors = per_factor ? mx->cells : 1;
1147 nr->data[*cp] = pool_nalloc (mx->container,
1148 n_vectors, sizeof **nr->data);
1150 for (i = 0; i < n_vectors; i++)
1151 nr->data[*cp][i] = pool_nalloc (mx->container,
1152 n_entries, sizeof ***nr->data);
1161 if (!nr_read_splits (nr, 0))
1164 for (bp = mx->contents; *bp != EOC; bp = np)
1168 /* Trap the CONTENTS that we should parse in this pass
1169 between bp and ep. Set np to the starting bp for next
1174 while (*ep != RPAREN)
1182 while (*ep != EOC && *ep != LPAREN)
1191 for (i = 0; i < (per_factor ? mx->cells : 1); i++)
1195 for (cp = bp; cp < ep; cp++)
1196 if (!nr_read_data_lines (nr, per_factor, i, *cp, cp != bp))
1202 if (!nr_output_data (nr, c, write_case, wc_data))
1205 if (dict_get_split_cnt (nr->dict) == 0
1206 || !another_token (mx->reader))
1211 /* Read the split file variables. If COMPARE is 1, compares the
1212 values read to the last values read and returns true if they're equal,
1215 nr_read_splits (struct nr_aux_data *nr, int compare)
1217 struct matrix_data_pgm *mx = nr->mx;
1218 static int just_read = 0; /* FIXME: WTF? */
1222 if (compare && just_read)
1228 if (dict_get_split_vars (nr->dict) == NULL)
1231 if (mx->single_split)
1235 struct mxd_var *mv = dict_get_split_vars (nr->dict)[0]->aux;
1236 nr->split_values[0] = ++mv->sub_type;
1244 split_cnt = dict_get_split_cnt (nr->dict);
1245 for (i = 0; i < split_cnt; i++)
1247 struct matrix_token token;
1248 if (!mget_token (&token, mx->reader))
1250 if (token.type != MNUM)
1252 msg (SE, _("Syntax error expecting SPLIT FILE value %s."),
1253 context (mx->reader));
1258 nr->split_values[i] = token.number;
1259 else if (nr->split_values[i] != token.number)
1261 msg (SE, _("Expecting value %g for %s."),
1262 nr->split_values[i],
1263 dict_get_split_vars (nr->dict)[i]->name);
1271 /* Read the factors for cell CELL. If COMPARE is 1, compares the
1272 values read to the last values read and returns true if they're equal,
1275 nr_read_factors (struct nr_aux_data *nr, int cell)
1277 struct matrix_data_pgm *mx = nr->mx;
1280 if (mx->n_factors == 0)
1283 assert (nr->max_cell_idx >= cell);
1284 if (cell != nr->max_cell_idx)
1295 for (i = 0; i < mx->n_factors; i++)
1297 struct matrix_token token;
1298 if (!mget_token (&token, mx->reader))
1300 if (token.type != MNUM)
1302 msg (SE, _("Syntax error expecting factor value %s."),
1303 context (mx->reader));
1308 nr->factor_values[i + mx->n_factors * cell] = token.number;
1309 else if (nr->factor_values[i + mx->n_factors * cell] != token.number)
1311 msg (SE, _("Syntax error expecting value %g for %s %s."),
1312 nr->factor_values[i + mx->n_factors * cell],
1313 mx->factors[i]->name, context (mx->reader));
1322 /* Write the contents of a cell having content type CONTENT and data
1323 CP to the active file.
1324 Returns true if successful, false if an I/O error occurred. */
1326 dump_cell_content (const struct dictionary *dict,
1327 struct matrix_data_pgm *mx, int content, double *cp,
1329 write_case_func *write_case, write_case_data wc_data)
1331 int type = content_type[content];
1334 buf_copy_str_rpad (case_data_rw (c, mx->rowtype_->fv)->s, 8,
1335 content_names[content]);
1338 memset (case_data_rw (c, mx->varname_->fv)->s, ' ', 8);
1342 int n_lines = (type == 1) ? mx->n_continuous : 1;
1345 for (i = 0; i < n_lines; i++)
1349 for (j = 0; j < mx->n_continuous; j++)
1351 int fv = dict_get_var (dict, mx->first_continuous + j)->fv;
1352 case_data_rw (c, fv)->f = *cp;
1356 buf_copy_str_rpad (case_data_rw (c, mx->varname_->fv)->s, 8,
1358 mx->first_continuous + i)->name);
1359 if (!write_case (wc_data))
1366 /* Finally dump out everything from nr_data[] to the output file. */
1368 nr_output_data (struct nr_aux_data *nr, struct ccase *c,
1369 write_case_func *write_case, write_case_data wc_data)
1371 struct matrix_data_pgm *mx = nr->mx;
1374 struct variable *const *split;
1378 split_cnt = dict_get_split_cnt (nr->dict);
1379 split = dict_get_split_vars (nr->dict);
1380 for (i = 0; i < split_cnt; i++)
1381 case_data_rw (c, split[i]->fv)->f = nr->split_values[i];
1388 for (cell = 0; cell < mx->cells; cell++)
1393 for (factor = 0; factor < mx->n_factors; factor++)
1394 case_data_rw (c, mx->factors[factor]->fv)->f
1395 = nr->factor_values[factor + cell * mx->n_factors];
1401 for (content = 0; content <= PROX; content++)
1402 if (mx->is_per_factor[content])
1404 assert (nr->data[content] != NULL
1405 && nr->data[content][cell] != NULL);
1407 if (!dump_cell_content (nr->dict, mx,
1408 content, nr->data[content][cell],
1409 c, write_case, wc_data))
1422 for (factor = 0; factor < mx->n_factors; factor++)
1423 case_data_rw (c, mx->factors[factor]->fv)->f = SYSMIS;
1426 for (content = 0; content <= PROX; content++)
1427 if (!mx->is_per_factor[content] && nr->data[content] != NULL)
1429 if (!dump_cell_content (nr->dict, mx, content, nr->data[content][0],
1430 c, write_case, wc_data))
1438 /* Back end, with ROWTYPE_. */
1440 /* All the data for one set of factor values. */
1444 int n_rows[PROX + 1];
1445 double *data[PROX + 1];
1446 struct factor_data *next;
1449 /* With ROWTYPE_ auxiliary data. */
1452 const struct dictionary *dict; /* The dictionary */
1453 struct matrix_data_pgm *mx; /* MATRIX DATA program. */
1454 int content; /* Type of current row. */
1455 double *split_values; /* SPLIT FILE variable values. */
1456 struct factor_data *data; /* All the data. */
1457 struct factor_data *current; /* Current factor. */
1460 static bool wr_read_splits (struct wr_aux_data *, struct ccase *,
1461 write_case_func *, write_case_data);
1462 static bool wr_output_data (struct wr_aux_data *, struct ccase *,
1463 write_case_func *, write_case_data);
1464 static bool wr_read_rowtype (struct wr_aux_data *,
1465 const struct matrix_token *, struct dfm_reader *);
1466 static bool wr_read_factors (struct wr_aux_data *);
1467 static bool wr_read_indeps (struct wr_aux_data *);
1468 static bool matrix_data_read_with_rowtype (struct case_source *,
1473 /* When ROWTYPE_ appears in the data, reads the matrices and writes
1474 them to the output file.
1475 Returns true if successful, false if an I/O error occurred. */
1477 read_matrices_with_rowtype (struct dataset *ds, struct matrix_data_pgm *mx)
1479 struct wr_aux_data wr;
1484 wr.split_values = NULL;
1487 wr.dict = dataset_dict (ds);
1490 proc_set_source (ds,
1491 create_case_source (&matrix_data_with_rowtype_source_class,
1493 ok = procedure (ds, NULL, NULL);
1495 free (wr.split_values);
1499 /* Read from the data file and write it to the active file.
1500 Returns true if successful, false if an I/O error occurred. */
1502 matrix_data_read_with_rowtype (struct case_source *source,
1504 write_case_func *write_case,
1505 write_case_data wc_data)
1507 struct wr_aux_data *wr = source->aux;
1508 struct matrix_data_pgm *mx = wr->mx;
1512 if (!wr_read_splits (wr, c, write_case, wc_data))
1515 if (!wr_read_factors (wr))
1518 if (!wr_read_indeps (wr))
1521 while (another_token (mx->reader));
1523 return wr_output_data (wr, c, write_case, wc_data);
1526 /* Read the split file variables. If they differ from the previous
1527 set of split variables then output the data. Returns success. */
1529 wr_read_splits (struct wr_aux_data *wr,
1531 write_case_func *write_case, write_case_data wc_data)
1533 struct matrix_data_pgm *mx = wr->mx;
1537 split_cnt = dict_get_split_cnt (wr->dict);
1541 if (wr->split_values)
1546 wr->split_values = xnmalloc (split_cnt, sizeof *wr->split_values);
1550 bool different = false;
1553 for (i = 0; i < split_cnt; i++)
1555 struct matrix_token token;
1556 if (!mget_token (&token, mx->reader))
1558 if (token.type != MNUM)
1560 msg (SE, _("Syntax error %s expecting SPLIT FILE value."),
1561 context (mx->reader));
1565 if (compare && wr->split_values[i] != token.number && !different)
1567 if (!wr_output_data (wr, c, write_case, wc_data))
1572 wr->split_values[i] = token.number;
1579 /* Compares doubles A and B, treating SYSMIS as greatest. */
1581 compare_doubles (const void *a_, const void *b_, const void *aux UNUSED)
1583 const double *a = a_;
1584 const double *b = b_;
1588 else if (*a == SYSMIS)
1590 else if (*b == SYSMIS)
1598 /* Return strcmp()-type comparison of the MX->n_factors factors at _A and
1599 _B. Sort missing values toward the end. */
1601 compare_factors (const void *a_, const void *b_, const void *mx_)
1603 const struct matrix_data_pgm *mx = mx_;
1604 struct factor_data *const *pa = a_;
1605 struct factor_data *const *pb = b_;
1606 const double *a = (*pa)->factors;
1607 const double *b = (*pb)->factors;
1609 return lexicographical_compare_3way (a, mx->n_factors,
1612 compare_doubles, NULL);
1615 /* Write out the data for the current split file to the active
1617 Returns true if successful, false if an I/O error occurred. */
1619 wr_output_data (struct wr_aux_data *wr,
1621 write_case_func *write_case, write_case_data wc_data)
1623 struct matrix_data_pgm *mx = wr->mx;
1627 struct variable *const *split;
1631 split_cnt = dict_get_split_cnt (wr->dict);
1632 split = dict_get_split_vars (wr->dict);
1633 for (i = 0; i < split_cnt; i++)
1634 case_data_rw (c, split[i]->fv)->f = wr->split_values[i];
1637 /* Sort the wr->data list. */
1639 struct factor_data **factors;
1640 struct factor_data *iter;
1643 factors = xnmalloc (mx->cells, sizeof *factors);
1645 for (i = 0, iter = wr->data; iter; iter = iter->next, i++)
1648 sort (factors, mx->cells, sizeof *factors, compare_factors, mx);
1650 wr->data = factors[0];
1651 for (i = 0; i < mx->cells - 1; i++)
1652 factors[i]->next = factors[i + 1];
1653 factors[mx->cells - 1]->next = NULL;
1658 /* Write out records for every set of factor values. */
1660 struct factor_data *iter;
1662 for (iter = wr->data; iter; iter = iter->next)
1667 for (factor = 0; factor < mx->n_factors; factor++)
1668 case_data_rw (c, mx->factors[factor]->fv)->f
1669 = iter->factors[factor];
1675 for (content = 0; content <= PROX; content++)
1677 if (!iter->n_rows[content])
1681 int type = content_type[content];
1682 int n_lines = (type == 1
1684 - (mx->section != FULL && mx->diag == NODIAGONAL))
1687 if (n_lines != iter->n_rows[content])
1689 msg (SE, _("Expected %d lines of data for %s content; "
1690 "actually saw %d lines. No data will be "
1691 "output for this content."),
1692 n_lines, content_names[content],
1693 iter->n_rows[content]);
1698 fill_matrix (mx, content, iter->data[content]);
1700 ok = dump_cell_content (wr->dict, mx, content,
1701 iter->data[content],
1702 c, write_case, wc_data);
1710 pool_destroy (mx->container);
1711 mx->container = pool_create ();
1713 wr->data = wr->current = NULL;
1718 /* Sets ROWTYPE_ based on the given TOKEN read from READER.
1721 wr_read_rowtype (struct wr_aux_data *wr,
1722 const struct matrix_token *token,
1723 struct dfm_reader *reader)
1725 if (wr->content != -1)
1727 msg (SE, _("Multiply specified ROWTYPE_ %s."), context (reader));
1730 if (token->type != MSTR)
1732 msg (SE, _("Syntax error %s expecting ROWTYPE_ string."),
1741 memcpy (s, token->string, MIN (15, token->length));
1742 s[MIN (15, token->length)] = 0;
1744 for (cp = s; *cp; cp++)
1745 *cp = toupper ((unsigned char) *cp);
1747 wr->content = string_to_content_type (s, NULL);
1750 if (wr->content == -1)
1752 msg (SE, _("Syntax error %s."), context (reader));
1759 /* Read the factors for the current row. Select a set of factors and
1760 point wr_current to it. */
1762 wr_read_factors (struct wr_aux_data *wr)
1764 struct matrix_data_pgm *mx = wr->mx;
1765 double *factor_values = local_alloc (sizeof *factor_values * mx->n_factors);
1771 for (i = 0; i < mx->n_factors; i++)
1773 struct matrix_token token;
1774 if (!mget_token (&token, mx->reader))
1776 if (token.type == MSTR)
1778 if (!wr_read_rowtype (wr, &token, mx->reader))
1780 if (!mget_token (&token, mx->reader))
1783 if (token.type != MNUM)
1785 msg (SE, _("Syntax error expecting factor value %s."),
1786 context (mx->reader));
1790 factor_values[i] = token.number;
1793 if (wr->content == -1)
1795 struct matrix_token token;
1796 if (!mget_token (&token, mx->reader))
1798 if (!wr_read_rowtype (wr, &token, mx->reader))
1802 /* Try the most recent factor first as a simple caching
1808 for (i = 0; i < mx->n_factors; i++)
1809 if (factor_values[i] != wr->current->factors[i])
1814 /* Linear search through the list. */
1817 struct factor_data *iter;
1819 for (iter = wr->data; iter; iter = iter->next)
1823 for (i = 0; i < mx->n_factors; i++)
1824 if (factor_values[i] != iter->factors[i])
1834 /* Not found. Make a new item. */
1836 struct factor_data *new = pool_alloc (mx->container, sizeof *new);
1838 new->factors = pool_nalloc (mx->container,
1839 mx->n_factors, sizeof *new->factors);
1844 for (i = 0; i < mx->n_factors; i++)
1845 new->factors[i] = factor_values[i];
1851 for (i = 0; i <= PROX; i++)
1854 new->data[i] = NULL;
1858 new->next = wr->data;
1859 wr->data = wr->current = new;
1864 local_free (factor_values);
1868 local_free (factor_values);
1872 /* Read the independent variables into wr->current. */
1874 wr_read_indeps (struct wr_aux_data *wr)
1876 struct matrix_data_pgm *mx = wr->mx;
1877 struct factor_data *c = wr->current;
1878 const int type = content_type[wr->content];
1879 const int n_rows = c->n_rows[wr->content];
1883 /* Allocate room for data if necessary. */
1884 if (c->data[wr->content] == NULL)
1886 int n_items = mx->n_continuous;
1888 n_items *= mx->n_continuous;
1890 c->data[wr->content] = pool_nalloc (mx->container,
1891 n_items, sizeof **c->data);
1894 cp = &c->data[wr->content][n_rows * mx->n_continuous];
1896 /* Figure out how much to read from this line. */
1903 msg (SE, _("Duplicate specification for %s."),
1904 content_names[wr->content]);
1908 n_cols = mx->n_continuous;
1913 if (n_rows >= mx->n_continuous - (mx->section != FULL && mx->diag == NODIAGONAL))
1915 msg (SE, _("Too many rows of matrix data for %s."),
1916 content_names[wr->content]);
1920 switch (mx->section)
1923 n_cols = n_rows + 1;
1924 if (mx->diag == NODIAGONAL)
1925 cp += mx->n_continuous;
1929 n_cols = mx->n_continuous - n_rows;
1930 if (mx->diag == NODIAGONAL)
1937 n_cols = mx->n_continuous;
1946 c->n_rows[wr->content]++;
1948 /* Read N_COLS items at CP. */
1952 for (j = 0; j < n_cols; j++)
1954 struct matrix_token token;
1955 if (!mget_token (&token, mx->reader))
1957 if (token.type != MNUM)
1959 msg (SE, _("Syntax error expecting value for %s %s."),
1960 dict_get_var (wr->dict, mx->first_continuous + j)->name,
1961 context (mx->reader));
1965 *cp++ = token.number;
1968 && !force_eol (mx->reader, content_names[wr->content]))
1975 /* Matrix source. */
1977 static const struct case_source_class matrix_data_with_rowtype_source_class =
1981 matrix_data_read_with_rowtype,
1985 static const struct case_source_class
1986 matrix_data_without_rowtype_source_class =
1990 matrix_data_read_without_rowtype,