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