View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.math3.stat.clustering;
19  
20  import java.io.Serializable;
21  import java.util.Arrays;
22  import java.util.Collection;
23  
24  import org.apache.commons.math3.util.MathArrays;
25  
26  /**
27   * A simple implementation of {@link Clusterable} for points with integer coordinates.
28   * @version $Id: EuclideanIntegerPoint.java 1461871 2013-03-27 22:01:25Z tn $
29   * @since 2.0
30   * @deprecated As of 3.2 (to be removed in 4.0),
31   * use {@link org.apache.commons.math3.ml.clustering.DoublePoint} instead
32   */
33  @Deprecated
34  public class EuclideanIntegerPoint implements Clusterable<EuclideanIntegerPoint>, Serializable {
35  
36      /** Serializable version identifier. */
37      private static final long serialVersionUID = 3946024775784901369L;
38  
39      /** Point coordinates. */
40      private final int[] point;
41  
42      /**
43       * Build an instance wrapping an integer array.
44       * <p>The wrapped array is referenced, it is <em>not</em> copied.</p>
45       * @param point the n-dimensional point in integer space
46       */
47      public EuclideanIntegerPoint(final int[] point) {
48          this.point = point;
49      }
50  
51      /**
52       * Get the n-dimensional point in integer space.
53       * @return a reference (not a copy!) to the wrapped array
54       */
55      public int[] getPoint() {
56          return point;
57      }
58  
59      /** {@inheritDoc} */
60      public double distanceFrom(final EuclideanIntegerPoint p) {
61          return MathArrays.distance(point, p.getPoint());
62      }
63  
64      /** {@inheritDoc} */
65      public EuclideanIntegerPoint centroidOf(final Collection<EuclideanIntegerPoint> points) {
66          int[] centroid = new int[getPoint().length];
67          for (EuclideanIntegerPoint p : points) {
68              for (int i = 0; i < centroid.length; i++) {
69                  centroid[i] += p.getPoint()[i];
70              }
71          }
72          for (int i = 0; i < centroid.length; i++) {
73              centroid[i] /= points.size();
74          }
75          return new EuclideanIntegerPoint(centroid);
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      public boolean equals(final Object other) {
81          if (!(other instanceof EuclideanIntegerPoint)) {
82              return false;
83          }
84          return Arrays.equals(point, ((EuclideanIntegerPoint) other).point);
85      }
86  
87      /** {@inheritDoc} */
88      @Override
89      public int hashCode() {
90          return Arrays.hashCode(point);
91      }
92  
93      /**
94       * {@inheritDoc}
95       * @since 2.1
96       */
97      @Override
98      public String toString() {
99          return Arrays.toString(point);
100     }
101 
102 }