EuclideanIntegerPoint.java

  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. package org.apache.commons.math3.stat.clustering;

  18. import java.io.Serializable;
  19. import java.util.Arrays;
  20. import java.util.Collection;

  21. import org.apache.commons.math3.util.MathArrays;

  22. /**
  23.  * A simple implementation of {@link Clusterable} for points with integer coordinates.
  24.  * @since 2.0
  25.  * @deprecated As of 3.2 (to be removed in 4.0),
  26.  * use {@link org.apache.commons.math3.ml.clustering.DoublePoint} instead
  27.  */
  28. @Deprecated
  29. public class EuclideanIntegerPoint implements Clusterable<EuclideanIntegerPoint>, Serializable {

  30.     /** Serializable version identifier. */
  31.     private static final long serialVersionUID = 3946024775784901369L;

  32.     /** Point coordinates. */
  33.     private final int[] point;

  34.     /**
  35.      * Build an instance wrapping an integer array.
  36.      * <p>The wrapped array is referenced, it is <em>not</em> copied.</p>
  37.      * @param point the n-dimensional point in integer space
  38.      */
  39.     public EuclideanIntegerPoint(final int[] point) {
  40.         this.point = point;
  41.     }

  42.     /**
  43.      * Get the n-dimensional point in integer space.
  44.      * @return a reference (not a copy!) to the wrapped array
  45.      */
  46.     public int[] getPoint() {
  47.         return point;
  48.     }

  49.     /** {@inheritDoc} */
  50.     public double distanceFrom(final EuclideanIntegerPoint p) {
  51.         return MathArrays.distance(point, p.getPoint());
  52.     }

  53.     /** {@inheritDoc} */
  54.     public EuclideanIntegerPoint centroidOf(final Collection<EuclideanIntegerPoint> points) {
  55.         int[] centroid = new int[getPoint().length];
  56.         for (EuclideanIntegerPoint p : points) {
  57.             for (int i = 0; i < centroid.length; i++) {
  58.                 centroid[i] += p.getPoint()[i];
  59.             }
  60.         }
  61.         for (int i = 0; i < centroid.length; i++) {
  62.             centroid[i] /= points.size();
  63.         }
  64.         return new EuclideanIntegerPoint(centroid);
  65.     }

  66.     /** {@inheritDoc} */
  67.     @Override
  68.     public boolean equals(final Object other) {
  69.         if (!(other instanceof EuclideanIntegerPoint)) {
  70.             return false;
  71.         }
  72.         return Arrays.equals(point, ((EuclideanIntegerPoint) other).point);
  73.     }

  74.     /** {@inheritDoc} */
  75.     @Override
  76.     public int hashCode() {
  77.         return Arrays.hashCode(point);
  78.     }

  79.     /**
  80.      * {@inheritDoc}
  81.      * @since 2.1
  82.      */
  83.     @Override
  84.     public String toString() {
  85.         return Arrays.toString(point);
  86.     }

  87. }