1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2021 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "data/any-reader.h"
22 #include "data/any-writer.h"
23 #include "data/casereader.h"
24 #include "data/casewriter.h"
25 #include "data/dataset.h"
26 #include "data/dictionary.h"
27 #include "language/data-io/file-handle.h"
28 #include "language/data-io/matrix-reader.h"
29 #include "language/lexer/lexer.h"
30 #include "language/command.h"
33 #define _(msgid) gettext (msgid)
36 cmd_mconvert (struct lexer *lexer, struct dataset *ds)
39 struct file_handle *in = NULL;
40 struct file_handle *out = NULL;
41 while (lex_token (lexer) != T_ENDCMD)
43 lex_match (lexer, T_SLASH);
45 if (lex_match_id (lexer, "APPEND"))
47 else if (lex_match_id (lexer, "REPLACE"))
51 if (lex_match_id (lexer, "MATRIX"))
52 lex_match (lexer, T_EQUALS);
54 struct file_handle **fhp = (lex_match_id (lexer, "IN") ? &in
55 : lex_match_id (lexer, "OUT") ? &out
59 lex_error_expecting (lexer, "IN", "OUT", "APPEND", "REPLACE");
62 if (!lex_force_match (lexer, T_LPAREN))
66 if (lex_match (lexer, T_ASTERISK))
70 *fhp = fh_parse (lexer, FH_REF_FILE, dataset_session (ds));
75 if (!lex_force_match (lexer, T_RPAREN))
80 if (!in && !dataset_has_source (ds))
82 msg (SE, _("No active file is defined and no external file is "
83 "specified on MATRIX=IN."));
88 struct casereader *cr;
91 cr = any_reader_open_and_decode (in, NULL, &d, NULL);
97 d = dict_clone (dataset_dict (ds));
101 struct matrix_reader *mr = matrix_reader_create (d, cr);
104 casereader_destroy (cr);
111 struct casewriter *cw;
114 cw = any_writer_open (out, d);
117 matrix_reader_destroy (mr);
118 casereader_destroy (cr);
126 cw = autopaging_writer_create (dict_get_proto (d));
130 struct matrix_material mm;
131 struct casereader *group;
132 if (!matrix_reader_next (&mm, mr, &group))
135 bool add_corr = mm.cov && !mm.corr;
136 bool add_cov = mm.corr && !mm.cov && mm.var_matrix;
137 bool add_stddev = add_corr && !mm.var_matrix;
138 bool remove_corr = add_cov && !append;
139 bool remove_cov = add_corr && !append;
141 struct ccase *model = casereader_peek (group, 0);
142 for (size_t i = 0; i < mr->n_fvars; i++)
143 *case_num_rw (model, mr->fvars[i]) = SYSMIS;
147 struct ccase *c = casereader_read (group);
151 struct substring rowtype = matrix_reader_get_string (c, mr->rowtype);
152 if ((remove_cov && ss_equals_case (rowtype, ss_cstr ("COV")))
153 || (remove_corr && ss_equals_case (rowtype, ss_cstr ("CORR"))))
156 casewriter_write (cw, c);
158 casereader_destroy (group);
162 for (size_t y = 0; y < mr->n_cvars; y++)
164 struct ccase *c = case_clone (model);
165 for (size_t x = 0; x < mr->n_cvars; x++)
167 double d1 = gsl_matrix_get (mm.cov, x, x);
168 double d2 = gsl_matrix_get (mm.cov, y, y);
169 double cov = gsl_matrix_get (mm.cov, y, x);
170 *case_num_rw (c, mr->cvars[x]) = cov / sqrt (d1 * d2);
172 matrix_reader_set_string (c, mr->rowtype, ss_cstr ("CORR"));
173 matrix_reader_set_string (c, mr->varname,
174 ss_cstr (var_get_name (mr->cvars[y])));
175 casewriter_write (cw, c);
181 struct ccase *c = case_clone (model);
182 for (size_t x = 0; x < mr->n_cvars; x++)
184 double variance = gsl_matrix_get (mm.cov, x, x);
185 *case_num_rw (c, mr->cvars[x]) = sqrt (variance);
187 matrix_reader_set_string (c, mr->rowtype, ss_cstr ("STDDEV"));
188 matrix_reader_set_string (c, mr->varname, ss_empty ());
189 casewriter_write (cw, c);
194 for (size_t y = 0; y < mr->n_cvars; y++)
196 struct ccase *c = case_clone (model);
197 for (size_t x = 0; x < mr->n_cvars; x++)
199 double d1 = gsl_matrix_get (mm.var_matrix, x, x);
200 double d2 = gsl_matrix_get (mm.var_matrix, y, y);
201 double corr = gsl_matrix_get (mm.corr, y, x);
202 *case_num_rw (c, mr->cvars[x]) = corr * sqrt (d1 * d2);
204 matrix_reader_set_string (c, mr->rowtype, ss_cstr ("COV"));
205 matrix_reader_set_string (c, mr->varname,
206 ss_cstr (var_get_name (mr->cvars[y])));
207 casewriter_write (cw, c);
212 matrix_material_uninit (&mm);
215 matrix_reader_destroy (mr);
219 casewriter_destroy (cw);
222 dataset_set_dict (ds, dict_ref (d));
223 dataset_set_source (ds, casewriter_make_reader (cw));