Update interface of gmx_stats_t
[alexxy/gromacs.git] / src / gromacs / statistics / statistics.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5  * Copyright (c) 2001-2008, The GROMACS development team.
6  * Copyright (c) 2010,2014,2015,2019,2021, by the GROMACS development team, led by
7  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8  * and including many others, as listed in the AUTHORS file in the
9  * top-level source directory and at http://www.gromacs.org.
10  *
11  * GROMACS is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public License
13  * as published by the Free Software Foundation; either version 2.1
14  * of the License, or (at your option) any later version.
15  *
16  * GROMACS is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with GROMACS; if not, see
23  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
25  *
26  * If you want to redistribute modifications to GROMACS, please
27  * consider that scientific software is very special. Version
28  * control is crucial - bugs must be traceable. We will be happy to
29  * consider code for inclusion in the official distribution, but
30  * derived work must not be called official GROMACS. Details are found
31  * in the README & COPYING files - if they are missing, get the
32  * official version at http://www.gromacs.org.
33  *
34  * To help us fund GROMACS development, we humbly ask that you cite
35  * the research papers on the package. Check out http://www.gromacs.org.
36  */
37 /*! \libinternal \file
38  * \brief
39  * Declares simple statistics toolbox
40  *
41  * \authors David van der Spoel <david.vanderspoel@icm.uu.se>
42  * \inlibraryapi
43  */
44 #ifndef GMX_STATISTICS_H
45 #define GMX_STATISTICS_H
46
47 #include <cstdio>
48
49 #include <tuple>
50
51 #include "gromacs/utility/real.h"
52
53 //! Abstract container type
54 typedef struct gmx_stats* gmx_stats_t;
55
56 //! Enum for statistical weights
57 enum
58 {
59     elsqWEIGHT_NONE,
60     elsqWEIGHT_X,
61     elsqWEIGHT_Y,
62     elsqWEIGHT_XY,
63     elsqWEIGHT_NR
64 };
65
66 /*! \brief
67  * Initiate a data structure
68  * \return the data structure
69  */
70 gmx_stats_t gmx_stats_init();
71
72 /*! \brief
73  * Destroy a data structure
74  * \param stats The data structure
75  */
76 void gmx_stats_free(gmx_stats_t stats);
77
78 /*! \brief
79  * Add a point to the data set
80  * \param[in] stats The data structure
81  * \param[in] x   The x value
82  * \param[in] y   The y value
83  * \param[in] dx  The error in the x value
84  * \param[in] dy  The error in the y value
85  */
86 void gmx_stats_add_point(gmx_stats_t stats, double x, double y, double dx, double dy);
87
88 /*! \brief
89  * Fit the data to y = ax + b, possibly weighted, if uncertainties
90  * have been input. da and db may be NULL.
91  * \param[in] stats The data structure
92  * \param[in] weight type of weighting
93  * \param[out] a slope
94  * \param[out] b intercept
95  * \param[out] da sigma in a
96  * \param[out] db sigma in b
97  * \param[out] chi2 normalized quality of fit
98  * \param[out] Rfit correlation coefficient
99  */
100 void gmx_stats_get_ab(gmx_stats_t stats, int weight, real* a, real* b, real* da, real* db, real* chi2, real* Rfit);
101
102 /*! \brief
103  * Computes and returns the average value.
104  * \param[in]  stats The data structure
105  * \param[out] aver  Average value
106  * \return Average value
107  * \throws  InconsistentInputError if given no points to average
108  */
109 real gmx_stats_get_average(gmx_stats_t stats);
110
111 /*! \brief
112  * Pointers may be null, in which case no assignment will be done.
113  * \param[in]  stats The data structure
114  * \return Tuple of (average value, its standard deviation, its standard error)
115  * \throws  InconsistentInputError if given no points to analyze
116  */
117 std::tuple<real, real, real> gmx_stats_get_ase(gmx_stats_t gstats);
118
119 /****************************************************
120  * Some statistics utilities for convenience: useful when a complete data
121  * set is available already from another source, e.g. an xvg file.
122  ****************************************************/
123
124 /*! \brief
125  * Fit a straight line y=ax+b thru the n data points x, y.
126  * \param[in] n number of points
127  * \param[in] x data points x
128  * \param[in] y data point y
129  * \param[out] a slope
130  * \param[out] b intercept
131  * \param[out] r correlation coefficient
132  * \param[out] chi2 quality of fit
133  *
134  * \throws  InconsistentInputError if given no points to fit
135  */
136 void lsq_y_ax_b(int n, real x[], real y[], real* a, real* b, real* r, real* chi2);
137
138 /*! \copydoc lsq_y_ax_b
139  * Suits cases where x is already always computed in double precision
140  * even in a mixed-precision build configuration.
141  */
142 void lsq_y_ax_b_xdouble(int n, double x[], real y[], real* a, real* b, real* r, real* chi2);
143
144 /*! \brief
145  * Fit a straight line y=ax+b thru the n data points x, y.
146  * \param[in] n number of points
147  * \param[in] x data points x
148  * \param[in] y data point y
149  * \param[in] dy uncertainty in data point y
150  * \param[out] a slope
151  * \param[out] b intercept
152  * \param[out] da error in slope
153  * \param[out] db error in intercept
154  * \param[out] r correlation coefficient
155  * \param[out] chi2 quality of fit
156  *
157  * \throws  InconsistentInputError if given no points to fit
158  */
159 void lsq_y_ax_b_error(int n, real x[], real y[], real dy[], real* a, real* b, real* da, real* db, real* r, real* chi2);
160
161 #endif