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., 59 Temple Place - Suite 330, Boston, MA
25 #include "algorithm.h"
31 #include "file-handle.h"
39 #include "debug-print.h"
41 /* FIXME: /N subcommand not implemented. It should be pretty simple,
44 /* Format type enums. */
51 /* Matrix section enums. */
59 /* Diagonal inclusion enums. */
87 /* 0=vector, 1=matrix, 2=scalar. */
88 static const int content_type[PROX + 1] =
90 0, 2, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1,
93 /* Name of each content type. */
94 static const char *content_names[PROX + 1] =
96 "N", "N", "N_MATRIX", "MEAN", "STDDEV", "COUNT", "MSE",
97 "DFE", "MAT", "COV", "CORR", "PROX",
100 /* A MATRIX DATA input program. */
101 struct matrix_data_pgm
103 struct pool *container; /* Arena used for all allocations. */
104 struct file_handle *data_file; /* The data file to be read. */
107 enum format_type fmt; /* LIST or FREE. */
108 enum matrix_section section;/* LOWER or UPPER or FULL. */
109 enum include_diagonal diag; /* DIAGONAL or NODIAGONAL. */
111 int explicit_rowtype; /* ROWTYPE_ specified explicitly in data? */
112 struct variable *rowtype_, *varname_; /* ROWTYPE_, VARNAME_ variables. */
114 struct variable *single_split; /* Single SPLIT FILE variable. */
116 /* Factor variables. */
117 int n_factors; /* Number of factor variables. */
118 struct variable **factors; /* Factor variables. */
119 int is_per_factor[PROX + 1]; /* Is there per-factor data? */
121 int cells; /* Number of cells, or -1 if none. */
123 int pop_n; /* Population N specified by user. */
125 /* CONTENTS subcommand. */
126 int contents[EOC * 3 + 1]; /* Contents. */
127 int n_contents; /* Number of entries. */
129 /* Continuous variables. */
130 int n_continuous; /* Number of continuous variables. */
131 int first_continuous; /* Index into default_dict.var of
132 first continuous variable. */
135 static const struct case_source_class matrix_data_with_rowtype_source_class;
136 static const struct case_source_class matrix_data_without_rowtype_source_class;
138 static int compare_variables_by_mxd_vartype (const void *pa,
140 static void read_matrices_without_rowtype (struct matrix_data_pgm *);
141 static void read_matrices_with_rowtype (struct matrix_data_pgm *);
142 static int string_to_content_type (char *, int *);
145 static void debug_print (void);
149 cmd_matrix_data (void)
152 struct matrix_data_pgm *mx;
156 discard_variables ();
158 pool = pool_create ();
159 mx = pool_alloc (pool, sizeof *mx);
160 mx->container = pool;
161 mx->data_file = inline_file;
165 mx->explicit_rowtype = 0;
168 mx->single_split = NULL;
171 memset (mx->is_per_factor, 0, sizeof mx->is_per_factor);
175 mx->n_continuous = 0;
176 mx->first_continuous = 0;
181 if (lex_match_id ("VARIABLES"))
188 msg (SE, _("VARIABLES subcommand multiply specified."));
194 if (!parse_DATA_LIST_vars (&v, &nv, PV_NO_DUPLICATE))
200 for (i = 0; i < nv; i++)
201 if (!strcmp (v[i], "VARNAME_"))
203 msg (SE, _("VARNAME_ cannot be explicitly specified on "
205 for (i = 0; i < nv; i++)
215 for (i = 0; i < nv; i++)
217 struct variable *new_var;
219 if (strcmp (v[i], "ROWTYPE_"))
221 new_var = dict_create_var_assert (default_dict, v[i], 0);
222 new_var->p.mxd.vartype = MXD_CONTINUOUS;
223 new_var->p.mxd.subtype = i;
226 mx->explicit_rowtype = 1;
233 mx->rowtype_ = dict_create_var_assert (default_dict,
235 mx->rowtype_->p.mxd.vartype = MXD_ROWTYPE;
236 mx->rowtype_->p.mxd.subtype = 0;
239 else if (lex_match_id ("FILE"))
242 mx->data_file = fh_parse_file_handle ();
243 if (mx->data_file == NULL)
246 else if (lex_match_id ("FORMAT"))
250 while (token == T_ID)
252 if (lex_match_id ("LIST"))
254 else if (lex_match_id ("FREE"))
256 else if (lex_match_id ("LOWER"))
258 else if (lex_match_id ("UPPER"))
260 else if (lex_match_id ("FULL"))
262 else if (lex_match_id ("DIAGONAL"))
264 else if (lex_match_id ("NODIAGONAL"))
265 mx->diag = NODIAGONAL;
268 lex_error (_("in FORMAT subcommand"));
273 else if (lex_match_id ("SPLIT"))
279 msg (SE, _("SPLIT subcommand multiply specified."));
286 lex_error (_("in SPLIT subcommand"));
290 if (dict_lookup_var (default_dict, tokid) == NULL
291 && (lex_look_ahead () == '.' || lex_look_ahead () == '/'))
293 if (!strcmp (tokid, "ROWTYPE_") || !strcmp (tokid, "VARNAME_"))
295 msg (SE, _("Split variable may not be named ROWTYPE_ "
300 mx->single_split = dict_create_var_assert (default_dict,
304 mx->single_split->p.mxd.vartype = MXD_CONTINUOUS;
306 dict_set_split_vars (default_dict, &mx->single_split, 1);
310 struct variable **split;
313 if (!parse_variables (default_dict, &split, &n, PV_NO_DUPLICATE))
316 dict_set_split_vars (default_dict, split, n);
320 struct variable *const *split = dict_get_split_vars (default_dict);
321 size_t split_cnt = dict_get_split_cnt (default_dict);
324 for (i = 0; i < split_cnt; i++)
326 if (split[i]->p.mxd.vartype != MXD_CONTINUOUS)
328 msg (SE, _("Split variable %s is already another type."),
332 split[i]->p.mxd.vartype = MXD_SPLIT;
333 split[i]->p.mxd.subtype = i;
337 else if (lex_match_id ("FACTORS"))
343 msg (SE, _("FACTORS subcommand multiply specified."));
348 if (!parse_variables (default_dict, &mx->factors, &mx->n_factors, PV_NONE))
354 for (i = 0; i < mx->n_factors; i++)
356 if (mx->factors[i]->p.mxd.vartype != MXD_CONTINUOUS)
358 msg (SE, _("Factor variable %s is already another type."),
362 mx->factors[i]->p.mxd.vartype = MXD_FACTOR;
363 mx->factors[i]->p.mxd.subtype = i;
367 else if (lex_match_id ("CELLS"))
373 msg (SE, _("CELLS subcommand multiply specified."));
377 if (!lex_integer_p () || lex_integer () < 1)
379 lex_error (_("expecting positive integer"));
383 mx->cells = lex_integer ();
386 else if (lex_match_id ("N"))
392 msg (SE, _("N subcommand multiply specified."));
396 if (!lex_integer_p () || lex_integer () < 1)
398 lex_error (_("expecting positive integer"));
402 mx->pop_n = lex_integer ();
405 else if (lex_match_id ("CONTENTS"))
407 int inside_parens = 0;
408 unsigned collide = 0;
413 msg (SE, _("CONTENTS subcommand multiply specified."));
423 for (i = 0; i <= PROX; i++)
424 mx->is_per_factor[i] = 0;
433 msg (SE, _("Nested parentheses not allowed."));
439 else if (lex_match (')'))
443 msg (SE, _("Mismatched right parenthesis (`(')."));
446 if (mx->contents[mx->n_contents - 1] == LPAREN)
448 msg (SE, _("Empty parentheses not allowed."));
461 lex_error (_("in CONTENTS subcommand"));
465 content_type = string_to_content_type (tokid,
467 if (content_type == -1)
469 lex_error (_("in CONTENTS subcommand"));
474 if (collide & (1 << collide_index))
476 msg (SE, _("Content multiply specified for %s."),
477 content_names[content_type]);
480 collide |= (1 << collide_index);
483 mx->is_per_factor[item] = inside_parens;
485 mx->contents[mx->n_contents++] = item;
487 if (token == '/' || token == '.')
493 msg (SE, _("Missing right parenthesis."));
496 mx->contents[mx->n_contents] = EOC;
507 lex_error (_("expecting end of command"));
513 msg (SE, _("Missing VARIABLES subcommand."));
517 if (!mx->n_contents && !mx->explicit_rowtype)
519 msg (SW, _("CONTENTS subcommand not specified: assuming file "
520 "contains only CORR matrix."));
522 mx->contents[0] = CORR;
523 mx->contents[1] = EOC;
527 if (mx->n_factors && !mx->explicit_rowtype && mx->cells == -1)
529 msg (SE, _("Missing CELLS subcommand. CELLS is required "
530 "when ROWTYPE_ is not given in the data and "
531 "factors are present."));
535 if (mx->explicit_rowtype && mx->single_split)
537 msg (SE, _("Split file values must be present in the data when "
538 "ROWTYPE_ is present."));
542 /* Create VARNAME_. */
544 mx->varname_ = dict_create_var_assert (default_dict, "VARNAME_", 8);
545 mx->varname_->p.mxd.vartype = MXD_VARNAME;
546 mx->varname_->p.mxd.subtype = 0;
549 /* Sort the dictionary variables into the desired order for the
550 system file output. */
555 dict_get_vars (default_dict, &v, &nv, 0);
556 qsort (v, nv, sizeof *v, compare_variables_by_mxd_vartype);
557 dict_reorder_vars (default_dict, v, nv);
563 static const struct fmt_spec fmt_tab[MXD_COUNT] =
574 mx->first_continuous = -1;
575 for (i = 0; i < dict_get_var_cnt (default_dict); i++)
577 struct variable *v = dict_get_var (default_dict, i);
578 int type = v->p.mxd.vartype;
580 assert (type >= 0 && type < MXD_COUNT);
581 v->print = v->write = fmt_tab[type];
583 if (type == MXD_CONTINUOUS)
585 if (mx->first_continuous == -1 && type == MXD_CONTINUOUS)
586 mx->first_continuous = i;
590 if (mx->n_continuous == 0)
592 msg (SE, _("No continuous variables specified."));
600 if (!dfm_open_for_reading (mx->data_file))
603 if (mx->explicit_rowtype)
604 read_matrices_with_rowtype (mx);
606 read_matrices_without_rowtype (mx);
608 pool_destroy (mx->container);
613 discard_variables ();
615 pool_destroy (mx->container);
619 /* Look up string S as a content-type name and return the
620 corresponding enumerated value, or -1 if there is no match. If
621 COLLIDE is non-NULL then *COLLIDE returns a value (suitable for use
622 as a bit-index) which can be used for determining whether a related
623 statistic has already been used. */
625 string_to_content_type (char *s, int *collide)
636 {N_VECTOR, 0, "N_VECTOR"},
638 {N_SCALAR, 0, "N_SCALAR"},
639 {N_MATRIX, 1, "N_MATRIX"},
641 {STDDEV, 3, "STDDEV"},
653 for (tp = tab; tp->value != -1; tp++)
654 if (!strcmp (s, tp->string))
657 *collide = tp->collide;
664 /* Compare two variables using p.mxd.vartype and p.mxd.subtype
667 compare_variables_by_mxd_vartype (const void *a_, const void *b_)
669 struct variable *const *pa = a_;
670 struct variable *const *pb = b_;
671 const struct matrix_data_proc *a = &(*pa)->p.mxd;
672 const struct matrix_data_proc *b = &(*pb)->p.mxd;
674 if (a->vartype != b->vartype)
675 return a->vartype > b->vartype ? 1 : -1;
677 return a->subtype < b->subtype ? -1 : a->subtype > b->subtype;
681 /* Print out the command as input. */
685 printf ("MATRIX DATA\n\t/VARIABLES=");
690 for (i = 0; i < default_dict.nvar; i++)
691 printf ("%s ", default_dict.var[i]->name);
695 printf ("\t/FORMAT=");
698 else if (fmt == FREE)
702 if (section == LOWER)
704 else if (section == UPPER)
706 else if (section == FULL)
710 if (diag == DIAGONAL)
711 printf (" DIAGONAL\n");
712 else if (diag == NODIAGONAL)
713 printf (" NODIAGONAL\n");
717 if (dict_get_split_cnt (default_dict) != 0)
721 printf ("\t/SPLIT=");
722 for (i = 0; i < dict_get_split_cnt (default_dict); i++)
723 printf ("%s ", dict_get_split_vars (default_dict)[i]->name);
725 printf ("\t/* single split");
733 printf ("\t/FACTORS=");
734 for (i = 0; i < n_factors; i++)
735 printf ("%s ", factors[i]->name);
740 printf ("\t/CELLS=%d\n", cells);
743 printf ("\t/N=%d\n", mx->pop_n);
750 printf ("\t/CONTENTS=");
751 for (i = 0; i < mx->n_contents; i++)
753 if (mx->contents[i] == LPAREN)
760 else if (mx->contents[i] == RPAREN)
768 assert (mx->contents[i] >= 0 && mx->contents[i] <= PROX);
771 printf ("%s", content_names[mx->contents[i]]);
778 #endif /* DEBUGGING */
780 /* Matrix tokenizer. */
782 /* Matrix token types. */
783 enum matrix_token_type
789 /* A MATRIX DATA parsing token. */
792 enum matrix_token_type type;
793 double number; /* MNUM: token value. */
794 char *string; /* MSTR: token string; not null-terminated. */
795 int length; /* MSTR: tokstr length. */
798 static int mget_token (struct matrix_token *, struct file_handle *);
801 #define mget_token(TOKEN, HANDLE) mget_token_dump(TOKEN, HANDLE)
804 mdump_token (const struct matrix_token *token)
809 printf (" #%g", token->number);
812 printf (" '%.*s'", token->length, token->string);
821 mget_token_dump (struct matrix_token *token, struct file_handle *data_file)
823 int result = (mget_token) (token, data_file);
829 /* Return the current position in DATA_FILE. */
831 context (struct file_handle *data_file)
835 char *p = dfm_get_record (data_file, &len);
838 strcpy (buf, "at end of line");
842 int n_copy = min (10, len);
843 cp = stpcpy (buf, "before `");
844 while (n_copy && isspace ((unsigned char) *p))
846 while (n_copy && !isspace ((unsigned char) *p))
847 *cp++ = *p++, n_copy--;
855 /* Is there at least one token left in the data file? */
857 another_token (struct file_handle *data_file)
864 cp = dfm_get_record (data_file, &len);
869 while (isspace ((unsigned char) *cp) && cp < ep)
875 dfm_fwd_record (data_file);
878 dfm_set_record (data_file, cp);
883 /* Parse a MATRIX DATA token from mx->data_file into TOKEN. */
885 (mget_token) (struct matrix_token *token, struct file_handle *data_file)
893 cp = dfm_get_record (data_file, &len);
898 while (isspace ((unsigned char) *cp) && cp < ep)
904 dfm_fwd_record (data_file);
907 dfm_set_record (data_file, cp);
908 first_column = dfm_get_cur_col (data_file) + 1;
910 /* Three types of fields: quoted with ', quoted with ", unquoted. */
911 if (*cp == '\'' || *cp == '"')
916 token->string = ++cp;
917 while (cp < ep && *cp != quote)
919 token->length = cp - token->string;
923 msg (SW, _("Scope of string exceeds line."));
927 int is_num = isdigit ((unsigned char) *cp) || *cp == '.';
929 token->string = cp++;
930 while (cp < ep && !isspace ((unsigned char) *cp) && *cp != ','
931 && *cp != '-' && *cp != '+')
933 if (isdigit ((unsigned char) *cp))
936 if ((tolower ((unsigned char) *cp) == 'd'
937 || tolower ((unsigned char) *cp) == 'e')
938 && (cp[1] == '+' || cp[1] == '-'))
944 token->length = cp - token->string;
945 assert (token->length);
951 di.s = token->string;
952 di.e = token->string + token->length;
953 di.v = (union value *) &token->number;
954 di.f1 = first_column;
955 di.format.type = FMT_F;
956 di.format.w = token->length;
966 dfm_set_record (data_file, cp);
971 /* Forcibly skip the end of a line for content type CONTENT in
974 force_eol (struct file_handle *data_file, const char *content)
979 cp = dfm_get_record (data_file, &len);
982 while (len && isspace (*cp))
987 msg (SE, _("End of line expected %s while reading %s."),
988 context (data_file), content);
992 dfm_fwd_record (data_file);
997 /* Back end, omitting ROWTYPE_. */
1001 struct matrix_data_pgm *mx; /* MATRIX DATA program. */
1002 double ***data; /* MATRIX DATA data. */
1003 double *factor_values; /* Factor values. */
1004 int max_cell_idx; /* Max-numbered cell that we have
1005 read so far, plus one. */
1006 double *split_values; /* SPLIT FILE variable values. */
1009 static int nr_read_splits (struct nr_aux_data *, int compare);
1010 static int nr_read_factors (struct nr_aux_data *, int cell);
1011 static void nr_output_data (struct nr_aux_data *, struct ccase *,
1012 write_case_func *, write_case_data);
1013 static void matrix_data_read_without_rowtype (struct case_source *source,
1018 /* Read from the data file and write it to the active file. */
1020 read_matrices_without_rowtype (struct matrix_data_pgm *mx)
1022 struct nr_aux_data nr;
1024 if (mx->cells == -1)
1029 nr.factor_values = xmalloc (sizeof *nr.factor_values * mx->n_factors * mx->cells);
1030 nr.max_cell_idx = 0;
1031 nr.split_values = xmalloc (sizeof *nr.split_values
1032 * dict_get_split_cnt (default_dict));
1034 vfm_source = create_case_source (&matrix_data_without_rowtype_source_class,
1037 procedure (NULL, &nr);
1039 free (nr.split_values);
1040 free (nr.factor_values);
1042 fh_close_handle (mx->data_file);
1045 /* Mirror data across the diagonal of matrix CP which contains
1046 CONTENT type data. */
1048 fill_matrix (struct matrix_data_pgm *mx, int content, double *cp)
1050 int type = content_type[content];
1052 if (type == 1 && mx->section != FULL)
1054 if (mx->diag == NODIAGONAL)
1056 const double fill = content == CORR ? 1.0 : SYSMIS;
1059 for (i = 0; i < mx->n_continuous; i++)
1060 cp[i * (1 + mx->n_continuous)] = fill;
1066 if (mx->section == LOWER)
1068 int n_lines = mx->n_continuous;
1069 if (mx->section != FULL && mx->diag == NODIAGONAL)
1072 for (r = 1; r < n_lines; r++)
1073 for (c = 0; c < r; c++)
1074 cp[r + c * mx->n_continuous] = cp[c + r * mx->n_continuous];
1078 assert (mx->section == UPPER);
1079 for (r = 1; r < mx->n_continuous; r++)
1080 for (c = 0; c < r; c++)
1081 cp[c + r * mx->n_continuous] = cp[r + c * mx->n_continuous];
1089 for (c = 1; c < mx->n_continuous; c++)
1094 /* Read data lines for content type CONTENT from the data file.
1095 If PER_FACTOR is nonzero, then factor information is read from
1096 the data file. Data is for cell number CELL. */
1098 nr_read_data_lines (struct nr_aux_data *nr,
1099 int per_factor, int cell, int content, int compare)
1101 struct matrix_data_pgm *mx = nr->mx;
1102 const int type = content_type[content]; /* Content type. */
1103 int n_lines; /* Number of lines to parse from data file for this type. */
1104 double *cp; /* Current position in vector or matrix. */
1111 n_lines = mx->n_continuous;
1112 if (mx->section != FULL && mx->diag == NODIAGONAL)
1116 cp = nr->data[content][cell];
1117 if (type == 1 && mx->section == LOWER && mx->diag == NODIAGONAL)
1118 cp += mx->n_continuous;
1120 for (i = 0; i < n_lines; i++)
1124 if (!nr_read_splits (nr, 1))
1126 if (per_factor && !nr_read_factors (nr, cell))
1133 n_cols = mx->n_continuous;
1136 switch (mx->section)
1143 n_cols = mx->n_continuous - i;
1144 if (mx->diag == NODIAGONAL)
1151 n_cols = mx->n_continuous;
1167 for (j = 0; j < n_cols; j++)
1169 struct matrix_token token;
1170 if (!mget_token (&token, mx->data_file))
1172 if (token.type != MNUM)
1174 msg (SE, _("expecting value for %s %s"),
1175 dict_get_var (default_dict, j)->name,
1176 context (mx->data_file));
1180 *cp++ = token.number;
1183 && !force_eol (mx->data_file, content_names[content]))
1185 debug_printf (("\n"));
1188 if (mx->section == LOWER)
1189 cp += mx->n_continuous - n_cols;
1192 fill_matrix (mx, content, nr->data[content][cell]);
1197 /* When ROWTYPE_ does not appear in the data, reads the matrices and
1198 writes them to the output file. Returns success. */
1200 matrix_data_read_without_rowtype (struct case_source *source,
1202 write_case_func *write_case,
1203 write_case_data wc_data)
1205 struct nr_aux_data *nr = source->aux;
1206 struct matrix_data_pgm *mx = nr->mx;
1211 nr->data = pool_alloc (mx->container, (PROX + 1) * sizeof *nr->data);
1216 for (i = 0; i <= PROX; i++)
1220 for (cp = mx->contents; *cp != EOC; cp++)
1221 if (*cp != LPAREN && *cp != RPAREN)
1223 int per_factor = mx->is_per_factor[*cp];
1226 n_entries = mx->n_continuous;
1227 if (content_type[*cp] == 1)
1228 n_entries *= mx->n_continuous;
1231 int n_vectors = per_factor ? mx->cells : 1;
1234 nr->data[*cp] = pool_alloc (mx->container,
1235 n_vectors * sizeof **nr->data);
1237 for (i = 0; i < n_vectors; i++)
1238 nr->data[*cp][i] = pool_alloc (mx->container,
1239 n_entries * sizeof ***nr->data);
1248 if (!nr_read_splits (nr, 0))
1251 for (bp = mx->contents; *bp != EOC; bp = np)
1255 /* Trap the CONTENTS that we should parse in this pass
1256 between bp and ep. Set np to the starting bp for next
1261 while (*ep != RPAREN)
1269 while (*ep != EOC && *ep != LPAREN)
1278 for (i = 0; i < (per_factor ? mx->cells : 1); i++)
1282 for (cp = bp; cp < ep; cp++)
1283 if (!nr_read_data_lines (nr, per_factor, i, *cp, cp != bp))
1289 nr_output_data (nr, c, write_case, wc_data);
1291 if (dict_get_split_cnt (default_dict) == 0
1292 || !another_token (mx->data_file))
1297 /* Read the split file variables. If COMPARE is 1, compares the
1298 values read to the last values read and returns 1 if they're equal,
1301 nr_read_splits (struct nr_aux_data *nr, int compare)
1303 struct matrix_data_pgm *mx = nr->mx;
1304 static int just_read = 0; /* FIXME: WTF? */
1308 if (compare && just_read)
1314 if (dict_get_split_vars (default_dict) == NULL)
1317 if (mx->single_split)
1321 = ++dict_get_split_vars (default_dict)[0]->p.mxd.subtype;
1328 split_cnt = dict_get_split_cnt (default_dict);
1329 for (i = 0; i < split_cnt; i++)
1331 struct matrix_token token;
1332 if (!mget_token (&token, mx->data_file))
1334 if (token.type != MNUM)
1336 msg (SE, _("Syntax error expecting SPLIT FILE value %s."),
1337 context (mx->data_file));
1342 nr->split_values[i] = token.number;
1343 else if (nr->split_values[i] != token.number)
1345 msg (SE, _("Expecting value %g for %s."),
1346 nr->split_values[i],
1347 dict_get_split_vars (default_dict)[i]->name);
1355 /* Read the factors for cell CELL. If COMPARE is 1, compares the
1356 values read to the last values read and returns 1 if they're equal,
1359 nr_read_factors (struct nr_aux_data *nr, int cell)
1361 struct matrix_data_pgm *mx = nr->mx;
1364 if (mx->n_factors == 0)
1367 assert (nr->max_cell_idx >= cell);
1368 if (cell != nr->max_cell_idx)
1379 for (i = 0; i < mx->n_factors; i++)
1381 struct matrix_token token;
1382 if (!mget_token (&token, mx->data_file))
1384 if (token.type != MNUM)
1386 msg (SE, _("Syntax error expecting factor value %s."),
1387 context (mx->data_file));
1392 nr->factor_values[i + mx->n_factors * cell] = token.number;
1393 else if (nr->factor_values[i + mx->n_factors * cell] != token.number)
1395 msg (SE, _("Syntax error expecting value %g for %s %s."),
1396 nr->factor_values[i + mx->n_factors * cell],
1397 mx->factors[i]->name, context (mx->data_file));
1406 /* Write the contents of a cell having content type CONTENT and data
1407 CP to the active file. */
1409 dump_cell_content (struct matrix_data_pgm *mx, int content, double *cp,
1411 write_case_func *write_case, write_case_data wc_data)
1413 int type = content_type[content];
1416 st_bare_pad_copy (c->data[mx->rowtype_->fv].s,
1417 content_names[content], 8);
1420 memset (&c->data[mx->varname_->fv].s, ' ', 8);
1424 int n_lines = (type == 1) ? mx->n_continuous : 1;
1427 for (i = 0; i < n_lines; i++)
1431 for (j = 0; j < mx->n_continuous; j++)
1433 int fv = dict_get_var (default_dict, mx->first_continuous + j)->fv;
1434 c->data[fv].f = *cp;
1438 st_bare_pad_copy (c->data[mx->varname_->fv].s,
1439 dict_get_var (default_dict,
1440 mx->first_continuous + i)->name,
1442 write_case (wc_data);
1447 /* Finally dump out everything from nr_data[] to the output file. */
1449 nr_output_data (struct nr_aux_data *nr, struct ccase *c,
1450 write_case_func *write_case, write_case_data wc_data)
1452 struct matrix_data_pgm *mx = nr->mx;
1455 struct variable *const *split;
1459 split_cnt = dict_get_split_cnt (default_dict);
1460 split = dict_get_split_vars (default_dict);
1461 for (i = 0; i < split_cnt; i++)
1462 c->data[split[i]->fv].f = nr->split_values[i];
1469 for (cell = 0; cell < mx->cells; cell++)
1474 for (factor = 0; factor < mx->n_factors; factor++)
1476 c->data[mx->factors[factor]->fv].f
1477 = nr->factor_values[factor + cell * mx->n_factors];
1478 debug_printf (("f:%s ", mx->factors[factor]->name));
1485 for (content = 0; content <= PROX; content++)
1486 if (mx->is_per_factor[content])
1488 assert (nr->data[content] != NULL
1489 && nr->data[content][cell] != NULL);
1491 dump_cell_content (mx, content, nr->data[content][cell],
1492 c, write_case, wc_data);
1504 for (factor = 0; factor < mx->n_factors; factor++)
1505 c->data[mx->factors[factor]->fv].f = SYSMIS;
1508 for (content = 0; content <= PROX; content++)
1509 if (!mx->is_per_factor[content] && nr->data[content] != NULL)
1510 dump_cell_content (mx, content, nr->data[content][0],
1511 c, write_case, wc_data);
1515 /* Back end, with ROWTYPE_. */
1517 /* All the data for one set of factor values. */
1521 int n_rows[PROX + 1];
1522 double *data[PROX + 1];
1523 struct factor_data *next;
1526 /* With ROWTYPE_ auxiliary data. */
1529 struct matrix_data_pgm *mx; /* MATRIX DATA program. */
1530 int content; /* Type of current row. */
1531 double *split_values; /* SPLIT FILE variable values. */
1532 struct factor_data *data; /* All the data. */
1533 struct factor_data *current; /* Current factor. */
1536 static int wr_read_splits (struct wr_aux_data *, struct ccase *,
1537 write_case_func *, write_case_data);
1538 static int wr_output_data (struct wr_aux_data *, struct ccase *,
1539 write_case_func *, write_case_data);
1540 static int wr_read_rowtype (struct wr_aux_data *,
1541 const struct matrix_token *, struct file_handle *);
1542 static int wr_read_factors (struct wr_aux_data *);
1543 static int wr_read_indeps (struct wr_aux_data *);
1544 static void matrix_data_read_with_rowtype (struct case_source *,
1549 /* When ROWTYPE_ appears in the data, reads the matrices and writes
1550 them to the output file. */
1552 read_matrices_with_rowtype (struct matrix_data_pgm *mx)
1554 struct wr_aux_data wr;
1558 wr.split_values = NULL;
1563 vfm_source = create_case_source (&matrix_data_with_rowtype_source_class,
1565 procedure (NULL, &wr);
1567 free (wr.split_values);
1568 fh_close_handle (mx->data_file);
1571 /* Read from the data file and write it to the active file. */
1573 matrix_data_read_with_rowtype (struct case_source *source,
1575 write_case_func *write_case,
1576 write_case_data wc_data)
1578 struct wr_aux_data *wr = source->aux;
1579 struct matrix_data_pgm *mx = wr->mx;
1583 if (!wr_read_splits (wr, c, write_case, wc_data))
1586 if (!wr_read_factors (wr))
1589 if (!wr_read_indeps (wr))
1592 while (another_token (mx->data_file));
1594 wr_output_data (wr, c, write_case, wc_data);
1597 /* Read the split file variables. If they differ from the previous
1598 set of split variables then output the data. Returns success. */
1600 wr_read_splits (struct wr_aux_data *wr,
1602 write_case_func *write_case, write_case_data wc_data)
1604 struct matrix_data_pgm *mx = wr->mx;
1608 split_cnt = dict_get_split_cnt (default_dict);
1612 if (wr->split_values)
1617 wr->split_values = xmalloc (split_cnt * sizeof *wr->split_values);
1624 for (i = 0; i < split_cnt; i++)
1626 struct matrix_token token;
1627 if (!mget_token (&token, mx->data_file))
1629 if (token.type != MNUM)
1631 msg (SE, _("Syntax error %s expecting SPLIT FILE value."),
1632 context (mx->data_file));
1636 if (compare && wr->split_values[i] != token.number && !different)
1638 if (!wr_output_data (wr, c, write_case, wc_data))
1643 wr->split_values[i] = token.number;
1650 /* Compares doubles A and B, treating SYSMIS as greatest. */
1652 compare_doubles (const void *a_, const void *b_, void *aux UNUSED)
1654 const double *a = a_;
1655 const double *b = b_;
1659 else if (*a == SYSMIS)
1661 else if (*b == SYSMIS)
1669 /* Return strcmp()-type comparison of the MX->n_factors factors at _A and
1670 _B. Sort missing values toward the end. */
1672 compare_factors (const void *a_, const void *b_, void *mx_)
1674 struct matrix_data_pgm *mx = mx_;
1675 struct factor_data *const *pa = a_;
1676 struct factor_data *const *pb = b_;
1677 const double *a = (*pa)->factors;
1678 const double *b = (*pb)->factors;
1680 return lexicographical_compare_3way (a, mx->n_factors,
1683 compare_doubles, NULL);
1686 /* Write out the data for the current split file to the active
1689 wr_output_data (struct wr_aux_data *wr,
1691 write_case_func *write_case, write_case_data wc_data)
1693 struct matrix_data_pgm *mx = wr->mx;
1696 struct variable *const *split;
1700 split_cnt = dict_get_split_cnt (default_dict);
1701 split = dict_get_split_vars (default_dict);
1702 for (i = 0; i < split_cnt; i++)
1703 c->data[split[i]->fv].f = wr->split_values[i];
1706 /* Sort the wr->data list. */
1708 struct factor_data **factors;
1709 struct factor_data *iter;
1712 factors = xmalloc (sizeof *factors * mx->cells);
1714 for (i = 0, iter = wr->data; iter; iter = iter->next, i++)
1717 sort (factors, mx->cells, sizeof *factors, compare_factors, mx);
1719 wr->data = factors[0];
1720 for (i = 0; i < mx->cells - 1; i++)
1721 factors[i]->next = factors[i + 1];
1722 factors[mx->cells - 1]->next = NULL;
1727 /* Write out records for every set of factor values. */
1729 struct factor_data *iter;
1731 for (iter = wr->data; iter; iter = iter->next)
1736 for (factor = 0; factor < mx->n_factors; factor++)
1738 c->data[mx->factors[factor]->fv].f
1739 = iter->factors[factor];
1740 debug_printf (("f:%s ", factors[factor]->name));
1747 for (content = 0; content <= PROX; content++)
1749 if (!iter->n_rows[content])
1753 int type = content_type[content];
1754 int n_lines = (type == 1
1756 - (mx->section != FULL && mx->diag == NODIAGONAL))
1759 if (n_lines != iter->n_rows[content])
1761 msg (SE, _("Expected %d lines of data for %s content; "
1762 "actually saw %d lines. No data will be "
1763 "output for this content."),
1764 n_lines, content_names[content],
1765 iter->n_rows[content]);
1770 fill_matrix (mx, content, iter->data[content]);
1772 dump_cell_content (mx, content, iter->data[content],
1773 c, write_case, wc_data);
1779 pool_destroy (mx->container);
1780 mx->container = pool_create ();
1782 wr->data = wr->current = NULL;
1787 /* Sets ROWTYPE_ based on the given TOKEN read from DATA_FILE.
1790 wr_read_rowtype (struct wr_aux_data *wr,
1791 const struct matrix_token *token,
1792 struct file_handle *data_file)
1794 if (wr->content != -1)
1796 msg (SE, _("Multiply specified ROWTYPE_ %s."), context (data_file));
1799 if (token->type != MSTR)
1801 msg (SE, _("Syntax error %s expecting ROWTYPE_ string."),
1802 context (data_file));
1810 memcpy (s, token->string, min (15, token->length));
1811 s[min (15, token->length)] = 0;
1813 for (cp = s; *cp; cp++)
1814 *cp = toupper ((unsigned char) *cp);
1816 wr->content = string_to_content_type (s, NULL);
1819 if (wr->content == -1)
1821 msg (SE, _("Syntax error %s."), context (data_file));
1828 /* Read the factors for the current row. Select a set of factors and
1829 point wr_current to it. */
1831 wr_read_factors (struct wr_aux_data *wr)
1833 struct matrix_data_pgm *mx = wr->mx;
1834 double *factor_values = local_alloc (sizeof *factor_values * mx->n_factors);
1840 for (i = 0; i < mx->n_factors; i++)
1842 struct matrix_token token;
1843 if (!mget_token (&token, mx->data_file))
1845 if (token.type == MSTR)
1847 if (!wr_read_rowtype (wr, &token, mx->data_file))
1849 if (!mget_token (&token, mx->data_file))
1852 if (token.type != MNUM)
1854 msg (SE, _("Syntax error expecting factor value %s."),
1855 context (mx->data_file));
1859 factor_values[i] = token.number;
1862 if (wr->content == -1)
1864 struct matrix_token token;
1865 if (!mget_token (&token, mx->data_file))
1867 if (!wr_read_rowtype (wr, &token, mx->data_file))
1871 /* Try the most recent factor first as a simple caching
1877 for (i = 0; i < mx->n_factors; i++)
1878 if (factor_values[i] != wr->current->factors[i])
1883 /* Linear search through the list. */
1886 struct factor_data *iter;
1888 for (iter = wr->data; iter; iter = iter->next)
1892 for (i = 0; i < mx->n_factors; i++)
1893 if (factor_values[i] != iter->factors[i])
1903 /* Not found. Make a new item. */
1905 struct factor_data *new = pool_alloc (mx->container, sizeof *new);
1907 new->factors = pool_alloc (mx->container, sizeof *new->factors * mx->n_factors);
1912 for (i = 0; i < mx->n_factors; i++)
1913 new->factors[i] = factor_values[i];
1919 for (i = 0; i <= PROX; i++)
1922 new->data[i] = NULL;
1926 new->next = wr->data;
1927 wr->data = wr->current = new;
1932 local_free (factor_values);
1936 local_free (factor_values);
1940 /* Read the independent variables into wr->current. */
1942 wr_read_indeps (struct wr_aux_data *wr)
1944 struct matrix_data_pgm *mx = wr->mx;
1945 struct factor_data *c = wr->current;
1946 const int type = content_type[wr->content];
1947 const int n_rows = c->n_rows[wr->content];
1951 /* Allocate room for data if necessary. */
1952 if (c->data[wr->content] == NULL)
1954 int n_items = mx->n_continuous;
1956 n_items *= mx->n_continuous;
1958 c->data[wr->content] = pool_alloc (mx->container,
1959 sizeof **c->data * n_items);
1962 cp = &c->data[wr->content][n_rows * mx->n_continuous];
1964 /* Figure out how much to read from this line. */
1971 msg (SE, _("Duplicate specification for %s."),
1972 content_names[wr->content]);
1976 n_cols = mx->n_continuous;
1981 if (n_rows >= mx->n_continuous - (mx->section != FULL && mx->diag == NODIAGONAL))
1983 msg (SE, _("Too many rows of matrix data for %s."),
1984 content_names[wr->content]);
1988 switch (mx->section)
1991 n_cols = n_rows + 1;
1992 if (mx->diag == NODIAGONAL)
1993 cp += mx->n_continuous;
1997 n_cols = mx->n_continuous - n_rows;
1998 if (mx->diag == NODIAGONAL)
2005 n_cols = mx->n_continuous;
2014 c->n_rows[wr->content]++;
2016 debug_printf ((" (c=%p,r=%d,n=%d)", c, n_rows + 1, n_cols));
2018 /* Read N_COLS items at CP. */
2022 for (j = 0; j < n_cols; j++)
2024 struct matrix_token token;
2025 if (!mget_token (&token, mx->data_file))
2027 if (token.type != MNUM)
2029 msg (SE, _("Syntax error expecting value for %s %s."),
2030 dict_get_var (default_dict, mx->first_continuous + j)->name,
2031 context (mx->data_file));
2035 *cp++ = token.number;
2038 && !force_eol (mx->data_file, content_names[wr->content]))
2040 debug_printf (("\n"));
2046 /* Matrix source. */
2048 static const struct case_source_class matrix_data_with_rowtype_source_class =
2052 matrix_data_read_with_rowtype,
2056 static const struct case_source_class
2057 matrix_data_without_rowtype_source_class =
2061 matrix_data_read_without_rowtype,