-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcluster.cpp
4605 lines (4046 loc) · 137 KB
/
cluster.cpp
1
2
3
4
5
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
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
171
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
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
345
346
347
348
349
350
351
352
353
354
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
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
746
747
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
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
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
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* The C clustering library.
* Copyright (C) 2002 Michiel Jan Laurens de Hoon.
*
* This library was written at the Laboratory of DNA Information Analysis,
* Human Genome Center, Institute of Medical Science, University of Tokyo,
* 4-6-1 Shirokanedai, Minato-ku, Tokyo 108-8639, Japan.
* Contact: mdehoon 'AT' gsc.riken.jp
*
* Permission to use, copy, modify, and distribute this software and its
* documentation with or without modifications and for any purpose and
* without fee is hereby granted, provided that any copyright notices
* appear in all copies and that both those copyright notices and this
* permission notice appear in supporting documentation, and that the
* names of the contributors or copyright holders not be used in
* advertising or publicity pertaining to distribution of the software
* without specific prior permission.
*
* THE CONTRIBUTORS AND COPYRIGHT HOLDERS OF THIS SOFTWARE DISCLAIM ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE
* CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT
* OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
* OR PERFORMANCE OF THIS SOFTWARE.
*
*/
/**********************************************************************************
*
* ÒýÈëcluster
* by Hu yangyang 2016/1/12
*
***********************************************************************************/
#include <time.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <string.h>
#include "cluster.h"
#ifdef WINDOWS
# include <windows.h>
#endif
/* ************************************************************************ */
#ifdef WINDOWS
/* Then we make a Windows DLL */
int WINAPI
clusterdll_init (HANDLE h, DWORD reason, void* foo)
{
return 1;
}
#endif
/* ************************************************************************ */
double mean(int n, double x[])
{ double result = 0.;
int i;
for (i = 0; i < n; i++) result += x[i];
result /= n;
return result;
}
/* ************************************************************************ */
double median (int n, double x[])
/*
Find the median of X(1), ... , X(N), using as much of the quicksort
algorithm as is needed to isolate it.
N.B. On exit, the array X is partially ordered.
Based on Alan J. Miller's median.f90 routine.
*/
{ int i, j;
int nr = n / 2;
int nl = nr - 1;
int even = 0;
/* hi & lo are position limits encompassing the median. */
int lo = 0;
int hi = n-1;
if (n==2*nr) even = 1;
if (n<3)
{ if (n<1) return 0.;
if (n == 1) return x[0];
return 0.5*(x[0]+x[1]);
}
/* Find median of 1st, middle & last values. */
do
{ int loop;
int mid = (lo + hi)/2;
double result = x[mid];
double xlo = x[lo];
double xhi = x[hi];
if (xhi<xlo)
{ double temp = xlo;
xlo = xhi;
xhi = temp;
}
if (result>xhi) result = xhi;
else if (result<xlo) result = xlo;
/* The basic quicksort algorithm to move all values <= the sort key (XMED)
* to the left-hand end, and all higher values to the other end.
*/
i = lo;
j = hi;
do
{ while (x[i]<result) i++;
while (x[j]>result) j--;
loop = 0;
if (i<j)
{ double temp = x[i];
x[i] = x[j];
x[j] = temp;
i++;
j--;
if (i<=j) loop = 1;
}
} while (loop); /* Decide which half the median is in. */
if (even)
{ if (j==nl && i==nr)
/* Special case, n even, j = n/2 & i = j + 1, so the median is
* between the two halves of the series. Find max. of the first
* half & min. of the second half, then average.
*/
{ int k;
double xmax = x[0];
double xmin = x[n-1];
for (k = lo; k <= j; k++) xmax = max(xmax,x[k]);
for (k = i; k <= hi; k++) xmin = min(xmin,x[k]);
return 0.5*(xmin + xmax);
}
if (j<nl) lo = i;
if (i>nr) hi = j;
if (i==j)
{ if (i==nl) lo = nl;
if (j==nr) hi = nr;
}
}
else
{ if (j<nr) lo = i;
if (i>nr) hi = j;
/* Test whether median has been isolated. */
if (i==j && i==nr) return result;
}
}
while (lo<hi-1);
if (even) return (0.5*(x[nl]+x[nr]));
if (x[lo]>x[hi])
{ double temp = x[lo];
x[lo] = x[hi];
x[hi] = temp;
}
return x[nr];
}
/* ********************************************************************** */
static const double* sortdata = NULL; /* used in the quicksort algorithm */
/* ---------------------------------------------------------------------- */
static
int compare(const void* a, const void* b)
/* Helper function for sort. Previously, this was a nested function under
* sort, which is not allowed under ANSI C.
*/
{ const int i1 = *(const int*)a;
const int i2 = *(const int*)b;
const double term1 = sortdata[i1];
const double term2 = sortdata[i2];
if (term1 < term2) return -1;
if (term1 > term2) return +1;
return 0;
}
/* ---------------------------------------------------------------------- */
void sort(int n, const double data[], int index[])
/* Sets up an index table given the data, such that data[index[]] is in
* increasing order. Sorting is done on the indices; the array data
* is unchanged.
*/
{ int i;
sortdata = data;
for (i = 0; i < n; i++) index[i] = i;
qsort(index, n, sizeof(int), compare);
}
/* ********************************************************************** */
static double* getrank (int n, double data[])
/* Calculates the ranks of the elements in the array data. Two elements with
* the same value get the same rank, equal to the average of the ranks had the
* elements different values. The ranks are returned as a newly allocated
* array that should be freed by the calling routine. If getrank fails due to
* a memory allocation error, it returns NULL.
*/
{ int i;
double* rank;
int* index;
rank = (double*)malloc(n*sizeof(double));
if (!rank) return NULL;
index = (int*)malloc(n*sizeof(int));
if (!index)
{ free(rank);
return NULL;
}
/* Call sort to get an index table */
sort (n, data, index);
/* Build a rank table */
for (i = 0; i < n; i++) rank[index[i]] = i;
/* Fix for equal ranks */
i = 0;
while (i < n)
{ int m;
double value = data[index[i]];
int j = i + 1;
while (j < n && data[index[j]] == value) j++;
m = j - i; /* number of equal ranks found */
value = rank[index[i]] + (m-1)/2.;
for (j = i; j < i + m; j++) rank[index[j]] = value;
i += m;
}
free (index);
return rank;
}
/* ---------------------------------------------------------------------- */
static int
makedatamask(int nrows, int ncols, double*** pdata, int*** pmask)
{ int i;
double** data;
int** mask;
data = (double**)malloc(nrows*sizeof(double*));
if(!data) return 0;
mask = (int**)malloc(nrows*sizeof(int*));
if(!mask)
{ free(data);
return 0;
}
for (i = 0; i < nrows; i++)
{ data[i] = (double*)malloc(ncols*sizeof(double));
if(!data[i]) break;
mask[i] = (int*)malloc(ncols*sizeof(int));
if(!mask[i])
{ free(data[i]);
break;
}
}
if (i==nrows) /* break not encountered */
{ *pdata = data;
*pmask = mask;
return 1;
}
*pdata = NULL;
*pmask = NULL;
nrows = i;
for (i = 0; i < nrows; i++)
{ free(data[i]);
free(mask[i]);
}
free(data);
free(mask);
return 0;
}
/* ---------------------------------------------------------------------- */
static void
freedatamask(int n, double** data, int** mask)
{ int i;
for (i = 0; i < n; i++)
{ free(mask[i]);
free(data[i]);
}
free(mask);
free(data);
}
/* ---------------------------------------------------------------------- */
static
double find_closest_pair(int n, double** distmatrix, int* ip, int* jp)
/*
This function searches the distance matrix to find the pair with the shortest
distance between them. The indices of the pair are returned in ip and jp; the
distance itself is returned by the function.
n (input) int
The number of elements in the distance matrix.
distmatrix (input) double**
A ragged array containing the distance matrix. The number of columns in each
row is one less than the row index.
ip (output) int*
A pointer to the integer that is to receive the first index of the pair with
the shortest distance.
jp (output) int*
A pointer to the integer that is to receive the second index of the pair with
the shortest distance.
*/
{ int i, j;
double temp;
double distance = distmatrix[1][0];
*ip = 1;
*jp = 0;
for (i = 1; i < n; i++)
{ for (j = 0; j < i; j++)
{ temp = distmatrix[i][j];
if (temp<distance)
{ distance = temp;
*ip = i;
*jp = j;
}
}
}
return distance;
}
/* ********************************************************************* */
static int svd(int m, int n, double** u, double w[], double** vt)
/*
* This subroutine is a translation of the Algol procedure svd,
* Num. Math. 14, 403-420(1970) by Golub and Reinsch.
* Handbook for Auto. Comp., Vol II-Linear Algebra, 134-151(1971).
*
* This subroutine determines the singular value decomposition
* t
* A=usv of a real m by n rectangular matrix, where m is greater
* than or equal to n. Householder bidiagonalization and a variant
* of the QR algorithm are used.
*
*
* On input.
*
* m is the number of rows of A (and u).
*
* n is the number of columns of A (and u) and the order of v.
*
* u contains the rectangular input matrix A to be decomposed.
*
* On output.
*
* the routine returns an integer ierr equal to
* 0 to indicate a normal return,
* k if the k-th singular value has not been
* determined after 30 iterations,
* -1 if memory allocation fails.
*
*
* w contains the n (non-negative) singular values of a (the
* diagonal elements of s). they are unordered. if an
* error exit is made, the singular values should be correct
* for indices ierr+1,ierr+2,...,n.
*
*
* u contains the matrix u (orthogonal column vectors) of the
* decomposition.
* if an error exit is made, the columns of u corresponding
* to indices of correct singular values should be correct.
*
* t
* vt contains the matrix v (orthogonal) of the decomposition.
* if an error exit is made, the columns of v corresponding
* to indices of correct singular values should be correct.
*
*
* Questions and comments should be directed to B. S. Garbow,
* Applied Mathematics division, Argonne National Laboratory
*
* Modified to eliminate machep
*
* Translated to C by Michiel de Hoon, Human Genome Center,
* University of Tokyo, for inclusion in the C Clustering Library.
* This routine is less general than the original svd routine, as
* it focuses on the singular value decomposition as needed for
* clustering. In particular,
* - We calculate both u and v in all cases
* - We pass the input array A via u; this array is subsequently
* overwritten.
* - We allocate for the array rv1, used as a working space,
* internally in this routine, instead of passing it as an
* argument. If the allocation fails, svd returns -1.
* 2003.06.05
*/
{ int i, j, k, i1, k1, l1, its;
double c,f,h,s,x,y,z;
int l = 0;
int ierr = 0;
double g = 0.0;
double scale = 0.0;
double anorm = 0.0;
double* rv1 = (double*)malloc(n*sizeof(double));
if (!rv1) return -1;
if (m >= n)
{ /* Householder reduction to bidiagonal form */
for (i = 0; i < n; i++)
{ l = i + 1;
rv1[i] = scale * g;
g = 0.0;
s = 0.0;
scale = 0.0;
for (k = i; k < m; k++) scale += fabs(u[k][i]);
if (scale != 0.0)
{ for (k = i; k < m; k++)
{ u[k][i] /= scale;
s += u[k][i]*u[k][i];
}
f = u[i][i];
g = (f >= 0) ? -sqrt(s) : sqrt(s);
h = f * g - s;
u[i][i] = f - g;
if (i < n-1)
{ for (j = l; j < n; j++)
{ s = 0.0;
for (k = i; k < m; k++) s += u[k][i] * u[k][j];
f = s / h;
for (k = i; k < m; k++) u[k][j] += f * u[k][i];
}
}
for (k = i; k < m; k++) u[k][i] *= scale;
}
w[i] = scale * g;
g = 0.0;
s = 0.0;
scale = 0.0;
if (i<n-1)
{ for (k = l; k < n; k++) scale += fabs(u[i][k]);
if (scale != 0.0)
{ for (k = l; k < n; k++)
{ u[i][k] /= scale;
s += u[i][k] * u[i][k];
}
f = u[i][l];
g = (f >= 0) ? -sqrt(s) : sqrt(s);
h = f * g - s;
u[i][l] = f - g;
for (k = l; k < n; k++) rv1[k] = u[i][k] / h;
for (j = l; j < m; j++)
{ s = 0.0;
for (k = l; k < n; k++) s += u[j][k] * u[i][k];
for (k = l; k < n; k++) u[j][k] += s * rv1[k];
}
for (k = l; k < n; k++) u[i][k] *= scale;
}
}
anorm = max(anorm,fabs(w[i])+fabs(rv1[i]));
}
/* accumulation of right-hand transformations */
for (i = n-1; i>=0; i--)
{ if (i < n-1)
{ if (g != 0.0)
{ for (j = l; j < n; j++) vt[i][j] = (u[i][j] / u[i][l]) / g;
/* double division avoids possible underflow */
for (j = l; j < n; j++)
{ s = 0.0;
for (k = l; k < n; k++) s += u[i][k] * vt[j][k];
for (k = l; k < n; k++) vt[j][k] += s * vt[i][k];
}
}
}
for (j = l; j < n; j++)
{ vt[j][i] = 0.0;
vt[i][j] = 0.0;
}
vt[i][i] = 1.0;
g = rv1[i];
l = i;
}
/* accumulation of left-hand transformations */
for (i = n-1; i >= 0; i--)
{ l = i + 1;
g = w[i];
if (i!=n-1)
for (j = l; j < n; j++) u[i][j] = 0.0;
if (g!=0.0)
{ if (i!=n-1)
{ for (j = l; j < n; j++)
{ s = 0.0;
for (k = l; k < m; k++) s += u[k][i] * u[k][j];
/* double division avoids possible underflow */
f = (s / u[i][i]) / g;
for (k = i; k < m; k++) u[k][j] += f * u[k][i];
}
}
for (j = i; j < m; j++) u[j][i] /= g;
}
else
for (j = i; j < m; j++) u[j][i] = 0.0;
u[i][i] += 1.0;
}
/* diagonalization of the bidiagonal form */
for (k = n-1; k >= 0; k--)
{ k1 = k-1;
its = 0;
while(1)
/* test for splitting */
{ for (l = k; l >= 0; l--)
{ l1 = l-1;
if (fabs(rv1[l]) + anorm == anorm) break;
/* rv1[0] is always zero, so there is no exit
* through the bottom of the loop */
if (fabs(w[l1]) + anorm == anorm)
/* cancellation of rv1[l] if l greater than 0 */
{ c = 0.0;
s = 1.0;
for (i = l; i <= k; i++)
{ f = s * rv1[i];
rv1[i] *= c;
if (fabs(f) + anorm == anorm) break;
g = w[i];
h = sqrt(f*f+g*g);
w[i] = h;
c = g / h;
s = -f / h;
for (j = 0; j < m; j++)
{ y = u[j][l1];
z = u[j][i];
u[j][l1] = y * c + z * s;
u[j][i] = -y * s + z * c;
}
}
break;
}
}
/* test for convergence */
z = w[k];
if (l==k) /* convergence */
{ if (z < 0.0)
/* w[k] is made non-negative */
{ w[k] = -z;
for (j = 0; j < n; j++) vt[k][j] = -vt[k][j];
}
break;
}
else if (its==30)
{ ierr = k;
break;
}
else
/* shift from bottom 2 by 2 minor */
{ its++;
x = w[l];
y = w[k1];
g = rv1[k1];
h = rv1[k];
f = ((y - z) * (y + z) + (g - h) * (g + h)) / (2.0 * h * y);
g = sqrt(f*f+1.0);
f = ((x - z) * (x + z) + h * (y / (f + (f >= 0 ? g : -g)) - h)) / x;
/* next qr transformation */
c = 1.0;
s = 1.0;
for (i1 = l; i1 <= k1; i1++)
{ i = i1 + 1;
g = rv1[i];
y = w[i];
h = s * g;
g = c * g;
z = sqrt(f*f+h*h);
rv1[i1] = z;
c = f / z;
s = h / z;
f = x * c + g * s;
g = -x * s + g * c;
h = y * s;
y = y * c;
for (j = 0; j < n; j++)
{ x = vt[i1][j];
z = vt[i][j];
vt[i1][j] = x * c + z * s;
vt[i][j] = -x * s + z * c;
}
z = sqrt(f*f+h*h);
w[i1] = z;
/* rotation can be arbitrary if z is zero */
if (z!=0.0)
{ c = f / z;
s = h / z;
}
f = c * g + s * y;
x = -s * g + c * y;
for (j = 0; j < m; j++)
{ y = u[j][i1];
z = u[j][i];
u[j][i1] = y * c + z * s;
u[j][i] = -y * s + z * c;
}
}
rv1[l] = 0.0;
rv1[k] = f;
w[k] = x;
}
}
}
}
else /* m < n */
{ /* Householder reduction to bidiagonal form */
for (i = 0; i < m; i++)
{ l = i + 1;
rv1[i] = scale * g;
g = 0.0;
s = 0.0;
scale = 0.0;
for (k = i; k < n; k++) scale += fabs(u[i][k]);
if (scale != 0.0)
{ for (k = i; k < n; k++)
{ u[i][k] /= scale;
s += u[i][k]*u[i][k];
}
f = u[i][i];
g = (f >= 0) ? -sqrt(s) : sqrt(s);
h = f * g - s;
u[i][i] = f - g;
if (i < m-1)
{ for (j = l; j < m; j++)
{ s = 0.0;
for (k = i; k < n; k++) s += u[i][k] * u[j][k];
f = s / h;
for (k = i; k < n; k++) u[j][k] += f * u[i][k];
}
}
for (k = i; k < n; k++) u[i][k] *= scale;
}
w[i] = scale * g;
g = 0.0;
s = 0.0;
scale = 0.0;
if (i<m-1)
{ for (k = l; k < m; k++) scale += fabs(u[k][i]);
if (scale != 0.0)
{ for (k = l; k < m; k++)
{ u[k][i] /= scale;
s += u[k][i] * u[k][i];
}
f = u[l][i];
g = (f >= 0) ? -sqrt(s) : sqrt(s);
h = f * g - s;
u[l][i] = f - g;
for (k = l; k < m; k++) rv1[k] = u[k][i] / h;
for (j = l; j < n; j++)
{ s = 0.0;
for (k = l; k < m; k++) s += u[k][j] * u[k][i];
for (k = l; k < m; k++) u[k][j] += s * rv1[k];
}
for (k = l; k < m; k++) u[k][i] *= scale;
}
}
anorm = max(anorm,fabs(w[i])+fabs(rv1[i]));
}
/* accumulation of right-hand transformations */
for (i = m-1; i>=0; i--)
{ if (i < m-1)
{ if (g != 0.0)
{ for (j = l; j < m; j++) vt[j][i] = (u[j][i] / u[l][i]) / g;
/* double division avoids possible underflow */
for (j = l; j < m; j++)
{ s = 0.0;
for (k = l; k < m; k++) s += u[k][i] * vt[k][j];
for (k = l; k < m; k++) vt[k][j] += s * vt[k][i];
}
}
}
for (j = l; j < m; j++)
{ vt[i][j] = 0.0;
vt[j][i] = 0.0;
}
vt[i][i] = 1.0;
g = rv1[i];
l = i;
}
/* accumulation of left-hand transformations */
for (i = m-1; i >= 0; i--)
{ l = i + 1;
g = w[i];
if (i!=m-1)
for (j = l; j < m; j++) u[j][i] = 0.0;
if (g!=0.0)
{ if (i!=m-1)
{ for (j = l; j < m; j++)
{ s = 0.0;
for (k = l; k < n; k++) s += u[i][k] * u[j][k];
/* double division avoids possible underflow */
f = (s / u[i][i]) / g;
for (k = i; k < n; k++) u[j][k] += f * u[i][k];
}
}
for (j = i; j < n; j++) u[i][j] /= g;
}
else
for (j = i; j < n; j++) u[i][j] = 0.0;
u[i][i] += 1.0;
}
/* diagonalization of the bidiagonal form */
for (k = m-1; k >= 0; k--)
{ k1 = k-1;
its = 0;
while(1)
/* test for splitting */
{ for (l = k; l >= 0; l--)
{ l1 = l-1;
if (fabs(rv1[l]) + anorm == anorm) break;
/* rv1[0] is always zero, so there is no exit
* through the bottom of the loop */
if (fabs(w[l1]) + anorm == anorm)
/* cancellation of rv1[l] if l greater than 0 */
{ c = 0.0;
s = 1.0;
for (i = l; i <= k; i++)
{ f = s * rv1[i];
rv1[i] *= c;
if (fabs(f) + anorm == anorm) break;
g = w[i];
h = sqrt(f*f+g*g);
w[i] = h;
c = g / h;
s = -f / h;
for (j = 0; j < n; j++)
{ y = u[l1][j];
z = u[i][j];
u[l1][j] = y * c + z * s;
u[i][j] = -y * s + z * c;
}
}
break;
}
}
/* test for convergence */
z = w[k];
if (l==k) /* convergence */
{ if (z < 0.0)
/* w[k] is made non-negative */
{ w[k] = -z;
for (j = 0; j < m; j++) vt[j][k] = -vt[j][k];
}
break;
}
else if (its==30)
{ ierr = k;
break;
}
else
/* shift from bottom 2 by 2 minor */
{ its++;
x = w[l];
y = w[k1];
g = rv1[k1];
h = rv1[k];
f = ((y - z) * (y + z) + (g - h) * (g + h)) / (2.0 * h * y);
g = sqrt(f*f+1.0);
f = ((x - z) * (x + z) + h * (y / (f + (f >= 0 ? g : -g)) - h)) / x;
/* next qr transformation */
c = 1.0;
s = 1.0;
for (i1 = l; i1 <= k1; i1++)
{ i = i1 + 1;
g = rv1[i];
y = w[i];
h = s * g;
g = c * g;
z = sqrt(f*f+h*h);
rv1[i1] = z;
c = f / z;
s = h / z;
f = x * c + g * s;
g = -x * s + g * c;
h = y * s;
y = y * c;
for (j = 0; j < m; j++)
{ x = vt[j][i1];
z = vt[j][i];
vt[j][i1] = x * c + z * s;
vt[j][i] = -x * s + z * c;
}
z = sqrt(f*f+h*h);
w[i1] = z;
/* rotation can be arbitrary if z is zero */
if (z!=0.0)
{ c = f / z;
s = h / z;
}
f = c * g + s * y;
x = -s * g + c * y;
for (j = 0; j < n; j++)
{ y = u[i1][j];
z = u[i][j];
u[i1][j] = y * c + z * s;
u[i][j] = -y * s + z * c;
}
}
rv1[l] = 0.0;
rv1[k] = f;
w[k] = x;
}
}
}
}
free(rv1);
return ierr;
}
/* ********************************************************************* */
int pca(int nrows, int ncolumns, double** u, double** v, double* w)
/*
Purpose
=======
This subroutine uses the singular value decomposition to perform principal
components analysis of a real nrows by ncolumns rectangular matrix.
Arguments
=========
nrows (input) int
The number of rows in the matrix u.
ncolumns (input) int
The number of columns in the matrix v.
u (input) double[nrows][ncolumns]
On input, the array containing the data to which the principal component
analysis should be applied. The function assumes that the mean has already been
subtracted of each column, and hence that the mean of each column is zero.
On output, see below.
v (input) double[n][n], where n = min(nrows, ncolumns)
Not used on input.
w (input) double[n], where n = min(nrows, ncolumns)
Not used on input.
Return value
============
On output:
If nrows >= ncolumns, then
u contains the coordinates with respect to the principal components;
v contains the principal component vectors.
The dot product u . v reproduces the data that were passed in u.
If nrows < ncolumns, then
u contains the principal component vectors;
v contains the coordinates with respect to the principal components.
The dot product v . u reproduces the data that were passed in u.
The eigenvalues of the covariance matrix are returned in w.
The arrays u, v, and w are sorted according to eigenvalue, with the largest
eigenvalues appearing first.
The function returns 0 if successful, -1 if memory allocation fails, and a
positive integer if the singular value decomposition fails to converge.
*/
{
int i;
int j;
int error;
int* index = (int*)malloc(ncolumns*sizeof(int));
double* temp = (double*)malloc(ncolumns*sizeof(double));
if (!index || !temp)
{ if (index) free(index);
if (temp) free(temp);
return -1;
}
error = svd(nrows, ncolumns, u, w, v);
if (error==0)
{
if (nrows >= ncolumns)
{ for (j = 0; j < ncolumns; j++)
{ const double s = w[j];
for (i = 0; i < nrows; i++) u[i][j] *= s;
}
sort(ncolumns, w, index);
for (i = 0; i < ncolumns/2; i++)
{ j = index[i];
index[i] = index[ncolumns-1-i];
index[ncolumns-1-i] = j;
}
for (i = 0; i < nrows; i++)
{ for (j = 0; j < ncolumns; j++) temp[j] = u[i][index[j]];
for (j = 0; j < ncolumns; j++) u[i][j] = temp[j];
}
for (i = 0; i < ncolumns; i++)
{ for (j = 0; j < ncolumns; j++) temp[j] = v[index[j]][i];
for (j = 0; j < ncolumns; j++) v[j][i] = temp[j];
}
for (i = 0; i < ncolumns; i++) temp[i] = w[index[i]];
for (i = 0; i < ncolumns; i++) w[i] = temp[i];
}
else /* nrows < ncolumns */
{ for (j = 0; j < nrows; j++)
{ const double s = w[j];
for (i = 0; i < nrows; i++) v[i][j] *= s;
}
sort(nrows, w, index);
for (i = 0; i < nrows/2; i++)
{ j = index[i];
index[i] = index[nrows-1-i];
index[nrows-1-i] = j;
}
for (j = 0; j < ncolumns; j++)
{ for (i = 0; i < nrows; i++) temp[i] = u[index[i]][j];
for (i = 0; i < nrows; i++) u[i][j] = temp[i];
}
for (j = 0; j < nrows; j++)
{ for (i = 0; i < nrows; i++) temp[i] = v[j][index[i]];
for (i = 0; i < nrows; i++) v[j][i] = temp[i];
}
for (i = 0; i < nrows; i++) temp[i] = w[index[i]];
for (i = 0; i < nrows; i++) w[i] = temp[i];
}
}
free(index);
free(temp);
return error;
}
/* ********************************************************************* */
static
double euclid (int n, double** data1, double** data2, int** mask1, int** mask2,
const double weight[], int index1, int index2, int transpose)
/*
Purpose
=======
The euclid routine calculates the weighted Euclidean distance between two
rows or columns in a matrix.
Arguments
=========
n (input) int
The number of elements in a row or column. If transpose==0, then n is the number
of columns; otherwise, n is the number of rows.
data1 (input) double array
The data array containing the first vector.
data2 (input) double array
The data array containing the second vector.
mask1 (input) int array
This array which elements in data1 are missing. If mask1[i][j]==0, then
data1[i][j] is missing.
mask2 (input) int array
This array which elements in data2 are missing. If mask2[i][j]==0, then
data2[i][j] is missing.
weight (input) double[n]
The weights that are used to calculate the distance.
index1 (input) int
Index of the first row or column.
index2 (input) int
Index of the second row or column.
transpose (input) int
If transpose==0, the distance between two rows in the matrix is calculated.
Otherwise, the distance between two columns in the matrix is calculated.
============================================================================
*/
{ double result = 0.;
double tweight = 0;
int i;
if (transpose==0) /* Calculate the distance between two rows */
{ for (i = 0; i < n; i++)
{ if (mask1[index1][i] && mask2[index2][i])
{ double term = data1[index1][i] - data2[index2][i];
result += weight[i]*term*term;
tweight += weight[i];
}
}
}
else
{ for (i = 0; i < n; i++)
{ if (mask1[i][index1] && mask2[i][index2])