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
25 #include "algorithm.h"
31 #include "dictionary.h"
33 #include "file-handle.h"
42 #define _(msgid) gettext (msgid)
44 #include "debug-print.h"
46 /* FIXME: /N subcommand not implemented. It should be pretty simple,
49 /* Different types of variables for MATRIX DATA procedure. Order is
50 important: these are used for sort keys. */
53 MXD_SPLIT, /* SPLIT FILE variables. */
54 MXD_ROWTYPE, /* ROWTYPE_. */
55 MXD_FACTOR, /* Factor variables. */
56 MXD_VARNAME, /* VARNAME_. */
57 MXD_CONTINUOUS, /* Continuous variables. */
62 /* Format type enums. */
69 /* Matrix section enums. */
77 /* Diagonal inclusion enums. */
105 /* 0=vector, 1=matrix, 2=scalar. */
106 static const int content_type[PROX + 1] =
108 0, 2, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1,
111 /* Name of each content type. */
112 static const char *content_names[PROX + 1] =
114 "N", "N", "N_MATRIX", "MEAN", "STDDEV", "COUNT", "MSE",
115 "DFE", "MAT", "COV", "CORR", "PROX",
118 /* A MATRIX DATA input program. */
119 struct matrix_data_pgm
121 struct pool *container; /* Arena used for all allocations. */
122 struct dfm_reader *reader; /* Data file to read. */
125 enum format_type fmt; /* LIST or FREE. */
126 enum matrix_section section;/* LOWER or UPPER or FULL. */
127 enum include_diagonal diag; /* DIAGONAL or NODIAGONAL. */
129 int explicit_rowtype; /* ROWTYPE_ specified explicitly in data? */
130 struct variable *rowtype_, *varname_; /* ROWTYPE_, VARNAME_ variables. */
132 struct variable *single_split; /* Single SPLIT FILE variable. */
134 /* Factor variables. */
135 size_t n_factors; /* Number of factor variables. */
136 struct variable **factors; /* Factor variables. */
137 int is_per_factor[PROX + 1]; /* Is there per-factor data? */
139 int cells; /* Number of cells, or -1 if none. */
141 int pop_n; /* Population N specified by user. */
143 /* CONTENTS subcommand. */
144 int contents[EOC * 3 + 1]; /* Contents. */
145 int n_contents; /* Number of entries. */
147 /* Continuous variables. */
148 int n_continuous; /* Number of continuous variables. */
149 int first_continuous; /* Index into default_dict.var of
150 first continuous variable. */
153 /* Auxiliary data attached to MATRIX DATA variables. */
156 int var_type; /* Variable type. */
157 int sub_type; /* Subtype. */
160 static const struct case_source_class matrix_data_with_rowtype_source_class;
161 static const struct case_source_class matrix_data_without_rowtype_source_class;
163 static int compare_variables_by_mxd_var_type (const void *pa,
165 static void read_matrices_without_rowtype (struct matrix_data_pgm *);
166 static void read_matrices_with_rowtype (struct matrix_data_pgm *);
167 static int string_to_content_type (char *, int *);
168 static void attach_mxd_aux (struct variable *, int var_type, int sub_type);
171 cmd_matrix_data (void)
174 struct matrix_data_pgm *mx;
175 struct file_handle *fh = fh_inline_file ();
179 discard_variables ();
181 pool = pool_create ();
182 mx = pool_alloc (pool, sizeof *mx);
183 mx->container = pool;
188 mx->explicit_rowtype = 0;
191 mx->single_split = NULL;
194 memset (mx->is_per_factor, 0, sizeof mx->is_per_factor);
198 mx->n_continuous = 0;
199 mx->first_continuous = 0;
204 if (lex_match_id ("VARIABLES"))
211 msg (SE, _("VARIABLES subcommand multiply specified."));
217 if (!parse_DATA_LIST_vars (&v, &nv, PV_NO_DUPLICATE))
223 for (i = 0; i < nv; i++)
224 if (!strcasecmp (v[i], "VARNAME_"))
226 msg (SE, _("VARNAME_ cannot be explicitly specified on "
228 for (i = 0; i < nv; i++)
238 for (i = 0; i < nv; i++)
240 struct variable *new_var;
242 if (strcasecmp (v[i], "ROWTYPE_"))
244 new_var = dict_create_var_assert (default_dict, v[i], 0);
245 attach_mxd_aux (new_var, MXD_CONTINUOUS, i);
248 mx->explicit_rowtype = 1;
254 mx->rowtype_ = dict_create_var_assert (default_dict,
256 attach_mxd_aux (mx->rowtype_, MXD_ROWTYPE, 0);
258 else if (lex_match_id ("FILE"))
261 fh = fh_parse (FH_REF_FILE | FH_REF_INLINE);
265 else if (lex_match_id ("FORMAT"))
269 while (token == T_ID)
271 if (lex_match_id ("LIST"))
273 else if (lex_match_id ("FREE"))
275 else if (lex_match_id ("LOWER"))
277 else if (lex_match_id ("UPPER"))
279 else if (lex_match_id ("FULL"))
281 else if (lex_match_id ("DIAGONAL"))
283 else if (lex_match_id ("NODIAGONAL"))
284 mx->diag = NODIAGONAL;
287 lex_error (_("in FORMAT subcommand"));
292 else if (lex_match_id ("SPLIT"))
298 msg (SE, _("SPLIT subcommand multiply specified."));
305 lex_error (_("in SPLIT subcommand"));
309 if (dict_lookup_var (default_dict, tokid) == NULL
310 && (lex_look_ahead () == '.' || lex_look_ahead () == '/'))
312 if (!strcasecmp (tokid, "ROWTYPE_")
313 || !strcasecmp (tokid, "VARNAME_"))
315 msg (SE, _("Split variable may not be named ROWTYPE_ "
320 mx->single_split = dict_create_var_assert (default_dict,
322 attach_mxd_aux (mx->single_split, MXD_CONTINUOUS, 0);
325 dict_set_split_vars (default_dict, &mx->single_split, 1);
329 struct variable **split;
332 if (!parse_variables (default_dict, &split, &n, PV_NO_DUPLICATE))
335 dict_set_split_vars (default_dict, split, n);
339 struct variable *const *split = dict_get_split_vars (default_dict);
340 size_t split_cnt = dict_get_split_cnt (default_dict);
343 for (i = 0; i < split_cnt; i++)
345 struct mxd_var *mv = split[i]->aux;
347 if (mv->var_type != MXD_CONTINUOUS)
349 msg (SE, _("Split variable %s is already another type."),
353 var_clear_aux (split[i]);
354 attach_mxd_aux (split[i], MXD_SPLIT, i);
358 else if (lex_match_id ("FACTORS"))
364 msg (SE, _("FACTORS subcommand multiply specified."));
369 if (!parse_variables (default_dict, &mx->factors, &mx->n_factors,
376 for (i = 0; i < mx->n_factors; i++)
378 struct variable *v = mx->factors[i];
379 struct mxd_var *mv = v->aux;
381 if (mv->var_type != MXD_CONTINUOUS)
383 msg (SE, _("Factor variable %s is already another type."),
388 attach_mxd_aux (v, MXD_FACTOR, i);
392 else if (lex_match_id ("CELLS"))
398 msg (SE, _("CELLS subcommand multiply specified."));
402 if (!lex_is_integer () || lex_integer () < 1)
404 lex_error (_("expecting positive integer"));
408 mx->cells = lex_integer ();
411 else if (lex_match_id ("N"))
417 msg (SE, _("N subcommand multiply specified."));
421 if (!lex_is_integer () || lex_integer () < 1)
423 lex_error (_("expecting positive integer"));
427 mx->pop_n = lex_integer ();
430 else if (lex_match_id ("CONTENTS"))
432 int inside_parens = 0;
433 unsigned collide = 0;
438 msg (SE, _("CONTENTS subcommand multiply specified."));
448 for (i = 0; i <= PROX; i++)
449 mx->is_per_factor[i] = 0;
458 msg (SE, _("Nested parentheses not allowed."));
464 else if (lex_match (')'))
468 msg (SE, _("Mismatched right parenthesis (`(')."));
471 if (mx->contents[mx->n_contents - 1] == LPAREN)
473 msg (SE, _("Empty parentheses not allowed."));
486 lex_error (_("in CONTENTS subcommand"));
490 content_type = string_to_content_type (tokid,
492 if (content_type == -1)
494 lex_error (_("in CONTENTS subcommand"));
499 if (collide & (1 << collide_index))
501 msg (SE, _("Content multiply specified for %s."),
502 content_names[content_type]);
505 collide |= (1 << collide_index);
508 mx->is_per_factor[item] = inside_parens;
510 mx->contents[mx->n_contents++] = item;
512 if (token == '/' || token == '.')
518 msg (SE, _("Missing right parenthesis."));
521 mx->contents[mx->n_contents] = EOC;
532 lex_error (_("expecting end of command"));
538 msg (SE, _("Missing VARIABLES subcommand."));
542 if (!mx->n_contents && !mx->explicit_rowtype)
544 msg (SW, _("CONTENTS subcommand not specified: assuming file "
545 "contains only CORR matrix."));
547 mx->contents[0] = CORR;
548 mx->contents[1] = EOC;
552 if (mx->n_factors && !mx->explicit_rowtype && mx->cells == -1)
554 msg (SE, _("Missing CELLS subcommand. CELLS is required "
555 "when ROWTYPE_ is not given in the data and "
556 "factors are present."));
560 if (mx->explicit_rowtype && mx->single_split)
562 msg (SE, _("Split file values must be present in the data when "
563 "ROWTYPE_ is present."));
567 /* Create VARNAME_. */
568 mx->varname_ = dict_create_var_assert (default_dict, "VARNAME_", 8);
569 attach_mxd_aux (mx->varname_, MXD_VARNAME, 0);
571 /* Sort the dictionary variables into the desired order for the
572 system file output. */
577 dict_get_vars (default_dict, &v, &nv, 0);
578 qsort (v, nv, sizeof *v, compare_variables_by_mxd_var_type);
579 dict_reorder_vars (default_dict, v, nv);
585 static const struct fmt_spec fmt_tab[MXD_COUNT] =
596 mx->first_continuous = -1;
597 for (i = 0; i < dict_get_var_cnt (default_dict); i++)
599 struct variable *v = dict_get_var (default_dict, i);
600 struct mxd_var *mv = v->aux;
601 int type = mv->var_type;
603 assert (type >= 0 && type < MXD_COUNT);
604 v->print = v->write = fmt_tab[type];
606 if (type == MXD_CONTINUOUS)
608 if (mx->first_continuous == -1 && type == MXD_CONTINUOUS)
609 mx->first_continuous = i;
613 if (mx->n_continuous == 0)
615 msg (SE, _("No continuous variables specified."));
619 mx->reader = dfm_open_reader (fh);
620 if (mx->reader == NULL)
623 if (mx->explicit_rowtype)
624 read_matrices_with_rowtype (mx);
626 read_matrices_without_rowtype (mx);
628 dfm_close_reader (mx->reader);
630 pool_destroy (mx->container);
635 discard_variables ();
637 pool_destroy (mx->container);
641 /* Look up string S as a content-type name and return the
642 corresponding enumerated value, or -1 if there is no match. If
643 COLLIDE is non-NULL then *COLLIDE returns a value (suitable for use
644 as a bit-index) which can be used for determining whether a related
645 statistic has already been used. */
647 string_to_content_type (char *s, int *collide)
658 {N_VECTOR, 0, "N_VECTOR"},
660 {N_SCALAR, 0, "N_SCALAR"},
661 {N_MATRIX, 1, "N_MATRIX"},
663 {STDDEV, 3, "STDDEV"},
675 for (tp = tab; tp->value != -1; tp++)
676 if (!strcasecmp (s, tp->string))
679 *collide = tp->collide;
686 /* Compare two variables using p.mxd.var_type and p.mxd.sub_type
689 compare_variables_by_mxd_var_type (const void *a_, const void *b_)
691 struct variable *const *pa = a_;
692 struct variable *const *pb = b_;
693 const struct mxd_var *a = (*pa)->aux;
694 const struct mxd_var *b = (*pb)->aux;
696 if (a->var_type != b->var_type)
697 return a->var_type > b->var_type ? 1 : -1;
699 return a->sub_type < b->sub_type ? -1 : a->sub_type > b->sub_type;
702 /* Attaches a struct mxd_var with the specific member values to
705 attach_mxd_aux (struct variable *v, int var_type, int sub_type)
709 assert (v->aux == NULL);
710 mv = xmalloc (sizeof *mv);
711 mv->var_type = var_type;
712 mv->sub_type = sub_type;
713 var_attach_aux (v, mv, var_dtor_free);
716 /* Matrix tokenizer. */
718 /* Matrix token types. */
719 enum matrix_token_type
725 /* A MATRIX DATA parsing token. */
728 enum matrix_token_type type;
729 double number; /* MNUM: token value. */
730 char *string; /* MSTR: token string; not null-terminated. */
731 int length; /* MSTR: tokstr length. */
734 static int mget_token (struct matrix_token *, struct dfm_reader *);
737 #define mget_token(TOKEN, READER) mget_token_dump(TOKEN, READER)
740 mdump_token (const struct matrix_token *token)
745 printf (" #%g", token->number);
748 printf (" '%.*s'", token->length, token->string);
757 mget_token_dump (struct matrix_token *token, struct dfm_reader *reader)
759 int result = (mget_token) (token, reader);
765 /* Return the current position in READER. */
767 context (struct dfm_reader *reader)
771 if (dfm_eof (reader))
772 strcpy (buf, "at end of file");
775 struct fixed_string line;
778 dfm_get_record (reader, &line);
779 sp = ls_c_str (&line);
780 while (sp < ls_end (&line) && isspace ((unsigned char) *sp))
782 if (sp >= ls_end (&line))
783 strcpy (buf, "at end of line");
789 dp = stpcpy (buf, "before `");
790 while (sp < ls_end (&line) && !isspace ((unsigned char) *sp)
803 /* Is there at least one token left in the data file? */
805 another_token (struct dfm_reader *reader)
809 struct fixed_string line;
812 if (dfm_eof (reader))
814 dfm_get_record (reader, &line);
816 cp = ls_c_str (&line);
817 while (isspace ((unsigned char) *cp) && cp < ls_end (&line))
820 if (cp < ls_end (&line))
822 dfm_forward_columns (reader, cp - ls_c_str (&line));
826 dfm_forward_record (reader);
830 /* Parse a MATRIX DATA token from READER into TOKEN. */
832 (mget_token) (struct matrix_token *token, struct dfm_reader *reader)
834 struct fixed_string line;
838 if (!another_token (reader))
841 dfm_get_record (reader, &line);
842 first_column = dfm_column_start (reader);
844 /* Three types of fields: quoted with ', quoted with ", unquoted. */
845 cp = ls_c_str (&line);
846 if (*cp == '\'' || *cp == '"')
851 token->string = ++cp;
852 while (cp < ls_end (&line) && *cp != quote)
854 token->length = cp - token->string;
855 if (cp < ls_end (&line))
858 msg (SW, _("Scope of string exceeds line."));
862 int is_num = isdigit ((unsigned char) *cp) || *cp == '.';
864 token->string = cp++;
865 while (cp < ls_end (&line)
866 && !isspace ((unsigned char) *cp) && *cp != ','
867 && *cp != '-' && *cp != '+')
869 if (isdigit ((unsigned char) *cp))
872 if ((tolower ((unsigned char) *cp) == 'd'
873 || tolower ((unsigned char) *cp) == 'e')
874 && (cp[1] == '+' || cp[1] == '-'))
880 token->length = cp - token->string;
881 assert (token->length);
887 di.s = token->string;
888 di.e = token->string + token->length;
889 di.v = (union value *) &token->number;
890 di.f1 = first_column;
891 di.format = make_output_format (FMT_F, token->length, 0);
900 dfm_forward_columns (reader, cp - ls_c_str (&line));
905 /* Forcibly skip the end of a line for content type CONTENT in
908 force_eol (struct dfm_reader *reader, const char *content)
910 struct fixed_string line;
913 if (dfm_eof (reader))
915 dfm_get_record (reader, &line);
917 cp = ls_c_str (&line);
918 while (isspace ((unsigned char) *cp) && cp < ls_end (&line))
921 if (cp < ls_end (&line))
923 msg (SE, _("End of line expected %s while reading %s."),
924 context (reader), content);
928 dfm_forward_record (reader);
932 /* Back end, omitting ROWTYPE_. */
936 struct matrix_data_pgm *mx; /* MATRIX DATA program. */
937 double ***data; /* MATRIX DATA data. */
938 double *factor_values; /* Factor values. */
939 int max_cell_idx; /* Max-numbered cell that we have
940 read so far, plus one. */
941 double *split_values; /* SPLIT FILE variable values. */
944 static int nr_read_splits (struct nr_aux_data *, int compare);
945 static int nr_read_factors (struct nr_aux_data *, int cell);
946 static void nr_output_data (struct nr_aux_data *, struct ccase *,
947 write_case_func *, write_case_data);
948 static void matrix_data_read_without_rowtype (struct case_source *source,
953 /* Read from the data file and write it to the active file. */
955 read_matrices_without_rowtype (struct matrix_data_pgm *mx)
957 struct nr_aux_data nr;
964 nr.factor_values = xnmalloc (mx->n_factors * mx->cells,
965 sizeof *nr.factor_values);
967 nr.split_values = xnmalloc (dict_get_split_cnt (default_dict),
968 sizeof *nr.split_values);
970 vfm_source = create_case_source (&matrix_data_without_rowtype_source_class, &nr);
972 procedure (NULL, NULL);
974 free (nr.split_values);
975 free (nr.factor_values);
978 /* Mirror data across the diagonal of matrix CP which contains
979 CONTENT type data. */
981 fill_matrix (struct matrix_data_pgm *mx, int content, double *cp)
983 int type = content_type[content];
985 if (type == 1 && mx->section != FULL)
987 if (mx->diag == NODIAGONAL)
989 const double fill = content == CORR ? 1.0 : SYSMIS;
992 for (i = 0; i < mx->n_continuous; i++)
993 cp[i * (1 + mx->n_continuous)] = fill;
999 if (mx->section == LOWER)
1001 int n_lines = mx->n_continuous;
1002 if (mx->section != FULL && mx->diag == NODIAGONAL)
1005 for (r = 1; r < n_lines; r++)
1006 for (c = 0; c < r; c++)
1007 cp[r + c * mx->n_continuous] = cp[c + r * mx->n_continuous];
1011 assert (mx->section == UPPER);
1012 for (r = 1; r < mx->n_continuous; r++)
1013 for (c = 0; c < r; c++)
1014 cp[c + r * mx->n_continuous] = cp[r + c * mx->n_continuous];
1022 for (c = 1; c < mx->n_continuous; c++)
1027 /* Read data lines for content type CONTENT from the data file.
1028 If PER_FACTOR is nonzero, then factor information is read from
1029 the data file. Data is for cell number CELL. */
1031 nr_read_data_lines (struct nr_aux_data *nr,
1032 int per_factor, int cell, int content, int compare)
1034 struct matrix_data_pgm *mx = nr->mx;
1035 const int type = content_type[content]; /* Content type. */
1036 int n_lines; /* Number of lines to parse from data file for this type. */
1037 double *cp; /* Current position in vector or matrix. */
1044 n_lines = mx->n_continuous;
1045 if (mx->section != FULL && mx->diag == NODIAGONAL)
1049 cp = nr->data[content][cell];
1050 if (type == 1 && mx->section == LOWER && mx->diag == NODIAGONAL)
1051 cp += mx->n_continuous;
1053 for (i = 0; i < n_lines; i++)
1057 if (!nr_read_splits (nr, 1))
1059 if (per_factor && !nr_read_factors (nr, cell))
1066 n_cols = mx->n_continuous;
1069 switch (mx->section)
1076 n_cols = mx->n_continuous - i;
1077 if (mx->diag == NODIAGONAL)
1084 n_cols = mx->n_continuous;
1102 for (j = 0; j < n_cols; j++)
1104 struct matrix_token token;
1105 if (!mget_token (&token, mx->reader))
1107 if (token.type != MNUM)
1109 msg (SE, _("expecting value for %s %s"),
1110 dict_get_var (default_dict, j)->name,
1111 context (mx->reader));
1115 *cp++ = token.number;
1118 && !force_eol (mx->reader, content_names[content]))
1120 debug_printf (("\n"));
1123 if (mx->section == LOWER)
1124 cp += mx->n_continuous - n_cols;
1127 fill_matrix (mx, content, nr->data[content][cell]);
1132 /* When ROWTYPE_ does not appear in the data, reads the matrices and
1133 writes them to the output file. Returns success. */
1135 matrix_data_read_without_rowtype (struct case_source *source,
1137 write_case_func *write_case,
1138 write_case_data wc_data)
1140 struct nr_aux_data *nr = source->aux;
1141 struct matrix_data_pgm *mx = nr->mx;
1146 nr->data = pool_nalloc (mx->container, PROX + 1, sizeof *nr->data);
1151 for (i = 0; i <= PROX; i++)
1155 for (cp = mx->contents; *cp != EOC; cp++)
1156 if (*cp != LPAREN && *cp != RPAREN)
1158 int per_factor = mx->is_per_factor[*cp];
1161 n_entries = mx->n_continuous;
1162 if (content_type[*cp] == 1)
1163 n_entries *= mx->n_continuous;
1166 int n_vectors = per_factor ? mx->cells : 1;
1169 nr->data[*cp] = pool_nalloc (mx->container,
1170 n_vectors, sizeof **nr->data);
1172 for (i = 0; i < n_vectors; i++)
1173 nr->data[*cp][i] = pool_nalloc (mx->container,
1174 n_entries, sizeof ***nr->data);
1183 if (!nr_read_splits (nr, 0))
1186 for (bp = mx->contents; *bp != EOC; bp = np)
1190 /* Trap the CONTENTS that we should parse in this pass
1191 between bp and ep. Set np to the starting bp for next
1196 while (*ep != RPAREN)
1204 while (*ep != EOC && *ep != LPAREN)
1213 for (i = 0; i < (per_factor ? mx->cells : 1); i++)
1217 for (cp = bp; cp < ep; cp++)
1218 if (!nr_read_data_lines (nr, per_factor, i, *cp, cp != bp))
1224 nr_output_data (nr, c, write_case, wc_data);
1226 if (dict_get_split_cnt (default_dict) == 0
1227 || !another_token (mx->reader))
1232 /* Read the split file variables. If COMPARE is 1, compares the
1233 values read to the last values read and returns 1 if they're equal,
1236 nr_read_splits (struct nr_aux_data *nr, int compare)
1238 struct matrix_data_pgm *mx = nr->mx;
1239 static int just_read = 0; /* FIXME: WTF? */
1243 if (compare && just_read)
1249 if (dict_get_split_vars (default_dict) == NULL)
1252 if (mx->single_split)
1256 struct mxd_var *mv = dict_get_split_vars (default_dict)[0]->aux;
1257 nr->split_values[0] = ++mv->sub_type;
1265 split_cnt = dict_get_split_cnt (default_dict);
1266 for (i = 0; i < split_cnt; i++)
1268 struct matrix_token token;
1269 if (!mget_token (&token, mx->reader))
1271 if (token.type != MNUM)
1273 msg (SE, _("Syntax error expecting SPLIT FILE value %s."),
1274 context (mx->reader));
1279 nr->split_values[i] = token.number;
1280 else if (nr->split_values[i] != token.number)
1282 msg (SE, _("Expecting value %g for %s."),
1283 nr->split_values[i],
1284 dict_get_split_vars (default_dict)[i]->name);
1292 /* Read the factors for cell CELL. If COMPARE is 1, compares the
1293 values read to the last values read and returns 1 if they're equal,
1296 nr_read_factors (struct nr_aux_data *nr, int cell)
1298 struct matrix_data_pgm *mx = nr->mx;
1301 if (mx->n_factors == 0)
1304 assert (nr->max_cell_idx >= cell);
1305 if (cell != nr->max_cell_idx)
1316 for (i = 0; i < mx->n_factors; i++)
1318 struct matrix_token token;
1319 if (!mget_token (&token, mx->reader))
1321 if (token.type != MNUM)
1323 msg (SE, _("Syntax error expecting factor value %s."),
1324 context (mx->reader));
1329 nr->factor_values[i + mx->n_factors * cell] = token.number;
1330 else if (nr->factor_values[i + mx->n_factors * cell] != token.number)
1332 msg (SE, _("Syntax error expecting value %g for %s %s."),
1333 nr->factor_values[i + mx->n_factors * cell],
1334 mx->factors[i]->name, context (mx->reader));
1343 /* Write the contents of a cell having content type CONTENT and data
1344 CP to the active file. */
1346 dump_cell_content (struct matrix_data_pgm *mx, int content, double *cp,
1348 write_case_func *write_case, write_case_data wc_data)
1350 int type = content_type[content];
1353 buf_copy_str_rpad (case_data_rw (c, mx->rowtype_->fv)->s, 8,
1354 content_names[content]);
1357 memset (case_data_rw (c, mx->varname_->fv)->s, ' ', 8);
1361 int n_lines = (type == 1) ? mx->n_continuous : 1;
1364 for (i = 0; i < n_lines; i++)
1368 for (j = 0; j < mx->n_continuous; j++)
1370 int fv = dict_get_var (default_dict, mx->first_continuous + j)->fv;
1371 case_data_rw (c, fv)->f = *cp;
1375 buf_copy_str_rpad (case_data_rw (c, mx->varname_->fv)->s, 8,
1376 dict_get_var (default_dict,
1377 mx->first_continuous + i)->name);
1378 write_case (wc_data);
1383 /* Finally dump out everything from nr_data[] to the output file. */
1385 nr_output_data (struct nr_aux_data *nr, struct ccase *c,
1386 write_case_func *write_case, write_case_data wc_data)
1388 struct matrix_data_pgm *mx = nr->mx;
1391 struct variable *const *split;
1395 split_cnt = dict_get_split_cnt (default_dict);
1396 split = dict_get_split_vars (default_dict);
1397 for (i = 0; i < split_cnt; i++)
1398 case_data_rw (c, split[i]->fv)->f = nr->split_values[i];
1405 for (cell = 0; cell < mx->cells; cell++)
1410 for (factor = 0; factor < mx->n_factors; factor++)
1412 case_data_rw (c, mx->factors[factor]->fv)->f
1413 = nr->factor_values[factor + cell * mx->n_factors];
1414 debug_printf (("f:%s ", mx->factors[factor]->name));
1421 for (content = 0; content <= PROX; content++)
1422 if (mx->is_per_factor[content])
1424 assert (nr->data[content] != NULL
1425 && nr->data[content][cell] != NULL);
1427 dump_cell_content (mx, content, nr->data[content][cell],
1428 c, write_case, wc_data);
1440 for (factor = 0; factor < mx->n_factors; factor++)
1441 case_data_rw (c, mx->factors[factor]->fv)->f = SYSMIS;
1444 for (content = 0; content <= PROX; content++)
1445 if (!mx->is_per_factor[content] && nr->data[content] != NULL)
1446 dump_cell_content (mx, content, nr->data[content][0],
1447 c, write_case, wc_data);
1451 /* Back end, with ROWTYPE_. */
1453 /* All the data for one set of factor values. */
1457 int n_rows[PROX + 1];
1458 double *data[PROX + 1];
1459 struct factor_data *next;
1462 /* With ROWTYPE_ auxiliary data. */
1465 struct matrix_data_pgm *mx; /* MATRIX DATA program. */
1466 int content; /* Type of current row. */
1467 double *split_values; /* SPLIT FILE variable values. */
1468 struct factor_data *data; /* All the data. */
1469 struct factor_data *current; /* Current factor. */
1472 static int wr_read_splits (struct wr_aux_data *, struct ccase *,
1473 write_case_func *, write_case_data);
1474 static int wr_output_data (struct wr_aux_data *, struct ccase *,
1475 write_case_func *, write_case_data);
1476 static int wr_read_rowtype (struct wr_aux_data *,
1477 const struct matrix_token *, struct dfm_reader *);
1478 static int wr_read_factors (struct wr_aux_data *);
1479 static int wr_read_indeps (struct wr_aux_data *);
1480 static void matrix_data_read_with_rowtype (struct case_source *,
1485 /* When ROWTYPE_ appears in the data, reads the matrices and writes
1486 them to the output file. */
1488 read_matrices_with_rowtype (struct matrix_data_pgm *mx)
1490 struct wr_aux_data wr;
1494 wr.split_values = NULL;
1499 vfm_source = create_case_source (&matrix_data_with_rowtype_source_class,
1501 procedure (NULL, NULL);
1503 free (wr.split_values);
1506 /* Read from the data file and write it to the active file. */
1508 matrix_data_read_with_rowtype (struct case_source *source,
1510 write_case_func *write_case,
1511 write_case_data wc_data)
1513 struct wr_aux_data *wr = source->aux;
1514 struct matrix_data_pgm *mx = wr->mx;
1518 if (!wr_read_splits (wr, c, write_case, wc_data))
1521 if (!wr_read_factors (wr))
1524 if (!wr_read_indeps (wr))
1527 while (another_token (mx->reader));
1529 wr_output_data (wr, c, write_case, wc_data);
1532 /* Read the split file variables. If they differ from the previous
1533 set of split variables then output the data. Returns success. */
1535 wr_read_splits (struct wr_aux_data *wr,
1537 write_case_func *write_case, write_case_data wc_data)
1539 struct matrix_data_pgm *mx = wr->mx;
1543 split_cnt = dict_get_split_cnt (default_dict);
1547 if (wr->split_values)
1552 wr->split_values = xnmalloc (split_cnt, sizeof *wr->split_values);
1559 for (i = 0; i < split_cnt; i++)
1561 struct matrix_token token;
1562 if (!mget_token (&token, mx->reader))
1564 if (token.type != MNUM)
1566 msg (SE, _("Syntax error %s expecting SPLIT FILE value."),
1567 context (mx->reader));
1571 if (compare && wr->split_values[i] != token.number && !different)
1573 if (!wr_output_data (wr, c, write_case, wc_data))
1578 wr->split_values[i] = token.number;
1585 /* Compares doubles A and B, treating SYSMIS as greatest. */
1587 compare_doubles (const void *a_, const void *b_, void *aux UNUSED)
1589 const double *a = a_;
1590 const double *b = b_;
1594 else if (*a == SYSMIS)
1596 else if (*b == SYSMIS)
1604 /* Return strcmp()-type comparison of the MX->n_factors factors at _A and
1605 _B. Sort missing values toward the end. */
1607 compare_factors (const void *a_, const void *b_, void *mx_)
1609 struct matrix_data_pgm *mx = mx_;
1610 struct factor_data *const *pa = a_;
1611 struct factor_data *const *pb = b_;
1612 const double *a = (*pa)->factors;
1613 const double *b = (*pb)->factors;
1615 return lexicographical_compare_3way (a, mx->n_factors,
1618 compare_doubles, NULL);
1621 /* Write out the data for the current split file to the active
1624 wr_output_data (struct wr_aux_data *wr,
1626 write_case_func *write_case, write_case_data wc_data)
1628 struct matrix_data_pgm *mx = wr->mx;
1631 struct variable *const *split;
1635 split_cnt = dict_get_split_cnt (default_dict);
1636 split = dict_get_split_vars (default_dict);
1637 for (i = 0; i < split_cnt; i++)
1638 case_data_rw (c, split[i]->fv)->f = wr->split_values[i];
1641 /* Sort the wr->data list. */
1643 struct factor_data **factors;
1644 struct factor_data *iter;
1647 factors = xnmalloc (mx->cells, sizeof *factors);
1649 for (i = 0, iter = wr->data; iter; iter = iter->next, i++)
1652 sort (factors, mx->cells, sizeof *factors, compare_factors, mx);
1654 wr->data = factors[0];
1655 for (i = 0; i < mx->cells - 1; i++)
1656 factors[i]->next = factors[i + 1];
1657 factors[mx->cells - 1]->next = NULL;
1662 /* Write out records for every set of factor values. */
1664 struct factor_data *iter;
1666 for (iter = wr->data; iter; iter = iter->next)
1671 for (factor = 0; factor < mx->n_factors; factor++)
1672 case_data_rw (c, mx->factors[factor]->fv)->f
1673 = iter->factors[factor];
1679 for (content = 0; content <= PROX; content++)
1681 if (!iter->n_rows[content])
1685 int type = content_type[content];
1686 int n_lines = (type == 1
1688 - (mx->section != FULL && mx->diag == NODIAGONAL))
1691 if (n_lines != iter->n_rows[content])
1693 msg (SE, _("Expected %d lines of data for %s content; "
1694 "actually saw %d lines. No data will be "
1695 "output for this content."),
1696 n_lines, content_names[content],
1697 iter->n_rows[content]);
1702 fill_matrix (mx, content, iter->data[content]);
1704 dump_cell_content (mx, content, iter->data[content],
1705 c, write_case, wc_data);
1711 pool_destroy (mx->container);
1712 mx->container = pool_create ();
1714 wr->data = wr->current = NULL;
1719 /* Sets ROWTYPE_ based on the given TOKEN read from READER.
1722 wr_read_rowtype (struct wr_aux_data *wr,
1723 const struct matrix_token *token,
1724 struct dfm_reader *reader)
1726 if (wr->content != -1)
1728 msg (SE, _("Multiply specified ROWTYPE_ %s."), context (reader));
1731 if (token->type != MSTR)
1733 msg (SE, _("Syntax error %s expecting ROWTYPE_ string."),
1742 memcpy (s, token->string, min (15, token->length));
1743 s[min (15, token->length)] = 0;
1745 for (cp = s; *cp; cp++)
1746 *cp = toupper ((unsigned char) *cp);
1748 wr->content = string_to_content_type (s, NULL);
1751 if (wr->content == -1)
1753 msg (SE, _("Syntax error %s."), context (reader));
1760 /* Read the factors for the current row. Select a set of factors and
1761 point wr_current to it. */
1763 wr_read_factors (struct wr_aux_data *wr)
1765 struct matrix_data_pgm *mx = wr->mx;
1766 double *factor_values = local_alloc (sizeof *factor_values * mx->n_factors);
1772 for (i = 0; i < mx->n_factors; i++)
1774 struct matrix_token token;
1775 if (!mget_token (&token, mx->reader))
1777 if (token.type == MSTR)
1779 if (!wr_read_rowtype (wr, &token, mx->reader))
1781 if (!mget_token (&token, mx->reader))
1784 if (token.type != MNUM)
1786 msg (SE, _("Syntax error expecting factor value %s."),
1787 context (mx->reader));
1791 factor_values[i] = token.number;
1794 if (wr->content == -1)
1796 struct matrix_token token;
1797 if (!mget_token (&token, mx->reader))
1799 if (!wr_read_rowtype (wr, &token, mx->reader))
1803 /* Try the most recent factor first as a simple caching
1809 for (i = 0; i < mx->n_factors; i++)
1810 if (factor_values[i] != wr->current->factors[i])
1815 /* Linear search through the list. */
1818 struct factor_data *iter;
1820 for (iter = wr->data; iter; iter = iter->next)
1824 for (i = 0; i < mx->n_factors; i++)
1825 if (factor_values[i] != iter->factors[i])
1835 /* Not found. Make a new item. */
1837 struct factor_data *new = pool_alloc (mx->container, sizeof *new);
1839 new->factors = pool_nalloc (mx->container,
1840 mx->n_factors, sizeof *new->factors);
1845 for (i = 0; i < mx->n_factors; i++)
1846 new->factors[i] = factor_values[i];
1852 for (i = 0; i <= PROX; i++)
1855 new->data[i] = NULL;
1859 new->next = wr->data;
1860 wr->data = wr->current = new;
1865 local_free (factor_values);
1869 local_free (factor_values);
1873 /* Read the independent variables into wr->current. */
1875 wr_read_indeps (struct wr_aux_data *wr)
1877 struct matrix_data_pgm *mx = wr->mx;
1878 struct factor_data *c = wr->current;
1879 const int type = content_type[wr->content];
1880 const int n_rows = c->n_rows[wr->content];
1884 /* Allocate room for data if necessary. */
1885 if (c->data[wr->content] == NULL)
1887 int n_items = mx->n_continuous;
1889 n_items *= mx->n_continuous;
1891 c->data[wr->content] = pool_nalloc (mx->container,
1892 n_items, sizeof **c->data);
1895 cp = &c->data[wr->content][n_rows * mx->n_continuous];
1897 /* Figure out how much to read from this line. */
1904 msg (SE, _("Duplicate specification for %s."),
1905 content_names[wr->content]);
1909 n_cols = mx->n_continuous;
1914 if (n_rows >= mx->n_continuous - (mx->section != FULL && mx->diag == NODIAGONAL))
1916 msg (SE, _("Too many rows of matrix data for %s."),
1917 content_names[wr->content]);
1921 switch (mx->section)
1924 n_cols = n_rows + 1;
1925 if (mx->diag == NODIAGONAL)
1926 cp += mx->n_continuous;
1930 n_cols = mx->n_continuous - n_rows;
1931 if (mx->diag == NODIAGONAL)
1938 n_cols = mx->n_continuous;
1949 c->n_rows[wr->content]++;
1951 debug_printf ((" (c=%p,r=%d,n=%d)", c, n_rows + 1, n_cols));
1953 /* Read N_COLS items at CP. */
1957 for (j = 0; j < n_cols; j++)
1959 struct matrix_token token;
1960 if (!mget_token (&token, mx->reader))
1962 if (token.type != MNUM)
1964 msg (SE, _("Syntax error expecting value for %s %s."),
1965 dict_get_var (default_dict, mx->first_continuous + j)->name,
1966 context (mx->reader));
1970 *cp++ = token.number;
1973 && !force_eol (mx->reader, content_names[wr->content]))
1975 debug_printf (("\n"));
1981 /* Matrix source. */
1983 static const struct case_source_class matrix_data_with_rowtype_source_class =
1987 matrix_data_read_with_rowtype,
1991 static const struct case_source_class
1992 matrix_data_without_rowtype_source_class =
1996 matrix_data_read_without_rowtype,