TPCCLIB
Loading...
Searching...
No Matches
tpcstatist.h File Reference

Header file for libtpcstatist. More...

#include "tpcclibConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "tpcextensions.h"

Go to the source code of this file.

Functions

int statMeanSD (double *data, unsigned int n, double *mean, double *sd, unsigned int *vn)
int fstatMeanSD (float *data, unsigned int n, float *mean, float *sd, unsigned int *vn)
double statMedian (double *a, const int n)
double statKthSmallest (double *a, const int n, const int k)
void statSortInt (int *data, unsigned int n, int order)
void statSortUnsigned (unsigned int *data, unsigned int n, int order)
void statSortDouble (double *data, unsigned int n, int order)

Detailed Description

Header file for libtpcstatist.

Header file for libtpcstatist.

Author
Vesa Oikonen

Definition in file tpcstatist.h.

Function Documentation

◆ fstatMeanSD()

int fstatMeanSD ( float * data,
unsigned int n,
float * mean,
float * sd,
unsigned int * vn )
extern

Calculate mean and sample standard deviation in an array of given float data. NaNs in the data are left out.

Author
Vesa Oikonen
See also
statMeanSD, floatMean, floatSum, floatMatch
Returns
0 if successful, otherwise <>0.
Parameters
dataPointer to data array of size n; data is not changed in any way.
nLength of data array.
meanPointer to variable where mean will be written; enter NULL if not needed.
sdPointer to variable where SD will be written; enter NULL if not needed.
vnPointer to variable where the number of valid (not NaN) samples will be written; enter NULL if not needed.

Definition at line 73 of file mean.c.

85 {
86 unsigned int i, m;
87 float sumsqr=0.0, sqrsum=0.0;
88
89 if(mean!=NULL) *mean=nanf("");
90 if(sd!=NULL) *sd=nanf("");
91 if(vn!=NULL) *vn=0;
92 if(n<1 || data==NULL) return(1);
93 for(i=m=0; i<n; i++) if(!isnan(data[i])) m++;
94 if(m<1) return(2);
95 if(vn!=NULL) *vn=m;
96
97 /* Calculate mean, and sum for possible SD calculation */
98 for(i=0; i<n; i++) if(!isnan(data[i])) sqrsum+=data[i];
99 if(mean!=NULL) *mean=sqrsum/(float)m;
100
101 /* Calculate SD, if required */
102 if(sd==NULL) return(0);
103 if(m==1) {*sd=0.0; return(0);}
104 sqrsum*=sqrsum;
105
106 for(i=0; i<n; i++) if(!isnan(data[i])) sumsqr+=data[i]*data[i];
107 float ff=sumsqr - sqrsum/(float)m; if(!(ff>0.0)) {*sd=0.0; return(0);}
108 *sd=sqrtf( ff/(float)(m-1) );
109
110 return(0);
111}

◆ statKthSmallest()

double statKthSmallest ( double * a,
const int n,
const int k )
extern

Returns the kth smallest value in an array of given data.

Algorithm is based on the book Wirth N. Algorithms + data structures = programs. Englewood Cliffs, Prentice-Hall, 1976.

See also
statMeanSD, doubleMean, statSortDouble, doubleNaNs
Returns
Returns the kth smallest value in a[0..n-1], or NaN in case of an error.
Parameters
aPointer to data array of size n; data is partially sorted. NaNs are not checked but will lead to a wrong result.
nLength of the data array
kThe K value, 0<=K<n.
Note
The K value is zero based.

Definition at line 55 of file median.c.

64 {
65 //printf("\n%s(a[], %u, %u)\n", __func__, n, k); fflush(stdout);
66 if(a==NULL || n<1 || k<0 || k>=n) return(nan(""));
67 if(n==1) return(a[0]);
68
69 int l=0, m=n-1; // note that algorithm does not work with unsigned integers
70 while(l<m) {
71 int i=l, j=m;
72 double x=a[k];
73 do {
74 while(a[i]<x) i++;
75 while(x<a[j]) j--;
76 if(i<=j) {double s=a[i]; a[i]=a[j]; a[j]=s; i++; j--;}
77 } while(i<=j);
78 if(j<k) l=i;
79 if(k<i) m=j;
80 }
81 return(a[k]);
82}

Referenced by statMedian().

◆ statMeanSD()

int statMeanSD ( double * data,
unsigned int n,
double * mean,
double * sd,
unsigned int * vn )
extern

Calculate mean and sample standard deviation in an array of given data. NaNs in the data are left out.

Author
Vesa Oikonen
See also
statMedian, doubleMean, doubleWMean, floatMean, fitLine, statSortDouble, doubleMatch, doubleRange, fstatMeanSD
Returns
0 if successful, otherwise <>0.
Parameters
dataPointer to data array of size n; data is not changed in any way.
nLength of data array.
meanPointer to variable where mean will be written; enter NULL if not needed.
sdPointer to variable where SD will be written; enter NULL if not needed.
vnPointer to variable where the number of valid (not NaN) samples will be written; enter NULL if not needed.

Definition at line 25 of file mean.c.

37 {
38 unsigned int i, m;
39 double sumsqr=0.0, sqrsum=0.0;
40
41 if(mean!=NULL) *mean=nan("");
42 if(sd!=NULL) *sd=nan("");
43 if(vn!=NULL) *vn=0;
44 if(n<1 || data==NULL) return(1);
45 for(i=m=0; i<n; i++) if(!isnan(data[i])) m++;
46 if(m<1) return(2);
47 if(vn!=NULL) *vn=m;
48
49 /* Calculate mean, and sum for possible SD calculation */
50 for(i=0; i<n; i++) if(!isnan(data[i])) sqrsum+=data[i];
51 if(mean!=NULL) *mean=sqrsum/(double)m;
52
53 /* Calculate SD, if required */
54 if(sd==NULL) return(0);
55 if(m==1) {*sd=0.0; return(0);}
56 sqrsum*=sqrsum;
57
58 for(i=0; i<n; i++) if(!isnan(data[i])) sumsqr+=data[i]*data[i];
59 double ff=sumsqr - sqrsum/(double)m; if(!(ff>0.0)) {*sd=0.0; return(0);}
60 *sd=sqrt( ff/(double)(m-1) );
61
62 return(0);
63}

Referenced by nloptIATGO(), nloptMPSOabsvMean(), and nloptMPSOpMean().

◆ statMedian()

double statMedian ( double * a,
const int n )
extern

Returns the median in an array of given data. Algorithm is based on the book Wirth N. Algorithms + data structures = programs. Englewood Cliffs, Prentice-Hall, 1976.

See also
statMeanSD, doubleMean, statSortDouble, statKthSmallest, doubleNaNs, doubleCopyFinite, doubleRange
Returns
Returns the median in array a[0..n-1], or NaN in case of an error.
Parameters
aPointer to data array of size n; data is partially sorted. NaNs are not checked but will lead to a wrong result.
nLength of data array.

Definition at line 25 of file median.c.

31 {
32 if(a==NULL || n<1) return(nan(""));
33 if(n==1) return(a[0]);
34
35 if(n%2) {
36 int k=(n-1)/2;
37 return(statKthSmallest(a, n, k));
38 } else {
39 int k=n/2;
40 double d1=statKthSmallest(a, n, k-1);
41 double d2=statKthSmallest(a, n, k);
42 return(0.5*(d1+d2));
43 }
44}
double statKthSmallest(double *a, const int n, const int k)
Definition median.c:55

◆ statSortDouble()

void statSortDouble ( double * data,
unsigned int n,
int order )
extern

Sort the given double list into ascending or descending order.

Author
Vesa Oikonen
Parameters
dataPointer to data array of size n
nLength of data array
orderAscending (0) or descending (<>0) order

Definition at line 99 of file sort.c.

106 {
107 if(n<2 || data==NULL) return;
108 if(order==0) qsort(data, n, sizeof(double), statDoubleCompAsc);
109 else qsort(data, n, sizeof(double), statDoubleCompDesc);
110}

Referenced by tacInput2sim().

◆ statSortInt()

void statSortInt ( int * data,
unsigned int n,
int order )
extern

Sort the given integer list into ascending or descending order.

Author
Vesa Oikonen
Parameters
dataPointer to data array of size n
nLength of data array
orderAscending (0) or descending (<>0) order

Definition at line 63 of file sort.c.

70 {
71 if(n<2 || data==NULL) return;
72 if(order==0) qsort(data, n, sizeof(int), statIntCompAsc);
73 else qsort(data, n, sizeof(int), statIntCompDesc);
74}

◆ statSortUnsigned()

void statSortUnsigned ( unsigned int * data,
unsigned int n,
int order )
extern

Sort the given unsigned integer list into ascending or descending order.

Author
Vesa Oikonen
Parameters
dataPointer to data array of size n
nLength of data array
orderAscending (0) or descending (<>0) order

Definition at line 81 of file sort.c.

88 {
89 if(n<2 || data==NULL) return;
90 if(order==0) qsort(data, n, sizeof(unsigned int), statUnsignedCompAsc);
91 else qsort(data, n, sizeof(unsigned int), statUnsignedCompDesc);
92}