1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000 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, '*'))
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) != '/')
84 lex_force_match (lexer, '/');
87 fprintf (stderr, "%s => ", lex_rest_of_line (lexer));
92 struct moments *m = NULL;
94 m = moments_create (MOMENT_KURTOSIS);
95 if (!read_values (lexer, &values, &weights, &cnt))
100 for (i = 0; i < cnt; i++)
101 moments_pass_one (m, values[i], weights[i]);
102 for (i = 0; i < cnt; i++)
103 moments_pass_two (m, values[i], weights[i]);
104 moments_calculate (m, &weight, &M[0], &M[1], &M[2], &M[3]);
109 struct moments1 *m = NULL;
111 m = moments1_create (MOMENT_KURTOSIS);
112 if (!read_values (lexer, &values, &weights, &cnt))
114 moments1_destroy (m);
117 for (i = 0; i < cnt; i++)
118 moments1_add (m, values[i], weights[i]);
119 moments1_calculate (m, &weight, &M[0], &M[1], &M[2], &M[3]);
120 moments1_destroy (m);
123 fprintf (stderr, "W=%.3f", weight);
124 for (i = 0; i < 4; i++)
126 fprintf (stderr, " M%zu=", i + 1);
128 fprintf (stderr, "sysmis");
129 else if (fabs (M[i]) <= 0.0005)
130 fprintf (stderr, "0.000");
132 fprintf (stderr, "%.3f", M[i]);
134 fprintf (stderr, "\n");
136 retval = lex_end_of_command (lexer);