Bioplib
Protein Structure C Library
 All Data Structures Files Functions Variables Typedefs Macros Pages
CalcSD.c
Go to the documentation of this file.
1 /************************************************************************/
2 /**
3 
4  \file CalcSD.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  CalcSD(val,action,mean,SD)
52  --------------------------
53  Input: val int The value to be sampled
54  Input: action short 0: Sample the value
55  1: Calculate & return mean and SD
56  2: Clear the sample lists
57  Output: mean *REAL The returned mean
58  Output: SD *REAL The returned standard deviation
59 
60  The output values are only set when action=1
61 
62 
63 **************************************************************************
64 
65  Revision History:
66  =================
67 - V1.0 30.03.90 Original By: ACRM
68 - V1.1 17.06.93 Modified for book
69 - V1.2 01.03.94 Added CalcExtSD()
70 - V1.3 22.06.94 Fixed for just one value
71 - V1.4 07.07.14 Use bl prefix for functions By: CTP
72 
73 *************************************************************************/
74 /* Doxygen
75  -------
76  #GROUP Maths
77  #SUBGROUP Statistics
78  #FUNCTION blCalcSD()
79  Calculate the mean and standard deviation from a set of numbers.
80  The routine is called with each value to be sampled and then
81  called again to extract the results
82 */
83 /************************************************************************/
84 /* Includes
85 */
86 #include <math.h>
87 #include "MathType.h"
88 
89 /************************************************************************/
90 /* Defines and macros
91 */
92 
93 /************************************************************************/
94 /* Globals
95 */
96 
97 /************************************************************************/
98 /* Prototypes
99 */
100 
101 /************************************************************************/
102 /*>void blCalcSD(REAL val, int action, REAL *mean, REAL *SD)
103  ---------------------------------------------------------
104 *//**
105 
106  \param[in] val The value to be sampled
107  \param[in] action 0: Sample the value
108  1: Calculate & return mean and SD
109  2: Clear the sample lists
110  \param[out] mean The returned mean
111  \param[out] SD The returned standard deviation
112 
113  Calculate the mean and standard deviation from a set of numbers.
114  The routine is called with each value to be sampled and the action
115  required is specified.
116 
117  The output values are only set when action=1
118 
119 - 30.03.90 Original By: ACRM
120 - 17.06.93 Modified for book
121 - 22.06.94 Fixed for 0 values supplied
122 - 07.07.14 Use bl prefix for functions By: CTP
123 */
124 void blCalcSD(REAL val, int action, REAL *mean, REAL *SD)
125 {
126  static REAL SxSq = 0.0,
127  Sx = 0.0;
128  static int NSDValues = 0;
129 
130  switch(action)
131  {
132  case 0:
133  NSDValues++;
134  SxSq += (val * val);
135  Sx += val;
136  break;
137 
138  case 1:
139  *mean = *SD = (REAL)0.0;
140  if(NSDValues > 0)
141  *mean = Sx / NSDValues;
142  if(NSDValues > 1)
143  *SD = sqrt((SxSq - (Sx * Sx)/NSDValues)/(NSDValues - 1));
144  break;
145 
146  case 2:
147  SxSq = 0.0;
148  Sx = 0.0;
149  NSDValues = 0;
150  }
151 }
152 
double REAL
Definition: MathType.h:67
void blCalcSD(REAL val, int action, REAL *mean, REAL *SD)
Definition: CalcSD.c:124
Type definitions for maths.