The outage for Sunday 24th November has been cancelled.
Bioplib
Protein Structure C Library
 All Data Structures Files Functions Variables Typedefs Macros Pages
CalcExtSD.c
Go to the documentation of this file.
1 /************************************************************************/
2 /**
3 
4  \file CalcExtSD.c
5 
6  \version V1.4
7  \date 07.07.14
8  \brief Calculate mean and standard deviation
9 
10  \copyright (c) UCL / Dr. Andrew C. R. Martin 1990-2014
11  \author Dr. Andrew C. R. Martin
12  \par
13  Institute of Structural & Molecular Biology,
14  University College London,
15  Gower Street,
16  London.
17  WC1E 6BT.
18  \par
19  andrew@bioinf.org.uk
20  andrew.martin@ucl.ac.uk
21 
22 **************************************************************************
23 
24  This code is NOT IN THE PUBLIC DOMAIN, but it may be copied
25  according to the conditions laid out in the accompanying file
26  COPYING.DOC.
27 
28  The code may be modified as required, but any modifications must be
29  documented so that the person responsible can be identified.
30 
31  The code may not be sold commercially or included as part of a
32  commercial product except as described in the file COPYING.DOC.
33 
34 **************************************************************************
35 
36  Description:
37  ============
38 
39  This routine calculates the mean and standard deviation from a set
40  of numbers. The routine is called with each value to be sampled
41  and the action required is specified.
42 
43 **************************************************************************
44 
45  Usage:
46  ======
47 
48 
49  #include "MathUtil.h" before using these routines
50 
51  void CalcExtSD(REAL val, int action, REAL *Sx, REAL *SxSq,
52  int *NValues, REAL *mean, REAL *SD)
53  ----------------------------------------------------------
54  Input: val int The value to be sampled
55  Input: action short 0: Sample the value
56  1: Calculate & return mean and SD
57  2: Clear the sample lists
58  Output: mean *REAL The returned mean
59  Output: SD *REAL The returned standard deviation
60  I/O: Sx *REAL Sum of values
61  I/O: SxSq *REAL Sum of values squared
62  I/O: NValues *int Number of values
63 
64  The output values are only set when action==1
65 
66 **************************************************************************
67 
68  Revision History:
69  =================
70 - V1.0 30.03.90 Original By: ACRM
71 - V1.1 17.06.93 Modified for book
72 - V1.2 01.03.94 Added CalcExtSD()
73 - V1.3 22.06.94 Fixed for just one value
74 - V1.4 07.07.14 Use bl prefix for functions By: CTP
75 
76 *************************************************************************/
77 /* Doxygen
78  -------
79  #GROUP Maths
80  #SUBGROUP Statistics
81  #FUNCTION blCalcExtSD()
82  Calculate the mean and standard deviation from a set of numbers.
83  The routine is called with each value to be sampled and then again
84  to obtain the results.
85  This is the same as blCalcSD() except that the variables used to
86  accumulate totals are kept outside the function instead of being
87  static within the function
88 */
89 /************************************************************************/
90 /* Includes
91 */
92 #include <math.h>
93 #include "MathType.h"
94 
95 /************************************************************************/
96 /* Defines and macros
97 */
98 
99 /************************************************************************/
100 /* Globals
101 */
102 
103 /************************************************************************/
104 /* Prototypes
105 */
106 
107 /************************************************************************/
108 /*>void blCalcExtSD(REAL val, int action, REAL *Sx, REAL *SxSq,
109  int *NValues, REAL *mean, REAL *SD)
110  ------------------------------------------------------------
111 *//**
112 
113  \param[in] val The value to be sampled
114  \param[in] action 0: Sample the value
115  1: Calculate & return mean and SD
116  2: Clear the sample lists
117  \param[out] mean The returned mean
118  \param[out] SD The returned standard deviation
119  \param[in,out] Sx Sum of values
120  \param[in,out] SxSq Sum of values squared
121  \param[in,out] NValues Number of values
122 
123  Calculate the mean and standard deviation from a set of numbers.
124  The routine is called with each value to be sampled and the action
125  required is specified:
126 
127  The output values are only set when action==1
128 
129  This is the same as blCalcSD() except that the Sx, SxSq and NValues
130  variables are kept outside the function instead of being static
131  within the function
132 
133 - 13.10.93 Original based on CalcSD By: ACRM
134 - 22.06.94 Fixed for only one value supplied
135 - 07.07.14 Use bl prefix for functions By: CTP
136 */
137 void blCalcExtSD(REAL val, int action, REAL *Sx, REAL *SxSq,
138  int *NValues, REAL *mean, REAL *SD)
139 {
140  switch(action)
141  {
142  case 0:
143  (*NValues)++;
144  *SxSq += (val * val);
145  *Sx += val;
146  break;
147 
148  case 1:
149  *mean = *SD = (REAL)0.0;
150  if(*NValues > 0)
151  *mean = (*Sx) / (*NValues);
152  if(*NValues > 1)
153  *SD = sqrt((*SxSq - ((*Sx) * (*Sx)) / (*NValues)) /
154  (*NValues - 1));
155  break;
156 
157  case 2:
158  *SxSq = 0.0;
159  *Sx = 0.0;
160  *NValues = 0;
161  }
162 }
163 
double REAL
Definition: MathType.h:67
void blCalcExtSD(REAL val, int action, REAL *Sx, REAL *SxSq, int *NValues, REAL *mean, REAL *SD)
Definition: CalcExtSD.c:137
Type definitions for maths.