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