work on pivot table and FREQUENCIES usage
[pspp] / src / output / pivot-table.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2010 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #ifndef OUTPUT_PIVOT_TABLE_H
18 #define OUTPUT_PIVOT_TABLE_H 1
19
20 #include "data/format.h"
21 #include "data/subcase.h"
22 #include "output/table.h"
23
24 struct pivot_pane
25   {
26     int n[TABLE_N_AXES];
27     struct pivot_cell **cells;  /* cells[y][x] */
28   };
29
30 struct pivot_table
31   {
32     struct casereader *data;
33     struct dictionary *dict;
34     struct subcase split;
35     struct subcase dimensions[TABLE_N_AXES];
36     struct pivot_pane pane;
37     struct pivot_pane *summaries[TABLE_N_AXES];
38   };
39
40 void pivot_table_dump (struct pivot_table *);
41
42 enum pivot_comparison_type
43   {
44     PIVOT_RAW,                  /* No comparison. */
45
46     /* 1-dimensional comparisons. */
47     PIVOT_FRACTION,             /* Fraction of an aggregate. */
48     PIVOT_PERCENT,              /* Percentage of an aggregate. */
49
50     /* 2-dimensional comparisons. */
51     PIVOT_EXPECTED,             /* row_sum * col_sum / grand_total */
52     PIVOT_RESIDUAL,             /* value - expected */
53     PIVOT_SRESIDUAL,            /* (value - expected) / sqrt(expected) */
54     PIVOT_ASRESIDUAL            /* (value - expected)
55                                    / sqrt(expected
56                                    * (1 - row_sum / grand_total)
57                                    * (1 - col_sum / grand_total)) */
58   };
59
60 /* A value, possibly compared against other values. */
61 struct pivot_cell
62   {
63     struct pivot_value *base;
64
65     /* Comparison. */
66     enum pivot_comparison_type cmp;
67     struct pivot_value *cmp_args[3];
68
69     /* Formatting. */
70     char *label;
71     struct fmt_spec format;
72   };
73
74 void pivot_cell_init (struct pivot_cell *, struct pivot_value *);
75
76 /* How to combine potentially multiple values. */
77 enum pivot_operator
78   {
79     PIVOT_EXACTLY_ONE,          /* SYSMIS if no values or more than one. */
80     PIVOT_SUM,
81     PIVOT_MEAN,
82
83     PIVOT_N,
84     PIVOT_N_VALID,
85     PIVOT_N_MISSING,
86
87     PIVOT_MODE,
88
89     PIVOT_STDDEV,
90     PIVOT_VARIANCE,
91
92     PIVOT_MINIMUM,
93     PIVOT_MAXIMUM,
94     PIVOT_RANGE,
95     PIVOT_MEDIAN,
96     PIVOT_PERCENTILE
97   };
98
99 enum pivot_value_include
100   {
101     PIVOT_INCLUDE_VALID = 1 << 0,
102     PIVOT_INCLUDE_SYSTEM_MISSING = 1 << 1,
103     PIVOT_INCLUDE_USER_MISSING = 1 << 2,
104     PIVOT_INCLUDE_ALL = 7
105   };
106
107 /* A pivot_value specifies a function whose arguments are the values of:
108
109         - The split and dimension variables of the pivot table.
110
111         - The variable specified as part of the pivot_value.  (PIVOT_N does not
112           require a variable.)
113
114    A pivot_value is notionally evaluated as:
115
116    Given a value for every variable:
117        For every row in the input data:
118            If the row's split variable equal their assigned values:
119              and the row's dimension variables equal their assigned values,
120              except that the aggregate variables, if any, are ignored,
121              and that the cumulative variables are equal or lexicographically
122              less than the assigned values,
123              and the value variable is included according to "include":
124                Submit the value to the operator.
125 */
126 struct pivot_value
127   {
128     const struct variable *var;
129     enum pivot_operator operator;
130     int n_agg_vars[TABLE_N_AXES];
131     int n_cum_vars[TABLE_N_AXES];
132     enum pivot_value_include include;
133   };
134
135 struct pivot_value *pivot_value_create (const struct variable *,
136                                         enum pivot_operator,
137                                         int n_vars_horz,
138                                         int n_vars_vert,
139                                         enum pivot_value_include);
140
141
142 #endif /* output/pivot-table.h */