1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 #include <data/case-source.h>
27 #include <data/case.h>
28 #include <data/data-in.h>
29 #include <data/dictionary.h>
30 #include <data/procedure.h>
31 #include <data/variable.h>
32 #include <language/command.h>
33 #include <language/data-io/data-reader.h>
34 #include <language/data-io/file-handle.h>
35 #include <language/lexer/lexer.h>
36 #include <libpspp/alloc.h>
37 #include <libpspp/array.h>
38 #include <libpspp/compiler.h>
39 #include <libpspp/message.h>
40 #include <libpspp/message.h>
41 #include <libpspp/misc.h>
42 #include <libpspp/pool.h>
43 #include <libpspp/str.h>
46 #define _(msgid) gettext (msgid)
48 /* FIXME: /N subcommand not implemented. It should be pretty simple,
51 /* Different types of variables for MATRIX DATA procedure. Order is
52 important: these are used for sort keys. */
55 MXD_SPLIT, /* SPLIT FILE variables. */
56 MXD_ROWTYPE, /* ROWTYPE_. */
57 MXD_FACTOR, /* Factor variables. */
58 MXD_VARNAME, /* VARNAME_. */
59 MXD_CONTINUOUS, /* Continuous variables. */
64 /* Format type enums. */
71 /* Matrix section enums. */
79 /* Diagonal inclusion enums. */
107 /* 0=vector, 1=matrix, 2=scalar. */
108 static const int content_type[PROX + 1] =
110 0, 2, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1,
113 /* Name of each content type. */
114 static const char *content_names[PROX + 1] =
116 "N", "N", "N_MATRIX", "MEAN", "STDDEV", "COUNT", "MSE",
117 "DFE", "MAT", "COV", "CORR", "PROX",
120 /* A MATRIX DATA input program. */
121 struct matrix_data_pgm
123 struct pool *container; /* Arena used for all allocations. */
124 struct dfm_reader *reader; /* Data file to read. */
127 enum format_type fmt; /* LIST or FREE. */
128 enum matrix_section section;/* LOWER or UPPER or FULL. */
129 enum include_diagonal diag; /* DIAGONAL or NODIAGONAL. */
131 int explicit_rowtype; /* ROWTYPE_ specified explicitly in data? */
132 struct variable *rowtype_, *varname_; /* ROWTYPE_, VARNAME_ variables. */
134 struct variable *single_split; /* Single SPLIT FILE variable. */
136 /* Factor variables. */
137 size_t n_factors; /* Number of factor variables. */
138 struct variable **factors; /* Factor variables. */
139 int is_per_factor[PROX + 1]; /* Is there per-factor data? */
141 int cells; /* Number of cells, or -1 if none. */
143 int pop_n; /* Population N specified by user. */
145 /* CONTENTS subcommand. */
146 int contents[EOC * 3 + 1]; /* Contents. */
147 int n_contents; /* Number of entries. */
149 /* Continuous variables. */
150 int n_continuous; /* Number of continuous variables. */
151 int first_continuous; /* Index into default_dict.var of
152 first continuous variable. */
155 /* Auxiliary data attached to MATRIX DATA variables. */
158 int var_type; /* Variable type. */
159 int sub_type; /* Subtype. */
162 static const struct case_source_class matrix_data_with_rowtype_source_class;
163 static const struct case_source_class matrix_data_without_rowtype_source_class;
165 static int compare_variables_by_mxd_var_type (const void *pa,
167 static bool read_matrices_without_rowtype (struct matrix_data_pgm *);
168 static bool read_matrices_with_rowtype (struct matrix_data_pgm *);
169 static int string_to_content_type (char *, int *);
170 static void attach_mxd_aux (struct variable *, int var_type, int sub_type);
173 cmd_matrix_data (void)
176 struct matrix_data_pgm *mx;
177 struct file_handle *fh = fh_inline_file ();
182 discard_variables ();
184 pool = pool_create ();
185 mx = pool_alloc (pool, sizeof *mx);
186 mx->container = pool;
191 mx->explicit_rowtype = 0;
194 mx->single_split = NULL;
197 memset (mx->is_per_factor, 0, sizeof mx->is_per_factor);
201 mx->n_continuous = 0;
202 mx->first_continuous = 0;
207 if (lex_match_id ("VARIABLES"))
214 msg (SE, _("VARIABLES subcommand multiply specified."));
220 if (!parse_DATA_LIST_vars (&v, &nv, PV_NO_DUPLICATE))
226 for (i = 0; i < nv; i++)
227 if (!strcasecmp (v[i], "VARNAME_"))
229 msg (SE, _("VARNAME_ cannot be explicitly specified on "
231 for (i = 0; i < nv; i++)
241 for (i = 0; i < nv; i++)
243 struct variable *new_var;
245 if (strcasecmp (v[i], "ROWTYPE_"))
247 new_var = dict_create_var_assert (default_dict, v[i], 0);
248 attach_mxd_aux (new_var, MXD_CONTINUOUS, i);
251 mx->explicit_rowtype = 1;
257 mx->rowtype_ = dict_create_var_assert (default_dict,
259 attach_mxd_aux (mx->rowtype_, MXD_ROWTYPE, 0);
261 else if (lex_match_id ("FILE"))
264 fh = fh_parse (FH_REF_FILE | FH_REF_INLINE);
268 else if (lex_match_id ("FORMAT"))
272 while (token == T_ID)
274 if (lex_match_id ("LIST"))
276 else if (lex_match_id ("FREE"))
278 else if (lex_match_id ("LOWER"))
280 else if (lex_match_id ("UPPER"))
282 else if (lex_match_id ("FULL"))
284 else if (lex_match_id ("DIAGONAL"))
286 else if (lex_match_id ("NODIAGONAL"))
287 mx->diag = NODIAGONAL;
290 lex_error (_("in FORMAT subcommand"));
295 else if (lex_match_id ("SPLIT"))
301 msg (SE, _("SPLIT subcommand multiply specified."));
308 lex_error (_("in SPLIT subcommand"));
312 if (dict_lookup_var (default_dict, tokid) == NULL
313 && (lex_look_ahead () == '.' || lex_look_ahead () == '/'))
315 if (!strcasecmp (tokid, "ROWTYPE_")
316 || !strcasecmp (tokid, "VARNAME_"))
318 msg (SE, _("Split variable may not be named ROWTYPE_ "
323 mx->single_split = dict_create_var_assert (default_dict,
325 attach_mxd_aux (mx->single_split, MXD_CONTINUOUS, 0);
328 dict_set_split_vars (default_dict, &mx->single_split, 1);
332 struct variable **split;
335 if (!parse_variables (default_dict, &split, &n, PV_NO_DUPLICATE))
338 dict_set_split_vars (default_dict, split, n);
342 struct variable *const *split = dict_get_split_vars (default_dict);
343 size_t split_cnt = dict_get_split_cnt (default_dict);
346 for (i = 0; i < split_cnt; i++)
348 struct mxd_var *mv = split[i]->aux;
350 if (mv->var_type != MXD_CONTINUOUS)
352 msg (SE, _("Split variable %s is already another type."),
356 var_clear_aux (split[i]);
357 attach_mxd_aux (split[i], MXD_SPLIT, i);
361 else if (lex_match_id ("FACTORS"))
367 msg (SE, _("FACTORS subcommand multiply specified."));
372 if (!parse_variables (default_dict, &mx->factors, &mx->n_factors,
379 for (i = 0; i < mx->n_factors; i++)
381 struct variable *v = mx->factors[i];
382 struct mxd_var *mv = v->aux;
384 if (mv->var_type != MXD_CONTINUOUS)
386 msg (SE, _("Factor variable %s is already another type."),
391 attach_mxd_aux (v, MXD_FACTOR, i);
395 else if (lex_match_id ("CELLS"))
401 msg (SE, _("CELLS subcommand multiply specified."));
405 if (!lex_is_integer () || lex_integer () < 1)
407 lex_error (_("expecting positive integer"));
411 mx->cells = lex_integer ();
414 else if (lex_match_id ("N"))
420 msg (SE, _("N subcommand multiply specified."));
424 if (!lex_is_integer () || lex_integer () < 1)
426 lex_error (_("expecting positive integer"));
430 mx->pop_n = lex_integer ();
433 else if (lex_match_id ("CONTENTS"))
435 int inside_parens = 0;
436 unsigned collide = 0;
441 msg (SE, _("CONTENTS subcommand multiply specified."));
451 for (i = 0; i <= PROX; i++)
452 mx->is_per_factor[i] = 0;
461 msg (SE, _("Nested parentheses not allowed."));
467 else if (lex_match (')'))
471 msg (SE, _("Mismatched right parenthesis (`(')."));
474 if (mx->contents[mx->n_contents - 1] == LPAREN)
476 msg (SE, _("Empty parentheses not allowed."));
489 lex_error (_("in CONTENTS subcommand"));
493 content_type = string_to_content_type (tokid,
495 if (content_type == -1)
497 lex_error (_("in CONTENTS subcommand"));
502 if (collide & (1 << collide_index))
504 msg (SE, _("Content multiply specified for %s."),
505 content_names[content_type]);
508 collide |= (1 << collide_index);
511 mx->is_per_factor[item] = inside_parens;
513 mx->contents[mx->n_contents++] = item;
515 if (token == '/' || token == '.')
521 msg (SE, _("Missing right parenthesis."));
524 mx->contents[mx->n_contents] = EOC;
535 lex_error (_("expecting end of command"));
541 msg (SE, _("Missing VARIABLES subcommand."));
545 if (!mx->n_contents && !mx->explicit_rowtype)
547 msg (SW, _("CONTENTS subcommand not specified: assuming file "
548 "contains only CORR matrix."));
550 mx->contents[0] = CORR;
551 mx->contents[1] = EOC;
555 if (mx->n_factors && !mx->explicit_rowtype && mx->cells == -1)
557 msg (SE, _("Missing CELLS subcommand. CELLS is required "
558 "when ROWTYPE_ is not given in the data and "
559 "factors are present."));
563 if (mx->explicit_rowtype && mx->single_split)
565 msg (SE, _("Split file values must be present in the data when "
566 "ROWTYPE_ is present."));
570 /* Create VARNAME_. */
571 mx->varname_ = dict_create_var_assert (default_dict, "VARNAME_", 8);
572 attach_mxd_aux (mx->varname_, MXD_VARNAME, 0);
574 /* Sort the dictionary variables into the desired order for the
575 system file output. */
580 dict_get_vars (default_dict, &v, &nv, 0);
581 qsort (v, nv, sizeof *v, compare_variables_by_mxd_var_type);
582 dict_reorder_vars (default_dict, v, nv);
588 static const struct fmt_spec fmt_tab[MXD_COUNT] =
599 mx->first_continuous = -1;
600 for (i = 0; i < dict_get_var_cnt (default_dict); i++)
602 struct variable *v = dict_get_var (default_dict, i);
603 struct mxd_var *mv = v->aux;
604 int type = mv->var_type;
606 assert (type >= 0 && type < MXD_COUNT);
607 v->print = v->write = fmt_tab[type];
609 if (type == MXD_CONTINUOUS)
611 if (mx->first_continuous == -1 && type == MXD_CONTINUOUS)
612 mx->first_continuous = i;
616 if (mx->n_continuous == 0)
618 msg (SE, _("No continuous variables specified."));
622 mx->reader = dfm_open_reader (fh);
623 if (mx->reader == NULL)
626 if (mx->explicit_rowtype)
627 ok = read_matrices_with_rowtype (mx);
629 ok = read_matrices_without_rowtype (mx);
631 dfm_close_reader (mx->reader);
633 pool_destroy (mx->container);
635 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
638 discard_variables ();
640 pool_destroy (mx->container);
641 return CMD_CASCADING_FAILURE;
644 /* Look up string S as a content-type name and return the
645 corresponding enumerated value, or -1 if there is no match. If
646 COLLIDE is non-NULL then *COLLIDE returns a value (suitable for use
647 as a bit-index) which can be used for determining whether a related
648 statistic has already been used. */
650 string_to_content_type (char *s, int *collide)
661 {N_VECTOR, 0, "N_VECTOR"},
663 {N_SCALAR, 0, "N_SCALAR"},
664 {N_MATRIX, 1, "N_MATRIX"},
666 {STDDEV, 3, "STDDEV"},
678 for (tp = tab; tp->value != -1; tp++)
679 if (!strcasecmp (s, tp->string))
682 *collide = tp->collide;
689 /* Compare two variables using p.mxd.var_type and p.mxd.sub_type
692 compare_variables_by_mxd_var_type (const void *a_, const void *b_)
694 struct variable *const *pa = a_;
695 struct variable *const *pb = b_;
696 const struct mxd_var *a = (*pa)->aux;
697 const struct mxd_var *b = (*pb)->aux;
699 if (a->var_type != b->var_type)
700 return a->var_type > b->var_type ? 1 : -1;
702 return a->sub_type < b->sub_type ? -1 : a->sub_type > b->sub_type;
705 /* Attaches a struct mxd_var with the specific member values to
708 attach_mxd_aux (struct variable *v, int var_type, int sub_type)
712 assert (v->aux == NULL);
713 mv = xmalloc (sizeof *mv);
714 mv->var_type = var_type;
715 mv->sub_type = sub_type;
716 var_attach_aux (v, mv, var_dtor_free);
719 /* Matrix tokenizer. */
721 /* Matrix token types. */
722 enum matrix_token_type
728 /* A MATRIX DATA parsing token. */
731 enum matrix_token_type type;
732 double number; /* MNUM: token value. */
733 char *string; /* MSTR: token string; not null-terminated. */
734 int length; /* MSTR: tokstr length. */
737 static int mget_token (struct matrix_token *, struct dfm_reader *);
740 #define mget_token(TOKEN, READER) mget_token_dump(TOKEN, READER)
743 mdump_token (const struct matrix_token *token)
748 printf (" #%g", token->number);
751 printf (" '%.*s'", token->length, token->string);
760 mget_token_dump (struct matrix_token *token, struct dfm_reader *reader)
762 int result = (mget_token) (token, reader);
768 /* Return the current position in READER. */
770 context (struct dfm_reader *reader)
774 if (dfm_eof (reader))
775 strcpy (buf, "at end of file");
778 struct fixed_string line;
781 dfm_get_record (reader, &line);
782 sp = ls_c_str (&line);
783 while (sp < ls_end (&line) && isspace ((unsigned char) *sp))
785 if (sp >= ls_end (&line))
786 strcpy (buf, "at end of line");
792 dp = stpcpy (buf, "before `");
793 while (sp < ls_end (&line) && !isspace ((unsigned char) *sp)
806 /* Is there at least one token left in the data file? */
808 another_token (struct dfm_reader *reader)
812 struct fixed_string line;
815 if (dfm_eof (reader))
817 dfm_get_record (reader, &line);
819 cp = ls_c_str (&line);
820 while (isspace ((unsigned char) *cp) && cp < ls_end (&line))
823 if (cp < ls_end (&line))
825 dfm_forward_columns (reader, cp - ls_c_str (&line));
829 dfm_forward_record (reader);
833 /* Parse a MATRIX DATA token from READER into TOKEN. */
835 (mget_token) (struct matrix_token *token, struct dfm_reader *reader)
837 struct fixed_string line;
841 if (!another_token (reader))
844 dfm_get_record (reader, &line);
845 first_column = dfm_column_start (reader);
847 /* Three types of fields: quoted with ', quoted with ", unquoted. */
848 cp = ls_c_str (&line);
849 if (*cp == '\'' || *cp == '"')
854 token->string = ++cp;
855 while (cp < ls_end (&line) && *cp != quote)
857 token->length = cp - token->string;
858 if (cp < ls_end (&line))
861 msg (SW, _("Scope of string exceeds line."));
865 int is_num = isdigit ((unsigned char) *cp) || *cp == '.';
867 token->string = cp++;
868 while (cp < ls_end (&line)
869 && !isspace ((unsigned char) *cp) && *cp != ','
870 && *cp != '-' && *cp != '+')
872 if (isdigit ((unsigned char) *cp))
875 if ((tolower ((unsigned char) *cp) == 'd'
876 || tolower ((unsigned char) *cp) == 'e')
877 && (cp[1] == '+' || cp[1] == '-'))
883 token->length = cp - token->string;
884 assert (token->length);
890 di.s = token->string;
891 di.e = token->string + token->length;
892 di.v = (union value *) &token->number;
893 di.f1 = first_column;
894 di.format = make_output_format (FMT_F, token->length, 0);
903 dfm_forward_columns (reader, cp - ls_c_str (&line));
908 /* Forcibly skip the end of a line for content type CONTENT in
911 force_eol (struct dfm_reader *reader, const char *content)
913 struct fixed_string line;
916 if (dfm_eof (reader))
918 dfm_get_record (reader, &line);
920 cp = ls_c_str (&line);
921 while (isspace ((unsigned char) *cp) && cp < ls_end (&line))
924 if (cp < ls_end (&line))
926 msg (SE, _("End of line expected %s while reading %s."),
927 context (reader), content);
931 dfm_forward_record (reader);
935 /* Back end, omitting ROWTYPE_. */
939 struct matrix_data_pgm *mx; /* MATRIX DATA program. */
940 double ***data; /* MATRIX DATA data. */
941 double *factor_values; /* Factor values. */
942 int max_cell_idx; /* Max-numbered cell that we have
943 read so far, plus one. */
944 double *split_values; /* SPLIT FILE variable values. */
947 static int nr_read_splits (struct nr_aux_data *, int compare);
948 static int nr_read_factors (struct nr_aux_data *, int cell);
949 static bool nr_output_data (struct nr_aux_data *, struct ccase *,
950 write_case_func *, write_case_data);
951 static bool matrix_data_read_without_rowtype (struct case_source *source,
956 /* Read from the data file and write it to the active file.
957 Returns true if successful, false if an I/O error occurred. */
959 read_matrices_without_rowtype (struct matrix_data_pgm *mx)
961 struct nr_aux_data nr;
969 nr.factor_values = xnmalloc (mx->n_factors * mx->cells,
970 sizeof *nr.factor_values);
972 nr.split_values = xnmalloc (dict_get_split_cnt (default_dict),
973 sizeof *nr.split_values);
975 proc_set_source (create_case_source (
976 &matrix_data_without_rowtype_source_class, &nr));
978 ok = procedure (NULL, NULL);
980 free (nr.split_values);
981 free (nr.factor_values);
986 /* Mirror data across the diagonal of matrix CP which contains
987 CONTENT type data. */
989 fill_matrix (struct matrix_data_pgm *mx, int content, double *cp)
991 int type = content_type[content];
993 if (type == 1 && mx->section != FULL)
995 if (mx->diag == NODIAGONAL)
997 const double fill = content == CORR ? 1.0 : SYSMIS;
1000 for (i = 0; i < mx->n_continuous; i++)
1001 cp[i * (1 + mx->n_continuous)] = fill;
1007 if (mx->section == LOWER)
1009 int n_lines = mx->n_continuous;
1010 if (mx->section != FULL && mx->diag == NODIAGONAL)
1013 for (r = 1; r < n_lines; r++)
1014 for (c = 0; c < r; c++)
1015 cp[r + c * mx->n_continuous] = cp[c + r * mx->n_continuous];
1019 assert (mx->section == UPPER);
1020 for (r = 1; r < mx->n_continuous; r++)
1021 for (c = 0; c < r; c++)
1022 cp[c + r * mx->n_continuous] = cp[r + c * mx->n_continuous];
1030 for (c = 1; c < mx->n_continuous; c++)
1035 /* Read data lines for content type CONTENT from the data file.
1036 If PER_FACTOR is nonzero, then factor information is read from
1037 the data file. Data is for cell number CELL. */
1039 nr_read_data_lines (struct nr_aux_data *nr,
1040 int per_factor, int cell, int content, int compare)
1042 struct matrix_data_pgm *mx = nr->mx;
1043 const int type = content_type[content]; /* Content type. */
1044 int n_lines; /* Number of lines to parse from data file for this type. */
1045 double *cp; /* Current position in vector or matrix. */
1052 n_lines = mx->n_continuous;
1053 if (mx->section != FULL && mx->diag == NODIAGONAL)
1057 cp = nr->data[content][cell];
1058 if (type == 1 && mx->section == LOWER && mx->diag == NODIAGONAL)
1059 cp += mx->n_continuous;
1061 for (i = 0; i < n_lines; i++)
1065 if (!nr_read_splits (nr, 1))
1067 if (per_factor && !nr_read_factors (nr, cell))
1074 n_cols = mx->n_continuous;
1077 switch (mx->section)
1084 n_cols = mx->n_continuous - i;
1085 if (mx->diag == NODIAGONAL)
1092 n_cols = mx->n_continuous;
1110 for (j = 0; j < n_cols; j++)
1112 struct matrix_token token;
1113 if (!mget_token (&token, mx->reader))
1115 if (token.type != MNUM)
1117 msg (SE, _("expecting value for %s %s"),
1118 dict_get_var (default_dict, j)->name,
1119 context (mx->reader));
1123 *cp++ = token.number;
1126 && !force_eol (mx->reader, content_names[content]))
1130 if (mx->section == LOWER)
1131 cp += mx->n_continuous - n_cols;
1134 fill_matrix (mx, content, nr->data[content][cell]);
1139 /* When ROWTYPE_ does not appear in the data, reads the matrices and
1140 writes them to the output file.
1141 Returns true if successful, false if an I/O error occurred. */
1143 matrix_data_read_without_rowtype (struct case_source *source,
1145 write_case_func *write_case,
1146 write_case_data wc_data)
1148 struct nr_aux_data *nr = source->aux;
1149 struct matrix_data_pgm *mx = nr->mx;
1154 nr->data = pool_nalloc (mx->container, PROX + 1, sizeof *nr->data);
1159 for (i = 0; i <= PROX; i++)
1163 for (cp = mx->contents; *cp != EOC; cp++)
1164 if (*cp != LPAREN && *cp != RPAREN)
1166 int per_factor = mx->is_per_factor[*cp];
1169 n_entries = mx->n_continuous;
1170 if (content_type[*cp] == 1)
1171 n_entries *= mx->n_continuous;
1174 int n_vectors = per_factor ? mx->cells : 1;
1177 nr->data[*cp] = pool_nalloc (mx->container,
1178 n_vectors, sizeof **nr->data);
1180 for (i = 0; i < n_vectors; i++)
1181 nr->data[*cp][i] = pool_nalloc (mx->container,
1182 n_entries, sizeof ***nr->data);
1191 if (!nr_read_splits (nr, 0))
1194 for (bp = mx->contents; *bp != EOC; bp = np)
1198 /* Trap the CONTENTS that we should parse in this pass
1199 between bp and ep. Set np to the starting bp for next
1204 while (*ep != RPAREN)
1212 while (*ep != EOC && *ep != LPAREN)
1221 for (i = 0; i < (per_factor ? mx->cells : 1); i++)
1225 for (cp = bp; cp < ep; cp++)
1226 if (!nr_read_data_lines (nr, per_factor, i, *cp, cp != bp))
1232 if (!nr_output_data (nr, c, write_case, wc_data))
1235 if (dict_get_split_cnt (default_dict) == 0
1236 || !another_token (mx->reader))
1241 /* Read the split file variables. If COMPARE is 1, compares the
1242 values read to the last values read and returns 1 if they're equal,
1245 nr_read_splits (struct nr_aux_data *nr, int compare)
1247 struct matrix_data_pgm *mx = nr->mx;
1248 static int just_read = 0; /* FIXME: WTF? */
1252 if (compare && just_read)
1258 if (dict_get_split_vars (default_dict) == NULL)
1261 if (mx->single_split)
1265 struct mxd_var *mv = dict_get_split_vars (default_dict)[0]->aux;
1266 nr->split_values[0] = ++mv->sub_type;
1274 split_cnt = dict_get_split_cnt (default_dict);
1275 for (i = 0; i < split_cnt; i++)
1277 struct matrix_token token;
1278 if (!mget_token (&token, mx->reader))
1280 if (token.type != MNUM)
1282 msg (SE, _("Syntax error expecting SPLIT FILE value %s."),
1283 context (mx->reader));
1288 nr->split_values[i] = token.number;
1289 else if (nr->split_values[i] != token.number)
1291 msg (SE, _("Expecting value %g for %s."),
1292 nr->split_values[i],
1293 dict_get_split_vars (default_dict)[i]->name);
1301 /* Read the factors for cell CELL. If COMPARE is 1, compares the
1302 values read to the last values read and returns 1 if they're equal,
1305 nr_read_factors (struct nr_aux_data *nr, int cell)
1307 struct matrix_data_pgm *mx = nr->mx;
1310 if (mx->n_factors == 0)
1313 assert (nr->max_cell_idx >= cell);
1314 if (cell != nr->max_cell_idx)
1325 for (i = 0; i < mx->n_factors; i++)
1327 struct matrix_token token;
1328 if (!mget_token (&token, mx->reader))
1330 if (token.type != MNUM)
1332 msg (SE, _("Syntax error expecting factor value %s."),
1333 context (mx->reader));
1338 nr->factor_values[i + mx->n_factors * cell] = token.number;
1339 else if (nr->factor_values[i + mx->n_factors * cell] != token.number)
1341 msg (SE, _("Syntax error expecting value %g for %s %s."),
1342 nr->factor_values[i + mx->n_factors * cell],
1343 mx->factors[i]->name, context (mx->reader));
1352 /* Write the contents of a cell having content type CONTENT and data
1353 CP to the active file.
1354 Returns true if successful, false if an I/O error occurred. */
1356 dump_cell_content (struct matrix_data_pgm *mx, int content, double *cp,
1358 write_case_func *write_case, write_case_data wc_data)
1360 int type = content_type[content];
1363 buf_copy_str_rpad (case_data_rw (c, mx->rowtype_->fv)->s, 8,
1364 content_names[content]);
1367 memset (case_data_rw (c, mx->varname_->fv)->s, ' ', 8);
1371 int n_lines = (type == 1) ? mx->n_continuous : 1;
1374 for (i = 0; i < n_lines; i++)
1378 for (j = 0; j < mx->n_continuous; j++)
1380 int fv = dict_get_var (default_dict, mx->first_continuous + j)->fv;
1381 case_data_rw (c, fv)->f = *cp;
1385 buf_copy_str_rpad (case_data_rw (c, mx->varname_->fv)->s, 8,
1386 dict_get_var (default_dict,
1387 mx->first_continuous + i)->name);
1388 if (!write_case (wc_data))
1395 /* Finally dump out everything from nr_data[] to the output file. */
1397 nr_output_data (struct nr_aux_data *nr, struct ccase *c,
1398 write_case_func *write_case, write_case_data wc_data)
1400 struct matrix_data_pgm *mx = nr->mx;
1403 struct variable *const *split;
1407 split_cnt = dict_get_split_cnt (default_dict);
1408 split = dict_get_split_vars (default_dict);
1409 for (i = 0; i < split_cnt; i++)
1410 case_data_rw (c, split[i]->fv)->f = nr->split_values[i];
1417 for (cell = 0; cell < mx->cells; cell++)
1422 for (factor = 0; factor < mx->n_factors; factor++)
1423 case_data_rw (c, mx->factors[factor]->fv)->f
1424 = nr->factor_values[factor + cell * mx->n_factors];
1430 for (content = 0; content <= PROX; content++)
1431 if (mx->is_per_factor[content])
1433 assert (nr->data[content] != NULL
1434 && nr->data[content][cell] != NULL);
1436 if (!dump_cell_content (mx, content, nr->data[content][cell],
1437 c, write_case, wc_data))
1450 for (factor = 0; factor < mx->n_factors; factor++)
1451 case_data_rw (c, mx->factors[factor]->fv)->f = SYSMIS;
1454 for (content = 0; content <= PROX; content++)
1455 if (!mx->is_per_factor[content] && nr->data[content] != NULL)
1457 if (!dump_cell_content (mx, content, nr->data[content][0],
1458 c, write_case, wc_data))
1466 /* Back end, with ROWTYPE_. */
1468 /* All the data for one set of factor values. */
1472 int n_rows[PROX + 1];
1473 double *data[PROX + 1];
1474 struct factor_data *next;
1477 /* With ROWTYPE_ auxiliary data. */
1480 struct matrix_data_pgm *mx; /* MATRIX DATA program. */
1481 int content; /* Type of current row. */
1482 double *split_values; /* SPLIT FILE variable values. */
1483 struct factor_data *data; /* All the data. */
1484 struct factor_data *current; /* Current factor. */
1487 static int wr_read_splits (struct wr_aux_data *, struct ccase *,
1488 write_case_func *, write_case_data);
1489 static bool wr_output_data (struct wr_aux_data *, struct ccase *,
1490 write_case_func *, write_case_data);
1491 static int wr_read_rowtype (struct wr_aux_data *,
1492 const struct matrix_token *, struct dfm_reader *);
1493 static int wr_read_factors (struct wr_aux_data *);
1494 static int wr_read_indeps (struct wr_aux_data *);
1495 static bool matrix_data_read_with_rowtype (struct case_source *,
1500 /* When ROWTYPE_ appears in the data, reads the matrices and writes
1501 them to the output file.
1502 Returns true if successful, false if an I/O error occurred. */
1504 read_matrices_with_rowtype (struct matrix_data_pgm *mx)
1506 struct wr_aux_data wr;
1511 wr.split_values = NULL;
1516 proc_set_source (create_case_source (&matrix_data_with_rowtype_source_class,
1518 ok = procedure (NULL, NULL);
1520 free (wr.split_values);
1524 /* Read from the data file and write it to the active file.
1525 Returns true if successful, false if an I/O error occurred. */
1527 matrix_data_read_with_rowtype (struct case_source *source,
1529 write_case_func *write_case,
1530 write_case_data wc_data)
1532 struct wr_aux_data *wr = source->aux;
1533 struct matrix_data_pgm *mx = wr->mx;
1537 if (!wr_read_splits (wr, c, write_case, wc_data))
1540 if (!wr_read_factors (wr))
1543 if (!wr_read_indeps (wr))
1546 while (another_token (mx->reader));
1548 return wr_output_data (wr, c, write_case, wc_data);
1551 /* Read the split file variables. If they differ from the previous
1552 set of split variables then output the data. Returns success. */
1554 wr_read_splits (struct wr_aux_data *wr,
1556 write_case_func *write_case, write_case_data wc_data)
1558 struct matrix_data_pgm *mx = wr->mx;
1562 split_cnt = dict_get_split_cnt (default_dict);
1566 if (wr->split_values)
1571 wr->split_values = xnmalloc (split_cnt, sizeof *wr->split_values);
1578 for (i = 0; i < split_cnt; i++)
1580 struct matrix_token token;
1581 if (!mget_token (&token, mx->reader))
1583 if (token.type != MNUM)
1585 msg (SE, _("Syntax error %s expecting SPLIT FILE value."),
1586 context (mx->reader));
1590 if (compare && wr->split_values[i] != token.number && !different)
1592 if (!wr_output_data (wr, c, write_case, wc_data))
1597 wr->split_values[i] = token.number;
1604 /* Compares doubles A and B, treating SYSMIS as greatest. */
1606 compare_doubles (const void *a_, const void *b_, void *aux UNUSED)
1608 const double *a = a_;
1609 const double *b = b_;
1613 else if (*a == SYSMIS)
1615 else if (*b == SYSMIS)
1623 /* Return strcmp()-type comparison of the MX->n_factors factors at _A and
1624 _B. Sort missing values toward the end. */
1626 compare_factors (const void *a_, const void *b_, void *mx_)
1628 struct matrix_data_pgm *mx = mx_;
1629 struct factor_data *const *pa = a_;
1630 struct factor_data *const *pb = b_;
1631 const double *a = (*pa)->factors;
1632 const double *b = (*pb)->factors;
1634 return lexicographical_compare_3way (a, mx->n_factors,
1637 compare_doubles, NULL);
1640 /* Write out the data for the current split file to the active
1642 Returns true if successful, false if an I/O error occurred. */
1644 wr_output_data (struct wr_aux_data *wr,
1646 write_case_func *write_case, write_case_data wc_data)
1648 struct matrix_data_pgm *mx = wr->mx;
1652 struct variable *const *split;
1656 split_cnt = dict_get_split_cnt (default_dict);
1657 split = dict_get_split_vars (default_dict);
1658 for (i = 0; i < split_cnt; i++)
1659 case_data_rw (c, split[i]->fv)->f = wr->split_values[i];
1662 /* Sort the wr->data list. */
1664 struct factor_data **factors;
1665 struct factor_data *iter;
1668 factors = xnmalloc (mx->cells, sizeof *factors);
1670 for (i = 0, iter = wr->data; iter; iter = iter->next, i++)
1673 sort (factors, mx->cells, sizeof *factors, compare_factors, mx);
1675 wr->data = factors[0];
1676 for (i = 0; i < mx->cells - 1; i++)
1677 factors[i]->next = factors[i + 1];
1678 factors[mx->cells - 1]->next = NULL;
1683 /* Write out records for every set of factor values. */
1685 struct factor_data *iter;
1687 for (iter = wr->data; iter; iter = iter->next)
1692 for (factor = 0; factor < mx->n_factors; factor++)
1693 case_data_rw (c, mx->factors[factor]->fv)->f
1694 = iter->factors[factor];
1700 for (content = 0; content <= PROX; content++)
1702 if (!iter->n_rows[content])
1706 int type = content_type[content];
1707 int n_lines = (type == 1
1709 - (mx->section != FULL && mx->diag == NODIAGONAL))
1712 if (n_lines != iter->n_rows[content])
1714 msg (SE, _("Expected %d lines of data for %s content; "
1715 "actually saw %d lines. No data will be "
1716 "output for this content."),
1717 n_lines, content_names[content],
1718 iter->n_rows[content]);
1723 fill_matrix (mx, content, iter->data[content]);
1725 ok = dump_cell_content (mx, content, iter->data[content],
1726 c, write_case, wc_data);
1734 pool_destroy (mx->container);
1735 mx->container = pool_create ();
1737 wr->data = wr->current = NULL;
1742 /* Sets ROWTYPE_ based on the given TOKEN read from READER.
1745 wr_read_rowtype (struct wr_aux_data *wr,
1746 const struct matrix_token *token,
1747 struct dfm_reader *reader)
1749 if (wr->content != -1)
1751 msg (SE, _("Multiply specified ROWTYPE_ %s."), context (reader));
1754 if (token->type != MSTR)
1756 msg (SE, _("Syntax error %s expecting ROWTYPE_ string."),
1765 memcpy (s, token->string, min (15, token->length));
1766 s[min (15, token->length)] = 0;
1768 for (cp = s; *cp; cp++)
1769 *cp = toupper ((unsigned char) *cp);
1771 wr->content = string_to_content_type (s, NULL);
1774 if (wr->content == -1)
1776 msg (SE, _("Syntax error %s."), context (reader));
1783 /* Read the factors for the current row. Select a set of factors and
1784 point wr_current to it. */
1786 wr_read_factors (struct wr_aux_data *wr)
1788 struct matrix_data_pgm *mx = wr->mx;
1789 double *factor_values = local_alloc (sizeof *factor_values * mx->n_factors);
1795 for (i = 0; i < mx->n_factors; i++)
1797 struct matrix_token token;
1798 if (!mget_token (&token, mx->reader))
1800 if (token.type == MSTR)
1802 if (!wr_read_rowtype (wr, &token, mx->reader))
1804 if (!mget_token (&token, mx->reader))
1807 if (token.type != MNUM)
1809 msg (SE, _("Syntax error expecting factor value %s."),
1810 context (mx->reader));
1814 factor_values[i] = token.number;
1817 if (wr->content == -1)
1819 struct matrix_token token;
1820 if (!mget_token (&token, mx->reader))
1822 if (!wr_read_rowtype (wr, &token, mx->reader))
1826 /* Try the most recent factor first as a simple caching
1832 for (i = 0; i < mx->n_factors; i++)
1833 if (factor_values[i] != wr->current->factors[i])
1838 /* Linear search through the list. */
1841 struct factor_data *iter;
1843 for (iter = wr->data; iter; iter = iter->next)
1847 for (i = 0; i < mx->n_factors; i++)
1848 if (factor_values[i] != iter->factors[i])
1858 /* Not found. Make a new item. */
1860 struct factor_data *new = pool_alloc (mx->container, sizeof *new);
1862 new->factors = pool_nalloc (mx->container,
1863 mx->n_factors, sizeof *new->factors);
1868 for (i = 0; i < mx->n_factors; i++)
1869 new->factors[i] = factor_values[i];
1875 for (i = 0; i <= PROX; i++)
1878 new->data[i] = NULL;
1882 new->next = wr->data;
1883 wr->data = wr->current = new;
1888 local_free (factor_values);
1892 local_free (factor_values);
1896 /* Read the independent variables into wr->current. */
1898 wr_read_indeps (struct wr_aux_data *wr)
1900 struct matrix_data_pgm *mx = wr->mx;
1901 struct factor_data *c = wr->current;
1902 const int type = content_type[wr->content];
1903 const int n_rows = c->n_rows[wr->content];
1907 /* Allocate room for data if necessary. */
1908 if (c->data[wr->content] == NULL)
1910 int n_items = mx->n_continuous;
1912 n_items *= mx->n_continuous;
1914 c->data[wr->content] = pool_nalloc (mx->container,
1915 n_items, sizeof **c->data);
1918 cp = &c->data[wr->content][n_rows * mx->n_continuous];
1920 /* Figure out how much to read from this line. */
1927 msg (SE, _("Duplicate specification for %s."),
1928 content_names[wr->content]);
1932 n_cols = mx->n_continuous;
1937 if (n_rows >= mx->n_continuous - (mx->section != FULL && mx->diag == NODIAGONAL))
1939 msg (SE, _("Too many rows of matrix data for %s."),
1940 content_names[wr->content]);
1944 switch (mx->section)
1947 n_cols = n_rows + 1;
1948 if (mx->diag == NODIAGONAL)
1949 cp += mx->n_continuous;
1953 n_cols = mx->n_continuous - n_rows;
1954 if (mx->diag == NODIAGONAL)
1961 n_cols = mx->n_continuous;
1972 c->n_rows[wr->content]++;
1974 /* Read N_COLS items at CP. */
1978 for (j = 0; j < n_cols; j++)
1980 struct matrix_token token;
1981 if (!mget_token (&token, mx->reader))
1983 if (token.type != MNUM)
1985 msg (SE, _("Syntax error expecting value for %s %s."),
1986 dict_get_var (default_dict, mx->first_continuous + j)->name,
1987 context (mx->reader));
1991 *cp++ = token.number;
1994 && !force_eol (mx->reader, content_names[wr->content]))
2001 /* Matrix source. */
2003 static const struct case_source_class matrix_data_with_rowtype_source_class =
2007 matrix_data_read_with_rowtype,
2011 static const struct case_source_class
2012 matrix_data_without_rowtype_source_class =
2016 matrix_data_read_without_rowtype,