clean out all my changes
[IRC.git] / Robust / src / Benchmarks / SingleTM / KMeans / Common.java
1 /* =============================================================================
2  *
3  * common.java
4  *
5  * =============================================================================
6  *
7  * For the license of bayes/sort.h and bayes/sort.c, please see the header
8  * of the files.
9  * 
10  * ------------------------------------------------------------------------
11  * 
12  * For the license of kmeans, please see kmeans/LICENSE.kmeans
13  * 
14  * ------------------------------------------------------------------------
15  * 
16  * For the license of ssca2, please see ssca2/COPYRIGHT
17  * 
18  * ------------------------------------------------------------------------
19  * 
20  * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
21  * header of the files.
22  * 
23  * ------------------------------------------------------------------------
24  * 
25  * For the license of lib/rbtree.h and lib/rbtree.c, please see
26  * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
27  * 
28  * ------------------------------------------------------------------------
29  * 
30  * Unless otherwise noted, the following license applies to STAMP files:
31  * 
32  * Copyright (c) 2007, Stanford University
33  * All rights reserved.
34  * 
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions are
37  * met:
38  * 
39  *     * Redistributions of source code must retain the above copyright
40  *       notice, this list of conditions and the following disclaimer.
41  * 
42  *     * Redistributions in binary form must reproduce the above copyright
43  *       notice, this list of conditions and the following disclaimer in
44  *       the documentation and/or other materials provided with the
45  *       distribution.
46  * 
47  *     * Neither the name of Stanford University nor the names of its
48  *       contributors may be used to endorse or promote products derived
49  *       from this software without specific prior written permission.
50  * 
51  * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
52  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
54  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
55  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
56  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
57  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
58  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
59  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
61  * THE POSSIBILITY OF SUCH DAMAGE.
62  *
63  * =============================================================================
64  */
65
66 public class Common {
67
68   public Common() {
69   }
70
71
72   /* =============================================================================
73    * common_euclidDist2
74    * -- multi-dimensional spatial Euclid distance square
75    * =============================================================================
76    */
77    public static float
78     common_euclidDist2 (float[] pt1, float[] pt2, int numdims)
79     {
80       int i;
81       float ans = 0.0f;
82
83       for (i = 0; i < numdims; i++) {
84         ans += (pt1[i] - pt2[i]) * (pt1[i] - pt2[i]);
85       }
86
87       return ans;
88     }
89
90
91   /* =============================================================================
92    * common_findNearestPoint
93    * =============================================================================
94    */
95   public static int
96     common_findNearestPoint (float[]  pt,        /* [nfeatures] */
97         int     nfeatures,
98         float[][] pts,       /* [npts][nfeatures] */
99         int     npts)
100     {
101       int index = -1;
102       int i;
103       //double max_dist = FLT_MAX;
104       float max_dist = (float)3.40282347e+38f;
105       float limit = (float) 0.99999;
106
107       /* Find the cluster center id with min distance to pt */
108       for (i = 0; i < npts; i++) {
109         float dist = common_euclidDist2(pt, pts[i], nfeatures);  /* no need square root */
110         if ((dist / max_dist) < limit) {
111           max_dist = dist;
112           index = i;
113           if (max_dist == 0) {
114             break;
115           }
116         }
117       }
118
119       return index;
120     }
121 }
122
123
124 /* =============================================================================
125  *
126  * End of common.java
127  *
128  * =============================================================================
129  */