distance_T.cpp 23.1 KB
Newer Older
1
2
3
4
/*! \file distance.c
 * \brief all functions requiered for R dist function and C hcluster function.
 *
 *  \date Created: probably in 1995
Antoine Lucas's avatar
Antoine Lucas committed
5
 *  \date Last modified: Time-stamp: <2010-01-21 18:42:14 antoine>
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 *
 *  \author R core members, and lately: Antoine Lucas 
 *
 *  
 *  R : A Computer Language for Statistical Data Analysis
 *  Copyright (C) 1995, 1996  Robert Gentleman and Ross Ihaka
 *  Copyright (C) 1998, 2001  Robert Gentleman, Ross Ihaka and the
 *                            R Development Core Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */


#define _AMAP_DISTANCE_TEMPLATE_CPP 1

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

38
#include "distance_T.h"
39
40
#include "distance.h"

Antoine Lucas's avatar
Antoine Lucas committed
41

42
#include <float.h>
Antoine Lucas's avatar
Antoine Lucas committed
43
44
45
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
46
47
48
#include <R_ext/Arith.h>
#include <R_ext/Error.h>
#include <limits>
Antoine Lucas's avatar
Antoine Lucas committed
49

Antoine Lucas's avatar
Antoine Lucas committed
50
#ifndef WIN32
51
#include <pthread.h>
Antoine Lucas's avatar
Antoine Lucas committed
52
#endif
53
54
55
56
57

#define MAX( A , B )  ( ( A ) > ( B ) ? ( A ) : ( B ) )
#define MIN( A , B )  ( ( A ) < ( B ) ? ( A ) : ( B ) )


Antoine Lucas's avatar
Antoine Lucas committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// ---------------------------------------------------------
// Distance euclidean (i.e. sqrt(sum of square) )
//
// Euclidean distance between 2 vectors a,b is
//  d = sqrt[ sum_i (a_i - b_i)^2 ]
//
// This function compute distance between 2 vectors x[i1,] & y[i2,]
// x and y are matrix; we use here only line i1 from x and
// line i2 from y. Number of column (nc) is the same in x and y,
// number of column can differ (nr_x, nr_y).
//
// Flag will be set to 0 if NA value computed in distance
//
// When call by function distance or hclust, x and y are the same; it computes
// distance between vector x[i1,] and x[i2,]
//
// \param x matrix of size nr_x * nc; line i1 is of interest
// \param y matrix of size nr_y * nc; line i1 is of interest
// \param nr_x number of row in matrix x
// \param nr_y number of row in matrix y
// \param nc number of column in matrix x or y
// \param i1 row choosen in matrix x
// \param i2 row choosen in matrix y
// \param flag set to 0 if NA value computed in distance
// \param opt: unused
// 
//  Return: distance value
//
// ---------------------------------------------------------
Antoine Lucas's avatar
Antoine Lucas committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
template<class T> T  distance_T<T>::R_euclidean(double * x, double * y , int nr_x, int nr_y, int nc, 
						int i1, int i2,
						int * flag, T_tri & opt)
{
  T dev, dist;
  int count, j;

  count= 0;
  dist = 0;
  for(j = 0 ; j < nc ; j++) {
    if(R_FINITE(x[i1]) && R_FINITE(y[i2])) {
      dev = (x[i1] - y[i2]);
      dist += dev * dev;
      count++;
101
    }
Antoine Lucas's avatar
Antoine Lucas committed
102
103
104
    i1 += nr_x;
    i2 += nr_y;
  }
Antoine Lucas's avatar
Antoine Lucas committed
105
  if(count == 0) // NA for all j: 
Antoine Lucas's avatar
Antoine Lucas committed
106
107
108
109
110
111
112
113
114
    { 
      *flag = 0;
      return NA_REAL;
    }

  if(count != nc) dist /= ((T)count/nc);
  return sqrt(dist);
}

Antoine Lucas's avatar
Antoine Lucas committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// ---------------------------------------------------------
//
// Distance maximum (supremum norm)
//
// Maximum distance between 2 vectors a,b is
// d = max |ai - bi |
//
// This function compute distance between 2 vectors x[i1,] & y[i2,]
// x and y are matrix; we use here only line i1 from x and
// line i2 from y. Number of column (nc) is the same in x and y,
// number of column can differ (nr_x, nr_y).
//
// Flag will be set to 0 if NA value computed in distance
//
// When call by function distance or hclust, x and y are the same; it computes
// distance between vector x[i1,] and x[i2,]
//
// \param x matrix of size nr_x * nc; line i1 is of interest
// \param y matrix of size nr_y * nc; line i1 is of interest
// \param nr_x number of row in matrix x
// \param nr_y number of row in matrix y
// \param nc number of column in matrix x or y
// \param i1 row choosen in matrix x
// \param i2 row choosen in matrix y
// \param flag set to 0 if NA value computed in distance
// \param opt: unused
//
//  Return: distance value
//
// ---------------------------------------------------------
Antoine Lucas's avatar
Antoine Lucas committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
template<class T> T  distance_T<T>::R_maximum(double * x, double * y , int nr_x, int nr_y, int nc, 
					      int i1, int i2,
					      int * flag, T_tri & opt)
{
  T dev, dist;
  int count, j;

  count = 0;
  dist = std::numeric_limits<T>::min();
  for(j = 0 ; j < nc ; j++) {
    if(R_FINITE(x[i1]) && R_FINITE(y[i2])) {
      dev = fabs(x[i1] - y[i2]);
      if(dev > dist)
	dist = dev;
      count++;
    }
    i1 += nr_x;
    i2 += nr_y;
  }
  if(count == 0)
    {
      *flag = 0;
      return NA_REAL;
    }
  return dist;
}
171

Antoine Lucas's avatar
Antoine Lucas committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

// ---------------------------------------------------------
// Distance manhattan (i.e. sum of abs difference )
//
// manhattan distance between 2 vectors a,b is
//  d = sum_i |a_i - b_i |
//
// This function compute distance between 2 vectors x[i1,] & y[i2,]
// x and y are matrix; we use here only line i1 from x and
// line i2 from y. Number of column (nc) is the same in x and y,
// number of column can differ (nr_x, nr_y).
//
// Flag will be set to 0 if NA value computed in distance
//
// When call by function distance or hclust, x and y are the same; it computes
// distance between vector x[i1,] and x[i2,]
//
// \param x matrix of size nr_x * nc; line i1 is of interest
// \param y matrix of size nr_y * nc; line i1 is of interest
// \param nr_x number of row in matrix x
// \param nr_y number of row in matrix y
// \param nc number of column in matrix x or y
// \param i1 row choosen in matrix x
// \param i2 row choosen in matrix y
// \param flag set to 0 if NA value computed in distance
// \param opt: unused
// 
//  Return: distance value
//
// ---------------------------------------------------------
Antoine Lucas's avatar
Antoine Lucas committed
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
template<class T> T  distance_T<T>::R_manhattan(double * x, double * y , int nr_x, int nr_y, int nc, 
						int i1, int i2,
						int * flag, T_tri & opt)
{
  T dist;
  int count, j;

  count = 0;
  dist = 0;
  for(j = 0 ; j < nc ; j++) {
    if(R_FINITE(x[i1]) && R_FINITE(y[i2])) {
      dist += fabs(x[i1] - y[i2]);
      count++;
    }
    i1 += nr_x;
    i2 += nr_y;
218
  }
Antoine Lucas's avatar
Antoine Lucas committed
219
220
221
222
223
224
225
226
  if(count == 0)
    {
      *flag = 0;
      return NA_REAL;
    }
  if(count != nc) dist /= ((T)count/nc);
  return dist;
}
227

Antoine Lucas's avatar
Antoine Lucas committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// ---------------------------------------------------------
// Distance Camberra
//
// Camberra distance between 2 vectors a,b is
//  d = sum_i | a_i - b_i | / | a_i + b_i |
//
// This function compute distance between 2 vectors x[i1,] & y[i2,]
// x and y are matrix; we use here only line i1 from x and
// line i2 from y. Number of column (nc) is the same in x and y,
// number of column can differ (nr_x, nr_y).
//
// Flag will be set to 0 if NA value computed in distance
//
// When call by function distance or hclust, x and y are the same; it computes
// distance between vector x[i1,] and x[i2,]
//
// \param x matrix of size nr_x * nc; line i1 is of interest
// \param y matrix of size nr_y * nc; line i1 is of interest
// \param nr_x number of row in matrix x
// \param nr_y number of row in matrix y
// \param nc number of column in matrix x or y
// \param i1 row choosen in matrix x
// \param i2 row choosen in matrix y
// \param flag set to 0 if NA value computed in distance
// \param opt: unused
// 
//  Return: distance value
//
// ---------------------------------------------------------
Antoine Lucas's avatar
Antoine Lucas committed
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
template<class T> T  distance_T<T>::R_canberra(double * x, double * y , int nr_x, int nr_y, int nc, 
					       int i1, int i2,
					       int * flag, T_tri & opt)
{
  T dist, sum, diff;
  int count, j;

  count = 0;
  dist = 0;
  for(j = 0 ; j < nc ; j++) {
    if(R_FINITE(x[i1]) && R_FINITE(y[i2])) {
      sum = fabs(x[i1] + y[i2]);
      diff = fabs(x[i1] - y[i2]);
      if (sum > DBL_MIN || diff > DBL_MIN) {
	dist += diff/sum;
272
273
274
	count++;
      }
    }
Antoine Lucas's avatar
Antoine Lucas committed
275
276
    i1 += nr_x;
    i2 += nr_y;
277
  }
Antoine Lucas's avatar
Antoine Lucas committed
278
279
280
281
282
283
284
285
  if(count == 0)
    {
      *flag = 0;
      return NA_REAL;
    }
  if(count != nc) dist /= ((T)count/nc);
  return dist;
}
286

Antoine Lucas's avatar
Antoine Lucas committed
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/** \brief Distance binary
 */
template<class T> T  distance_T<T>::R_dist_binary(double * x, double * y , int nr_x, int nr_y, int nc, 
						  int i1, int i2,
						  int * flag, T_tri & opt)
{
  int total, count, dist;
  int j;

  total = 0;
  count = 0;
  dist = 0;

  for(j = 0 ; j < nc ; j++) {
    if(R_FINITE(x[i1]) && R_FINITE(y[i2])) {
      if(x[i1] || y[i2]){
303
	count++;
Antoine Lucas's avatar
Antoine Lucas committed
304
	if( ! (x[i1] && y[i2]) ) dist++;
305
      }
Antoine Lucas's avatar
Antoine Lucas committed
306
      total++;
307
    }
Antoine Lucas's avatar
Antoine Lucas committed
308
309
    i1 += nr_x;
    i2 += nr_y;
310
311
  }

Antoine Lucas's avatar
Antoine Lucas committed
312
313
314
315
  if(total == 0)
    {
      *flag = 0;
      return NA_REAL;
316
    }
Antoine Lucas's avatar
Antoine Lucas committed
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
  if(count == 0) return 0;
  return (T) dist / count;
}

/** \brief Pearson / Pearson centered (correlation)
 *  \note Added by A. Lucas
 */
template<class T> T  distance_T<T>::R_pearson(double * x, double * y , int nr_x, int nr_y, int nc, 
					      int i1, int i2,
					      int * flag, T_tri & opt)
{
  T num,sum1,sum2, dist;
  int count,j;

  count= 0;
  num = 0;
  sum1 = 0;
  sum2 = 0;

  for(j = 0 ; j < nc ; j++) {
    if(R_FINITE(x[i1]) && R_FINITE(y[i2])) {
      num += (x[i1] * y[i2]);
      sum1 += (x[i1] * x[i1]);
      sum2 += (y[i2] * y[i2]);
      count++;
    }
    i1 += nr_x;
    i2 += nr_y;
345
  }
Antoine Lucas's avatar
Antoine Lucas committed
346
347
348
349
350
351
352
353
  if(count == 0) 
    {
      *flag = 0;
      return NA_REAL;
    }
  dist = 1 - ( num / sqrt(sum1 * sum2) );
  return dist;
}
354

Antoine Lucas's avatar
Antoine Lucas committed
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381

/** \brief Distance correlation (Uncentered Pearson)
 *  \note Added by A. Lucas
 */
template<class T> T  distance_T<T>::R_correlation(double * x, double * y , int nr_x, int nr_y, int nc, 
						  int i1, int i2,
						  int * flag, T_tri & opt)
{
  T num,denum,sumx,sumy,sumxx,sumyy,sumxy;
  int count,j;

  count= 0;
  sumx=0;
  sumy=0;
  sumxx=0;
  sumyy=0;
  sumxy=0;


  for(j = 0 ; j < nc ; j++) {
    if(R_FINITE(x[i1]) && R_FINITE(y[i2])) {
      sumxy += (x[i1] * y[i2]);
      sumx += x[i1];
      sumy += y[i2];
      sumxx += x[i1] * x[i1];
      sumyy += y[i2] * y[i2];
      count++;
382
    }
Antoine Lucas's avatar
Antoine Lucas committed
383
384
385
386
387
388
389
390
391
392
393
394
    i1 += nr_x;
    i2 += nr_y;
  }
  if(count == 0)
    {
      *flag = 0;
      return NA_REAL;
    }
  num = sumxy - ( sumx*sumy /count );
  denum = sqrt( (sumxx - (sumx*sumx /count ) )* (sumyy - (sumy*sumy /count ) ) );
  return 1 - (num / denum);
}
395

Antoine Lucas's avatar
Antoine Lucas committed
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// ---------------------------------------------------------
// Distance Spearman
//
// Spearman distance between 2 vectors a,b is
//  d = sum_i (rank(a_i) - rank(b_i) )^2
//
// If one NA found: return NA
//
// This function compute distance between 2 vectors x[i1,] & y[i2,]
// x and y are matrix; we use here only line i1 from x and
// line i2 from y. Number of column (nc) is the same in x and y,
// number of column can differ (nr_x, nr_y).
//
// Flag will be set to 0 if NA value computed in distance
//
// When call by function distance or hclust, x and y are the same; it computes
// distance between vector x[i1,] and x[i2,]
//
// \param x matrix of size nr_x * nc; line i1 is of interest
// \param y matrix of size nr_y * nc; line i1 is of interest
// \param nr_x number of row in matrix x
// \param nr_y number of row in matrix y
// \param nc number of column in matrix x or y
// \param i1 row choosen in matrix x
// \param i2 row choosen in matrix y
// \param flag set to 0 if NA value computed in distance
// \param opt:  a set of 6 vectors of size nc, allocated but uninitialised. 
//         aim of this parameter is to avoid several vector allocation 
// 
//  Return: distance value
//
// ---------------------------------------------------------
Antoine Lucas's avatar
Antoine Lucas committed
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
template<class T> T  distance_T<T>::R_spearman(double * x, double * y , int nr_x, int nr_y, int nc, 
					       int i1, int i2,
					       int * flag, T_tri & opt)
{
  int j;
  double * data_tri_x = opt.data_tri_x;
  int * order_tri_x = opt.order_tri_x;
  int * rank_tri_x = opt.rank_tri_x;
  double * data_tri_y = opt.data_tri_y;
  int * order_tri_y = opt.order_tri_y;
  int * rank_tri_y = opt.rank_tri_y;
  int n;
  T diffrang=0;

  for(j = 0 ; j < nc ; j++) {
    if(!(R_FINITE(x[i1]) && R_FINITE(y[i2])))
444
445
      {
	*flag = 0;
Antoine Lucas's avatar
Antoine Lucas committed
446
	return NA_REAL;	
447
      }
Antoine Lucas's avatar
Antoine Lucas committed
448
449
450
451
452
453
    order_tri_x[j] = rank_tri_x[j] = 
      order_tri_y[j] = rank_tri_y[j] = j;
    data_tri_x[j] = x[i1];
    data_tri_y[j] = y[i2];
    i1 += nr_x;
    i2 += nr_y;
454
455
  }

Antoine Lucas's avatar
Antoine Lucas committed
456
457
458
459
460
461
462
463
464
465
  n  = nc;
  /* sort and compute rank */
  /* First list */
  rsort_rank_order(data_tri_x, order_tri_x,rank_tri_x, &n);
  /* Second list */
  rsort_rank_order(data_tri_y, order_tri_y,rank_tri_y, &n);

  for(j=0;j<nc;j++)
    {
      diffrang += pow((T) ( rank_tri_x[j] - rank_tri_y[j]),2);
466
    }
Antoine Lucas's avatar
Antoine Lucas committed
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500

  return(  diffrang );

  /*
   * verification in R:
   * Dist(x,method='spearman') ; n =dim(x)[2]
   * l=c(x[3,],x[4,]); sum((rank(l[1:n])-rank(l[(n+1):(2*n)]))^2)
   * cor.test(x[3,],x[4,],method="spearm")$statistic
   */

}


/** \brief Kendall distance (rank base metric)
 * 1 - corr_kendall(x,y)
 *
 *  \note Added by A. Lucas
 *
template<class T> T  distance_T<T>::R_kendall_corr(double * x, double * y , int nr_x, int nr_y, int nc, 
						   int i1, int i2,
						   int * flag, T_tri & opt)
{
  int j,k;
  double * data_tri_x = opt.data_tri_x;
  int * order_tri_x = opt.order_tri_x;
  int * rank_tri_x = opt.rank_tri_x;
  double * data_tri_y = opt.data_tri_y;
  int * order_tri_y = opt.order_tri_y;
  int * rank_tri_y = opt.rank_tri_y;
  int n;
  T dist,P=0;

  for(j = 0 ; j < nc ; j++) {
    if(!(R_FINITE(x[i1]) && R_FINITE(y[i2])))
501
502
      {
	*flag = 0;
Antoine Lucas's avatar
Antoine Lucas committed
503
	return NA_REAL;	
504
      }
Antoine Lucas's avatar
Antoine Lucas committed
505
506
507
508
509
510
    order_tri_x[j] = rank_tri_x[j] = 
      order_tri_y[j] = rank_tri_y[j] = j;
    data_tri_x[j] = x[i1];
    data_tri_y[j] = y[i2];
    i1 += nr_x;
    i2 += nr_y;
511
512
  }

Antoine Lucas's avatar
Antoine Lucas committed
513
514
515
516
517
518
519
520
521
522
523
524
525
  n  = nc;
  // sort and compute rank 
  // First list 
  rsort_rank_order(data_tri_x, order_tri_x,rank_tri_x, &n);
  // Second list 
  rsort_rank_order(data_tri_y, order_tri_y,rank_tri_y, &n);

  for(j=0;j<nc;j++)
    {
     
      for(k=j+1; k < nc; ++k)
	if(rank_tri_y[order_tri_x[j]] < rank_tri_y[order_tri_x[k]])
	  ++P;
526
    }
Antoine Lucas's avatar
Antoine Lucas committed
527
528
529
530
531
532
533
534

  dist = 2 - ( 4*P / (n * (n-1) ) ) ;

  return( dist );

}
*/

Antoine Lucas's avatar
Antoine Lucas committed
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
// ---------------------------------------------------------
// Distance Kendall
//
// Kendall distance between 2 vectors a,b is
//  d = sum_i Kij (x,y)
//
// With Kij(x,y) is 0 if xi,xj in same order as yi,yj;
 //                 1 if not
//
// If one NA found: return NA
//
// This function compute distance between 2 vectors x[i1,] & y[i2,]
// x and y are matrix; we use here only line i1 from x and
// line i2 from y. Number of column (nc) is the same in x and y,
// number of column can differ (nr_x, nr_y).
//
// Flag will be set to 0 if NA value computed in distance
//
// When call by function distance or hclust, x and y are the same; it computes
// distance between vector x[i1,] and x[i2,]
//
// \param x matrix of size nr_x * nc; line i1 is of interest
// \param y matrix of size nr_y * nc; line i1 is of interest
// \param nr_x number of row in matrix x
// \param nr_y number of row in matrix y
// \param nc number of column in matrix x or y
// \param i1 row choosen in matrix x
// \param i2 row choosen in matrix y
// \param flag set to 0 if NA value computed in distance
// \param opt:  a set of 6 vectors of size nc, allocated but uninitialised. 
//         aim of this parameter is to avoid several vector allocation 
// 
//  Return: distance value
//
// ---------------------------------------------------------
Antoine Lucas's avatar
Antoine Lucas committed
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
template<class T> T  distance_T<T>::R_kendall(double * x, double * y , int nr_x, int nr_y, int nc, 
					      int i1, int i2,
					      int * flag, T_tri & opt)
{
  int j,k;
  double * data_tri_x = opt.data_tri_x;
  int * order_tri_x = opt.order_tri_x;
  int * rank_tri_x = opt.rank_tri_x;
  double * data_tri_y = opt.data_tri_y;
  int * order_tri_y = opt.order_tri_y;
  int * rank_tri_y = opt.rank_tri_y;
  int n;
  T dist,P=0;
  bool ordre_x,ordre_y;

  for(j = 0 ; j < nc ; j++) {
    if(!(R_FINITE(x[i1]) && R_FINITE(y[i2])))
587
588
      {
	*flag = 0;
Antoine Lucas's avatar
Antoine Lucas committed
589
	return NA_REAL;	
590
      }
Antoine Lucas's avatar
Antoine Lucas committed
591
592
593
594
595
596
    order_tri_x[j] = rank_tri_x[j] = 
      order_tri_y[j] = rank_tri_y[j] = j;
    data_tri_x[j] = x[i1];
    data_tri_y[j] = y[i2];
    i1 += nr_x;
    i2 += nr_y;
597
598
  }

Antoine Lucas's avatar
Antoine Lucas committed
599
600
601
602
603
604
605
606
607
608
  n  = nc;
  /* sort and compute rank */
  /* First list */
  rsort_rank_order(data_tri_x, order_tri_x,rank_tri_x, &n);
  /* Second list */
  rsort_rank_order(data_tri_y, order_tri_y,rank_tri_y, &n);

  for(j=0;j<nc;j++)
    {     
      for(k=j+1; k < nc; ++k)
609
	{
Antoine Lucas's avatar
Antoine Lucas committed
610
611
612
613
	  ordre_x = rank_tri_x[j] < rank_tri_x[k];
	  ordre_y = rank_tri_y[j] < rank_tri_y[k];
	  if(ordre_x != ordre_y)
	    ++P;
614
615
616
	}
    }

Antoine Lucas's avatar
Antoine Lucas committed
617
  dist = 2* P / (n * (n-1) )  ;
618

Antoine Lucas's avatar
Antoine Lucas committed
619
  return( dist );
620

Antoine Lucas's avatar
Antoine Lucas committed
621
}
622
623


Antoine Lucas's avatar
Antoine Lucas committed
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
// ---------------------------------------------------------
//
// R_distance: compute parallelized distance. Function called direclty by R
// \brief compute distance and call function thread_dist
// that call one of function R_euclidean or R_...
// \param x input matrix
// \param nr,nc number of row and columns
//        nr individuals with nc values.
// \param d distance half matrix.
// \param diag if we compute diagonal of dist matrix (usualy: no).
// \param method 1, 2,... method used
// \param nbprocess: number of threads to create
// \param ierr error return; 1 good; 0 missing values
//
// ---------------------------------------------------------
Antoine Lucas's avatar
Antoine Lucas committed
639
640
641
642
template<class T> void  distance_T<T>::distance(double *x, int *nr,
						int *nc, T *d, int *diag, 
						int *method,int *nbprocess, 
						int * ierr)
Antoine Lucas's avatar
Antoine Lucas committed
643
{
644

645
646


Antoine Lucas's avatar
Antoine Lucas committed
647
648
  int  i;
  T_argument * arguments;
Antoine Lucas's avatar
Antoine Lucas committed
649

Antoine Lucas's avatar
Antoine Lucas committed
650
  bool dc = (*diag) ? 0 : 1; /* diag=1:  we do the diagonal */ 
Antoine Lucas's avatar
Antoine Lucas committed
651
  
Antoine Lucas's avatar
Antoine Lucas committed
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
  /*
   * Arguments sent to thread (adress):
   * number of thread
   * nr
   * nc 
   * dc
   * *x
   * *d
   * *method
   * *ierr
   */ 

  arguments = (T_argument * ) malloc ((*nbprocess) * sizeof( T_argument ));


  //printf("nb processs %d\n",*nbprocess);

  for(i=0; i< *nbprocess; ++i)
    {
      arguments[i].id =i;
      arguments[i].x=x;
      arguments[i].nr = nr;
      arguments[i].nc = nc;
      arguments[i].dc = dc;
      arguments[i].d = d;
      arguments[i].method = method;
      arguments[i].nbprocess= *nbprocess;
      arguments[i].ierr=ierr;
    }
  *ierr = 1; /* res = 1 => no missing values
		res = 0 => missings values */
683
684


Antoine Lucas's avatar
Antoine Lucas committed
685
#ifndef WIN32
Antoine Lucas's avatar
Antoine Lucas committed
686
  pthread_t * th = (pthread_t *) malloc ( *nbprocess * sizeof(pthread_t));
687

Antoine Lucas's avatar
Antoine Lucas committed
688
689
690
691
  for (i=0; i < *nbprocess ; i++)
    {
      pthread_create(th+i,0,distance_T<T>::thread_dist,(void *)(arguments+i));
    }
692

Antoine Lucas's avatar
Antoine Lucas committed
693
694
695
696
697
698
  /* Attends la fin    */
  for (i=0; i < *nbprocess ; i++)
    {      
      pthread_join(*(th+i),NULL);
    }      
  free( th);
699
700
701

#else

Antoine Lucas's avatar
Antoine Lucas committed
702
  // p_thread not yet used on windows.
Antoine Lucas's avatar
Antoine Lucas committed
703

Antoine Lucas's avatar
Antoine Lucas committed
704
705
  arguments[0].nbprocess = 1;
  thread_dist((void *)arguments);
706
707
#endif

Antoine Lucas's avatar
Antoine Lucas committed
708
  free( arguments );
Antoine Lucas's avatar
Antoine Lucas committed
709

710
711
712
713




Antoine Lucas's avatar
Antoine Lucas committed
714
}
715

716

Antoine Lucas's avatar
Antoine Lucas committed
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
/** thread_dist function that compute distance.
 *
 */
template <class T> void* distance_T<T>::thread_dist(void* arguments_void)
{

  int nbprocess,nr,nc,i,j,dc,ij;
  T_argument * arguments = static_cast<T_argument*>(arguments_void); 
  T * d;
  double * x;
  int * method;
  int * ierr;
  /* for spearman dist */
  T_tri opt ;

  T (*distfun)(double*,double*,int, int, int, int, int, int *, T_tri &) = NULL;


  short int no = arguments[0].id;
  nr = *arguments[0].nr;
  nc = *arguments[0].nc;
  dc = arguments[0].dc;
  x  = arguments[0].x;
  d  = arguments[0].d;
  method =  arguments[0].method;
  nbprocess = arguments[0].nbprocess;
  ierr =  arguments[0].ierr;

  
746

747
    
Antoine Lucas's avatar
Antoine Lucas committed
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
  switch(*method) {
  case EUCLIDEAN:
    distfun = R_euclidean;
    break;
  case MAXIMUM:
    distfun = R_maximum;
    break;
  case MANHATTAN:
    distfun = R_manhattan;
    break;
  case CANBERRA:
    distfun = R_canberra;
    break;
  case BINARY:
    distfun = R_dist_binary;
    break;
  case PEARSON:
    distfun = R_pearson;
    break;
  case CORRELATION:
    distfun = R_correlation;
    break;
  case SPEARMAN:
    distfun = R_spearman;
    break;
  case KENDALL:
    distfun = R_kendall;
    break;


  default:
    error("distance(): invalid distance");
  }
781

Antoine Lucas's avatar
Antoine Lucas committed
782
783
784
785
786
787
788
789
790
791
792
793
  
  if( (*method == SPEARMAN) ||  (*method == KENDALL))
    {
      opt.data_tri_x  = (double * ) malloc ( (nc) * sizeof(double));
      opt.order_tri_x  = (int * ) malloc ( (nc) * sizeof(int));
      opt.rank_tri_x  = (int * ) malloc ( (nc) * sizeof(int));
      opt.data_tri_y  = (double * ) malloc ( (nc) * sizeof(double));
      opt.order_tri_y  = (int * ) malloc ( (nc) * sizeof(int));
      opt.rank_tri_y  = (int * ) malloc ( (nc) * sizeof(int));
      if( (opt.data_tri_x == NULL) || (opt.order_tri_x == NULL) || (opt.rank_tri_x == NULL) ||
	  (opt.data_tri_y == NULL) || (opt.order_tri_y == NULL) || (opt.rank_tri_y == NULL)) 
	error("distance(): unable to alloc memory");
794
795
    }

Antoine Lucas's avatar
Antoine Lucas committed
796
797
798
799
  /*
    debut = ((nr+1) / nbprocess + 1 ) * no ;
    fin =  min ( ((nr+1) / nbprocess + 1) * ( no + 1 ) , (nr+1));
  */
800

Antoine Lucas's avatar
Antoine Lucas committed
801
802
  /* debut des boucles 0
     fin: nr+1 */
803

804
    
Antoine Lucas's avatar
Antoine Lucas committed
805
806
  int debut = (int) floor( ((nr+1.)*nbprocess - sqrt( (nr+1.)*(nr+1.) * nbprocess * nbprocess - (nr+1.)*(nr+1.) * nbprocess * no  ) )/nbprocess);
  int fin = (int) floor(((nr+1.)*nbprocess - sqrt( (nr+1.)*(nr+1.) * nbprocess * nbprocess - (nr+1.)*(nr+1.) * nbprocess * (no+1.)  ) )/nbprocess);
807

808
    
Antoine Lucas's avatar
Antoine Lucas committed
809
  //printf("Thread %d debut %d fin %d\n",no,debut,fin);    
810
811


Antoine Lucas's avatar
Antoine Lucas committed
812
813
814
815
816
817
  // here: the computation !
  //    for(j = 0 ; j <= nr ; j++)
  for(j = debut ; j < fin ; j++)
    {
      ij = (2 * (nr-dc) - j +1) * (j) /2 ;
      for(i = j+dc ; i < nr ; i++)
818
	{
Antoine Lucas's avatar
Antoine Lucas committed
819
	  d[ij++] = distfun(x,x,nr, nr, nc, i, j,ierr,opt);
820
	}
Antoine Lucas's avatar
Antoine Lucas committed
821
    }
822

Antoine Lucas's avatar
Antoine Lucas committed
823
824
825
826
827
828
829
830
831
    if( (*method == SPEARMAN) ||  (*method == KENDALL))
    {
	free(opt.data_tri_x);
	free(opt.rank_tri_x);
	free(opt.order_tri_x);	
	free(opt.data_tri_y);
	free(opt.rank_tri_y);
	free(opt.order_tri_y);	
    }
832

Antoine Lucas's avatar
Antoine Lucas committed
833
  return (void*)0;
834
    
Antoine Lucas's avatar
Antoine Lucas committed
835
836
}

837
838


Antoine Lucas's avatar
Antoine Lucas committed
839

840

Antoine Lucas's avatar
Antoine Lucas committed
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
// ---------------------------------------------------------
//
// R_distance_kms: compute distance between individual i1 and
// centroid i2
//
// compute distance and call one of function R_euclidean or R_...
// This function is called by kmeans_Lloyd2 
//
// \param x input matrix (individuals)
// \param y input matrix (centroids)
// \param nr1,nr2,nc number of row (nr1:x, nr2:y) and columns
//        nr individuals with nc values.
// \param i1, i2: indice of individuals (individual i1, centroid i2)
// \param method 1, 2,... method used
// \param ierr for NA 0 if no value can be comuted due to NA
// \param opt optional parameter send to spearman dist.
//
// ---------------------------------------------------------
Antoine Lucas's avatar
Antoine Lucas committed
859
860
861
862
863
864
865
866
867
868
869
870
871
template <class T> T distance_T<T>::distance_kms(double *x,double *y, int nr1,int nr2, int nc,int i1,int i2, int *method, 
						 int * ierr, T_tri & opt)
{
  /*
   * compute distance x[i1,*] - y[i2,*]
   * x matrix n x p
   * y matrix m x p
   * nr1 = n; nr2 = m; nc =p
   */
  
  T res;

  T (*distfun)(double*,double*,int, int, int, int, int, int *, T_tri &) = NULL;
872

Antoine Lucas's avatar
Antoine Lucas committed
873
  // choice of distance
Antoine Lucas's avatar
Antoine Lucas committed
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
  switch(*method) {
  case EUCLIDEAN:
    distfun = R_euclidean;
    break;
  case MAXIMUM:
    distfun = R_maximum;
    break;
  case MANHATTAN:
    distfun = R_manhattan;
    break;
  case CANBERRA:
    distfun = R_canberra;
    break;
  case BINARY:
    distfun = R_dist_binary;
    break;
  case PEARSON:
    distfun = R_pearson;
    break;
  case CORRELATION:
    distfun = R_correlation;
    break;
  case SPEARMAN:
    distfun = R_spearman;
    break;
  case KENDALL:
    distfun = R_kendall;
    break;

  default:
    error("distance(): invalid distance");
  }
Antoine Lucas's avatar
Antoine Lucas committed
906
907

  // here: distance computation
Antoine Lucas's avatar
Antoine Lucas committed
908
909
910
  res = distfun(x,y, nr1,nr2, nc, i1, i2,ierr, opt);
  return( res);
}