2465d63e80a70fadb91bc1d2d488079cc4ba7304
[pspp] / src / language / expressions / helpers.h
1 /*
2 PSPP - a program for statistical analysis.
3 Copyright (C) 2017 Free Software Foundation, Inc.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef EXPRESSIONS_HELPERS_H
20 #define EXPRESSIONS_HELPERS_H
21
22 #include <ctype.h>
23 #include <float.h>
24 #include <gsl/gsl_cdf.h>
25 #include <gsl/gsl_randist.h>
26 #include <gsl/gsl_sf.h>
27 #include <limits.h>
28 #include <math.h>
29 #include <stdbool.h>
30
31 #include "data/calendar.h"
32 #include "data/case.h"
33 #include "data/data-in.h"
34 #include "data/data-out.h"
35 #include "data/dataset.h"
36 #include "data/dictionary.h"
37 #include "data/settings.h"
38 #include "data/value.h"
39 #include "data/variable.h"
40 #include "data/vector.h"
41 #include "language/expressions/public.h"
42 #include "libpspp/compiler.h"
43 #include "libpspp/i18n.h"
44 #include "libpspp/message.h"
45 #include "libpspp/misc.h"
46 #include "libpspp/str.h"
47 #include "math/moments.h"
48 #include "math/random.h"
49
50 #include "gettext.h"
51 #define _(msgid) gettext (msgid)
52
53 static inline double check_errno (double x)
54 {
55   return errno == 0 ? x : SYSMIS;
56 }
57
58 #define check_errno(EXPRESSION) (errno = 0, check_errno (EXPRESSION))
59
60 #define DAY_S (60. * 60. * 24.)         /* Seconds per day. */
61 #define DAY_H 24.                       /* Hours per day. */
62 #define H_S (60 * 60.)                  /* Seconds per hour. */
63 #define H_MIN 60.                       /* Minutes per hour. */
64 #define MIN_S 60.                       /* Seconds per minute. */
65 #define WEEK_DAY 7.                     /* Days per week. */
66 #define WEEK_S (WEEK_DAY * DAY_S)       /* Seconds per week. */
67
68 extern const struct substring empty_string;
69
70 int compare_string_3way (const struct substring *, const struct substring *);
71
72 double expr_ymd_to_date (double year, double month, double day);
73 double expr_ymd_to_ofs (double year, double month, double day);
74 double expr_wkyr_to_date (double wk, double yr);
75 double expr_yrday_to_date (double yr, double day);
76 double expr_yrmoda (double year, double month, double day);
77 double expr_date_difference (double date1, double date2,
78                              struct substring unit);
79 double expr_date_sum (double date, double quantity, struct substring unit_name,
80                       struct substring method_name);
81
82 struct substring alloc_string (struct expression *, size_t length);
83 struct substring copy_string (struct expression *,
84                               const char *, size_t length);
85
86 static inline bool
87 is_valid (double d)
88 {
89   return isfinite (d) && d != SYSMIS;
90 }
91
92 size_t count_valid (double *, size_t);
93
94 double idf_beta (double P, double a, double b);
95 double ncdf_beta (double x, double a, double b, double lambda);
96 double npdf_beta (double x, double a, double b, double lambda);
97
98 double cdf_bvnor (double x0, double x1, double r);
99
100 double idf_fdist (double P, double a, double b);
101
102 double round_nearest (double x, double mult, double fuzzbits);
103 double round_zero (double x, double mult, double fuzzbits);
104
105 struct substring replace_string (struct expression *,
106                                  struct substring haystack,
107                                  struct substring needle,
108                                  struct substring replacement,
109                                  double n);
110
111 double median (double *, size_t n);
112
113 #endif /* expressions/helpers.h */