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/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 <libpspp/alloc.h>
36 #include <libpspp/array.h>
37 #include <libpspp/compiler.h>
38 #include <libpspp/message.h>
39 #include <libpspp/message.h>
40 #include <libpspp/misc.h>
41 #include <libpspp/pool.h>
42 #include <libpspp/str.h>
43 #include <procedure.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 vfm_source = create_case_source (&matrix_data_without_rowtype_source_class, &nr);
977 ok = procedure (NULL, NULL);
979 free (nr.split_values);
980 free (nr.factor_values);
985 /* Mirror data across the diagonal of matrix CP which contains
986 CONTENT type data. */
988 fill_matrix (struct matrix_data_pgm *mx, int content, double *cp)
990 int type = content_type[content];
992 if (type == 1 && mx->section != FULL)
994 if (mx->diag == NODIAGONAL)
996 const double fill = content == CORR ? 1.0 : SYSMIS;
999 for (i = 0; i < mx->n_continuous; i++)
1000 cp[i * (1 + mx->n_continuous)] = fill;
1006 if (mx->section == LOWER)
1008 int n_lines = mx->n_continuous;
1009 if (mx->section != FULL && mx->diag == NODIAGONAL)
1012 for (r = 1; r < n_lines; r++)
1013 for (c = 0; c < r; c++)
1014 cp[r + c * mx->n_continuous] = cp[c + r * mx->n_continuous];
1018 assert (mx->section == UPPER);
1019 for (r = 1; r < mx->n_continuous; r++)
1020 for (c = 0; c < r; c++)
1021 cp[c + r * mx->n_continuous] = cp[r + c * mx->n_continuous];
1029 for (c = 1; c < mx->n_continuous; c++)
1034 /* Read data lines for content type CONTENT from the data file.
1035 If PER_FACTOR is nonzero, then factor information is read from
1036 the data file. Data is for cell number CELL. */
1038 nr_read_data_lines (struct nr_aux_data *nr,
1039 int per_factor, int cell, int content, int compare)
1041 struct matrix_data_pgm *mx = nr->mx;
1042 const int type = content_type[content]; /* Content type. */
1043 int n_lines; /* Number of lines to parse from data file for this type. */
1044 double *cp; /* Current position in vector or matrix. */
1051 n_lines = mx->n_continuous;
1052 if (mx->section != FULL && mx->diag == NODIAGONAL)
1056 cp = nr->data[content][cell];
1057 if (type == 1 && mx->section == LOWER && mx->diag == NODIAGONAL)
1058 cp += mx->n_continuous;
1060 for (i = 0; i < n_lines; i++)
1064 if (!nr_read_splits (nr, 1))
1066 if (per_factor && !nr_read_factors (nr, cell))
1073 n_cols = mx->n_continuous;
1076 switch (mx->section)
1083 n_cols = mx->n_continuous - i;
1084 if (mx->diag == NODIAGONAL)
1091 n_cols = mx->n_continuous;
1109 for (j = 0; j < n_cols; j++)
1111 struct matrix_token token;
1112 if (!mget_token (&token, mx->reader))
1114 if (token.type != MNUM)
1116 msg (SE, _("expecting value for %s %s"),
1117 dict_get_var (default_dict, j)->name,
1118 context (mx->reader));
1122 *cp++ = token.number;
1125 && !force_eol (mx->reader, content_names[content]))
1129 if (mx->section == LOWER)
1130 cp += mx->n_continuous - n_cols;
1133 fill_matrix (mx, content, nr->data[content][cell]);
1138 /* When ROWTYPE_ does not appear in the data, reads the matrices and
1139 writes them to the output file.
1140 Returns true if successful, false if an I/O error occurred. */
1142 matrix_data_read_without_rowtype (struct case_source *source,
1144 write_case_func *write_case,
1145 write_case_data wc_data)
1147 struct nr_aux_data *nr = source->aux;
1148 struct matrix_data_pgm *mx = nr->mx;
1153 nr->data = pool_nalloc (mx->container, PROX + 1, sizeof *nr->data);
1158 for (i = 0; i <= PROX; i++)
1162 for (cp = mx->contents; *cp != EOC; cp++)
1163 if (*cp != LPAREN && *cp != RPAREN)
1165 int per_factor = mx->is_per_factor[*cp];
1168 n_entries = mx->n_continuous;
1169 if (content_type[*cp] == 1)
1170 n_entries *= mx->n_continuous;
1173 int n_vectors = per_factor ? mx->cells : 1;
1176 nr->data[*cp] = pool_nalloc (mx->container,
1177 n_vectors, sizeof **nr->data);
1179 for (i = 0; i < n_vectors; i++)
1180 nr->data[*cp][i] = pool_nalloc (mx->container,
1181 n_entries, sizeof ***nr->data);
1190 if (!nr_read_splits (nr, 0))
1193 for (bp = mx->contents; *bp != EOC; bp = np)
1197 /* Trap the CONTENTS that we should parse in this pass
1198 between bp and ep. Set np to the starting bp for next
1203 while (*ep != RPAREN)
1211 while (*ep != EOC && *ep != LPAREN)
1220 for (i = 0; i < (per_factor ? mx->cells : 1); i++)
1224 for (cp = bp; cp < ep; cp++)
1225 if (!nr_read_data_lines (nr, per_factor, i, *cp, cp != bp))
1231 if (!nr_output_data (nr, c, write_case, wc_data))
1234 if (dict_get_split_cnt (default_dict) == 0
1235 || !another_token (mx->reader))
1240 /* Read the split file variables. If COMPARE is 1, compares the
1241 values read to the last values read and returns 1 if they're equal,
1244 nr_read_splits (struct nr_aux_data *nr, int compare)
1246 struct matrix_data_pgm *mx = nr->mx;
1247 static int just_read = 0; /* FIXME: WTF? */
1251 if (compare && just_read)
1257 if (dict_get_split_vars (default_dict) == NULL)
1260 if (mx->single_split)
1264 struct mxd_var *mv = dict_get_split_vars (default_dict)[0]->aux;
1265 nr->split_values[0] = ++mv->sub_type;
1273 split_cnt = dict_get_split_cnt (default_dict);
1274 for (i = 0; i < split_cnt; i++)
1276 struct matrix_token token;
1277 if (!mget_token (&token, mx->reader))
1279 if (token.type != MNUM)
1281 msg (SE, _("Syntax error expecting SPLIT FILE value %s."),
1282 context (mx->reader));
1287 nr->split_values[i] = token.number;
1288 else if (nr->split_values[i] != token.number)
1290 msg (SE, _("Expecting value %g for %s."),
1291 nr->split_values[i],
1292 dict_get_split_vars (default_dict)[i]->name);
1300 /* Read the factors for cell CELL. If COMPARE is 1, compares the
1301 values read to the last values read and returns 1 if they're equal,
1304 nr_read_factors (struct nr_aux_data *nr, int cell)
1306 struct matrix_data_pgm *mx = nr->mx;
1309 if (mx->n_factors == 0)
1312 assert (nr->max_cell_idx >= cell);
1313 if (cell != nr->max_cell_idx)
1324 for (i = 0; i < mx->n_factors; i++)
1326 struct matrix_token token;
1327 if (!mget_token (&token, mx->reader))
1329 if (token.type != MNUM)
1331 msg (SE, _("Syntax error expecting factor value %s."),
1332 context (mx->reader));
1337 nr->factor_values[i + mx->n_factors * cell] = token.number;
1338 else if (nr->factor_values[i + mx->n_factors * cell] != token.number)
1340 msg (SE, _("Syntax error expecting value %g for %s %s."),
1341 nr->factor_values[i + mx->n_factors * cell],
1342 mx->factors[i]->name, context (mx->reader));
1351 /* Write the contents of a cell having content type CONTENT and data
1352 CP to the active file.
1353 Returns true if successful, false if an I/O error occurred. */
1355 dump_cell_content (struct matrix_data_pgm *mx, int content, double *cp,
1357 write_case_func *write_case, write_case_data wc_data)
1359 int type = content_type[content];
1362 buf_copy_str_rpad (case_data_rw (c, mx->rowtype_->fv)->s, 8,
1363 content_names[content]);
1366 memset (case_data_rw (c, mx->varname_->fv)->s, ' ', 8);
1370 int n_lines = (type == 1) ? mx->n_continuous : 1;
1373 for (i = 0; i < n_lines; i++)
1377 for (j = 0; j < mx->n_continuous; j++)
1379 int fv = dict_get_var (default_dict, mx->first_continuous + j)->fv;
1380 case_data_rw (c, fv)->f = *cp;
1384 buf_copy_str_rpad (case_data_rw (c, mx->varname_->fv)->s, 8,
1385 dict_get_var (default_dict,
1386 mx->first_continuous + i)->name);
1387 if (!write_case (wc_data))
1394 /* Finally dump out everything from nr_data[] to the output file. */
1396 nr_output_data (struct nr_aux_data *nr, struct ccase *c,
1397 write_case_func *write_case, write_case_data wc_data)
1399 struct matrix_data_pgm *mx = nr->mx;
1402 struct variable *const *split;
1406 split_cnt = dict_get_split_cnt (default_dict);
1407 split = dict_get_split_vars (default_dict);
1408 for (i = 0; i < split_cnt; i++)
1409 case_data_rw (c, split[i]->fv)->f = nr->split_values[i];
1416 for (cell = 0; cell < mx->cells; cell++)
1421 for (factor = 0; factor < mx->n_factors; factor++)
1422 case_data_rw (c, mx->factors[factor]->fv)->f
1423 = nr->factor_values[factor + cell * mx->n_factors];
1429 for (content = 0; content <= PROX; content++)
1430 if (mx->is_per_factor[content])
1432 assert (nr->data[content] != NULL
1433 && nr->data[content][cell] != NULL);
1435 if (!dump_cell_content (mx, content, nr->data[content][cell],
1436 c, write_case, wc_data))
1449 for (factor = 0; factor < mx->n_factors; factor++)
1450 case_data_rw (c, mx->factors[factor]->fv)->f = SYSMIS;
1453 for (content = 0; content <= PROX; content++)
1454 if (!mx->is_per_factor[content] && nr->data[content] != NULL)
1456 if (!dump_cell_content (mx, content, nr->data[content][0],
1457 c, write_case, wc_data))
1465 /* Back end, with ROWTYPE_. */
1467 /* All the data for one set of factor values. */
1471 int n_rows[PROX + 1];
1472 double *data[PROX + 1];
1473 struct factor_data *next;
1476 /* With ROWTYPE_ auxiliary data. */
1479 struct matrix_data_pgm *mx; /* MATRIX DATA program. */
1480 int content; /* Type of current row. */
1481 double *split_values; /* SPLIT FILE variable values. */
1482 struct factor_data *data; /* All the data. */
1483 struct factor_data *current; /* Current factor. */
1486 static int wr_read_splits (struct wr_aux_data *, struct ccase *,
1487 write_case_func *, write_case_data);
1488 static bool wr_output_data (struct wr_aux_data *, struct ccase *,
1489 write_case_func *, write_case_data);
1490 static int wr_read_rowtype (struct wr_aux_data *,
1491 const struct matrix_token *, struct dfm_reader *);
1492 static int wr_read_factors (struct wr_aux_data *);
1493 static int wr_read_indeps (struct wr_aux_data *);
1494 static bool matrix_data_read_with_rowtype (struct case_source *,
1499 /* When ROWTYPE_ appears in the data, reads the matrices and writes
1500 them to the output file.
1501 Returns true if successful, false if an I/O error occurred. */
1503 read_matrices_with_rowtype (struct matrix_data_pgm *mx)
1505 struct wr_aux_data wr;
1510 wr.split_values = NULL;
1515 vfm_source = create_case_source (&matrix_data_with_rowtype_source_class,
1517 ok = procedure (NULL, NULL);
1519 free (wr.split_values);
1523 /* Read from the data file and write it to the active file.
1524 Returns true if successful, false if an I/O error occurred. */
1526 matrix_data_read_with_rowtype (struct case_source *source,
1528 write_case_func *write_case,
1529 write_case_data wc_data)
1531 struct wr_aux_data *wr = source->aux;
1532 struct matrix_data_pgm *mx = wr->mx;
1536 if (!wr_read_splits (wr, c, write_case, wc_data))
1539 if (!wr_read_factors (wr))
1542 if (!wr_read_indeps (wr))
1545 while (another_token (mx->reader));
1547 return wr_output_data (wr, c, write_case, wc_data);
1550 /* Read the split file variables. If they differ from the previous
1551 set of split variables then output the data. Returns success. */
1553 wr_read_splits (struct wr_aux_data *wr,
1555 write_case_func *write_case, write_case_data wc_data)
1557 struct matrix_data_pgm *mx = wr->mx;
1561 split_cnt = dict_get_split_cnt (default_dict);
1565 if (wr->split_values)
1570 wr->split_values = xnmalloc (split_cnt, sizeof *wr->split_values);
1577 for (i = 0; i < split_cnt; i++)
1579 struct matrix_token token;
1580 if (!mget_token (&token, mx->reader))
1582 if (token.type != MNUM)
1584 msg (SE, _("Syntax error %s expecting SPLIT FILE value."),
1585 context (mx->reader));
1589 if (compare && wr->split_values[i] != token.number && !different)
1591 if (!wr_output_data (wr, c, write_case, wc_data))
1596 wr->split_values[i] = token.number;
1603 /* Compares doubles A and B, treating SYSMIS as greatest. */
1605 compare_doubles (const void *a_, const void *b_, void *aux UNUSED)
1607 const double *a = a_;
1608 const double *b = b_;
1612 else if (*a == SYSMIS)
1614 else if (*b == SYSMIS)
1622 /* Return strcmp()-type comparison of the MX->n_factors factors at _A and
1623 _B. Sort missing values toward the end. */
1625 compare_factors (const void *a_, const void *b_, void *mx_)
1627 struct matrix_data_pgm *mx = mx_;
1628 struct factor_data *const *pa = a_;
1629 struct factor_data *const *pb = b_;
1630 const double *a = (*pa)->factors;
1631 const double *b = (*pb)->factors;
1633 return lexicographical_compare_3way (a, mx->n_factors,
1636 compare_doubles, NULL);
1639 /* Write out the data for the current split file to the active
1641 Returns true if successful, false if an I/O error occurred. */
1643 wr_output_data (struct wr_aux_data *wr,
1645 write_case_func *write_case, write_case_data wc_data)
1647 struct matrix_data_pgm *mx = wr->mx;
1651 struct variable *const *split;
1655 split_cnt = dict_get_split_cnt (default_dict);
1656 split = dict_get_split_vars (default_dict);
1657 for (i = 0; i < split_cnt; i++)
1658 case_data_rw (c, split[i]->fv)->f = wr->split_values[i];
1661 /* Sort the wr->data list. */
1663 struct factor_data **factors;
1664 struct factor_data *iter;
1667 factors = xnmalloc (mx->cells, sizeof *factors);
1669 for (i = 0, iter = wr->data; iter; iter = iter->next, i++)
1672 sort (factors, mx->cells, sizeof *factors, compare_factors, mx);
1674 wr->data = factors[0];
1675 for (i = 0; i < mx->cells - 1; i++)
1676 factors[i]->next = factors[i + 1];
1677 factors[mx->cells - 1]->next = NULL;
1682 /* Write out records for every set of factor values. */
1684 struct factor_data *iter;
1686 for (iter = wr->data; iter; iter = iter->next)
1691 for (factor = 0; factor < mx->n_factors; factor++)
1692 case_data_rw (c, mx->factors[factor]->fv)->f
1693 = iter->factors[factor];
1699 for (content = 0; content <= PROX; content++)
1701 if (!iter->n_rows[content])
1705 int type = content_type[content];
1706 int n_lines = (type == 1
1708 - (mx->section != FULL && mx->diag == NODIAGONAL))
1711 if (n_lines != iter->n_rows[content])
1713 msg (SE, _("Expected %d lines of data for %s content; "
1714 "actually saw %d lines. No data will be "
1715 "output for this content."),
1716 n_lines, content_names[content],
1717 iter->n_rows[content]);
1722 fill_matrix (mx, content, iter->data[content]);
1724 ok = dump_cell_content (mx, content, iter->data[content],
1725 c, write_case, wc_data);
1733 pool_destroy (mx->container);
1734 mx->container = pool_create ();
1736 wr->data = wr->current = NULL;
1741 /* Sets ROWTYPE_ based on the given TOKEN read from READER.
1744 wr_read_rowtype (struct wr_aux_data *wr,
1745 const struct matrix_token *token,
1746 struct dfm_reader *reader)
1748 if (wr->content != -1)
1750 msg (SE, _("Multiply specified ROWTYPE_ %s."), context (reader));
1753 if (token->type != MSTR)
1755 msg (SE, _("Syntax error %s expecting ROWTYPE_ string."),
1764 memcpy (s, token->string, min (15, token->length));
1765 s[min (15, token->length)] = 0;
1767 for (cp = s; *cp; cp++)
1768 *cp = toupper ((unsigned char) *cp);
1770 wr->content = string_to_content_type (s, NULL);
1773 if (wr->content == -1)
1775 msg (SE, _("Syntax error %s."), context (reader));
1782 /* Read the factors for the current row. Select a set of factors and
1783 point wr_current to it. */
1785 wr_read_factors (struct wr_aux_data *wr)
1787 struct matrix_data_pgm *mx = wr->mx;
1788 double *factor_values = local_alloc (sizeof *factor_values * mx->n_factors);
1794 for (i = 0; i < mx->n_factors; i++)
1796 struct matrix_token token;
1797 if (!mget_token (&token, mx->reader))
1799 if (token.type == MSTR)
1801 if (!wr_read_rowtype (wr, &token, mx->reader))
1803 if (!mget_token (&token, mx->reader))
1806 if (token.type != MNUM)
1808 msg (SE, _("Syntax error expecting factor value %s."),
1809 context (mx->reader));
1813 factor_values[i] = token.number;
1816 if (wr->content == -1)
1818 struct matrix_token token;
1819 if (!mget_token (&token, mx->reader))
1821 if (!wr_read_rowtype (wr, &token, mx->reader))
1825 /* Try the most recent factor first as a simple caching
1831 for (i = 0; i < mx->n_factors; i++)
1832 if (factor_values[i] != wr->current->factors[i])
1837 /* Linear search through the list. */
1840 struct factor_data *iter;
1842 for (iter = wr->data; iter; iter = iter->next)
1846 for (i = 0; i < mx->n_factors; i++)
1847 if (factor_values[i] != iter->factors[i])
1857 /* Not found. Make a new item. */
1859 struct factor_data *new = pool_alloc (mx->container, sizeof *new);
1861 new->factors = pool_nalloc (mx->container,
1862 mx->n_factors, sizeof *new->factors);
1867 for (i = 0; i < mx->n_factors; i++)
1868 new->factors[i] = factor_values[i];
1874 for (i = 0; i <= PROX; i++)
1877 new->data[i] = NULL;
1881 new->next = wr->data;
1882 wr->data = wr->current = new;
1887 local_free (factor_values);
1891 local_free (factor_values);
1895 /* Read the independent variables into wr->current. */
1897 wr_read_indeps (struct wr_aux_data *wr)
1899 struct matrix_data_pgm *mx = wr->mx;
1900 struct factor_data *c = wr->current;
1901 const int type = content_type[wr->content];
1902 const int n_rows = c->n_rows[wr->content];
1906 /* Allocate room for data if necessary. */
1907 if (c->data[wr->content] == NULL)
1909 int n_items = mx->n_continuous;
1911 n_items *= mx->n_continuous;
1913 c->data[wr->content] = pool_nalloc (mx->container,
1914 n_items, sizeof **c->data);
1917 cp = &c->data[wr->content][n_rows * mx->n_continuous];
1919 /* Figure out how much to read from this line. */
1926 msg (SE, _("Duplicate specification for %s."),
1927 content_names[wr->content]);
1931 n_cols = mx->n_continuous;
1936 if (n_rows >= mx->n_continuous - (mx->section != FULL && mx->diag == NODIAGONAL))
1938 msg (SE, _("Too many rows of matrix data for %s."),
1939 content_names[wr->content]);
1943 switch (mx->section)
1946 n_cols = n_rows + 1;
1947 if (mx->diag == NODIAGONAL)
1948 cp += mx->n_continuous;
1952 n_cols = mx->n_continuous - n_rows;
1953 if (mx->diag == NODIAGONAL)
1960 n_cols = mx->n_continuous;
1971 c->n_rows[wr->content]++;
1973 /* Read N_COLS items at CP. */
1977 for (j = 0; j < n_cols; j++)
1979 struct matrix_token token;
1980 if (!mget_token (&token, mx->reader))
1982 if (token.type != MNUM)
1984 msg (SE, _("Syntax error expecting value for %s %s."),
1985 dict_get_var (default_dict, mx->first_continuous + j)->name,
1986 context (mx->reader));
1990 *cp++ = token.number;
1993 && !force_eol (mx->reader, content_names[wr->content]))
2000 /* Matrix source. */
2002 static const struct case_source_class matrix_data_with_rowtype_source_class =
2006 matrix_data_read_with_rowtype,
2010 static const struct case_source_class
2011 matrix_data_without_rowtype_source_class =
2015 matrix_data_read_without_rowtype,