| 1 |
|
| 2 |
//------------------------------------------------------------------- |
| 3 |
// C-MEX implementation of SUMSKIPNAN - this function is part of the NaN-toolbox. |
| 4 |
// |
| 5 |
// |
| 6 |
// This program is free software; you can redistribute it and/or modify |
| 7 |
// it under the terms of the GNU General Public License as published by |
| 8 |
// the Free Software Foundation; either version 3 of the License, or |
| 9 |
// (at your option) any later version. |
| 10 |
// |
| 11 |
// This program is distributed in the hope that it will be useful, |
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
// GNU General Public License for more details. |
| 15 |
// |
| 16 |
// You should have received a copy of the GNU General Public License |
| 17 |
// along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 18 |
// |
| 19 |
// |
| 20 |
// sumskipnan: sums all non-NaN values |
| 21 |
// usage: |
| 22 |
// [o,count,SSQ] = sumskipnan_mex(x,DIM,flag,W); |
| 23 |
// |
| 24 |
// SUMSKIPNAN uses two techniques to reduce errors: |
| 25 |
// 1) long double (80bit) instead of 64-bit double is used internally |
| 26 |
// 2) The Kahan Summation formula is used to reduce the error margin from N*eps to 2*eps |
| 27 |
// The latter is only implemented in case of stride=1 (column vectors only, summation along 1st dimension). |
| 28 |
// |
| 29 |
// Input: |
| 30 |
// - x data array |
| 31 |
// - DIM (optional) dimension to sum |
| 32 |
// - flag (optional) is actually an output argument telling whether some NaN was observed |
| 33 |
// - W (optional) weight vector to compute weighted sum (default 1) |
| 34 |
// |
| 35 |
// Output: |
| 36 |
// - o (weighted) sum along dimension DIM |
| 37 |
// - count of valid elements |
| 38 |
// - sums of squares |
| 39 |
// |
| 40 |
// |
| 41 |
// Copyright (C) 2009-2026 Alois Schloegl <[email protected]> |
| 42 |
// This function is part of the NaN-toolbox |
| 43 |
// http://pub.ist.ac.at/~schloegl/matlab/NaN/ |
| 44 |
// |
| 45 |
//------------------------------------------------------------------- |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
#include <math.h> |
| 51 |
#include <stdint.h> |
| 52 |
#include "mex.h" |
| 53 |
|
| 54 |
/* |
| 55 |
math.h has isnan() defined for all sizes of floating point numbers, |
| 56 |
but c++ assumes isnan(double), causing possible conversions for float and long double |
| 57 |
*/ |
| 58 |
#define ISNAN(a) (a!=a) |
| 59 |
|
| 60 |
#ifndef typeof |
| 61 |
#define typeof __typeof__ |
| 62 |
#endif |
| 63 |
|
| 64 |
inline void __sumskipnan2w__(double *data, size_t Ni, double *s, double *No, char *flag_anyISNAN, double *W); |
| 65 |
inline void __sumskipnan3w__(double *data, size_t Ni, double *s, double *s2, double *No, char *flag_anyISNAN, double *W); |
| 66 |
inline void __sumskipnan2wr__(double *data, size_t Ni, double *s, double *No, char *flag_anyISNAN, double *W); |
| 67 |
inline void __sumskipnan3wr__(double *data, size_t Ni, double *s, double *s2, double *No, char *flag_anyISNAN, double *W); |
| 68 |
inline void __sumskipnan2we__(double *data, size_t Ni, double *s, double *No, char *flag_anyISNAN, double *W); |
| 69 |
inline void __sumskipnan3we__(double *data, size_t Ni, double *s, double *s2, double *No, char *flag_anyISNAN, double *W); |
| 70 |
inline void __sumskipnan2wer__(double *data, size_t Ni, double *s, double *No, char *flag_anyISNAN, double *W); |
| 71 |
inline void __sumskipnan3wer__(double *data, size_t Ni, double *s, double *s2, double *No, char *flag_anyISNAN, double *W); |
| 72 |
|
| 73 |
//#define NO_FLAG |
| 74 |
|
| 75 |
#ifdef tmwtypes_h |
| 76 |
#if (MX_API_VER<=0x07020000) |
| 77 |
typedef int mwSize; |
| 78 |
#endif |
| 79 |
#endif |
| 80 |
|
| 81 |
|
| 82 |
void mexFunction(int POutputCount, mxArray* POutput[], int PInputCount, const mxArray *PInputs[]) |
| 83 |
{ |
| 84 |
const mwSize *SZ; |
| 85 |
double* LInput; |
| 86 |
double* LOutputSum; |
| 87 |
double* LOutputCount; |
| 88 |
double* LOutputSum2; |
| 89 |
long double* LongOutputSum = NULL; |
| 90 |
long double* LongOutputCount = NULL; |
| 91 |
long double* LongOutputSum2 = NULL; |
| 92 |
double x; |
| 93 |
double* W = NULL; // weight vector |
| 94 |
|
| 95 |
size_t DIM = 0; |
| 96 |
size_t D1, D2, D3; // NN; |
| 97 |
size_t ND, ND2; // number of dimensions: input, output |
| 98 |
size_t ix0, ix1, ix2; // index to input and output |
| 99 |
size_t j, l; // running indices |
| 100 |
mwIndex *SZ2; // size of output |
| 101 |
char flag_isNaN = 0; |
| 102 |
|
| 103 |
// check for proper number of input and output arguments |
| 104 |
if ((PInputCount <= 0) || (PInputCount > 4)) |
| 105 |
mexErrMsgTxt("SUMSKIPNAN.MEX requires between 1 and 4 arguments."); |
| 106 |
if (POutputCount > 4) |
| 107 |
mexErrMsgTxt("SUMSKIPNAN.MEX has 1 to 3 output arguments."); |
| 108 |
|
| 109 |
// get 1st argument |
| 110 |
if(mxIsDouble(PInputs[0]) && !mxIsComplex(PInputs[0]) && !mxIsSparse(PInputs[0]) ) |
| 111 |
LInput = mxGetPr(PInputs[0]); |
| 112 |
else |
| 113 |
mexErrMsgTxt("First argument must be double and not sparse REAL/DOUBLE."); |
| 114 |
|
| 115 |
// get 2nd argument |
| 116 |
if (PInputCount > 1) { |
| 117 |
switch (mxGetNumberOfElements(PInputs[1])) { |
| 118 |
case 0: x = 0.0; // accept empty element |
| 119 |
break; |
| 120 |
case 1: x = (mxIsNumeric(PInputs[1]) ? mxGetScalar(PInputs[1]) : -1.0); |
| 121 |
break; |
| 122 |
default:x = -1.0; // invalid |
| 123 |
} |
| 124 |
if ((x < 0) || (x > 65535) || (x != floor(x))) |
| 125 |
mexErrMsgTxt("Error SUMSKIPNAN.MEX: DIM-argument must be a positive integer scalar"); |
| 126 |
|
| 127 |
DIM = (unsigned)floor(x); |
| 128 |
} |
| 129 |
|
| 130 |
// get size |
| 131 |
ND = mxGetNumberOfDimensions(PInputs[0]); |
| 132 |
// NN = mxGetNumberOfElements(PInputs[0]); |
| 133 |
SZ = mxGetDimensions(PInputs[0]); |
| 134 |
|
| 135 |
// if DIM==0 (undefined), look for first dimension with more than 1 element. |
| 136 |
for (j = 0; (DIM < 1) && (j < ND); j++) |
| 137 |
if (SZ[j]>1) DIM = j+1; |
| 138 |
|
| 139 |
if (DIM < 1) DIM=1; // in case DIM is still undefined |
| 140 |
|
| 141 |
ND2 = (ND>DIM ? ND : DIM); // number of dimensions of output |
| 142 |
|
| 143 |
SZ2 = (mwSize*)mxCalloc(ND2, sizeof(mwSize)); // allocate memory for output size |
| 144 |
|
| 145 |
for (j=0; j<ND; j++) // copy size of input; |
| 146 |
SZ2[j] = SZ[j]; |
| 147 |
for (j=ND; j<ND2; j++) // in case DIM > ND, add extra elements 1 |
| 148 |
SZ2[j] = 1; |
| 149 |
|
| 150 |
for (j=0, D1=1; j<DIM-1; D1=D1*SZ2[j++]); // D1 is the number of elements between two elements along dimension DIM |
| 151 |
D2 = SZ2[DIM-1]; // D2 contains the size along dimension DIM |
| 152 |
for (j=DIM, D3=1; j<ND; D3=D3*SZ2[j++]); // D3 is the number of blocks containing D1*D2 elements |
| 153 |
|
| 154 |
SZ2[DIM-1] = 1; // size of output is same as size of input but SZ(DIM)=1; |
| 155 |
|
| 156 |
// get weight vector for weighted sumskipnan |
| 157 |
if (PInputCount > 3) { |
| 158 |
if (!mxGetNumberOfElements(PInputs[3])) |
| 159 |
; // empty weight vector - no weighting |
| 160 |
else if (mxGetNumberOfElements(PInputs[3])==D2) |
| 161 |
W = mxGetPr(PInputs[3]); |
| 162 |
else |
| 163 |
mexErrMsgTxt("Error SUMSKIPNAN.MEX: length of weight vector does not match size of dimension"); |
| 164 |
} |
| 165 |
|
| 166 |
int ACC_LEVEL = 0; |
| 167 |
{ |
| 168 |
mxArray *LEVEL = NULL; |
| 169 |
int s = mexCallMATLAB(1, &LEVEL, 0, NULL, "flag_accuracy_level"); |
| 170 |
if (!s) { |
| 171 |
ACC_LEVEL = (int) mxGetScalar(LEVEL); |
| 172 |
if ((D1>1) && (ACC_LEVEL>2)) |
| 173 |
mexWarnMsgTxt("Warning: Kahan summation not supported with stride > 1 !"); |
| 174 |
} |
| 175 |
mxDestroyArray(LEVEL); |
| 176 |
} |
| 177 |
// mexPrintf("Accuracy Level=%i\n",ACC_LEVEL); |
| 178 |
|
| 179 |
// create outputs |
| 180 |
#define TYP mxDOUBLE_CLASS |
| 181 |
|
| 182 |
POutput[0] = mxCreateNumericArray(ND2, SZ2, TYP, mxREAL); |
| 183 |
LOutputSum = mxGetPr(POutput[0]); |
| 184 |
if (D1!=1 && D2>0) LongOutputSum = (long double*) mxCalloc(D1*D3,sizeof(long double)); |
| 185 |
if (POutputCount >= 2) { |
| 186 |
POutput[1] = mxCreateNumericArray(ND2, SZ2, TYP, mxREAL); |
| 187 |
LOutputCount = mxGetPr(POutput[1]); |
| 188 |
if (D1!=1 && D2>0) LongOutputCount = (long double*) mxCalloc(D1*D3,sizeof(long double)); |
| 189 |
} |
| 190 |
if (POutputCount >= 3) { |
| 191 |
POutput[2] = mxCreateNumericArray(ND2, SZ2, TYP, mxREAL); |
| 192 |
LOutputSum2 = mxGetPr(POutput[2]); |
| 193 |
if (D1!=1 && D2>0) LongOutputSum2 = (long double*) mxCalloc(D1*D3,sizeof(long double)); |
| 194 |
} |
| 195 |
mxFree(SZ2); |
| 196 |
|
| 197 |
|
| 198 |
if (!D1 || !D2 || !D3) // zero size array |
| 199 |
; // do nothing |
| 200 |
else if (D1==1) { |
| 201 |
if (ACC_LEVEL<1) { |
| 202 |
// double accuray, naive summation, error = N*2^-52 |
| 203 |
switch (POutputCount) { |
| 204 |
case 0: |
| 205 |
case 1: |
| 206 |
#pragma omp parallel for schedule(dynamic) |
| 207 |
for (l = 0; l<D3; l++) { |
| 208 |
double count; |
| 209 |
__sumskipnan2wr__(LInput+l*D2, D2, LOutputSum+l, &count, &flag_isNaN, W); |
| 210 |
} |
| 211 |
break; |
| 212 |
case 2: |
| 213 |
#pragma omp parallel for schedule(dynamic) |
| 214 |
for (l = 0; l<D3; l++) { |
| 215 |
__sumskipnan2wr__(LInput+l*D2, D2, LOutputSum+l, LOutputCount+l, &flag_isNaN, W); |
| 216 |
} |
| 217 |
break; |
| 218 |
case 3: |
| 219 |
#pragma omp parallel for schedule(dynamic) |
| 220 |
for (l = 0; l<D3; l++) { |
| 221 |
__sumskipnan3wr__(LInput+l*D2, D2, LOutputSum+l, LOutputSum2+l, LOutputCount+l, &flag_isNaN, W); |
| 222 |
} |
| 223 |
break; |
| 224 |
} |
| 225 |
} |
| 226 |
else if (ACC_LEVEL==1) { |
| 227 |
// extended accuray, naive summation, error = N*2^-64 |
| 228 |
switch (POutputCount) { |
| 229 |
case 0: |
| 230 |
case 1: |
| 231 |
#pragma omp parallel for schedule(dynamic) |
| 232 |
for (l = 0; l<D3; l++) { |
| 233 |
double count; |
| 234 |
__sumskipnan2w__(LInput+l*D2, D2, LOutputSum+l, &count, &flag_isNaN, W); |
| 235 |
} |
| 236 |
break; |
| 237 |
case 2: |
| 238 |
#pragma omp parallel for schedule(dynamic) |
| 239 |
for (l = 0; l<D3; l++) { |
| 240 |
__sumskipnan2w__(LInput+l*D2, D2, LOutputSum+l, LOutputCount+l, &flag_isNaN, W); |
| 241 |
} |
| 242 |
break; |
| 243 |
case 3: |
| 244 |
#pragma omp parallel for schedule(dynamic) |
| 245 |
for (l = 0; l<D3; l++) { |
| 246 |
__sumskipnan3w__(LInput+l*D2, D2, LOutputSum+l, LOutputSum2+l, LOutputCount+l, &flag_isNaN, W); |
| 247 |
} |
| 248 |
break; |
| 249 |
} |
| 250 |
} |
| 251 |
else if (ACC_LEVEL==3) { |
| 252 |
// ACC_LEVEL==3: extended accuracy and Kahan Summation, error = 2^-64 |
| 253 |
switch (POutputCount) { |
| 254 |
case 0: |
| 255 |
case 1: |
| 256 |
#pragma omp parallel for schedule(dynamic) |
| 257 |
for (l = 0; l<D3; l++) { |
| 258 |
double count; |
| 259 |
__sumskipnan2we__(LInput+l*D2, D2, LOutputSum+l, &count, &flag_isNaN, W); |
| 260 |
} |
| 261 |
break; |
| 262 |
case 2: |
| 263 |
#pragma omp parallel for schedule(dynamic) |
| 264 |
for (l = 0; l<D3; l++) { |
| 265 |
__sumskipnan2we__(LInput+l*D2, D2, LOutputSum+l, LOutputCount+l, &flag_isNaN, W); |
| 266 |
} |
| 267 |
break; |
| 268 |
case 3: |
| 269 |
#pragma omp parallel for schedule(dynamic) |
| 270 |
for (l = 0; l<D3; l++) { |
| 271 |
__sumskipnan3we__(LInput+l*D2, D2, LOutputSum+l, LOutputSum2+l, LOutputCount+l, &flag_isNaN, W); |
| 272 |
} |
| 273 |
break; |
| 274 |
} |
| 275 |
} |
| 276 |
else if (ACC_LEVEL==2) { |
| 277 |
// ACC_LEVEL==2: double accuracy and Kahan Summation, error = 2^-52 |
| 278 |
switch (POutputCount) { |
| 279 |
case 0: |
| 280 |
case 1: |
| 281 |
#pragma omp parallel for schedule(dynamic) |
| 282 |
for (l = 0; l<D3; l++) { |
| 283 |
double count; |
| 284 |
__sumskipnan2wer__(LInput+l*D2, D2, LOutputSum+l, &count, &flag_isNaN, W); |
| 285 |
} |
| 286 |
break; |
| 287 |
case 2: |
| 288 |
#pragma omp parallel for schedule(dynamic) |
| 289 |
for (l = 0; l<D3; l++) { |
| 290 |
__sumskipnan2wer__(LInput+l*D2, D2, LOutputSum+l, LOutputCount+l, &flag_isNaN, W); |
| 291 |
} |
| 292 |
break; |
| 293 |
case 3: |
| 294 |
#pragma omp parallel for schedule(dynamic) |
| 295 |
for (l = 0; l<D3; l++) { |
| 296 |
__sumskipnan3wer__(LInput+l*D2, D2, LOutputSum+l, LOutputSum2+l, LOutputCount+l, &flag_isNaN, W); |
| 297 |
} |
| 298 |
break; |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
else if (POutputCount <= 1) { |
| 303 |
// OUTER LOOP: along dimensions > DIM |
| 304 |
for (l = 0; l<D3; l++) { |
| 305 |
ix0 = l*D1; // index for output |
| 306 |
ix1 = ix0*D2; // index for input |
| 307 |
for (j=0; j<D2; j++) { |
| 308 |
// minimize cache misses |
| 309 |
ix2 = ix0; // index for output |
| 310 |
// Inner LOOP: along dimensions < DIM |
| 311 |
if (W) do { |
| 312 |
long double x = *LInput; |
| 313 |
if (!ISNAN(x)) { |
| 314 |
LongOutputSum[ix2] += W[j]*x; |
| 315 |
} |
| 316 |
#ifndef NO_FLAG |
| 317 |
else |
| 318 |
flag_isNaN = 1; |
| 319 |
#endif |
| 320 |
LInput++; |
| 321 |
ix2++; |
| 322 |
} while (ix2 != (l+1)*D1); |
| 323 |
else do { |
| 324 |
long double x = *LInput; |
| 325 |
if (!ISNAN(x)) { |
| 326 |
LongOutputSum[ix2] += x; |
| 327 |
} |
| 328 |
#ifndef NO_FLAG |
| 329 |
else |
| 330 |
flag_isNaN = 1; |
| 331 |
#endif |
| 332 |
LInput++; |
| 333 |
ix2++; |
| 334 |
} while (ix2 != (l+1)*D1); |
| 335 |
} // end for (j= |
| 336 |
|
| 337 |
/* copy to output */ |
| 338 |
for (j=0; j<D1; j++) { |
| 339 |
LOutputSum[ix0+j] = (typeof(*LOutputSum))LongOutputSum[ix0+j]; |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
else if (POutputCount == 2) { |
| 345 |
// OUTER LOOP: along dimensions > DIM |
| 346 |
for (l = 0; l<D3; l++) { |
| 347 |
ix0 = l*D1; |
| 348 |
ix1 = ix0*D2; // index for input |
| 349 |
for (j=0; j<D2; j++) { |
| 350 |
// minimize cache misses |
| 351 |
ix2 = ix0; // index for output |
| 352 |
// Inner LOOP: along dimensions < DIM |
| 353 |
if (W) do { |
| 354 |
long double x = *LInput; |
| 355 |
if (!ISNAN(x)) { |
| 356 |
LongOutputCount[ix2] += W[j]; |
| 357 |
LongOutputSum[ix2] += W[j]*x; |
| 358 |
} |
| 359 |
#ifndef NO_FLAG |
| 360 |
else |
| 361 |
flag_isNaN = 1; |
| 362 |
#endif |
| 363 |
LInput++; |
| 364 |
ix2++; |
| 365 |
} while (ix2 != (l+1)*D1); |
| 366 |
else do { |
| 367 |
long double x = *LInput; |
| 368 |
if (!ISNAN(x)) { |
| 369 |
LongOutputCount[ix2] += 1.0; |
| 370 |
LongOutputSum[ix2] += x; |
| 371 |
} |
| 372 |
#ifndef NO_FLAG |
| 373 |
else |
| 374 |
flag_isNaN = 1; |
| 375 |
#endif |
| 376 |
LInput++; |
| 377 |
ix2++; |
| 378 |
} while (ix2 != (l+1)*D1); |
| 379 |
} // end for (j= |
| 380 |
|
| 381 |
/* copy to output */ |
| 382 |
for (j=0; j<D1; j++) { |
| 383 |
LOutputSum[ix0+j] = (typeof(*LOutputSum))LongOutputSum[ix0+j]; |
| 384 |
LOutputCount[ix0+j] = (typeof(*LOutputCount))LongOutputCount[ix0+j]; |
| 385 |
} // end else |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
else if (POutputCount == 3) { |
| 390 |
// OUTER LOOP: along dimensions > DIM |
| 391 |
for (l = 0; l<D3; l++) { |
| 392 |
ix0 = l*D1; |
| 393 |
ix1 = ix0*D2; // index for input |
| 394 |
for (j=0; j<D2; j++) { |
| 395 |
// minimize cache misses |
| 396 |
ix2 = ix0; // index for output |
| 397 |
// Inner LOOP: along dimensions < DIM |
| 398 |
if (W) do { |
| 399 |
long double x = *LInput; |
| 400 |
if (!ISNAN(x)) { |
| 401 |
LongOutputCount[ix2] += W[j]; |
| 402 |
long double t = W[j]*x; |
| 403 |
LongOutputSum[ix2] += t; |
| 404 |
LongOutputSum2[ix2] += x*t; |
| 405 |
} |
| 406 |
#ifndef NO_FLAG |
| 407 |
else |
| 408 |
flag_isNaN = 1; |
| 409 |
#endif |
| 410 |
LInput++; |
| 411 |
ix2++; |
| 412 |
} while (ix2 != (l+1)*D1); |
| 413 |
else do { |
| 414 |
long double x = *LInput; |
| 415 |
if (!ISNAN(x)) { |
| 416 |
LongOutputCount[ix2] += 1.0; |
| 417 |
LongOutputSum[ix2] += x; |
| 418 |
LongOutputSum2[ix2] += x*x; |
| 419 |
} |
| 420 |
#ifndef NO_FLAG |
| 421 |
else |
| 422 |
flag_isNaN = 1; |
| 423 |
#endif |
| 424 |
LInput++; |
| 425 |
ix2++; |
| 426 |
} while (ix2 != (l+1)*D1); |
| 427 |
} // end for (j= |
| 428 |
|
| 429 |
/* copy to output */ |
| 430 |
for (j=0; j<D1; j++) { |
| 431 |
LOutputSum[ix0+j] = (typeof(*LOutputSum))LongOutputSum[ix0+j]; |
| 432 |
LOutputCount[ix0+j] = (typeof(*LOutputCount))LongOutputCount[ix0+j]; |
| 433 |
LOutputSum2[ix0+j] = (typeof(*LOutputSum2))LongOutputSum2[ix0+j]; |
| 434 |
} |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
if (LongOutputSum) mxFree(LongOutputSum); |
| 439 |
if (LongOutputCount) mxFree(LongOutputCount); |
| 440 |
if (LongOutputSum2) mxFree(LongOutputSum2); |
| 441 |
|
| 442 |
#ifndef NO_FLAG |
| 443 |
//mexPrintf("Third argument must be not empty - otherwise status whether a NaN occured or not cannot be returned."); |
| 444 |
/* this is a hack, the third input argument is used to return whether a NaN occured or not. |
| 445 |
this requires that the input argument is a non-empty variable |
| 446 |
*/ |
| 447 |
if (flag_isNaN && (PInputCount > 2) && mxGetNumberOfElements(PInputs[2])) { |
| 448 |
// set FLAG_NANS_OCCURED |
| 449 |
switch (mxGetClassID(PInputs[2])) { |
| 450 |
case mxLOGICAL_CLASS: |
| 451 |
case mxCHAR_CLASS: |
| 452 |
case mxINT8_CLASS: |
| 453 |
case mxUINT8_CLASS: |
| 454 |
*(uint8_t*)mxGetData(PInputs[2]) = 1; |
| 455 |
break; |
| 456 |
case mxDOUBLE_CLASS: |
| 457 |
*(double*)mxGetData(PInputs[2]) = 1.0; |
| 458 |
break; |
| 459 |
case mxSINGLE_CLASS: |
| 460 |
*(float*)mxGetData(PInputs[2]) = 1.0; |
| 461 |
break; |
| 462 |
case mxINT16_CLASS: |
| 463 |
case mxUINT16_CLASS: |
| 464 |
*(uint16_t*)mxGetData(PInputs[2]) = 1; |
| 465 |
break; |
| 466 |
case mxINT32_CLASS: |
| 467 |
case mxUINT32_CLASS: |
| 468 |
*(uint32_t*)mxGetData(PInputs[2])= 1; |
| 469 |
break; |
| 470 |
case mxINT64_CLASS: |
| 471 |
case mxUINT64_CLASS: |
| 472 |
*(uint64_t*)mxGetData(PInputs[2]) = 1; |
| 473 |
break; |
| 474 |
case mxFUNCTION_CLASS: |
| 475 |
case mxUNKNOWN_CLASS: |
| 476 |
case mxCELL_CLASS: |
| 477 |
case mxSTRUCT_CLASS: |
| 478 |
default: |
| 479 |
mexPrintf("Type of 3rd input argument not supported."); |
| 480 |
} |
| 481 |
} |
| 482 |
#endif |
| 483 |
} |
| 484 |
|
| 485 |
#define stride 1 |
| 486 |
inline void __sumskipnan2w__(double *data, size_t Ni, double *s, double *No, char *flag_anyISNAN, double *W) |
| 487 |
{ |
| 488 |
long double sum=0; |
| 489 |
char flag=0; |
| 490 |
// LOOP along dimension DIM |
| 491 |
|
| 492 |
double *end = data + stride*Ni; |
| 493 |
if (W) { |
| 494 |
// with weight vector |
| 495 |
long double count = 0.0; |
| 496 |
do { |
| 497 |
long double x = *data; |
| 498 |
if (!ISNAN(x)) |
| 499 |
{ |
| 500 |
count += *W; |
| 501 |
sum += *W*x; |
| 502 |
} |
| 503 |
#ifndef NO_FLAG |
| 504 |
else |
| 505 |
flag = 1; |
| 506 |
#endif |
| 507 |
|
| 508 |
data++; // stride=1 |
| 509 |
W++; |
| 510 |
} |
| 511 |
while (data < end); |
| 512 |
*No = (typeof(*No))count; |
| 513 |
} else { |
| 514 |
// w/o weight vector |
| 515 |
size_t countI = 0; |
| 516 |
do { |
| 517 |
long double x = *data; |
| 518 |
if (!ISNAN(x)) |
| 519 |
{ |
| 520 |
countI++; |
| 521 |
sum += x; |
| 522 |
} |
| 523 |
#ifndef NO_FLAG |
| 524 |
else |
| 525 |
flag = 1; |
| 526 |
#endif |
| 527 |
data++; // stride=1 |
| 528 |
} |
| 529 |
while (data < end); |
| 530 |
*No = (typeof(*No))countI; |
| 531 |
} |
| 532 |
|
| 533 |
#ifndef NO_FLAG |
| 534 |
if (flag && (flag_anyISNAN != NULL)) *flag_anyISNAN = 1; |
| 535 |
#endif |
| 536 |
*s = (typeof(*s))sum; |
| 537 |
|
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
inline void __sumskipnan3w__(double *data, size_t Ni, double *s, double *s2, double *No, char *flag_anyISNAN, double *W) |
| 542 |
{ |
| 543 |
long double sum=0; |
| 544 |
long double msq=0; |
| 545 |
char flag=0; |
| 546 |
// LOOP along dimension DIM |
| 547 |
|
| 548 |
double *end = data + stride*Ni; |
| 549 |
if (W) { |
| 550 |
// with weight vector |
| 551 |
long double count = 0.0; |
| 552 |
do { |
| 553 |
long double x = *data; |
| 554 |
if (!ISNAN(x)) { |
| 555 |
count += *W; |
| 556 |
long double t = *W*x; |
| 557 |
sum += t; |
| 558 |
msq += x*t; |
| 559 |
} |
| 560 |
#ifndef NO_FLAG |
| 561 |
else |
| 562 |
flag = 1; |
| 563 |
#endif |
| 564 |
data++; // stride=1 |
| 565 |
W++; |
| 566 |
} |
| 567 |
while (data < end); |
| 568 |
*No = (typeof(*No))count; |
| 569 |
} else { |
| 570 |
// w/o weight vector |
| 571 |
size_t countI = 0; |
| 572 |
do { |
| 573 |
long double x = *data; |
| 574 |
if (!ISNAN(x)) { |
| 575 |
countI++; |
| 576 |
sum += x; |
| 577 |
msq += x*x; |
| 578 |
} |
| 579 |
#ifndef NO_FLAG |
| 580 |
else |
| 581 |
flag = 1; |
| 582 |
#endif |
| 583 |
data++; // stride=1 |
| 584 |
} |
| 585 |
while (data < end); |
| 586 |
*No = (typeof(*No))countI; |
| 587 |
} |
| 588 |
|
| 589 |
#ifndef NO_FLAG |
| 590 |
if (flag && (flag_anyISNAN != NULL)) *flag_anyISNAN = 1; |
| 591 |
#endif |
| 592 |
*s = (typeof(*s))sum; |
| 593 |
*s2 = (typeof(*s2))msq; |
| 594 |
} |
| 595 |
|
| 596 |
inline void __sumskipnan2wr__(double *data, size_t Ni, double *s, double *No, char *flag_anyISNAN, double *W) |
| 597 |
{ |
| 598 |
double sum=0; |
| 599 |
char flag=0; |
| 600 |
// LOOP along dimension DIM |
| 601 |
|
| 602 |
double *end = data + stride*Ni; |
| 603 |
if (W) { |
| 604 |
// with weight vector |
| 605 |
double count = 0.0; |
| 606 |
do { |
| 607 |
double x = *data; |
| 608 |
if (!ISNAN(x)) |
| 609 |
{ |
| 610 |
count += *W; |
| 611 |
sum += *W*x; |
| 612 |
} |
| 613 |
#ifndef NO_FLAG |
| 614 |
else |
| 615 |
flag = 1; |
| 616 |
#endif |
| 617 |
|
| 618 |
data++; // stride=1 |
| 619 |
W++; |
| 620 |
} |
| 621 |
while (data < end); |
| 622 |
*No = (typeof(*No))count; |
| 623 |
} else { |
| 624 |
// w/o weight vector |
| 625 |
size_t countI = 0; |
| 626 |
do { |
| 627 |
double x = *data; |
| 628 |
if (!ISNAN(x)) |
| 629 |
{ |
| 630 |
countI++; |
| 631 |
sum += x; |
| 632 |
} |
| 633 |
#ifndef NO_FLAG |
| 634 |
else |
| 635 |
flag = 1; |
| 636 |
#endif |
| 637 |
data++; // stride=1 |
| 638 |
} |
| 639 |
while (data < end); |
| 640 |
*No = (typeof(*No))countI; |
| 641 |
} |
| 642 |
|
| 643 |
#ifndef NO_FLAG |
| 644 |
if (flag && (flag_anyISNAN != NULL)) *flag_anyISNAN = 1; |
| 645 |
#endif |
| 646 |
*s = (typeof(*s))sum; |
| 647 |
|
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
inline void __sumskipnan3wr__(double *data, size_t Ni, double *s, double *s2, double *No, char *flag_anyISNAN, double *W) |
| 652 |
{ |
| 653 |
double sum=0; |
| 654 |
double msq=0; |
| 655 |
char flag=0; |
| 656 |
// LOOP along dimension DIM |
| 657 |
|
| 658 |
double *end = data + stride*Ni; |
| 659 |
if (W) { |
| 660 |
// with weight vector |
| 661 |
double count = 0.0; |
| 662 |
do { |
| 663 |
double x = *data; |
| 664 |
if (!ISNAN(x)) { |
| 665 |
count += *W; |
| 666 |
double t = *W*x; |
| 667 |
sum += t; |
| 668 |
msq += x*t; |
| 669 |
} |
| 670 |
#ifndef NO_FLAG |
| 671 |
else |
| 672 |
flag = 1; |
| 673 |
#endif |
| 674 |
data++; // stride=1 |
| 675 |
W++; |
| 676 |
} |
| 677 |
while (data < end); |
| 678 |
*No = count; |
| 679 |
} else { |
| 680 |
// w/o weight vector |
| 681 |
size_t countI = 0; |
| 682 |
do { |
| 683 |
double x = *data; |
| 684 |
if (!ISNAN(x)) { |
| 685 |
countI++; |
| 686 |
sum += x; |
| 687 |
msq += x*x; |
| 688 |
} |
| 689 |
#ifndef NO_FLAG |
| 690 |
else |
| 691 |
flag = 1; |
| 692 |
#endif |
| 693 |
data++; // stride=1 |
| 694 |
} |
| 695 |
while (data < end); |
| 696 |
*No = (typeof(*No))countI; |
| 697 |
} |
| 698 |
|
| 699 |
#ifndef NO_FLAG |
| 700 |
if (flag && (flag_anyISNAN != NULL)) *flag_anyISNAN = 1; |
| 701 |
#endif |
| 702 |
*s = (typeof(*s))sum; |
| 703 |
*s2 = (typeof(*s2))msq; |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
/*************************************** |
| 709 |
using Kahan's summation formula [1] |
| 710 |
this gives more accurate results while the computational effort within the loop is about 4x as high |
| 711 |
First tests show a penalty of about 40% in terms of computational time. |
| 712 |
|
| 713 |
[1] David Goldberg, |
| 714 |
What Every Computer Scientist Should Know About Floating-Point Arithmetic |
| 715 |
ACM Computing Surveys, Vol 23, No 1, March 1991. |
| 716 |
****************************************/ |
| 717 |
|
| 718 |
inline void __sumskipnan2we__(double *data, size_t Ni, double *s, double *No, char *flag_anyISNAN, double *W) |
| 719 |
{ |
| 720 |
long double sum=0; |
| 721 |
char flag=0; |
| 722 |
// LOOP along dimension DIM |
| 723 |
|
| 724 |
double *end = data + stride*Ni; |
| 725 |
if (W) { |
| 726 |
// with weight vector |
| 727 |
long double count = 0.0; |
| 728 |
long double rc=0.0, rn=0.0; |
| 729 |
do { |
| 730 |
long double x = *data; |
| 731 |
long double t,y; |
| 732 |
if (!ISNAN(x)) |
| 733 |
{ |
| 734 |
//count += *W; [1] |
| 735 |
y = *W-rn; |
| 736 |
t = count+y; |
| 737 |
rn= (t-count)-y; |
| 738 |
count= t; |
| 739 |
|
| 740 |
//sum += *W*x; [1] |
| 741 |
y = *W*x-rc; |
| 742 |
t = sum+y; |
| 743 |
rc= (t-sum)-y; |
| 744 |
sum= t; |
| 745 |
} |
| 746 |
#ifndef NO_FLAG |
| 747 |
else |
| 748 |
flag = 1; |
| 749 |
#endif |
| 750 |
|
| 751 |
data++; // stride=1 |
| 752 |
W++; |
| 753 |
} |
| 754 |
while (data < end); |
| 755 |
*No = (typeof(*No))count; |
| 756 |
} else { |
| 757 |
// w/o weight vector |
| 758 |
size_t countI = 0; |
| 759 |
long double rc=0.0; |
| 760 |
do { |
| 761 |
long double x = *data; |
| 762 |
long double t,y; |
| 763 |
if (!ISNAN(x)) |
| 764 |
{ |
| 765 |
countI++; |
| 766 |
// sum += x; [1] |
| 767 |
y = x-rc; |
| 768 |
t = sum+y; |
| 769 |
rc= (t-sum)-y; |
| 770 |
sum= t; |
| 771 |
} |
| 772 |
#ifndef NO_FLAG |
| 773 |
else |
| 774 |
flag = 1; |
| 775 |
#endif |
| 776 |
data++; // stride=1 |
| 777 |
} |
| 778 |
while (data < end); |
| 779 |
*No = (typeof(*No))countI; |
| 780 |
} |
| 781 |
|
| 782 |
#ifndef NO_FLAG |
| 783 |
if (flag && (flag_anyISNAN != NULL)) *flag_anyISNAN = 1; |
| 784 |
#endif |
| 785 |
*s = (typeof(*s))sum; |
| 786 |
|
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
inline void __sumskipnan3we__(double *data, size_t Ni, double *s, double *s2, double *No, char *flag_anyISNAN, double *W) |
| 791 |
{ |
| 792 |
long double sum=0; |
| 793 |
long double msq=0; |
| 794 |
char flag=0; |
| 795 |
// LOOP along dimension DIM |
| 796 |
|
| 797 |
double *end = data + stride*Ni; |
| 798 |
if (W) { |
| 799 |
// with weight vector |
| 800 |
long double count = 0.0; |
| 801 |
long double rc=0.0, rn=0.0, rq=0.0; |
| 802 |
do { |
| 803 |
long double x = *data; |
| 804 |
long double t,y; |
| 805 |
if (!ISNAN(x)) { |
| 806 |
//count += *W; [1] |
| 807 |
y = *W-rn; |
| 808 |
t = count+y; |
| 809 |
rn= (t-count)-y; |
| 810 |
count= t; |
| 811 |
|
| 812 |
long double w = *W*x; |
| 813 |
//sum += *W*x; [1] |
| 814 |
y = *W*x-rc; |
| 815 |
t = sum+y; |
| 816 |
rc= (t-sum)-y; |
| 817 |
sum= t; |
| 818 |
|
| 819 |
// msq += x*w; |
| 820 |
y = w*x-rq; |
| 821 |
t = msq+y; |
| 822 |
rq= (t-msq)-y; |
| 823 |
msq= t; |
| 824 |
} |
| 825 |
#ifndef NO_FLAG |
| 826 |
else |
| 827 |
flag = 1; |
| 828 |
#endif |
| 829 |
data++; // stride=1 |
| 830 |
W++; |
| 831 |
} |
| 832 |
while (data < end); |
| 833 |
*No = (typeof(*No))count; |
| 834 |
} else { |
| 835 |
// w/o weight vector |
| 836 |
size_t countI = 0; |
| 837 |
long double rc=0.0, rq=0.0; |
| 838 |
do { |
| 839 |
long double x = *data; |
| 840 |
long double t,y; |
| 841 |
if (!ISNAN(x)) { |
| 842 |
countI++; |
| 843 |
//sum += x; [1] |
| 844 |
y = x-rc; |
| 845 |
t = sum+y; |
| 846 |
rc= (t-sum)-y; |
| 847 |
sum= t; |
| 848 |
|
| 849 |
// msq += x*x; |
| 850 |
y = x*x-rq; |
| 851 |
t = msq+y; |
| 852 |
rq= (t-msq)-y; |
| 853 |
msq= t; |
| 854 |
} |
| 855 |
#ifndef NO_FLAG |
| 856 |
else |
| 857 |
flag = 1; |
| 858 |
#endif |
| 859 |
data++; // stride=1 |
| 860 |
} |
| 861 |
while (data < end); |
| 862 |
*No = (typeof(*No))countI; |
| 863 |
} |
| 864 |
|
| 865 |
#ifndef NO_FLAG |
| 866 |
if (flag && (flag_anyISNAN != NULL)) *flag_anyISNAN = 1; |
| 867 |
#endif |
| 868 |
*s = (typeof(*s))sum; |
| 869 |
*s2 = (typeof(*s))msq; |
| 870 |
} |
| 871 |
|
| 872 |
inline void __sumskipnan2wer__(double *data, size_t Ni, double *s, double *No, char *flag_anyISNAN, double *W) |
| 873 |
{ |
| 874 |
double sum=0; |
| 875 |
char flag=0; |
| 876 |
// LOOP along dimension DIM |
| 877 |
|
| 878 |
double *end = data + stride*Ni; |
| 879 |
if (W) { |
| 880 |
// with weight vector |
| 881 |
double count = 0.0; |
| 882 |
double rc=0.0, rn=0.0; |
| 883 |
do { |
| 884 |
double x = *data; |
| 885 |
double t,y; |
| 886 |
if (!ISNAN(x)) |
| 887 |
{ |
| 888 |
//count += *W; [1] |
| 889 |
y = *W-rn; |
| 890 |
t = count+y; |
| 891 |
rn= (t-count)-y; |
| 892 |
count= t; |
| 893 |
|
| 894 |
//sum += *W*x; [1] |
| 895 |
y = *W*x-rc; |
| 896 |
t = sum+y; |
| 897 |
rc= (t-sum)-y; |
| 898 |
sum= t; |
| 899 |
} |
| 900 |
#ifndef NO_FLAG |
| 901 |
else |
| 902 |
flag = 1; |
| 903 |
#endif |
| 904 |
|
| 905 |
data++; // stride=1 |
| 906 |
W++; |
| 907 |
} |
| 908 |
while (data < end); |
| 909 |
*No = (typeof(*No))count; |
| 910 |
} else { |
| 911 |
// w/o weight vector |
| 912 |
size_t countI = 0; |
| 913 |
double rc=0.0; |
| 914 |
do { |
| 915 |
double x = *data; |
| 916 |
double t,y; |
| 917 |
if (!ISNAN(x)) |
| 918 |
{ |
| 919 |
countI++; |
| 920 |
// sum += x; [1] |
| 921 |
y = x-rc; |
| 922 |
t = sum+y; |
| 923 |
rc= (t-sum)-y; |
| 924 |
sum= t; |
| 925 |
} |
| 926 |
#ifndef NO_FLAG |
| 927 |
else |
| 928 |
flag = 1; |
| 929 |
#endif |
| 930 |
data++; // stride=1 |
| 931 |
} |
| 932 |
while (data < end); |
| 933 |
*No = (typeof(*No))countI; |
| 934 |
} |
| 935 |
|
| 936 |
#ifndef NO_FLAG |
| 937 |
if (flag && (flag_anyISNAN != NULL)) *flag_anyISNAN = 1; |
| 938 |
#endif |
| 939 |
*s = (typeof(*s))sum; |
| 940 |
|
| 941 |
} |
| 942 |
|
| 943 |
|
| 944 |
inline void __sumskipnan3wer__(double *data, size_t Ni, double *s, double *s2, double *No, char *flag_anyISNAN, double *W) |
| 945 |
{ |
| 946 |
double sum=0; |
| 947 |
double msq=0; |
| 948 |
char flag=0; |
| 949 |
// LOOP along dimension DIM |
| 950 |
|
| 951 |
double *end = data + stride*Ni; |
| 952 |
if (W) { |
| 953 |
// with weight vector |
| 954 |
double count = 0.0; |
| 955 |
double rc=0.0, rn=0.0, rq=0.0; |
| 956 |
do { |
| 957 |
double x = *data; |
| 958 |
double t,y; |
| 959 |
if (!ISNAN(x)) { |
| 960 |
//count += *W; [1] |
| 961 |
y = *W-rn; |
| 962 |
t = count+y; |
| 963 |
rn= (t-count)-y; |
| 964 |
count= t; |
| 965 |
|
| 966 |
double w = *W*x; |
| 967 |
//sum += *W*x; [1] |
| 968 |
y = *W*x-rc; |
| 969 |
t = sum+y; |
| 970 |
rc= (t-sum)-y; |
| 971 |
sum= t; |
| 972 |
|
| 973 |
// msq += x*w; |
| 974 |
y = w*x-rq; |
| 975 |
t = msq+y; |
| 976 |
rq= (t-msq)-y; |
| 977 |
msq= t; |
| 978 |
} |
| 979 |
#ifndef NO_FLAG |
| 980 |
else |
| 981 |
flag = 1; |
| 982 |
#endif |
| 983 |
data++; // stride=1 |
| 984 |
W++; |
| 985 |
} |
| 986 |
while (data < end); |
| 987 |
*No = (typeof(*No))count; |
| 988 |
} else { |
| 989 |
// w/o weight vector |
| 990 |
size_t countI = 0; |
| 991 |
double rc=0.0, rq=0.0; |
| 992 |
do { |
| 993 |
double x = *data; |
| 994 |
double t,y; |
| 995 |
if (!ISNAN(x)) { |
| 996 |
countI++; |
| 997 |
//sum += x; [1] |
| 998 |
y = x-rc; |
| 999 |
t = sum+y; |
| 1000 |
rc= (t-sum)-y; |
| 1001 |
sum= t; |
| 1002 |
|
| 1003 |
// msq += x*x; |
| 1004 |
y = x*x-rq; |
| 1005 |
t = msq+y; |
| 1006 |
rq= (t-msq)-y; |
| 1007 |
msq= t; |
| 1008 |
} |
| 1009 |
#ifndef NO_FLAG |
| 1010 |
else |
| 1011 |
flag = 1; |
| 1012 |
#endif |
| 1013 |
data++; // stride=1 |
| 1014 |
} |
| 1015 |
while (data < end); |
| 1016 |
*No = (typeof(*No))countI; |
| 1017 |
} |
| 1018 |
|
| 1019 |
#ifndef NO_FLAG |
| 1020 |
if (flag && (flag_anyISNAN != NULL)) *flag_anyISNAN = 1; |
| 1021 |
#endif |
| 1022 |
*s = (typeof(*s))sum; |
| 1023 |
*s2 = (typeof(*s))msq; |
| 1024 |
} |
| 1025 |
|