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
31 #include "data-reader.h"
32 #include "dictionary.h"
34 #include "file-handle.h"
40 #include "procedure.h"
43 #define _(msgid) gettext (msgid)
45 #include "debug-print.h"
47 /* FIXME: /N subcommand not implemented. It should be pretty simple,
50 /* Different types of variables for MATRIX DATA procedure. Order is
51 important: these are used for sort keys. */
54 MXD_SPLIT, /* SPLIT FILE variables. */
55 MXD_ROWTYPE, /* ROWTYPE_. */
56 MXD_FACTOR, /* Factor variables. */
57 MXD_VARNAME, /* VARNAME_. */
58 MXD_CONTINUOUS, /* Continuous variables. */
63 /* Format type enums. */
70 /* Matrix section enums. */
78 /* Diagonal inclusion enums. */
106 /* 0=vector, 1=matrix, 2=scalar. */
107 static const int content_type[PROX + 1] =
109 0, 2, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1,
112 /* Name of each content type. */
113 static const char *content_names[PROX + 1] =
115 "N", "N", "N_MATRIX", "MEAN", "STDDEV", "COUNT", "MSE",
116 "DFE", "MAT", "COV", "CORR", "PROX",
119 /* A MATRIX DATA input program. */
120 struct matrix_data_pgm
122 struct pool *container; /* Arena used for all allocations. */
123 struct dfm_reader *reader; /* Data file to read. */
126 enum format_type fmt; /* LIST or FREE. */
127 enum matrix_section section;/* LOWER or UPPER or FULL. */
128 enum include_diagonal diag; /* DIAGONAL or NODIAGONAL. */
130 int explicit_rowtype; /* ROWTYPE_ specified explicitly in data? */
131 struct variable *rowtype_, *varname_; /* ROWTYPE_, VARNAME_ variables. */
133 struct variable *single_split; /* Single SPLIT FILE variable. */
135 /* Factor variables. */
136 size_t n_factors; /* Number of factor variables. */
137 struct variable **factors; /* Factor variables. */
138 int is_per_factor[PROX + 1]; /* Is there per-factor data? */
140 int cells; /* Number of cells, or -1 if none. */
142 int pop_n; /* Population N specified by user. */
144 /* CONTENTS subcommand. */
145 int contents[EOC * 3 + 1]; /* Contents. */
146 int n_contents; /* Number of entries. */
148 /* Continuous variables. */
149 int n_continuous; /* Number of continuous variables. */
150 int first_continuous; /* Index into default_dict.var of
151 first continuous variable. */
154 /* Auxiliary data attached to MATRIX DATA variables. */
157 int var_type; /* Variable type. */
158 int sub_type; /* Subtype. */
161 static const struct case_source_class matrix_data_with_rowtype_source_class;
162 static const struct case_source_class matrix_data_without_rowtype_source_class;
164 static int compare_variables_by_mxd_var_type (const void *pa,
166 static bool read_matrices_without_rowtype (struct matrix_data_pgm *);
167 static bool read_matrices_with_rowtype (struct matrix_data_pgm *);
168 static int string_to_content_type (char *, int *);
169 static void attach_mxd_aux (struct variable *, int var_type, int sub_type);
172 cmd_matrix_data (void)
175 struct matrix_data_pgm *mx;
176 struct file_handle *fh = fh_inline_file ();
181 discard_variables ();
183 pool = pool_create ();
184 mx = pool_alloc (pool, sizeof *mx);
185 mx->container = pool;
190 mx->explicit_rowtype = 0;
193 mx->single_split = NULL;
196 memset (mx->is_per_factor, 0, sizeof mx->is_per_factor);
200 mx->n_continuous = 0;
201 mx->first_continuous = 0;
206 if (lex_match_id ("VARIABLES"))
213 msg (SE, _("VARIABLES subcommand multiply specified."));
219 if (!parse_DATA_LIST_vars (&v, &nv, PV_NO_DUPLICATE))
225 for (i = 0; i < nv; i++)
226 if (!strcasecmp (v[i], "VARNAME_"))
228 msg (SE, _("VARNAME_ cannot be explicitly specified on "
230 for (i = 0; i < nv; i++)
240 for (i = 0; i < nv; i++)
242 struct variable *new_var;
244 if (strcasecmp (v[i], "ROWTYPE_"))
246 new_var = dict_create_var_assert (default_dict, v[i], 0);
247 attach_mxd_aux (new_var, MXD_CONTINUOUS, i);
250 mx->explicit_rowtype = 1;
256 mx->rowtype_ = dict_create_var_assert (default_dict,
258 attach_mxd_aux (mx->rowtype_, MXD_ROWTYPE, 0);
260 else if (lex_match_id ("FILE"))
263 fh = fh_parse (FH_REF_FILE | FH_REF_INLINE);
267 else if (lex_match_id ("FORMAT"))
271 while (token == T_ID)
273 if (lex_match_id ("LIST"))
275 else if (lex_match_id ("FREE"))
277 else if (lex_match_id ("LOWER"))
279 else if (lex_match_id ("UPPER"))
281 else if (lex_match_id ("FULL"))
283 else if (lex_match_id ("DIAGONAL"))
285 else if (lex_match_id ("NODIAGONAL"))
286 mx->diag = NODIAGONAL;
289 lex_error (_("in FORMAT subcommand"));
294 else if (lex_match_id ("SPLIT"))
300 msg (SE, _("SPLIT subcommand multiply specified."));
307 lex_error (_("in SPLIT subcommand"));
311 if (dict_lookup_var (default_dict, tokid) == NULL
312 && (lex_look_ahead () == '.' || lex_look_ahead () == '/'))
314 if (!strcasecmp (tokid, "ROWTYPE_")
315 || !strcasecmp (tokid, "VARNAME_"))
317 msg (SE, _("Split variable may not be named ROWTYPE_ "
322 mx->single_split = dict_create_var_assert (default_dict,
324 attach_mxd_aux (mx->single_split, MXD_CONTINUOUS, 0);
327 dict_set_split_vars (default_dict, &mx->single_split, 1);
331 struct variable **split;
334 if (!parse_variables (default_dict, &split, &n, PV_NO_DUPLICATE))
337 dict_set_split_vars (default_dict, split, n);
341 struct variable *const *split = dict_get_split_vars (default_dict);
342 size_t split_cnt = dict_get_split_cnt (default_dict);
345 for (i = 0; i < split_cnt; i++)
347 struct mxd_var *mv = split[i]->aux;
349 if (mv->var_type != MXD_CONTINUOUS)
351 msg (SE, _("Split variable %s is already another type."),
355 var_clear_aux (split[i]);
356 attach_mxd_aux (split[i], MXD_SPLIT, i);
360 else if (lex_match_id ("FACTORS"))
366 msg (SE, _("FACTORS subcommand multiply specified."));
371 if (!parse_variables (default_dict, &mx->factors, &mx->n_factors,
378 for (i = 0; i < mx->n_factors; i++)
380 struct variable *v = mx->factors[i];
381 struct mxd_var *mv = v->aux;
383 if (mv->var_type != MXD_CONTINUOUS)
385 msg (SE, _("Factor variable %s is already another type."),
390 attach_mxd_aux (v, MXD_FACTOR, i);
394 else if (lex_match_id ("CELLS"))
400 msg (SE, _("CELLS subcommand multiply specified."));
404 if (!lex_is_integer () || lex_integer () < 1)
406 lex_error (_("expecting positive integer"));
410 mx->cells = lex_integer ();
413 else if (lex_match_id ("N"))
419 msg (SE, _("N subcommand multiply specified."));
423 if (!lex_is_integer () || lex_integer () < 1)
425 lex_error (_("expecting positive integer"));
429 mx->pop_n = lex_integer ();
432 else if (lex_match_id ("CONTENTS"))
434 int inside_parens = 0;
435 unsigned collide = 0;
440 msg (SE, _("CONTENTS subcommand multiply specified."));
450 for (i = 0; i <= PROX; i++)
451 mx->is_per_factor[i] = 0;
460 msg (SE, _("Nested parentheses not allowed."));
466 else if (lex_match (')'))
470 msg (SE, _("Mismatched right parenthesis (`(')."));
473 if (mx->contents[mx->n_contents - 1] == LPAREN)
475 msg (SE, _("Empty parentheses not allowed."));
488 lex_error (_("in CONTENTS subcommand"));
492 content_type = string_to_content_type (tokid,
494 if (content_type == -1)
496 lex_error (_("in CONTENTS subcommand"));
501 if (collide & (1 << collide_index))
503 msg (SE, _("Content multiply specified for %s."),
504 content_names[content_type]);
507 collide |= (1 << collide_index);
510 mx->is_per_factor[item] = inside_parens;
512 mx->contents[mx->n_contents++] = item;
514 if (token == '/' || token == '.')
520 msg (SE, _("Missing right parenthesis."));
523 mx->contents[mx->n_contents] = EOC;
534 lex_error (_("expecting end of command"));
540 msg (SE, _("Missing VARIABLES subcommand."));
544 if (!mx->n_contents && !mx->explicit_rowtype)
546 msg (SW, _("CONTENTS subcommand not specified: assuming file "
547 "contains only CORR matrix."));
549 mx->contents[0] = CORR;
550 mx->contents[1] = EOC;
554 if (mx->n_factors && !mx->explicit_rowtype && mx->cells == -1)
556 msg (SE, _("Missing CELLS subcommand. CELLS is required "
557 "when ROWTYPE_ is not given in the data and "
558 "factors are present."));
562 if (mx->explicit_rowtype && mx->single_split)
564 msg (SE, _("Split file values must be present in the data when "
565 "ROWTYPE_ is present."));
569 /* Create VARNAME_. */
570 mx->varname_ = dict_create_var_assert (default_dict, "VARNAME_", 8);
571 attach_mxd_aux (mx->varname_, MXD_VARNAME, 0);
573 /* Sort the dictionary variables into the desired order for the
574 system file output. */
579 dict_get_vars (default_dict, &v, &nv, 0);
580 qsort (v, nv, sizeof *v, compare_variables_by_mxd_var_type);
581 dict_reorder_vars (default_dict, v, nv);
587 static const struct fmt_spec fmt_tab[MXD_COUNT] =
598 mx->first_continuous = -1;
599 for (i = 0; i < dict_get_var_cnt (default_dict); i++)
601 struct variable *v = dict_get_var (default_dict, i);
602 struct mxd_var *mv = v->aux;
603 int type = mv->var_type;
605 assert (type >= 0 && type < MXD_COUNT);
606 v->print = v->write = fmt_tab[type];
608 if (type == MXD_CONTINUOUS)
610 if (mx->first_continuous == -1 && type == MXD_CONTINUOUS)
611 mx->first_continuous = i;
615 if (mx->n_continuous == 0)
617 msg (SE, _("No continuous variables specified."));
621 mx->reader = dfm_open_reader (fh);
622 if (mx->reader == NULL)
625 if (mx->explicit_rowtype)
626 ok = read_matrices_with_rowtype (mx);
628 ok = read_matrices_without_rowtype (mx);
630 dfm_close_reader (mx->reader);
632 pool_destroy (mx->container);
634 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
637 discard_variables ();
639 pool_destroy (mx->container);
640 return CMD_CASCADING_FAILURE;
643 /* Look up string S as a content-type name and return the
644 corresponding enumerated value, or -1 if there is no match. If
645 COLLIDE is non-NULL then *COLLIDE returns a value (suitable for use
646 as a bit-index) which can be used for determining whether a related
647 statistic has already been used. */
649 string_to_content_type (char *s, int *collide)
660 {N_VECTOR, 0, "N_VECTOR"},
662 {N_SCALAR, 0, "N_SCALAR"},
663 {N_MATRIX, 1, "N_MATRIX"},
665 {STDDEV, 3, "STDDEV"},
677 for (tp = tab; tp->value != -1; tp++)
678 if (!strcasecmp (s, tp->string))
681 *collide = tp->collide;
688 /* Compare two variables using p.mxd.var_type and p.mxd.sub_type
691 compare_variables_by_mxd_var_type (const void *a_, const void *b_)
693 struct variable *const *pa = a_;
694 struct variable *const *pb = b_;
695 const struct mxd_var *a = (*pa)->aux;
696 const struct mxd_var *b = (*pb)->aux;
698 if (a->var_type != b->var_type)
699 return a->var_type > b->var_type ? 1 : -1;
701 return a->sub_type < b->sub_type ? -1 : a->sub_type > b->sub_type;
704 /* Attaches a struct mxd_var with the specific member values to
707 attach_mxd_aux (struct variable *v, int var_type, int sub_type)
711 assert (v->aux == NULL);
712 mv = xmalloc (sizeof *mv);
713 mv->var_type = var_type;
714 mv->sub_type = sub_type;
715 var_attach_aux (v, mv, var_dtor_free);
718 /* Matrix tokenizer. */
720 /* Matrix token types. */
721 enum matrix_token_type
727 /* A MATRIX DATA parsing token. */
730 enum matrix_token_type type;
731 double number; /* MNUM: token value. */
732 char *string; /* MSTR: token string; not null-terminated. */
733 int length; /* MSTR: tokstr length. */
736 static int mget_token (struct matrix_token *, struct dfm_reader *);
739 #define mget_token(TOKEN, READER) mget_token_dump(TOKEN, READER)
742 mdump_token (const struct matrix_token *token)
747 printf (" #%g", token->number);
750 printf (" '%.*s'", token->length, token->string);
759 mget_token_dump (struct matrix_token *token, struct dfm_reader *reader)
761 int result = (mget_token) (token, reader);
767 /* Return the current position in READER. */
769 context (struct dfm_reader *reader)
773 if (dfm_eof (reader))
774 strcpy (buf, "at end of file");
777 struct fixed_string line;
780 dfm_get_record (reader, &line);
781 sp = ls_c_str (&line);
782 while (sp < ls_end (&line) && isspace ((unsigned char) *sp))
784 if (sp >= ls_end (&line))
785 strcpy (buf, "at end of line");
791 dp = stpcpy (buf, "before `");
792 while (sp < ls_end (&line) && !isspace ((unsigned char) *sp)
805 /* Is there at least one token left in the data file? */
807 another_token (struct dfm_reader *reader)
811 struct fixed_string line;
814 if (dfm_eof (reader))
816 dfm_get_record (reader, &line);
818 cp = ls_c_str (&line);
819 while (isspace ((unsigned char) *cp) && cp < ls_end (&line))
822 if (cp < ls_end (&line))
824 dfm_forward_columns (reader, cp - ls_c_str (&line));
828 dfm_forward_record (reader);
832 /* Parse a MATRIX DATA token from READER into TOKEN. */
834 (mget_token) (struct matrix_token *token, struct dfm_reader *reader)
836 struct fixed_string line;
840 if (!another_token (reader))
843 dfm_get_record (reader, &line);
844 first_column = dfm_column_start (reader);
846 /* Three types of fields: quoted with ', quoted with ", unquoted. */
847 cp = ls_c_str (&line);
848 if (*cp == '\'' || *cp == '"')
853 token->string = ++cp;
854 while (cp < ls_end (&line) && *cp != quote)
856 token->length = cp - token->string;
857 if (cp < ls_end (&line))
860 msg (SW, _("Scope of string exceeds line."));
864 int is_num = isdigit ((unsigned char) *cp) || *cp == '.';
866 token->string = cp++;
867 while (cp < ls_end (&line)
868 && !isspace ((unsigned char) *cp) && *cp != ','
869 && *cp != '-' && *cp != '+')
871 if (isdigit ((unsigned char) *cp))
874 if ((tolower ((unsigned char) *cp) == 'd'
875 || tolower ((unsigned char) *cp) == 'e')
876 && (cp[1] == '+' || cp[1] == '-'))
882 token->length = cp - token->string;
883 assert (token->length);
889 di.s = token->string;
890 di.e = token->string + token->length;
891 di.v = (union value *) &token->number;
892 di.f1 = first_column;
893 di.format = make_output_format (FMT_F, token->length, 0);
902 dfm_forward_columns (reader, cp - ls_c_str (&line));
907 /* Forcibly skip the end of a line for content type CONTENT in
910 force_eol (struct dfm_reader *reader, const char *content)
912 struct fixed_string line;
915 if (dfm_eof (reader))
917 dfm_get_record (reader, &line);
919 cp = ls_c_str (&line);
920 while (isspace ((unsigned char) *cp) && cp < ls_end (&line))
923 if (cp < ls_end (&line))
925 msg (SE, _("End of line expected %s while reading %s."),
926 context (reader), content);
930 dfm_forward_record (reader);
934 /* Back end, omitting ROWTYPE_. */
938 struct matrix_data_pgm *mx; /* MATRIX DATA program. */
939 double ***data; /* MATRIX DATA data. */
940 double *factor_values; /* Factor values. */
941 int max_cell_idx; /* Max-numbered cell that we have
942 read so far, plus one. */
943 double *split_values; /* SPLIT FILE variable values. */
946 static int nr_read_splits (struct nr_aux_data *, int compare);
947 static int nr_read_factors (struct nr_aux_data *, int cell);
948 static bool nr_output_data (struct nr_aux_data *, struct ccase *,
949 write_case_func *, write_case_data);
950 static bool matrix_data_read_without_rowtype (struct case_source *source,
955 /* Read from the data file and write it to the active file.
956 Returns true if successful, false if an I/O error occurred. */
958 read_matrices_without_rowtype (struct matrix_data_pgm *mx)
960 struct nr_aux_data nr;
968 nr.factor_values = xnmalloc (mx->n_factors * mx->cells,
969 sizeof *nr.factor_values);
971 nr.split_values = xnmalloc (dict_get_split_cnt (default_dict),
972 sizeof *nr.split_values);
974 vfm_source = create_case_source (&matrix_data_without_rowtype_source_class, &nr);
976 ok = procedure (NULL, NULL);
978 free (nr.split_values);
979 free (nr.factor_values);
984 /* Mirror data across the diagonal of matrix CP which contains
985 CONTENT type data. */
987 fill_matrix (struct matrix_data_pgm *mx, int content, double *cp)
989 int type = content_type[content];
991 if (type == 1 && mx->section != FULL)
993 if (mx->diag == NODIAGONAL)
995 const double fill = content == CORR ? 1.0 : SYSMIS;
998 for (i = 0; i < mx->n_continuous; i++)
999 cp[i * (1 + mx->n_continuous)] = fill;
1005 if (mx->section == LOWER)
1007 int n_lines = mx->n_continuous;
1008 if (mx->section != FULL && mx->diag == NODIAGONAL)
1011 for (r = 1; r < n_lines; r++)
1012 for (c = 0; c < r; c++)
1013 cp[r + c * mx->n_continuous] = cp[c + r * mx->n_continuous];
1017 assert (mx->section == UPPER);
1018 for (r = 1; r < mx->n_continuous; r++)
1019 for (c = 0; c < r; c++)
1020 cp[c + r * mx->n_continuous] = cp[r + c * mx->n_continuous];
1028 for (c = 1; c < mx->n_continuous; c++)
1033 /* Read data lines for content type CONTENT from the data file.
1034 If PER_FACTOR is nonzero, then factor information is read from
1035 the data file. Data is for cell number CELL. */
1037 nr_read_data_lines (struct nr_aux_data *nr,
1038 int per_factor, int cell, int content, int compare)
1040 struct matrix_data_pgm *mx = nr->mx;
1041 const int type = content_type[content]; /* Content type. */
1042 int n_lines; /* Number of lines to parse from data file for this type. */
1043 double *cp; /* Current position in vector or matrix. */
1050 n_lines = mx->n_continuous;
1051 if (mx->section != FULL && mx->diag == NODIAGONAL)
1055 cp = nr->data[content][cell];
1056 if (type == 1 && mx->section == LOWER && mx->diag == NODIAGONAL)
1057 cp += mx->n_continuous;
1059 for (i = 0; i < n_lines; i++)
1063 if (!nr_read_splits (nr, 1))
1065 if (per_factor && !nr_read_factors (nr, cell))
1072 n_cols = mx->n_continuous;
1075 switch (mx->section)
1082 n_cols = mx->n_continuous - i;
1083 if (mx->diag == NODIAGONAL)
1090 n_cols = mx->n_continuous;
1108 for (j = 0; j < n_cols; j++)
1110 struct matrix_token token;
1111 if (!mget_token (&token, mx->reader))
1113 if (token.type != MNUM)
1115 msg (SE, _("expecting value for %s %s"),
1116 dict_get_var (default_dict, j)->name,
1117 context (mx->reader));
1121 *cp++ = token.number;
1124 && !force_eol (mx->reader, content_names[content]))
1126 debug_printf (("\n"));
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++)
1423 case_data_rw (c, mx->factors[factor]->fv)->f
1424 = nr->factor_values[factor + cell * mx->n_factors];
1425 debug_printf (("f:%s ", mx->factors[factor]->name));
1432 for (content = 0; content <= PROX; content++)
1433 if (mx->is_per_factor[content])
1435 assert (nr->data[content] != NULL
1436 && nr->data[content][cell] != NULL);
1438 if (!dump_cell_content (mx, content, nr->data[content][cell],
1439 c, write_case, wc_data))
1452 for (factor = 0; factor < mx->n_factors; factor++)
1453 case_data_rw (c, mx->factors[factor]->fv)->f = SYSMIS;
1456 for (content = 0; content <= PROX; content++)
1457 if (!mx->is_per_factor[content] && nr->data[content] != NULL)
1459 if (!dump_cell_content (mx, content, nr->data[content][0],
1460 c, write_case, wc_data))
1468 /* Back end, with ROWTYPE_. */
1470 /* All the data for one set of factor values. */
1474 int n_rows[PROX + 1];
1475 double *data[PROX + 1];
1476 struct factor_data *next;
1479 /* With ROWTYPE_ auxiliary data. */
1482 struct matrix_data_pgm *mx; /* MATRIX DATA program. */
1483 int content; /* Type of current row. */
1484 double *split_values; /* SPLIT FILE variable values. */
1485 struct factor_data *data; /* All the data. */
1486 struct factor_data *current; /* Current factor. */
1489 static int wr_read_splits (struct wr_aux_data *, struct ccase *,
1490 write_case_func *, write_case_data);
1491 static bool wr_output_data (struct wr_aux_data *, struct ccase *,
1492 write_case_func *, write_case_data);
1493 static int wr_read_rowtype (struct wr_aux_data *,
1494 const struct matrix_token *, struct dfm_reader *);
1495 static int wr_read_factors (struct wr_aux_data *);
1496 static int wr_read_indeps (struct wr_aux_data *);
1497 static bool matrix_data_read_with_rowtype (struct case_source *,
1502 /* When ROWTYPE_ appears in the data, reads the matrices and writes
1503 them to the output file.
1504 Returns true if successful, false if an I/O error occurred. */
1506 read_matrices_with_rowtype (struct matrix_data_pgm *mx)
1508 struct wr_aux_data wr;
1513 wr.split_values = NULL;
1518 vfm_source = create_case_source (&matrix_data_with_rowtype_source_class,
1520 ok = procedure (NULL, NULL);
1522 free (wr.split_values);
1526 /* Read from the data file and write it to the active file.
1527 Returns true if successful, false if an I/O error occurred. */
1529 matrix_data_read_with_rowtype (struct case_source *source,
1531 write_case_func *write_case,
1532 write_case_data wc_data)
1534 struct wr_aux_data *wr = source->aux;
1535 struct matrix_data_pgm *mx = wr->mx;
1539 if (!wr_read_splits (wr, c, write_case, wc_data))
1542 if (!wr_read_factors (wr))
1545 if (!wr_read_indeps (wr))
1548 while (another_token (mx->reader));
1550 return wr_output_data (wr, c, write_case, wc_data);
1553 /* Read the split file variables. If they differ from the previous
1554 set of split variables then output the data. Returns success. */
1556 wr_read_splits (struct wr_aux_data *wr,
1558 write_case_func *write_case, write_case_data wc_data)
1560 struct matrix_data_pgm *mx = wr->mx;
1564 split_cnt = dict_get_split_cnt (default_dict);
1568 if (wr->split_values)
1573 wr->split_values = xnmalloc (split_cnt, sizeof *wr->split_values);
1580 for (i = 0; i < split_cnt; i++)
1582 struct matrix_token token;
1583 if (!mget_token (&token, mx->reader))
1585 if (token.type != MNUM)
1587 msg (SE, _("Syntax error %s expecting SPLIT FILE value."),
1588 context (mx->reader));
1592 if (compare && wr->split_values[i] != token.number && !different)
1594 if (!wr_output_data (wr, c, write_case, wc_data))
1599 wr->split_values[i] = token.number;
1606 /* Compares doubles A and B, treating SYSMIS as greatest. */
1608 compare_doubles (const void *a_, const void *b_, void *aux UNUSED)
1610 const double *a = a_;
1611 const double *b = b_;
1615 else if (*a == SYSMIS)
1617 else if (*b == SYSMIS)
1625 /* Return strcmp()-type comparison of the MX->n_factors factors at _A and
1626 _B. Sort missing values toward the end. */
1628 compare_factors (const void *a_, const void *b_, void *mx_)
1630 struct matrix_data_pgm *mx = mx_;
1631 struct factor_data *const *pa = a_;
1632 struct factor_data *const *pb = b_;
1633 const double *a = (*pa)->factors;
1634 const double *b = (*pb)->factors;
1636 return lexicographical_compare_3way (a, mx->n_factors,
1639 compare_doubles, NULL);
1642 /* Write out the data for the current split file to the active
1644 Returns true if successful, false if an I/O error occurred. */
1646 wr_output_data (struct wr_aux_data *wr,
1648 write_case_func *write_case, write_case_data wc_data)
1650 struct matrix_data_pgm *mx = wr->mx;
1654 struct variable *const *split;
1658 split_cnt = dict_get_split_cnt (default_dict);
1659 split = dict_get_split_vars (default_dict);
1660 for (i = 0; i < split_cnt; i++)
1661 case_data_rw (c, split[i]->fv)->f = wr->split_values[i];
1664 /* Sort the wr->data list. */
1666 struct factor_data **factors;
1667 struct factor_data *iter;
1670 factors = xnmalloc (mx->cells, sizeof *factors);
1672 for (i = 0, iter = wr->data; iter; iter = iter->next, i++)
1675 sort (factors, mx->cells, sizeof *factors, compare_factors, mx);
1677 wr->data = factors[0];
1678 for (i = 0; i < mx->cells - 1; i++)
1679 factors[i]->next = factors[i + 1];
1680 factors[mx->cells - 1]->next = NULL;
1685 /* Write out records for every set of factor values. */
1687 struct factor_data *iter;
1689 for (iter = wr->data; iter; iter = iter->next)
1694 for (factor = 0; factor < mx->n_factors; factor++)
1695 case_data_rw (c, mx->factors[factor]->fv)->f
1696 = iter->factors[factor];
1702 for (content = 0; content <= PROX; content++)
1704 if (!iter->n_rows[content])
1708 int type = content_type[content];
1709 int n_lines = (type == 1
1711 - (mx->section != FULL && mx->diag == NODIAGONAL))
1714 if (n_lines != iter->n_rows[content])
1716 msg (SE, _("Expected %d lines of data for %s content; "
1717 "actually saw %d lines. No data will be "
1718 "output for this content."),
1719 n_lines, content_names[content],
1720 iter->n_rows[content]);
1725 fill_matrix (mx, content, iter->data[content]);
1727 ok = dump_cell_content (mx, content, iter->data[content],
1728 c, write_case, wc_data);
1736 pool_destroy (mx->container);
1737 mx->container = pool_create ();
1739 wr->data = wr->current = NULL;
1744 /* Sets ROWTYPE_ based on the given TOKEN read from READER.
1747 wr_read_rowtype (struct wr_aux_data *wr,
1748 const struct matrix_token *token,
1749 struct dfm_reader *reader)
1751 if (wr->content != -1)
1753 msg (SE, _("Multiply specified ROWTYPE_ %s."), context (reader));
1756 if (token->type != MSTR)
1758 msg (SE, _("Syntax error %s expecting ROWTYPE_ string."),
1767 memcpy (s, token->string, min (15, token->length));
1768 s[min (15, token->length)] = 0;
1770 for (cp = s; *cp; cp++)
1771 *cp = toupper ((unsigned char) *cp);
1773 wr->content = string_to_content_type (s, NULL);
1776 if (wr->content == -1)
1778 msg (SE, _("Syntax error %s."), context (reader));
1785 /* Read the factors for the current row. Select a set of factors and
1786 point wr_current to it. */
1788 wr_read_factors (struct wr_aux_data *wr)
1790 struct matrix_data_pgm *mx = wr->mx;
1791 double *factor_values = local_alloc (sizeof *factor_values * mx->n_factors);
1797 for (i = 0; i < mx->n_factors; i++)
1799 struct matrix_token token;
1800 if (!mget_token (&token, mx->reader))
1802 if (token.type == MSTR)
1804 if (!wr_read_rowtype (wr, &token, mx->reader))
1806 if (!mget_token (&token, mx->reader))
1809 if (token.type != MNUM)
1811 msg (SE, _("Syntax error expecting factor value %s."),
1812 context (mx->reader));
1816 factor_values[i] = token.number;
1819 if (wr->content == -1)
1821 struct matrix_token token;
1822 if (!mget_token (&token, mx->reader))
1824 if (!wr_read_rowtype (wr, &token, mx->reader))
1828 /* Try the most recent factor first as a simple caching
1834 for (i = 0; i < mx->n_factors; i++)
1835 if (factor_values[i] != wr->current->factors[i])
1840 /* Linear search through the list. */
1843 struct factor_data *iter;
1845 for (iter = wr->data; iter; iter = iter->next)
1849 for (i = 0; i < mx->n_factors; i++)
1850 if (factor_values[i] != iter->factors[i])
1860 /* Not found. Make a new item. */
1862 struct factor_data *new = pool_alloc (mx->container, sizeof *new);
1864 new->factors = pool_nalloc (mx->container,
1865 mx->n_factors, sizeof *new->factors);
1870 for (i = 0; i < mx->n_factors; i++)
1871 new->factors[i] = factor_values[i];
1877 for (i = 0; i <= PROX; i++)
1880 new->data[i] = NULL;
1884 new->next = wr->data;
1885 wr->data = wr->current = new;
1890 local_free (factor_values);
1894 local_free (factor_values);
1898 /* Read the independent variables into wr->current. */
1900 wr_read_indeps (struct wr_aux_data *wr)
1902 struct matrix_data_pgm *mx = wr->mx;
1903 struct factor_data *c = wr->current;
1904 const int type = content_type[wr->content];
1905 const int n_rows = c->n_rows[wr->content];
1909 /* Allocate room for data if necessary. */
1910 if (c->data[wr->content] == NULL)
1912 int n_items = mx->n_continuous;
1914 n_items *= mx->n_continuous;
1916 c->data[wr->content] = pool_nalloc (mx->container,
1917 n_items, sizeof **c->data);
1920 cp = &c->data[wr->content][n_rows * mx->n_continuous];
1922 /* Figure out how much to read from this line. */
1929 msg (SE, _("Duplicate specification for %s."),
1930 content_names[wr->content]);
1934 n_cols = mx->n_continuous;
1939 if (n_rows >= mx->n_continuous - (mx->section != FULL && mx->diag == NODIAGONAL))
1941 msg (SE, _("Too many rows of matrix data for %s."),
1942 content_names[wr->content]);
1946 switch (mx->section)
1949 n_cols = n_rows + 1;
1950 if (mx->diag == NODIAGONAL)
1951 cp += mx->n_continuous;
1955 n_cols = mx->n_continuous - n_rows;
1956 if (mx->diag == NODIAGONAL)
1963 n_cols = mx->n_continuous;
1974 c->n_rows[wr->content]++;
1976 debug_printf ((" (c=%p,r=%d,n=%d)", c, n_rows + 1, n_cols));
1978 /* Read N_COLS items at CP. */
1982 for (j = 0; j < n_cols; j++)
1984 struct matrix_token token;
1985 if (!mget_token (&token, mx->reader))
1987 if (token.type != MNUM)
1989 msg (SE, _("Syntax error expecting value for %s %s."),
1990 dict_get_var (default_dict, mx->first_continuous + j)->name,
1991 context (mx->reader));
1995 *cp++ = token.number;
1998 && !force_eol (mx->reader, content_names[wr->content]))
2000 debug_printf (("\n"));
2006 /* Matrix source. */
2008 static const struct case_source_class matrix_data_with_rowtype_source_class =
2012 matrix_data_read_with_rowtype,
2016 static const struct case_source_class
2017 matrix_data_without_rowtype_source_class =
2021 matrix_data_read_without_rowtype,