TPCCLIB
Loading...
Searching...
No Matches
doubleutil.c File Reference

Working with doubles. More...

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

Go to the source code of this file.

Functions

int doubleMatch (const double v1, const double v2, const double lim)
 
int doubleArrayMatch (const double *a1, const double *a2, const unsigned int n, const double lim)
 
int doubleMatchRel (const double v1, const double v2, const double lim)
 
double doubleMachEps ()
 
void doubleCopy (double *t, double *s, const unsigned int n)
 
unsigned int doubleCopyFinite (double *t, double *s, const unsigned int n)
 
unsigned int doubleNaNs (double *a, const unsigned int n)
 
unsigned int doubleRange (double *a, const unsigned int n, double *amin, double *amax)
 
double doubleSum (double *a, const unsigned int n)
 
double doubleMean (double *a, const unsigned int n)
 
double doubleWMean (double *a, double *w, const unsigned int n)
 
int doubleGetWithUnit (const char *s, double *v, int *u)
 
int doubleSpanPositives (double *a, const int n)
 
int doubleCSpanPositives (double *a, const int n)
 
unsigned int doubleNonzeroes (double *a, const unsigned int n)
 
unsigned int doubleMaxIndex (double *a, const unsigned int n)
 
unsigned int doubleAbsMaxIndex (double *a, const unsigned int n)
 
unsigned int doubleMinIndex (double *a, const unsigned int n)
 
unsigned int doubleAbsMinIndex (double *a, const unsigned int n)
 
unsigned int doubleGEIndex (double *a, const unsigned int n, double lim)
 
unsigned int doubleGTIndex (double *a, const unsigned int n, double lim)
 
double inverfc (double x)
 

Detailed Description

Working with doubles.

Author
Vesa Oikonen

Definition in file doubleutil.c.

Function Documentation

◆ doubleAbsMaxIndex()

unsigned int doubleAbsMaxIndex ( double * a,
const unsigned int n )

Find the maximum absolute value in given double array.

Returns
The index [0..n-1] of maximum absolute value in array; 0 is returned also in case of errors.
See also
doubleMaxIndex, doubleGTIndex, doubleGEIndex, doubleRange
Parameters
aPointer to double array.
nLength of array.

Definition at line 377 of file doubleutil.c.

382 {
383 if(a==NULL || n<1) return(0);
384 unsigned int i, mi=0;
385 double mv=nan("");
386 for(i=0; i<n; i++) if(isnan(mv) || fabs(a[i])>mv) {mv=fabs(a[i]); mi=i;}
387 return(mi);
388}

◆ doubleAbsMinIndex()

unsigned int doubleAbsMinIndex ( double * a,
const unsigned int n )

Find the minimum absolute value in given double array.

Returns
The index [0..n-1] of minimum absolute value in array; 0 is returned also in case of errors.
See also
doubleAbsMaxIndex, doubleMinIndex, doubleGTIndex, doubleGEIndex
Parameters
aPointer to double array.
nLength of array.

Definition at line 416 of file doubleutil.c.

421 {
422 if(a==NULL || n<1) return(0);
423 unsigned int i, mi=0;
424 double mv=nan("");
425 for(i=0; i<n; i++) if(isnan(mv) || fabs(a[i])<mv) {mv=fabs(a[i]); mi=i;}
426 return(mi);
427}

Referenced by spectralBFExtract().

◆ doubleArrayMatch()

int doubleArrayMatch ( const double * a1,
const double * a2,
const unsigned int n,
const double lim )

Verifies that given two double arrays have the same values inside given limits. Values are considered to match also if both are NaNs.

Returns
1 if match is found, and 0 if not.
Author
Vesa Oikonen
See also
doubleMatch, doubleMatchRel, doubleMachEps, statMeanSD
Parameters
a1First array.
a2Second array.
nLength of arrays.
limLimit for absolute difference (if <0 then test will fail every time).

Definition at line 51 of file doubleutil.c.

60 {
61 if(a1==NULL || a2==NULL || n<1) return 0;
62 for(unsigned int i=0; i<n; i++) if(!doubleMatch(a1[i], a2[i], lim)) return 0;
63 return 1;
64}
int doubleMatch(const double v1, const double v2, const double lim)
Definition doubleutil.c:27

◆ doubleCopy()

void doubleCopy ( double * t,
double * s,
const unsigned int n )

Copy double values from the 2nd array to the first.

See also
floatCopy, doubleCopyFinite
Parameters
tTarget array.
sSource array.
nLength of arrays.

Definition at line 117 of file doubleutil.c.

124 {
125 unsigned int i;
126 if(t==NULL || s==NULL || n<1) return;
127 for(i=0; i<n; i++) t[i]=s[i];
128}

Referenced by nloptIATGO(), nloptMPSO(), and nloptSimplexARRS().

◆ doubleCopyFinite()

unsigned int doubleCopyFinite ( double * t,
double * s,
const unsigned int n )

Copy finite double values from the 2nd array to the first.

See also
doubleCopy
Returns
The number of finite values that were copied.
Parameters
tTarget array.
sSource array.
nLength of arrays.

Definition at line 136 of file doubleutil.c.

143 {
144 unsigned int i, tn=0;
145 if(t==NULL || s==NULL || n<1) return(0);
146 for(i=0; i<n; i++) if(isfinite(s[i])) t[tn++]=s[i];
147 return(tn);
148}

◆ doubleCSpanPositives()

int doubleCSpanPositives ( double * a,
const int n )

Returns the length of array consisting of non-positive (<=0 and NaN) values.

Returns
Returns the index of first positive value in the given array.
See also
floatCSpanPositives, doubleSpanPositives, atofList
Parameters
aPointer to the array.
nLength of the array.

Definition at line 320 of file doubleutil.c.

325 {
326 if(a==NULL || n<1) return(0);
327 int i=0;
328 for(i=0; i<n; i++) if(a[i]>0.0) break;
329 return(i);
330}

Referenced by spectralBFExtract(), and spectralBFNr().

◆ doubleGEIndex()

unsigned int doubleGEIndex ( double * a,
const unsigned int n,
double lim )

Get the index of first array value greater or equal than given limit.

Returns
The index, or n, if not found.
See also
doubleGTIndex, doubleMaxIndex, doubleMinIndex, fitLine, statMeanSD
Parameters
aPointer to an array of values.
nThe number of samples (length of a[]).
limThe limit value.

Definition at line 435 of file doubleutil.c.

442 {
443 if(a==NULL || n<1) return(n);
444 for(unsigned int i=0; i<n; i++) if(a[i]>=lim) return(i);
445 return(n);
446}

◆ doubleGetWithUnit()

int doubleGetWithUnit ( const char * s,
double * v,
int * u )

Reads numerical value and its unit from argument string.

Both decimal point and comma are accepted. Optional result double value is set to NaN if string was not valid value. Optional unit is set to UNIT_UNKNOWN, if not valid or not found.

Returns
0 if numerical value successfully read, and 1 in case of an error.
See also
floatGetWithUnit, atofVerified, atofCheck
Parameters
sString from where double and unit is read; string must not contain any extra space characters.
vPointer to the double float; enter NULL, if not needed.
uPointer to int for unit code; enter NULL, if not needed.

Definition at line 272 of file doubleutil.c.

280 {
281 if(v!=NULL) *v=nan("");
282 if(u!=NULL) *u=UNIT_UNKNOWN;
283 if(s==NULL) return 1;
284 int n=strTokenNr(s, " \t"); if(n==0 || n>2) return 1;
285 if(n==1) return(atofCheck(s, v)); // no unit
286 /* separate number and unit */
287 char buf[64];
288 n=strTokenNCpy(s, " \t", 1, buf, 64);
289 if(atofCheck(buf, v)) return 1;
290 if(u==NULL) return 0;
291 n=strTokenNCpy(s, " \t", 2, buf, 64);
292 *u=unitIdentify(buf);
293 return 0;
294}
int atofCheck(const char *s, double *v)
Definition decpoint.c:94
int strTokenNr(const char *s1, const char *s2)
Definition stringext.c:25
int strTokenNCpy(const char *s1, const char *s2, int i, char *s3, int count)
Definition stringext.c:53
@ UNIT_UNKNOWN
Unknown unit.
int unitIdentify(const char *s)
Definition units.c:162

Referenced by iftCheckKeyValue(), and iftCheckKeyValues().

◆ doubleGTIndex()

unsigned int doubleGTIndex ( double * a,
const unsigned int n,
double lim )

Get the index of first array value greater than given limit.

Returns
The index, or n, if not found.
See also
doubleGEIndex, doubleMaxIndex, doubleMinIndex, fitLine, statMeanSD, doubleMatch
Parameters
aPointer to an array of values.
nThe number of samples (length of a[]).
limThe limit value.

Definition at line 452 of file doubleutil.c.

459 {
460 if(a==NULL || n<1) return(n);
461 for(unsigned int i=0; i<n; i++) if(a[i]>lim) return(i);
462 return(n);
463}

◆ doubleMachEps()

double doubleMachEps ( )

Estimates the machine epsilon, the upper bound on the relative error due to rounding in floating point arithmetic, within one order of magnitude of the true machine epsilon.

Standard C library should also have DBL_EPSILON in float.h.

Returns
Estimate of machine epsilon.
Author
Vesa Oikonen
See also
floatMachEps, doubleMatchRel, doubleMatch, factorial

Definition at line 105 of file doubleutil.c.

106{
107 double macheps=1.0;
108 do {macheps/=2.0;} while((1.0+macheps/2.0)!=1.0);
109 return(macheps);
110}

Referenced by householder_transform(), nlopt1D(), nloptFixedNr(), nloptLimitFixedNr(), and nloptPowellBrent().

◆ doubleMatch()

int doubleMatch ( const double v1,
const double v2,
const double lim )

Verifies that given two doubles have the same value inside given limits. Values are considered to match also if both are NaNs.

Returns
1 if match is found, and 0 if not.
Author
Vesa Oikonen
See also
floatMatch, doubleMatchRel, doubleMachEps, doubleArrayMatch, atofCheck, statMeanSD
Parameters
v1First value.
v2Second value.
limLimit for absolute difference (if <0 then test will fail every time).

Definition at line 27 of file doubleutil.c.

34 {
35 if(isnan(v1) && isnan(v2)) return 1;
36 if(isnan(v1) || isnan(v2)) return 0;
37 if(v1==v2) return 1;
38 if(isnan(lim) || lim<0.0) return 0;
39 if(fabs(v1-v2)<=lim) return 1;
40 return 0;
41}

Referenced by doubleArrayMatch(), parCompareParameters(), parCompareWSS(), parIsFitRange(), tacCheckX1X2X(), tacCompareConc(), tacCompareTimes(), tacCompareWeights(), and tacReadQView().

◆ doubleMatchRel()

int doubleMatchRel ( const double v1,
const double v2,
const double lim )

Verifies that given two doubles have the same value inside given relative limit |2*(v1-v2)/(v1+v2)|.

Values are considered to match also if both are NaNs, but not if mean is zero (unless both are exactly zero).

Returns
1 if match is found, and 0 if not.
Author
Vesa Oikonen
See also
floatMatchRel, doubleMatch, doubleMachEps
Parameters
v1First value.
v2Second value.
limLimit for relative difference (if <0 then test will fail every time).

Definition at line 77 of file doubleutil.c.

84 {
85 if(isnan(v1) && isnan(v2)) return 1;
86 if(isnan(v1) || isnan(v2)) return 0;
87 if(v1==v2) return 1;
88 if(isnan(lim)) return 0;
89 double mean;
90 mean=0.5*(v1+v2); if(!isnormal(mean)) return 0;
91 if(fabs((v1-v2)/mean)<=lim) return 1;
92 return 0;
93}

Referenced by parCompareParameters(), parCompareWSS(), tacCompareConc(), tacCompareTimes(), and tacCompareWeights().

◆ doubleMaxIndex()

unsigned int doubleMaxIndex ( double * a,
const unsigned int n )

Find the maximum value in given double array.

Returns
The index [0..n-1] of maximum value in array; 0 is returned also in case of errors.
See also
floatMaxIndex, doubleSpanPositives, doubleCSpanPositives, doubleGTIndex, doubleRange
Parameters
aPointer to double array.
nLength of array.

Definition at line 357 of file doubleutil.c.

362 {
363 if(a==NULL || n<1) return(0);
364 unsigned int i, mi=0;
365 double mv=nan("");
366 for(i=0; i<n; i++) if(isnan(mv) || a[i]>mv) {mv=a[i]; mi=i;}
367 return(mi);
368}

◆ doubleMean()

double doubleMean ( double * a,
const unsigned int n )

Calculate the mean of values in given double array.

Returns
The mean of array values, or NaN in case of an error.
See also
floatMean, doubleWMean, doubleSum, doubleMaxIndex, statMeanSD, statMedian
Parameters
aPointer to double array.
nLength of array.

Definition at line 224 of file doubleutil.c.

229 {
230 if(a==NULL || n<1) return(nan(""));
231 double s=0.0;
232 unsigned int i, ci=0;
233 for(i=0; i<n; i++) if(!isnan(a[i])) {ci++; s+=a[i];}
234 if(ci<1) return(nan(""));
235 return(s/(double)ci);
236}

◆ doubleMinIndex()

unsigned int doubleMinIndex ( double * a,
const unsigned int n )

Find the minimum value in given double array.

Returns
The index [0..n-1] of minimum value in array; 0 is returned also in case of errors.
See also
doubleMaxIndex, doubleAbsMinIndex, doubleRange
Parameters
aPointer to double array.
nLength of array.

Definition at line 396 of file doubleutil.c.

401 {
402 if(a==NULL || n<1) return(0);
403 unsigned int i, mi=0;
404 double mv=nan("");
405 for(i=0; i<n; i++) if(isnan(mv) || a[i]<mv) {mv=a[i]; mi=i;}
406 return(mi);
407}

◆ doubleNaNs()

unsigned int doubleNaNs ( double * a,
const unsigned int n )

Check the number of NaNs in a double array.

See also
doubleNonzeroes, doubleCopyFinite, doubleRange, doubleMean
Returns
The number of NaNs.
Parameters
aPointer to double array.
nLength of array.

Definition at line 156 of file doubleutil.c.

161 {
162 if(a==NULL || n<1) return(0);
163 unsigned int i, nn=0;
164 for(i=0; i<n; i++) if(isnan(a[i])) nn++;
165 return(nn);
166}

◆ doubleNonzeroes()

unsigned int doubleNonzeroes ( double * a,
const unsigned int n )

Find the number of non-zero values in given double array. Negative values are counted, but NaNs are not.

Returns
The number of non-zero values in array; 0 is returned also in case of errors.
See also
floatNonzeroes, doubleSpanPositives, doubleNaNs
Parameters
aPointer to double array.
nLength of array.

Definition at line 339 of file doubleutil.c.

344 {
345 if(a==NULL || n<1) return(0);
346 unsigned int i, m=0;
347 for(i=0; i<n; i++) if(fabs(a[i])>0.0) m++;
348 return(m);
349}

◆ doubleRange()

unsigned int doubleRange ( double * a,
const unsigned int n,
double * amin,
double * amax )

Get the range of finite values in a double array.

See also
doubleMaxIndex, doubleMinIndex, doubleNonzeroes, doubleNaNs, doubleMean, statMedian
Returns
The number of finite values in the array, or 0 in case of an error.
Parameters
aPointer to double array.
nLength of array.
aminPointer to the double float where minimum value is stored; enter NULL, if not needed.
amaxPointer to the double float where maximum value is stored; enter NULL, if not needed.

Definition at line 174 of file doubleutil.c.

183 {
184 if(amin!=NULL) *amin=nan("");
185 if(amax!=NULL) *amax=nan("");
186 if(a==NULL || n<1) return(0);
187
188 double mi=nan(""), ma=nan("");
189 unsigned int i, nn=0;
190 for(i=0; i<n; i++) if(isfinite(a[i])) {
191 nn++;
192 if(!isfinite(mi) || mi>a[i]) mi=a[i];
193 if(!isfinite(ma) || ma<a[i]) ma=a[i];
194 }
195 if(amin!=NULL) *amin=mi;
196 if(amax!=NULL) *amax=ma;
197 return(nn);
198}

◆ doubleSpanPositives()

int doubleSpanPositives ( double * a,
const int n )

Returns the length of array consisting of only positive (>0 and not NaN) values.

Returns
Returns the index of first non-positive value in the given array.
See also
floatSpanPositives, doubleCSpanPositives, atofList, doubleNonzeroes
Parameters
aPointer to the array.
nLength of the array.

Definition at line 302 of file doubleutil.c.

307 {
308 if(a==NULL || n<1) return(0);
309 int i=0;
310 for(i=0; i<n; i++) if(!(a[i]>0.0)) break;
311 return(i);
312}

Referenced by spectralBFExtract(), and spectralBFNr().

◆ doubleSum()

double doubleSum ( double * a,
const unsigned int n )

Calculate the sum of values in given double array.

Returns
The sum of array values.
See also
floatSum, doubleMean, doubleMaxIndex
Parameters
aPointer to double array.
nLength of array.

Definition at line 206 of file doubleutil.c.

211 {
212 double s=0.0;
213 if(a==NULL || n<1) return(s);
214 for(unsigned int i=0; i<n; i++) if(!isnan(a[i])) s+=a[i];
215 return(s);
216}

Referenced by spectralBFExtract().

◆ doubleWMean()

double doubleWMean ( double * a,
double * w,
const unsigned int n )

Calculate the weighted mean of values in given double array.

Returns
The mean of array values, or NaN in case of an error.
See also
doubleMean, floatMean, doubleSum, doubleMaxIndex, statMeanSD
Parameters
aPointer to double array to calculated weighted mean from.
wPointer to double array containing the weights. Negative or NaN weight is interpreted as zero weight.
nLength of arrays.

Definition at line 244 of file doubleutil.c.

252 {
253 if(a==NULL || w==NULL || n<1) return(nan(""));
254 double s=0.0, ws=0.0;
255 unsigned int i, ci=0;
256 for(i=0; i<n; i++) if(isnormal(a[i]) && isnormal(w[i]) && w[i]>0.0) {
257 ci++; s+=w[i]*a[i]; ws+=w[i];}
258 if(ci<1 || !(ws>0.0)) return(nan(""));
259 return(s/ws);
260}

Referenced by spectralBFExtract().

◆ inverfc()

double inverfc ( double x)

Inverse complementary error function erfc^{-1}(x).

Based on the code by Acklam PJ, 2010.

Returns
Returns an approximate value of erfc^{-1}(x).
See also
doubleMachEps, igam, igamc
Parameters
xParameter for the inverse complementary error function.

Definition at line 473 of file doubleutil.c.

476 {
477 static const double a[] = {
478 -3.969683028665376E+01, 2.209460984245205E+02, -2.759285104469687E+02,
479 1.383577518672690E+02, -3.066479806614716E+01, 2.506628277459239
480 };
481 static const double b[] = {
482 -5.447609879822406E+01, 1.615858368580409E+02, -1.556989798598866E+02,
483 6.680131188771972E+01, -1.328068155288572E+01
484 };
485 static const double c[] = {
486 -7.784894002430293E-03, -3.223964580411365E-01, -2.400758277161838,
487 -2.549732539343734, 4.374664141464968, 2.938163982698783
488 };
489 static const double d[] = {
490 7.784695709041462E-03, 3.224671290700398E-01, 2.445134137142996, 3.754408661907416
491 };
492 double y, e, u;
493 x/=2.0;
494 if(x<0.02425) {
495 double q=sqrt(-2.0 * log(x));
496 y = (((((c[0]*q + c[1])*q + c[2])*q + c[3])*q + c[4])*q + c[5])
497 / ((((d[0]*q + d[1])*q + d[2])*q + d[3])*q + 1.0);
498 } else if(x<=0.97575) {
499 double q=x-0.5;
500 double r=q*q;
501 y = (((((a[0]*r + a[1])*r + a[2])*r + a[3])*r + a[4])*r + a[5])*q
502 / (((((b[0]*r + b[1])*r + b[2])*r + b[3])*r + b[4])*r + 1.0);
503 } else {
504 double q=sqrt(-2.0 * log(1.0-x));
505 y = -(((((c[0]*q + c[1])*q + c[2])*q + c[3])*q + c[4])*q + c[5])
506 / ((((d[0]*q + d[1])*q + d[2])*q + d[3])*q + 1.0);
507 }
508 e=0.5*erfc(-y/M_SQRT2)-x;
509 u=(e)*M_SQRT2*M_PI*exp(0.5*y*y);
510 y-=u/(1.0 + 0.5*y*u);
511 return(fabs(y/M_SQRT2));
512}