1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2010 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/>. */
20 #include <language/command.h>
21 #include <language/lexer/lexer.h>
22 #include <math/moments.h>
26 #include <libpspp/compiler.h>
28 #define _(msgid) gettext (msgid)
31 read_values (struct lexer *lexer, double **values, double **weights, size_t *cnt)
38 while (lex_is_number (lexer))
40 double value = lex_tokval (lexer);
43 if (lex_match (lexer, T_ASTERISK))
45 if (!lex_is_number (lexer))
47 lex_error (lexer, _("expecting weight value"));
50 weight = lex_tokval (lexer);
57 *values = xnrealloc (*values, cap, sizeof **values);
58 *weights = xnrealloc (*weights, cap, sizeof **weights);
61 (*values)[*cnt] = value;
62 (*weights)[*cnt] = weight;
70 cmd_debug_moments (struct lexer *lexer, struct dataset *ds UNUSED)
72 int retval = CMD_FAILURE;
73 double *values = NULL;
74 double *weights = NULL;
80 if (lex_match_id (lexer, "ONEPASS"))
82 if (lex_token (lexer) != T_SLASH)
84 lex_force_match (lexer, T_SLASH);
91 struct moments *m = NULL;
93 m = moments_create (MOMENT_KURTOSIS);
94 if (!read_values (lexer, &values, &weights, &cnt))
99 for (i = 0; i < cnt; i++)
100 moments_pass_one (m, values[i], weights[i]);
101 for (i = 0; i < cnt; i++)
102 moments_pass_two (m, values[i], weights[i]);
103 moments_calculate (m, &weight, &M[0], &M[1], &M[2], &M[3]);
108 struct moments1 *m = NULL;
110 m = moments1_create (MOMENT_KURTOSIS);
111 if (!read_values (lexer, &values, &weights, &cnt))
113 moments1_destroy (m);
116 for (i = 0; i < cnt; i++)
117 moments1_add (m, values[i], weights[i]);
118 moments1_calculate (m, &weight, &M[0], &M[1], &M[2], &M[3]);
119 moments1_destroy (m);
122 fprintf (stderr, "W=%.3f", weight);
123 for (i = 0; i < 4; i++)
125 fprintf (stderr, " M%zu=", i + 1);
127 fprintf (stderr, "sysmis");
128 else if (fabs (M[i]) <= 0.0005)
129 fprintf (stderr, "0.000");
131 fprintf (stderr, "%.3f", M[i]);
133 fprintf (stderr, "\n");
135 retval = lex_end_of_command (lexer);