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 int 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 = NULL;
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"))
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, PV_NONE))
375 for (i = 0; i < mx->n_factors; i++)
377 struct variable *v = mx->factors[i];
378 struct mxd_var *mv = v->aux;
380 if (mv->var_type != MXD_CONTINUOUS)
382 msg (SE, _("Factor variable %s is already another type."),
387 attach_mxd_aux (v, MXD_FACTOR, i);
391 else if (lex_match_id ("CELLS"))
397 msg (SE, _("CELLS subcommand multiply specified."));
401 if (!lex_is_integer () || lex_integer () < 1)
403 lex_error (_("expecting positive integer"));
407 mx->cells = lex_integer ();
410 else if (lex_match_id ("N"))
416 msg (SE, _("N subcommand multiply specified."));
420 if (!lex_is_integer () || lex_integer () < 1)
422 lex_error (_("expecting positive integer"));
426 mx->pop_n = lex_integer ();
429 else if (lex_match_id ("CONTENTS"))
431 int inside_parens = 0;
432 unsigned collide = 0;
437 msg (SE, _("CONTENTS subcommand multiply specified."));
447 for (i = 0; i <= PROX; i++)
448 mx->is_per_factor[i] = 0;
457 msg (SE, _("Nested parentheses not allowed."));
463 else if (lex_match (')'))
467 msg (SE, _("Mismatched right parenthesis (`(')."));
470 if (mx->contents[mx->n_contents - 1] == LPAREN)
472 msg (SE, _("Empty parentheses not allowed."));
485 lex_error (_("in CONTENTS subcommand"));
489 content_type = string_to_content_type (tokid,
491 if (content_type == -1)
493 lex_error (_("in CONTENTS subcommand"));
498 if (collide & (1 << collide_index))
500 msg (SE, _("Content multiply specified for %s."),
501 content_names[content_type]);
504 collide |= (1 << collide_index);
507 mx->is_per_factor[item] = inside_parens;
509 mx->contents[mx->n_contents++] = item;
511 if (token == '/' || token == '.')
517 msg (SE, _("Missing right parenthesis."));
520 mx->contents[mx->n_contents] = EOC;
531 lex_error (_("expecting end of command"));
537 msg (SE, _("Missing VARIABLES subcommand."));
541 if (!mx->n_contents && !mx->explicit_rowtype)
543 msg (SW, _("CONTENTS subcommand not specified: assuming file "
544 "contains only CORR matrix."));
546 mx->contents[0] = CORR;
547 mx->contents[1] = EOC;
551 if (mx->n_factors && !mx->explicit_rowtype && mx->cells == -1)
553 msg (SE, _("Missing CELLS subcommand. CELLS is required "
554 "when ROWTYPE_ is not given in the data and "
555 "factors are present."));
559 if (mx->explicit_rowtype && mx->single_split)
561 msg (SE, _("Split file values must be present in the data when "
562 "ROWTYPE_ is present."));
566 /* Create VARNAME_. */
567 mx->varname_ = dict_create_var_assert (default_dict, "VARNAME_", 8);
568 attach_mxd_aux (mx->varname_, MXD_VARNAME, 0);
570 /* Sort the dictionary variables into the desired order for the
571 system file output. */
576 dict_get_vars (default_dict, &v, &nv, 0);
577 qsort (v, nv, sizeof *v, compare_variables_by_mxd_var_type);
578 dict_reorder_vars (default_dict, v, nv);
584 static const struct fmt_spec fmt_tab[MXD_COUNT] =
595 mx->first_continuous = -1;
596 for (i = 0; i < dict_get_var_cnt (default_dict); i++)
598 struct variable *v = dict_get_var (default_dict, i);
599 struct mxd_var *mv = v->aux;
600 int type = mv->var_type;
602 assert (type >= 0 && type < MXD_COUNT);
603 v->print = v->write = fmt_tab[type];
605 if (type == MXD_CONTINUOUS)
607 if (mx->first_continuous == -1 && type == MXD_CONTINUOUS)
608 mx->first_continuous = i;
612 if (mx->n_continuous == 0)
614 msg (SE, _("No continuous variables specified."));
618 mx->reader = dfm_open_reader (fh);
619 if (mx->reader == NULL)
622 if (mx->explicit_rowtype)
623 read_matrices_with_rowtype (mx);
625 read_matrices_without_rowtype (mx);
627 dfm_close_reader (mx->reader);
629 pool_destroy (mx->container);
634 discard_variables ();
636 pool_destroy (mx->container);
640 /* Look up string S as a content-type name and return the
641 corresponding enumerated value, or -1 if there is no match. If
642 COLLIDE is non-NULL then *COLLIDE returns a value (suitable for use
643 as a bit-index) which can be used for determining whether a related
644 statistic has already been used. */
646 string_to_content_type (char *s, int *collide)
657 {N_VECTOR, 0, "N_VECTOR"},
659 {N_SCALAR, 0, "N_SCALAR"},
660 {N_MATRIX, 1, "N_MATRIX"},
662 {STDDEV, 3, "STDDEV"},
674 for (tp = tab; tp->value != -1; tp++)
675 if (!strcasecmp (s, tp->string))
678 *collide = tp->collide;
685 /* Compare two variables using p.mxd.var_type and p.mxd.sub_type
688 compare_variables_by_mxd_var_type (const void *a_, const void *b_)
690 struct variable *const *pa = a_;
691 struct variable *const *pb = b_;
692 const struct mxd_var *a = (*pa)->aux;
693 const struct mxd_var *b = (*pb)->aux;
695 if (a->var_type != b->var_type)
696 return a->var_type > b->var_type ? 1 : -1;
698 return a->sub_type < b->sub_type ? -1 : a->sub_type > b->sub_type;
701 /* Attaches a struct mxd_var with the specific member values to
704 attach_mxd_aux (struct variable *v, int var_type, int sub_type)
708 assert (v->aux == NULL);
709 mv = xmalloc (sizeof *mv);
710 mv->var_type = var_type;
711 mv->sub_type = sub_type;
712 var_attach_aux (v, mv, var_dtor_free);
715 /* Matrix tokenizer. */
717 /* Matrix token types. */
718 enum matrix_token_type
724 /* A MATRIX DATA parsing token. */
727 enum matrix_token_type type;
728 double number; /* MNUM: token value. */
729 char *string; /* MSTR: token string; not null-terminated. */
730 int length; /* MSTR: tokstr length. */
733 static int mget_token (struct matrix_token *, struct dfm_reader *);
736 #define mget_token(TOKEN, READER) mget_token_dump(TOKEN, READER)
739 mdump_token (const struct matrix_token *token)
744 printf (" #%g", token->number);
747 printf (" '%.*s'", token->length, token->string);
756 mget_token_dump (struct matrix_token *token, struct dfm_reader *reader)
758 int result = (mget_token) (token, reader);
764 /* Return the current position in READER. */
766 context (struct dfm_reader *reader)
770 if (dfm_eof (reader))
771 strcpy (buf, "at end of file");
774 struct fixed_string line;
777 dfm_get_record (reader, &line);
778 sp = ls_c_str (&line);
779 while (sp < ls_end (&line) && isspace ((unsigned char) *sp))
781 if (sp >= ls_end (&line))
782 strcpy (buf, "at end of line");
788 dp = stpcpy (buf, "before `");
789 while (sp < ls_end (&line) && !isspace ((unsigned char) *sp)
802 /* Is there at least one token left in the data file? */
804 another_token (struct dfm_reader *reader)
808 struct fixed_string line;
811 if (dfm_eof (reader))
813 dfm_get_record (reader, &line);
815 cp = ls_c_str (&line);
816 while (isspace ((unsigned char) *cp) && cp < ls_end (&line))
819 if (cp < ls_end (&line))
821 dfm_forward_columns (reader, cp - ls_c_str (&line));
825 dfm_forward_record (reader);
829 /* Parse a MATRIX DATA token from READER into TOKEN. */
831 (mget_token) (struct matrix_token *token, struct dfm_reader *reader)
833 struct fixed_string line;
837 if (!another_token (reader))
840 dfm_get_record (reader, &line);
841 first_column = dfm_column_start (reader);
843 /* Three types of fields: quoted with ', quoted with ", unquoted. */
844 cp = ls_c_str (&line);
845 if (*cp == '\'' || *cp == '"')
850 token->string = ++cp;
851 while (cp < ls_end (&line) && *cp != quote)
853 token->length = cp - token->string;
854 if (cp < ls_end (&line))
857 msg (SW, _("Scope of string exceeds line."));
861 int is_num = isdigit ((unsigned char) *cp) || *cp == '.';
863 token->string = cp++;
864 while (cp < ls_end (&line)
865 && !isspace ((unsigned char) *cp) && *cp != ','
866 && *cp != '-' && *cp != '+')
868 if (isdigit ((unsigned char) *cp))
871 if ((tolower ((unsigned char) *cp) == 'd'
872 || tolower ((unsigned char) *cp) == 'e')
873 && (cp[1] == '+' || cp[1] == '-'))
879 token->length = cp - token->string;
880 assert (token->length);
886 di.s = token->string;
887 di.e = token->string + token->length;
888 di.v = (union value *) &token->number;
889 di.f1 = first_column;
890 di.format = make_output_format (FMT_F, token->length, 0);
899 dfm_forward_columns (reader, cp - ls_c_str (&line));
904 /* Forcibly skip the end of a line for content type CONTENT in
907 force_eol (struct dfm_reader *reader, const char *content)
909 struct fixed_string line;
912 if (dfm_eof (reader))
914 dfm_get_record (reader, &line);
916 cp = ls_c_str (&line);
917 while (isspace ((unsigned char) *cp) && cp < ls_end (&line))
920 if (cp < ls_end (&line))
922 msg (SE, _("End of line expected %s while reading %s."),
923 context (reader), content);
927 dfm_forward_record (reader);
931 /* Back end, omitting ROWTYPE_. */
935 struct matrix_data_pgm *mx; /* MATRIX DATA program. */
936 double ***data; /* MATRIX DATA data. */
937 double *factor_values; /* Factor values. */
938 int max_cell_idx; /* Max-numbered cell that we have
939 read so far, plus one. */
940 double *split_values; /* SPLIT FILE variable values. */
943 static int nr_read_splits (struct nr_aux_data *, int compare);
944 static int nr_read_factors (struct nr_aux_data *, int cell);
945 static void nr_output_data (struct nr_aux_data *, struct ccase *,
946 write_case_func *, write_case_data);
947 static void matrix_data_read_without_rowtype (struct case_source *source,
952 /* Read from the data file and write it to the active file. */
954 read_matrices_without_rowtype (struct matrix_data_pgm *mx)
956 struct nr_aux_data nr;
963 nr.factor_values = xmalloc (sizeof *nr.factor_values * mx->n_factors * mx->cells);
965 nr.split_values = xmalloc (sizeof *nr.split_values
966 * dict_get_split_cnt (default_dict));
968 vfm_source = create_case_source (&matrix_data_without_rowtype_source_class, &nr);
970 procedure (NULL, NULL);
972 free (nr.split_values);
973 free (nr.factor_values);
976 /* Mirror data across the diagonal of matrix CP which contains
977 CONTENT type data. */
979 fill_matrix (struct matrix_data_pgm *mx, int content, double *cp)
981 int type = content_type[content];
983 if (type == 1 && mx->section != FULL)
985 if (mx->diag == NODIAGONAL)
987 const double fill = content == CORR ? 1.0 : SYSMIS;
990 for (i = 0; i < mx->n_continuous; i++)
991 cp[i * (1 + mx->n_continuous)] = fill;
997 if (mx->section == LOWER)
999 int n_lines = mx->n_continuous;
1000 if (mx->section != FULL && mx->diag == NODIAGONAL)
1003 for (r = 1; r < n_lines; r++)
1004 for (c = 0; c < r; c++)
1005 cp[r + c * mx->n_continuous] = cp[c + r * mx->n_continuous];
1009 assert (mx->section == UPPER);
1010 for (r = 1; r < mx->n_continuous; r++)
1011 for (c = 0; c < r; c++)
1012 cp[c + r * mx->n_continuous] = cp[r + c * mx->n_continuous];
1020 for (c = 1; c < mx->n_continuous; c++)
1025 /* Read data lines for content type CONTENT from the data file.
1026 If PER_FACTOR is nonzero, then factor information is read from
1027 the data file. Data is for cell number CELL. */
1029 nr_read_data_lines (struct nr_aux_data *nr,
1030 int per_factor, int cell, int content, int compare)
1032 struct matrix_data_pgm *mx = nr->mx;
1033 const int type = content_type[content]; /* Content type. */
1034 int n_lines; /* Number of lines to parse from data file for this type. */
1035 double *cp; /* Current position in vector or matrix. */
1042 n_lines = mx->n_continuous;
1043 if (mx->section != FULL && mx->diag == NODIAGONAL)
1047 cp = nr->data[content][cell];
1048 if (type == 1 && mx->section == LOWER && mx->diag == NODIAGONAL)
1049 cp += mx->n_continuous;
1051 for (i = 0; i < n_lines; i++)
1055 if (!nr_read_splits (nr, 1))
1057 if (per_factor && !nr_read_factors (nr, cell))
1064 n_cols = mx->n_continuous;
1067 switch (mx->section)
1074 n_cols = mx->n_continuous - i;
1075 if (mx->diag == NODIAGONAL)
1082 n_cols = mx->n_continuous;
1100 for (j = 0; j < n_cols; j++)
1102 struct matrix_token token;
1103 if (!mget_token (&token, mx->reader))
1105 if (token.type != MNUM)
1107 msg (SE, _("expecting value for %s %s"),
1108 dict_get_var (default_dict, j)->name,
1109 context (mx->reader));
1113 *cp++ = token.number;
1116 && !force_eol (mx->reader, content_names[content]))
1118 debug_printf (("\n"));
1121 if (mx->section == LOWER)
1122 cp += mx->n_continuous - n_cols;
1125 fill_matrix (mx, content, nr->data[content][cell]);
1130 /* When ROWTYPE_ does not appear in the data, reads the matrices and
1131 writes them to the output file. Returns success. */
1133 matrix_data_read_without_rowtype (struct case_source *source,
1135 write_case_func *write_case,
1136 write_case_data wc_data)
1138 struct nr_aux_data *nr = source->aux;
1139 struct matrix_data_pgm *mx = nr->mx;
1144 nr->data = pool_alloc (mx->container, (PROX + 1) * sizeof *nr->data);
1149 for (i = 0; i <= PROX; i++)
1153 for (cp = mx->contents; *cp != EOC; cp++)
1154 if (*cp != LPAREN && *cp != RPAREN)
1156 int per_factor = mx->is_per_factor[*cp];
1159 n_entries = mx->n_continuous;
1160 if (content_type[*cp] == 1)
1161 n_entries *= mx->n_continuous;
1164 int n_vectors = per_factor ? mx->cells : 1;
1167 nr->data[*cp] = pool_alloc (mx->container,
1168 n_vectors * sizeof **nr->data);
1170 for (i = 0; i < n_vectors; i++)
1171 nr->data[*cp][i] = pool_alloc (mx->container,
1172 n_entries * sizeof ***nr->data);
1181 if (!nr_read_splits (nr, 0))
1184 for (bp = mx->contents; *bp != EOC; bp = np)
1188 /* Trap the CONTENTS that we should parse in this pass
1189 between bp and ep. Set np to the starting bp for next
1194 while (*ep != RPAREN)
1202 while (*ep != EOC && *ep != LPAREN)
1211 for (i = 0; i < (per_factor ? mx->cells : 1); i++)
1215 for (cp = bp; cp < ep; cp++)
1216 if (!nr_read_data_lines (nr, per_factor, i, *cp, cp != bp))
1222 nr_output_data (nr, c, write_case, wc_data);
1224 if (dict_get_split_cnt (default_dict) == 0
1225 || !another_token (mx->reader))
1230 /* Read the split file variables. If COMPARE is 1, compares the
1231 values read to the last values read and returns 1 if they're equal,
1234 nr_read_splits (struct nr_aux_data *nr, int compare)
1236 struct matrix_data_pgm *mx = nr->mx;
1237 static int just_read = 0; /* FIXME: WTF? */
1241 if (compare && just_read)
1247 if (dict_get_split_vars (default_dict) == NULL)
1250 if (mx->single_split)
1254 struct mxd_var *mv = dict_get_split_vars (default_dict)[0]->aux;
1255 nr->split_values[0] = ++mv->sub_type;
1263 split_cnt = dict_get_split_cnt (default_dict);
1264 for (i = 0; i < split_cnt; i++)
1266 struct matrix_token token;
1267 if (!mget_token (&token, mx->reader))
1269 if (token.type != MNUM)
1271 msg (SE, _("Syntax error expecting SPLIT FILE value %s."),
1272 context (mx->reader));
1277 nr->split_values[i] = token.number;
1278 else if (nr->split_values[i] != token.number)
1280 msg (SE, _("Expecting value %g for %s."),
1281 nr->split_values[i],
1282 dict_get_split_vars (default_dict)[i]->name);
1290 /* Read the factors for cell CELL. If COMPARE is 1, compares the
1291 values read to the last values read and returns 1 if they're equal,
1294 nr_read_factors (struct nr_aux_data *nr, int cell)
1296 struct matrix_data_pgm *mx = nr->mx;
1299 if (mx->n_factors == 0)
1302 assert (nr->max_cell_idx >= cell);
1303 if (cell != nr->max_cell_idx)
1314 for (i = 0; i < mx->n_factors; i++)
1316 struct matrix_token token;
1317 if (!mget_token (&token, mx->reader))
1319 if (token.type != MNUM)
1321 msg (SE, _("Syntax error expecting factor value %s."),
1322 context (mx->reader));
1327 nr->factor_values[i + mx->n_factors * cell] = token.number;
1328 else if (nr->factor_values[i + mx->n_factors * cell] != token.number)
1330 msg (SE, _("Syntax error expecting value %g for %s %s."),
1331 nr->factor_values[i + mx->n_factors * cell],
1332 mx->factors[i]->name, context (mx->reader));
1341 /* Write the contents of a cell having content type CONTENT and data
1342 CP to the active file. */
1344 dump_cell_content (struct matrix_data_pgm *mx, int content, double *cp,
1346 write_case_func *write_case, write_case_data wc_data)
1348 int type = content_type[content];
1351 buf_copy_str_rpad (case_data_rw (c, mx->rowtype_->fv)->s, 8,
1352 content_names[content]);
1355 memset (case_data_rw (c, mx->varname_->fv)->s, ' ', 8);
1359 int n_lines = (type == 1) ? mx->n_continuous : 1;
1362 for (i = 0; i < n_lines; i++)
1366 for (j = 0; j < mx->n_continuous; j++)
1368 int fv = dict_get_var (default_dict, mx->first_continuous + j)->fv;
1369 case_data_rw (c, fv)->f = *cp;
1373 buf_copy_str_rpad (case_data_rw (c, mx->varname_->fv)->s, 8,
1374 dict_get_var (default_dict,
1375 mx->first_continuous + i)->name);
1376 write_case (wc_data);
1381 /* Finally dump out everything from nr_data[] to the output file. */
1383 nr_output_data (struct nr_aux_data *nr, struct ccase *c,
1384 write_case_func *write_case, write_case_data wc_data)
1386 struct matrix_data_pgm *mx = nr->mx;
1389 struct variable *const *split;
1393 split_cnt = dict_get_split_cnt (default_dict);
1394 split = dict_get_split_vars (default_dict);
1395 for (i = 0; i < split_cnt; i++)
1396 case_data_rw (c, split[i]->fv)->f = nr->split_values[i];
1403 for (cell = 0; cell < mx->cells; cell++)
1408 for (factor = 0; factor < mx->n_factors; factor++)
1410 case_data_rw (c, mx->factors[factor]->fv)->f
1411 = nr->factor_values[factor + cell * mx->n_factors];
1412 debug_printf (("f:%s ", mx->factors[factor]->name));
1419 for (content = 0; content <= PROX; content++)
1420 if (mx->is_per_factor[content])
1422 assert (nr->data[content] != NULL
1423 && nr->data[content][cell] != NULL);
1425 dump_cell_content (mx, content, nr->data[content][cell],
1426 c, write_case, wc_data);
1438 for (factor = 0; factor < mx->n_factors; factor++)
1439 case_data_rw (c, mx->factors[factor]->fv)->f = SYSMIS;
1442 for (content = 0; content <= PROX; content++)
1443 if (!mx->is_per_factor[content] && nr->data[content] != NULL)
1444 dump_cell_content (mx, content, nr->data[content][0],
1445 c, write_case, wc_data);
1449 /* Back end, with ROWTYPE_. */
1451 /* All the data for one set of factor values. */
1455 int n_rows[PROX + 1];
1456 double *data[PROX + 1];
1457 struct factor_data *next;
1460 /* With ROWTYPE_ auxiliary data. */
1463 struct matrix_data_pgm *mx; /* MATRIX DATA program. */
1464 int content; /* Type of current row. */
1465 double *split_values; /* SPLIT FILE variable values. */
1466 struct factor_data *data; /* All the data. */
1467 struct factor_data *current; /* Current factor. */
1470 static int wr_read_splits (struct wr_aux_data *, struct ccase *,
1471 write_case_func *, write_case_data);
1472 static int wr_output_data (struct wr_aux_data *, struct ccase *,
1473 write_case_func *, write_case_data);
1474 static int wr_read_rowtype (struct wr_aux_data *,
1475 const struct matrix_token *, struct dfm_reader *);
1476 static int wr_read_factors (struct wr_aux_data *);
1477 static int wr_read_indeps (struct wr_aux_data *);
1478 static void matrix_data_read_with_rowtype (struct case_source *,
1483 /* When ROWTYPE_ appears in the data, reads the matrices and writes
1484 them to the output file. */
1486 read_matrices_with_rowtype (struct matrix_data_pgm *mx)
1488 struct wr_aux_data wr;
1492 wr.split_values = NULL;
1497 vfm_source = create_case_source (&matrix_data_with_rowtype_source_class,
1499 procedure (NULL, NULL);
1501 free (wr.split_values);
1504 /* Read from the data file and write it to the active file. */
1506 matrix_data_read_with_rowtype (struct case_source *source,
1508 write_case_func *write_case,
1509 write_case_data wc_data)
1511 struct wr_aux_data *wr = source->aux;
1512 struct matrix_data_pgm *mx = wr->mx;
1516 if (!wr_read_splits (wr, c, write_case, wc_data))
1519 if (!wr_read_factors (wr))
1522 if (!wr_read_indeps (wr))
1525 while (another_token (mx->reader));
1527 wr_output_data (wr, c, write_case, wc_data);
1530 /* Read the split file variables. If they differ from the previous
1531 set of split variables then output the data. Returns success. */
1533 wr_read_splits (struct wr_aux_data *wr,
1535 write_case_func *write_case, write_case_data wc_data)
1537 struct matrix_data_pgm *mx = wr->mx;
1541 split_cnt = dict_get_split_cnt (default_dict);
1545 if (wr->split_values)
1550 wr->split_values = xmalloc (split_cnt * sizeof *wr->split_values);
1557 for (i = 0; i < split_cnt; i++)
1559 struct matrix_token token;
1560 if (!mget_token (&token, mx->reader))
1562 if (token.type != MNUM)
1564 msg (SE, _("Syntax error %s expecting SPLIT FILE value."),
1565 context (mx->reader));
1569 if (compare && wr->split_values[i] != token.number && !different)
1571 if (!wr_output_data (wr, c, write_case, wc_data))
1576 wr->split_values[i] = token.number;
1583 /* Compares doubles A and B, treating SYSMIS as greatest. */
1585 compare_doubles (const void *a_, const void *b_, void *aux UNUSED)
1587 const double *a = a_;
1588 const double *b = b_;
1592 else if (*a == SYSMIS)
1594 else if (*b == SYSMIS)
1602 /* Return strcmp()-type comparison of the MX->n_factors factors at _A and
1603 _B. Sort missing values toward the end. */
1605 compare_factors (const void *a_, const void *b_, void *mx_)
1607 struct matrix_data_pgm *mx = mx_;
1608 struct factor_data *const *pa = a_;
1609 struct factor_data *const *pb = b_;
1610 const double *a = (*pa)->factors;
1611 const double *b = (*pb)->factors;
1613 return lexicographical_compare_3way (a, mx->n_factors,
1616 compare_doubles, NULL);
1619 /* Write out the data for the current split file to the active
1622 wr_output_data (struct wr_aux_data *wr,
1624 write_case_func *write_case, write_case_data wc_data)
1626 struct matrix_data_pgm *mx = wr->mx;
1629 struct variable *const *split;
1633 split_cnt = dict_get_split_cnt (default_dict);
1634 split = dict_get_split_vars (default_dict);
1635 for (i = 0; i < split_cnt; i++)
1636 case_data_rw (c, split[i]->fv)->f = wr->split_values[i];
1639 /* Sort the wr->data list. */
1641 struct factor_data **factors;
1642 struct factor_data *iter;
1645 factors = xmalloc (sizeof *factors * mx->cells);
1647 for (i = 0, iter = wr->data; iter; iter = iter->next, i++)
1650 sort (factors, mx->cells, sizeof *factors, compare_factors, mx);
1652 wr->data = factors[0];
1653 for (i = 0; i < mx->cells - 1; i++)
1654 factors[i]->next = factors[i + 1];
1655 factors[mx->cells - 1]->next = NULL;
1660 /* Write out records for every set of factor values. */
1662 struct factor_data *iter;
1664 for (iter = wr->data; iter; iter = iter->next)
1669 for (factor = 0; factor < mx->n_factors; factor++)
1670 case_data_rw (c, mx->factors[factor]->fv)->f
1671 = iter->factors[factor];
1677 for (content = 0; content <= PROX; content++)
1679 if (!iter->n_rows[content])
1683 int type = content_type[content];
1684 int n_lines = (type == 1
1686 - (mx->section != FULL && mx->diag == NODIAGONAL))
1689 if (n_lines != iter->n_rows[content])
1691 msg (SE, _("Expected %d lines of data for %s content; "
1692 "actually saw %d lines. No data will be "
1693 "output for this content."),
1694 n_lines, content_names[content],
1695 iter->n_rows[content]);
1700 fill_matrix (mx, content, iter->data[content]);
1702 dump_cell_content (mx, content, iter->data[content],
1703 c, write_case, wc_data);
1709 pool_destroy (mx->container);
1710 mx->container = pool_create ();
1712 wr->data = wr->current = NULL;
1717 /* Sets ROWTYPE_ based on the given TOKEN read from READER.
1720 wr_read_rowtype (struct wr_aux_data *wr,
1721 const struct matrix_token *token,
1722 struct dfm_reader *reader)
1724 if (wr->content != -1)
1726 msg (SE, _("Multiply specified ROWTYPE_ %s."), context (reader));
1729 if (token->type != MSTR)
1731 msg (SE, _("Syntax error %s expecting ROWTYPE_ string."),
1740 memcpy (s, token->string, min (15, token->length));
1741 s[min (15, token->length)] = 0;
1743 for (cp = s; *cp; cp++)
1744 *cp = toupper ((unsigned char) *cp);
1746 wr->content = string_to_content_type (s, NULL);
1749 if (wr->content == -1)
1751 msg (SE, _("Syntax error %s."), context (reader));
1758 /* Read the factors for the current row. Select a set of factors and
1759 point wr_current to it. */
1761 wr_read_factors (struct wr_aux_data *wr)
1763 struct matrix_data_pgm *mx = wr->mx;
1764 double *factor_values = local_alloc (sizeof *factor_values * mx->n_factors);
1770 for (i = 0; i < mx->n_factors; i++)
1772 struct matrix_token token;
1773 if (!mget_token (&token, mx->reader))
1775 if (token.type == MSTR)
1777 if (!wr_read_rowtype (wr, &token, mx->reader))
1779 if (!mget_token (&token, mx->reader))
1782 if (token.type != MNUM)
1784 msg (SE, _("Syntax error expecting factor value %s."),
1785 context (mx->reader));
1789 factor_values[i] = token.number;
1792 if (wr->content == -1)
1794 struct matrix_token token;
1795 if (!mget_token (&token, mx->reader))
1797 if (!wr_read_rowtype (wr, &token, mx->reader))
1801 /* Try the most recent factor first as a simple caching
1807 for (i = 0; i < mx->n_factors; i++)
1808 if (factor_values[i] != wr->current->factors[i])
1813 /* Linear search through the list. */
1816 struct factor_data *iter;
1818 for (iter = wr->data; iter; iter = iter->next)
1822 for (i = 0; i < mx->n_factors; i++)
1823 if (factor_values[i] != iter->factors[i])
1833 /* Not found. Make a new item. */
1835 struct factor_data *new = pool_alloc (mx->container, sizeof *new);
1837 new->factors = pool_alloc (mx->container, sizeof *new->factors * mx->n_factors);
1842 for (i = 0; i < mx->n_factors; i++)
1843 new->factors[i] = factor_values[i];
1849 for (i = 0; i <= PROX; i++)
1852 new->data[i] = NULL;
1856 new->next = wr->data;
1857 wr->data = wr->current = new;
1862 local_free (factor_values);
1866 local_free (factor_values);
1870 /* Read the independent variables into wr->current. */
1872 wr_read_indeps (struct wr_aux_data *wr)
1874 struct matrix_data_pgm *mx = wr->mx;
1875 struct factor_data *c = wr->current;
1876 const int type = content_type[wr->content];
1877 const int n_rows = c->n_rows[wr->content];
1881 /* Allocate room for data if necessary. */
1882 if (c->data[wr->content] == NULL)
1884 int n_items = mx->n_continuous;
1886 n_items *= mx->n_continuous;
1888 c->data[wr->content] = pool_alloc (mx->container,
1889 sizeof **c->data * n_items);
1892 cp = &c->data[wr->content][n_rows * mx->n_continuous];
1894 /* Figure out how much to read from this line. */
1901 msg (SE, _("Duplicate specification for %s."),
1902 content_names[wr->content]);
1906 n_cols = mx->n_continuous;
1911 if (n_rows >= mx->n_continuous - (mx->section != FULL && mx->diag == NODIAGONAL))
1913 msg (SE, _("Too many rows of matrix data for %s."),
1914 content_names[wr->content]);
1918 switch (mx->section)
1921 n_cols = n_rows + 1;
1922 if (mx->diag == NODIAGONAL)
1923 cp += mx->n_continuous;
1927 n_cols = mx->n_continuous - n_rows;
1928 if (mx->diag == NODIAGONAL)
1935 n_cols = mx->n_continuous;
1946 c->n_rows[wr->content]++;
1948 debug_printf ((" (c=%p,r=%d,n=%d)", c, n_rows + 1, n_cols));
1950 /* Read N_COLS items at CP. */
1954 for (j = 0; j < n_cols; j++)
1956 struct matrix_token token;
1957 if (!mget_token (&token, mx->reader))
1959 if (token.type != MNUM)
1961 msg (SE, _("Syntax error expecting value for %s %s."),
1962 dict_get_var (default_dict, mx->first_continuous + j)->name,
1963 context (mx->reader));
1967 *cp++ = token.number;
1970 && !force_eol (mx->reader, content_names[wr->content]))
1972 debug_printf (("\n"));
1978 /* Matrix source. */
1980 static const struct case_source_class matrix_data_with_rowtype_source_class =
1984 matrix_data_read_with_rowtype,
1988 static const struct case_source_class
1989 matrix_data_without_rowtype_source_class =
1993 matrix_data_read_without_rowtype,