6549
|
1 /* |
|
2 //------------------------------------------------------------------- |
|
3 // C-MEX implementation of COVM - 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 // covm: in-product of matrices, NaN are skipped. |
|
21 // usage: |
|
22 // [cc,nn] = covm_mex(X,Y,flag,W); |
|
23 // |
|
24 // Input: |
|
25 // - X: |
|
26 // - Y: [optional], if empty, Y=X; |
|
27 // - flag: if not empty, it is set to 1 if some NaN was observed |
|
28 // - W: weight vector to compute weighted correlation |
|
29 // |
|
30 // Output: |
|
31 // - CC = X' * sparse(diag(W)) * Y while NaN's are skipped |
|
32 // - NN = real(~isnan(X)')*sparse(diag(W))*real(~isnan(Y)) count of valid (non-NaN) elements |
|
33 // computed more efficiently |
|
34 // |
|
35 // $Id$ |
7889
|
36 // Copyright (C) 2009,2010,2011 Alois Schloegl <a.schloegl@ieee.org> |
6549
|
37 // This function is part of the NaN-toolbox |
7889
|
38 // http://pub.ist.ac.at/~schloegl/matlab/NaN/ |
6549
|
39 // |
|
40 //------------------------------------------------------------------- |
|
41 */ |
|
42 |
6585
|
43 #ifdef __GNUC__ |
7888
|
44 #include <stdint.h> |
6585
|
45 #endif |
6549
|
46 #include <math.h> |
|
47 #include "mex.h" |
|
48 |
|
49 /*#define NO_FLAG*/ |
|
50 |
|
51 |
|
52 void mexFunction(int POutputCount, mxArray* POutput[], int PInputCount, const mxArray *PInputs[]) |
|
53 { |
|
54 double *X0,*Y0=NULL,*X,*Y,*W=NULL; |
6585
|
55 double *CC,cc,nw; |
6549
|
56 double *NN=NULL; |
|
57 |
|
58 size_t rX,cX,rY,cY,nW = 0; |
6585
|
59 size_t i,j,k,nn; |
6549
|
60 char flag_isNaN = 0, flag_speed=0; |
6585
|
61 int ACC_LEVEL; |
6549
|
62 |
|
63 /*********** check input arguments *****************/ |
|
64 |
|
65 // check for proper number of input and output arguments |
|
66 if ((PInputCount <= 0) || (PInputCount > 5)) { |
|
67 mexPrintf("usage: [CC,NN] = covm_mex(X [,Y [,flag [,W [,'E']]]])\n\n"); |
|
68 mexPrintf("Do not use COVM_MEX directly, use COVM instead. \n"); |
|
69 /* |
|
70 mexPrintf("\nCOVM_MEX computes the covariance matrix of real matrices and skips NaN's\n"); |
|
71 mexPrintf("\t[CC,NN] = covm_mex(...)\n\t\t computes CC=X'*Y, NN contains the number of not-NaN elements\n"); |
|
72 mexPrintf("\t\t CC./NN is the unbiased covariance matrix\n"); |
|
73 mexPrintf("\t... = covm_mex(X,Y,...)\n\t\t computes CC=X'*sparse(diag(W))*Y, number of rows of X and Y must match\n"); |
|
74 mexPrintf("\t... = covm_mex(X,[], ...)\n\t\t computes CC=X'*sparse(diag(W))*X\n"); |
|
75 mexPrintf("\t... = covm_mex(...,flag,...)\n\t\t if flag is not empty, it is set to 1 if some NaN occured in X or Y\n"); |
|
76 mexPrintf("\t... = covm_mex(...,W)\n\t\t W to compute weighted covariance, number of elements must match the number of rows of X\n"); |
|
77 mexPrintf("\t\t if isempty(W), all weights are 1\n"); |
|
78 mexPrintf("\t[CC,NN]=covm_mex(X,Y,flag,W)\n"); |
|
79 */ return; |
|
80 } |
|
81 if (POutputCount > 2) |
|
82 mexErrMsgTxt("covm.MEX has 1 to 2 output arguments."); |
|
83 |
|
84 |
|
85 // get 1st argument |
|
86 if(mxIsDouble(PInputs[0]) && !mxIsComplex(PInputs[0])) |
|
87 X0 = mxGetPr(PInputs[0]); |
|
88 else |
|
89 mexErrMsgTxt("First argument must be REAL/DOUBLE."); |
|
90 rX = mxGetM(PInputs[0]); |
|
91 cX = mxGetN(PInputs[0]); |
|
92 |
|
93 // get 2nd argument |
|
94 if (PInputCount > 1) { |
|
95 if (!mxGetNumberOfElements(PInputs[1])) |
|
96 ; // Y0 = NULL; |
|
97 |
|
98 else if (mxIsDouble(PInputs[1]) && !mxIsComplex(PInputs[1])) |
|
99 Y0 = mxGetPr(PInputs[1]); |
|
100 |
|
101 else |
|
102 mexErrMsgTxt("Second argument must be REAL/DOUBLE."); |
|
103 } |
|
104 |
|
105 |
|
106 // get weight vector for weighted sumskipnan |
|
107 if (PInputCount > 3) { |
|
108 // get 4th argument |
|
109 nW = mxGetNumberOfElements(PInputs[3]); |
|
110 if (!nW) |
|
111 ; |
|
112 else if (nW == rX) |
|
113 W = mxGetPr(PInputs[3]); |
|
114 else |
|
115 mexErrMsgTxt("number of elements in W must match numbers of rows in X"); |
|
116 } |
|
117 |
6585
|
118 #ifdef __GNUC__ |
|
119 ACC_LEVEL = 0; |
6549
|
120 { |
|
121 mxArray *LEVEL = NULL; |
|
122 int s = mexCallMATLAB(1, &LEVEL, 0, NULL, "flag_accuracy_level"); |
|
123 if (!s) { |
|
124 ACC_LEVEL = (int) mxGetScalar(LEVEL); |
|
125 } |
|
126 mxDestroyArray(LEVEL); |
|
127 } |
|
128 // mexPrintf("Accuracy Level=%i\n",ACC_LEVEL); |
6585
|
129 #endif |
6549
|
130 if (Y0==NULL) { |
|
131 Y0 = X0; |
|
132 rY = rX; |
|
133 cY = cX; |
|
134 } |
|
135 else { |
|
136 rY = mxGetM(PInputs[1]); |
|
137 cY = mxGetN(PInputs[1]); |
|
138 } |
|
139 if (rX != rY) |
|
140 mexErrMsgTxt("number of rows in X and Y do not match"); |
|
141 |
|
142 /*********** create output arguments *****************/ |
|
143 |
|
144 POutput[0] = mxCreateDoubleMatrix(cX, cY, mxREAL); |
|
145 CC = mxGetPr(POutput[0]); |
|
146 |
|
147 if (POutputCount > 1) { |
|
148 POutput[1] = mxCreateDoubleMatrix(cX, cY, mxREAL); |
|
149 NN = mxGetPr(POutput[1]); |
|
150 } |
|
151 |
|
152 |
|
153 /*********** compute covariance *****************/ |
|
154 |
|
155 #if 0 |
|
156 /*------ version 1 --------------------- |
|
157 this solution is slower than the alternative solution below |
|
158 for transposed matrices, this might be faster. |
|
159 */ |
|
160 for (k=0; k<rX; k++) { |
|
161 double w; |
|
162 if (W) { |
|
163 w = W[k]; |
|
164 for (i=0; i<cX; i++) { |
|
165 double x = X0[k+i*rX]; |
|
166 if (isnan(x)) { |
|
167 #ifndef NO_FLAG |
|
168 flag_isNaN = 1; |
|
169 #endif |
|
170 continue; |
|
171 } |
|
172 for (j=0; j<cY; j++) { |
|
173 double y = Y0[k+j*rY]; |
|
174 if (isnan(y)) { |
|
175 #ifndef NO_FLAG |
|
176 flag_isNaN = 1; |
|
177 #endif |
|
178 continue; |
|
179 } |
|
180 CC[i+j*cX] += x*y*w; |
|
181 if (NN != NULL) |
|
182 NN[i+j*cX] += w; |
|
183 } |
|
184 } |
|
185 } |
|
186 else for (i=0; i<cX; i++) { |
|
187 double x = X0[k+i*rX]; |
|
188 if (isnan(x)) { |
|
189 #ifndef NO_FLAG |
|
190 flag_isNaN = 1; |
|
191 #endif |
|
192 continue; |
|
193 } |
|
194 for (j=0; j<cY; j++) { |
|
195 double y = Y0[k+j*rY]; |
|
196 if (isnan(y)) { |
|
197 #ifndef NO_FLAG |
|
198 flag_isNaN = 1; |
|
199 #endif |
|
200 continue; |
|
201 } |
|
202 CC[i+j*cX] += x*y; |
|
203 if (NN != NULL) |
|
204 NN[i+j*cX] += 1.0; |
|
205 } |
|
206 } |
|
207 } |
|
208 |
|
209 #else |
6585
|
210 #ifdef __GNUC__ |
|
211 if (ACC_LEVEL == 0) |
|
212 #endif |
|
213 { |
6549
|
214 /*------ version 2 --------------------- |
|
215 using naive summation with double accuracy [1] |
|
216 */ |
|
217 if ( (X0 != Y0) || (cX != cY) ) |
|
218 /******** X!=Y, output is not symetric *******/ |
|
219 if (W) /* weighted version */ |
|
220 for (i=0; i<cX; i++) |
|
221 for (j=0; j<cY; j++) { |
|
222 X = X0+i*rX; |
|
223 Y = Y0+j*rY; |
6585
|
224 cc=0.0; |
|
225 nw=0.0; |
6549
|
226 for (k=0; k<rX; k++) { |
|
227 double z = X[k]*Y[k]; |
|
228 if (isnan(z)) { |
|
229 #ifndef NO_FLAG |
|
230 flag_isNaN = 1; |
|
231 #endif |
|
232 continue; |
|
233 } |
|
234 cc += z*W[k]; |
6585
|
235 nw += W[k]; |
6549
|
236 } |
|
237 CC[i+j*cX] = cc; |
|
238 if (NN != NULL) |
6585
|
239 NN[i+j*cX] = nw; |
6549
|
240 } |
|
241 else /* no weights, all weights are 1 */ |
|
242 for (i=0; i<cX; i++) |
|
243 for (j=0; j<cY; j++) { |
|
244 X = X0+i*rX; |
|
245 Y = Y0+j*rY; |
6585
|
246 cc=0.0; |
|
247 nn=0; |
6549
|
248 for (k=0; k<rX; k++) { |
|
249 double z = X[k]*Y[k]; |
|
250 if (isnan(z)) { |
|
251 #ifndef NO_FLAG |
|
252 flag_isNaN = 1; |
|
253 #endif |
|
254 continue; |
|
255 } |
|
256 cc += z; |
|
257 nn++; |
|
258 } |
|
259 CC[i+j*cX] = cc; |
|
260 if (NN != NULL) |
|
261 NN[i+j*cX] = (double)nn; |
|
262 } |
|
263 else // if (X0==Y0) && (cX==cY) |
|
264 /******** X==Y, output is symetric *******/ |
|
265 if (W) /* weighted version */ |
|
266 for (i=0; i<cX; i++) |
|
267 for (j=i; j<cY; j++) { |
|
268 X = X0+i*rX; |
|
269 Y = Y0+j*rY; |
6585
|
270 cc=0.0; |
|
271 nw=0.0; |
6549
|
272 for (k=0; k<rX; k++) { |
|
273 double z = X[k]*Y[k]; |
|
274 if (isnan(z)) { |
|
275 #ifndef NO_FLAG |
|
276 flag_isNaN = 1; |
|
277 #endif |
|
278 continue; |
|
279 } |
|
280 cc += z*W[k]; |
6585
|
281 nw += W[k]; |
6549
|
282 } |
|
283 CC[i+j*cX] = cc; |
|
284 CC[j+i*cX] = cc; |
|
285 if (NN != NULL) { |
6585
|
286 NN[i+j*cX] = nw; |
|
287 NN[j+i*cX] = nw; |
6549
|
288 } |
|
289 } |
|
290 else /* no weights, all weights are 1 */ |
|
291 for (i=0; i<cX; i++) |
|
292 for (j=i; j<cY; j++) { |
|
293 X = X0+i*rX; |
|
294 Y = Y0+j*rY; |
6585
|
295 cc=0.0; |
|
296 nn=0; |
6549
|
297 for (k=0; k<rX; k++) { |
|
298 double z = X[k]*Y[k]; |
|
299 if (isnan(z)) { |
|
300 #ifndef NO_FLAG |
|
301 flag_isNaN = 1; |
|
302 #endif |
|
303 continue; |
|
304 } |
|
305 cc += z; |
|
306 nn++; |
|
307 } |
|
308 CC[i+j*cX] = cc; |
|
309 CC[j+i*cX] = cc; |
|
310 if (NN != NULL) { |
|
311 NN[i+j*cX] = (double)nn; |
|
312 NN[j+i*cX] = (double)nn; |
|
313 } |
|
314 } |
|
315 |
|
316 } |
6585
|
317 |
|
318 #ifdef __GNUC__ |
|
319 |
6549
|
320 else if (ACC_LEVEL == 1) { |
|
321 /*------ version 2 --------------------- |
|
322 using naive summation with extended accuracy [1] |
|
323 */ |
|
324 if ( (X0 != Y0) || (cX != cY) ) |
|
325 /******** X!=Y, output is not symetric *******/ |
|
326 if (W) /* weighted version */ |
|
327 for (i=0; i<cX; i++) |
|
328 for (j=0; j<cY; j++) { |
|
329 X = X0+i*rX; |
|
330 Y = Y0+j*rY; |
|
331 long double cc=0.0; |
|
332 long double nn=0.0; |
|
333 for (k=0; k<rX; k++) { |
|
334 long double z = ((long double)X[k])*Y[k]; |
|
335 if (isnan(z)) { |
|
336 #ifndef NO_FLAG |
|
337 flag_isNaN = 1; |
|
338 #endif |
|
339 continue; |
|
340 } |
|
341 cc += z*W[k]; |
|
342 nn += W[k]; |
|
343 } |
|
344 CC[i+j*cX] = cc; |
|
345 if (NN != NULL) |
|
346 NN[i+j*cX] = nn; |
|
347 } |
|
348 else /* no weights, all weights are 1 */ |
|
349 for (i=0; i<cX; i++) |
|
350 for (j=0; j<cY; j++) { |
|
351 X = X0+i*rX; |
|
352 Y = Y0+j*rY; |
|
353 long double cc=0.0; |
|
354 size_t nn=0; |
|
355 for (k=0; k<rX; k++) { |
|
356 long double z = ((long double)X[k])*Y[k]; |
|
357 if (isnan(z)) { |
|
358 #ifndef NO_FLAG |
|
359 flag_isNaN = 1; |
|
360 #endif |
|
361 continue; |
|
362 } |
|
363 cc += z; |
|
364 nn++; |
|
365 } |
|
366 CC[i+j*cX] = cc; |
|
367 if (NN != NULL) |
|
368 NN[i+j*cX] = (double)nn; |
|
369 } |
|
370 else // if (X0==Y0) && (cX==cY) |
|
371 /******** X==Y, output is symetric *******/ |
|
372 if (W) /* weighted version */ |
|
373 for (i=0; i<cX; i++) |
|
374 for (j=i; j<cY; j++) { |
|
375 X = X0+i*rX; |
|
376 Y = Y0+j*rY; |
|
377 long double cc=0.0; |
|
378 long double nn=0.0; |
|
379 for (k=0; k<rX; k++) { |
|
380 long double z = ((long double)X[k])*Y[k]; |
|
381 if (isnan(z)) { |
|
382 #ifndef NO_FLAG |
|
383 flag_isNaN = 1; |
|
384 #endif |
|
385 continue; |
|
386 } |
|
387 cc += z*W[k]; |
|
388 nn += W[k]; |
|
389 } |
|
390 CC[i+j*cX] = cc; |
|
391 CC[j+i*cX] = cc; |
|
392 if (NN != NULL) { |
|
393 NN[i+j*cX] = nn; |
|
394 NN[j+i*cX] = nn; |
|
395 } |
|
396 } |
|
397 else /* no weights, all weights are 1 */ |
|
398 for (i=0; i<cX; i++) |
|
399 for (j=i; j<cY; j++) { |
|
400 X = X0+i*rX; |
|
401 Y = Y0+j*rY; |
|
402 long double cc=0.0; |
|
403 size_t nn=0; |
|
404 for (k=0; k<rX; k++) { |
|
405 long double z = ((long double)X[k])*Y[k]; |
|
406 if (isnan(z)) { |
|
407 #ifndef NO_FLAG |
|
408 flag_isNaN = 1; |
|
409 #endif |
|
410 continue; |
|
411 } |
|
412 cc += z; |
|
413 nn++; |
|
414 } |
|
415 CC[i+j*cX] = cc; |
|
416 CC[j+i*cX] = cc; |
|
417 if (NN != NULL) { |
|
418 NN[i+j*cX] = (double)nn; |
|
419 NN[j+i*cX] = (double)nn; |
|
420 } |
|
421 } |
|
422 |
|
423 } |
|
424 else if (ACC_LEVEL == 3) { |
|
425 /*------ version 3 --------------------- |
|
426 using Kahan's summation with extended (long double) accuracy [1] |
|
427 this gives more accurate results while the computational effort within the loop is about 4x as high |
|
428 However, first test show an increase in computational time of only about 25 %. |
|
429 |
|
430 [1] David Goldberg, |
|
431 What Every Computer Scientist Should Know About Floating-Point Arithmetic |
|
432 ACM Computing Surveys, Vol 23, No 1, March 1991 |
|
433 */ |
|
434 if ( (X0 != Y0) || (cX != cY) ) |
|
435 /******** X!=Y, output is not symetric *******/ |
|
436 if (W) /* weighted version */ |
|
437 for (i=0; i<cX; i++) |
|
438 for (j=0; j<cY; j++) { |
|
439 X = X0+i*rX; |
|
440 Y = Y0+j*rY; |
|
441 long double cc=0.0; |
|
442 long double nn=0.0; |
|
443 long double rc=0.0; |
|
444 long double rn=0.0; |
|
445 for (k=0; k<rX; k++) { |
|
446 long double t,y; |
|
447 long double z = ((long double)X[k])*Y[k]; |
|
448 if (isnan(z)) { |
|
449 #ifndef NO_FLAG |
|
450 flag_isNaN = 1; |
|
451 #endif |
|
452 continue; |
|
453 } |
|
454 // cc += z*W[k]; [1] |
|
455 y = z*W[k]-rc; |
|
456 t = cc+y; |
|
457 rc= (t-cc)-y; |
|
458 cc= t; |
|
459 |
|
460 // nn += W[k]; [1] |
|
461 y = z*W[k]-rn; |
|
462 t = nn+y; |
|
463 rn= (t-nn)-y; |
|
464 nn= t; |
|
465 } |
|
466 CC[i+j*cX] = cc; |
|
467 if (NN != NULL) |
|
468 NN[i+j*cX] = nn; |
|
469 } |
|
470 else /* no weights, all weights are 1 */ |
|
471 for (i=0; i<cX; i++) |
|
472 for (j=0; j<cY; j++) { |
|
473 X = X0+i*rX; |
|
474 Y = Y0+j*rY; |
|
475 long double cc=0.0; |
|
476 long double rc=0.0; |
|
477 size_t nn=0; |
|
478 for (k=0; k<rX; k++) { |
|
479 long double t,y; |
|
480 long double z = ((long double)X[k])*Y[k]; |
|
481 if (isnan(z)) { |
|
482 #ifndef NO_FLAG |
|
483 flag_isNaN = 1; |
|
484 #endif |
|
485 continue; |
|
486 } |
|
487 // cc += z; [1] |
|
488 y = z-rc; |
|
489 t = cc+y; |
|
490 rc= (t-cc)-y; |
|
491 cc= t; |
|
492 |
|
493 nn++; |
|
494 } |
|
495 CC[i+j*cX] = cc; |
|
496 if (NN != NULL) |
|
497 NN[i+j*cX] = (double)nn; |
|
498 } |
|
499 else // if (X0==Y0) && (cX==cY) |
|
500 /******** X==Y, output is symetric *******/ |
|
501 if (W) /* weighted version */ |
|
502 for (i=0; i<cX; i++) |
|
503 for (j=i; j<cY; j++) { |
|
504 X = X0+i*rX; |
|
505 Y = Y0+j*rY; |
|
506 long double cc=0.0; |
|
507 long double nn=0.0; |
|
508 long double rc=0.0; |
|
509 long double rn=0.0; |
|
510 for (k=0; k<rX; k++) { |
|
511 long double t,y; |
|
512 long double z = ((long double)X[k])*Y[k]; |
|
513 if (isnan(z)) { |
|
514 #ifndef NO_FLAG |
|
515 flag_isNaN = 1; |
|
516 #endif |
|
517 continue; |
|
518 } |
|
519 // cc += z*W[k]; [1] |
|
520 y = z*W[k]-rc; |
|
521 t = cc+y; |
|
522 rc= (t-cc)-y; |
|
523 cc= t; |
|
524 |
|
525 // nn += W[k]; [1] |
|
526 y = z*W[k]-rn; |
|
527 t = nn+y; |
|
528 rn= (t-nn)-y; |
|
529 nn= t; |
|
530 } |
|
531 CC[i+j*cX] = cc; |
|
532 CC[j+i*cX] = cc; |
|
533 if (NN != NULL) { |
|
534 NN[i+j*cX] = nn; |
|
535 NN[j+i*cX] = nn; |
|
536 } |
|
537 } |
|
538 else /* no weights, all weights are 1 */ |
|
539 for (i=0; i<cX; i++) |
|
540 for (j=i; j<cY; j++) { |
|
541 X = X0+i*rX; |
|
542 Y = Y0+j*rY; |
|
543 long double cc=0.0; |
|
544 long double rc=0.0; |
|
545 size_t nn=0; |
|
546 for (k=0; k<rX; k++) { |
|
547 long double t,y; |
|
548 long double z = ((long double)X[k])*Y[k]; |
|
549 if (isnan(z)) { |
|
550 #ifndef NO_FLAG |
|
551 flag_isNaN = 1; |
|
552 #endif |
|
553 continue; |
|
554 } |
|
555 // cc += z; [1] |
|
556 y = z-rc; |
|
557 t = cc+y; |
|
558 rc= (t-cc)-y; |
|
559 cc= t; |
|
560 |
|
561 nn++; |
|
562 } |
|
563 CC[i+j*cX] = cc; |
|
564 CC[j+i*cX] = cc; |
|
565 if (NN != NULL) { |
|
566 NN[i+j*cX] = (double)nn; |
|
567 NN[j+i*cX] = (double)nn; |
|
568 } |
|
569 } |
|
570 } |
|
571 else if (ACC_LEVEL == 2) { |
|
572 /*------ version 3 --------------------- |
|
573 using Kahan's summation with double accuracy [1] |
|
574 this gives more accurate results while the computational effort within the loop is about 4x as high |
|
575 However, first test show an increase in computational time of only about 25 %. |
|
576 |
|
577 [1] David Goldberg, |
|
578 What Every Computer Scientist Should Know About Floating-Point Arithmetic |
|
579 ACM Computing Surveys, Vol 23, No 1, March 1991 |
|
580 */ |
|
581 if ( (X0 != Y0) || (cX != cY) ) |
|
582 /******** X!=Y, output is not symetric *******/ |
|
583 if (W) /* weighted version */ |
|
584 for (i=0; i<cX; i++) |
|
585 for (j=0; j<cY; j++) { |
|
586 X = X0+i*rX; |
|
587 Y = Y0+j*rY; |
|
588 double cc=0.0; |
|
589 double nn=0.0; |
|
590 double rc=0.0; |
|
591 double rn=0.0; |
|
592 for (k=0; k<rX; k++) { |
|
593 double t,y; |
|
594 double z = X[k]*Y[k]; |
|
595 if (isnan(z)) { |
|
596 #ifndef NO_FLAG |
|
597 flag_isNaN = 1; |
|
598 #endif |
|
599 continue; |
|
600 } |
|
601 // cc += z*W[k]; [1] |
|
602 y = z*W[k]-rc; |
|
603 t = cc+y; |
|
604 rc= (t-cc)-y; |
|
605 cc= t; |
|
606 |
|
607 // nn += W[k]; [1] |
|
608 y = z*W[k]-rn; |
|
609 t = nn+y; |
|
610 rn= (t-nn)-y; |
|
611 nn= t; |
|
612 } |
|
613 CC[i+j*cX] = cc; |
|
614 if (NN != NULL) |
|
615 NN[i+j*cX] = nn; |
|
616 } |
|
617 else /* no weights, all weights are 1 */ |
|
618 for (i=0; i<cX; i++) |
|
619 for (j=0; j<cY; j++) { |
|
620 X = X0+i*rX; |
|
621 Y = Y0+j*rY; |
|
622 double cc=0.0; |
|
623 double rc=0.0; |
|
624 size_t nn=0; |
|
625 for (k=0; k<rX; k++) { |
|
626 double t,y; |
|
627 double z = X[k]*Y[k]; |
|
628 if (isnan(z)) { |
|
629 #ifndef NO_FLAG |
|
630 flag_isNaN = 1; |
|
631 #endif |
|
632 continue; |
|
633 } |
|
634 // cc += z; [1] |
|
635 y = z-rc; |
|
636 t = cc+y; |
|
637 rc= (t-cc)-y; |
|
638 cc= t; |
|
639 |
|
640 nn++; |
|
641 } |
|
642 CC[i+j*cX] = cc; |
|
643 if (NN != NULL) |
|
644 NN[i+j*cX] = (double)nn; |
|
645 } |
|
646 else // if (X0==Y0) && (cX==cY) |
|
647 /******** X==Y, output is symetric *******/ |
|
648 if (W) /* weighted version */ |
|
649 for (i=0; i<cX; i++) |
|
650 for (j=i; j<cY; j++) { |
|
651 X = X0+i*rX; |
|
652 Y = Y0+j*rY; |
|
653 double cc=0.0; |
|
654 double nn=0.0; |
|
655 double rc=0.0; |
|
656 double rn=0.0; |
|
657 for (k=0; k<rX; k++) { |
|
658 double t,y; |
|
659 double z = X[k]*Y[k]; |
|
660 if (isnan(z)) { |
|
661 #ifndef NO_FLAG |
|
662 flag_isNaN = 1; |
|
663 #endif |
|
664 continue; |
|
665 } |
|
666 // cc += z*W[k]; [1] |
|
667 y = z*W[k]-rc; |
|
668 t = cc+y; |
|
669 rc= (t-cc)-y; |
|
670 cc= t; |
|
671 |
|
672 // nn += W[k]; [1] |
|
673 y = z*W[k]-rn; |
|
674 t = nn+y; |
|
675 rn= (t-nn)-y; |
|
676 nn= t; |
|
677 } |
|
678 CC[i+j*cX] = cc; |
|
679 CC[j+i*cX] = cc; |
|
680 if (NN != NULL) { |
|
681 NN[i+j*cX] = nn; |
|
682 NN[j+i*cX] = nn; |
|
683 } |
|
684 } |
|
685 else /* no weights, all weights are 1 */ |
|
686 for (i=0; i<cX; i++) |
|
687 for (j=i; j<cY; j++) { |
|
688 X = X0+i*rX; |
|
689 Y = Y0+j*rY; |
|
690 double cc=0.0; |
|
691 double rc=0.0; |
|
692 size_t nn=0; |
|
693 for (k=0; k<rX; k++) { |
|
694 double t,y; |
|
695 double z = X[k]*Y[k]; |
|
696 if (isnan(z)) { |
|
697 #ifndef NO_FLAG |
|
698 flag_isNaN = 1; |
|
699 #endif |
|
700 continue; |
|
701 } |
|
702 // cc += z; [1] |
|
703 y = z-rc; |
|
704 t = cc+y; |
|
705 rc= (t-cc)-y; |
|
706 cc= t; |
|
707 |
|
708 nn++; |
|
709 } |
|
710 CC[i+j*cX] = cc; |
|
711 CC[j+i*cX] = cc; |
|
712 if (NN != NULL) { |
|
713 NN[i+j*cX] = (double)nn; |
|
714 NN[j+i*cX] = (double)nn; |
|
715 } |
|
716 } |
|
717 } |
6585
|
718 #endif |
6549
|
719 |
|
720 #ifndef NO_FLAG |
|
721 //mexPrintf("Third argument must be not empty - otherwise status whether a NaN occured or not cannot be returned."); |
|
722 /* this is a hack, the third input argument is used to return whether a NaN occured or not. |
|
723 this requires that the input argument is a non-empty variable |
|
724 */ |
|
725 if (flag_isNaN && (PInputCount > 2) && mxGetNumberOfElements(PInputs[2])) { |
|
726 // set FLAG_NANS_OCCURED |
|
727 switch (mxGetClassID(PInputs[2])) { |
|
728 case mxDOUBLE_CLASS: |
|
729 *(double*)mxGetData(PInputs[2]) = 1.0; |
|
730 break; |
|
731 case mxSINGLE_CLASS: |
|
732 *(float*)mxGetData(PInputs[2]) = 1.0; |
|
733 break; |
6585
|
734 case mxLOGICAL_CLASS: |
|
735 case mxCHAR_CLASS: |
|
736 case mxINT8_CLASS: |
|
737 case mxUINT8_CLASS: |
|
738 *(char*)mxGetData(PInputs[2]) = 1; |
|
739 break; |
|
740 #ifdef __GNUC__ |
6549
|
741 case mxINT16_CLASS: |
|
742 case mxUINT16_CLASS: |
|
743 *(uint16_t*)mxGetData(PInputs[2]) = 1; |
|
744 break; |
|
745 case mxINT32_CLASS: |
|
746 case mxUINT32_CLASS: |
|
747 *(uint32_t*)mxGetData(PInputs[2])= 1; |
|
748 break; |
|
749 case mxINT64_CLASS: |
|
750 case mxUINT64_CLASS: |
|
751 *(uint64_t*)mxGetData(PInputs[2]) = 1; |
|
752 break; |
|
753 case mxFUNCTION_CLASS: |
|
754 case mxUNKNOWN_CLASS: |
|
755 case mxCELL_CLASS: |
|
756 case mxSTRUCT_CLASS: |
6585
|
757 #endif |
|
758 default: |
6549
|
759 mexPrintf("Type of 3rd input argument cannot be used to return status of NaN occurence."); |
|
760 } |
|
761 } |
|
762 #endif |
|
763 #endif |
|
764 } |
|
765 |