EuclideanDoublePoint.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.Collection;
  20. import java.util.Arrays;

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

  22. /**
  23.  * A simple implementation of {@link Clusterable} for points with double coordinates.
  24.  * @since 3.1
  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 EuclideanDoublePoint implements Clusterable<EuclideanDoublePoint>, Serializable {

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

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

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

  44.     /** {@inheritDoc} */
  45.     public EuclideanDoublePoint centroidOf(final Collection<EuclideanDoublePoint> points) {
  46.         final double[] centroid = new double[getPoint().length];
  47.         for (final EuclideanDoublePoint p : points) {
  48.             for (int i = 0; i < centroid.length; i++) {
  49.                 centroid[i] += p.getPoint()[i];
  50.             }
  51.         }
  52.         for (int i = 0; i < centroid.length; i++) {
  53.             centroid[i] /= points.size();
  54.         }
  55.         return new EuclideanDoublePoint(centroid);
  56.     }

  57.     /** {@inheritDoc} */
  58.     public double distanceFrom(final EuclideanDoublePoint p) {
  59.         return MathArrays.distance(point, p.getPoint());
  60.     }

  61.     /** {@inheritDoc} */
  62.     @Override
  63.     public boolean equals(final Object other) {
  64.         if (!(other instanceof EuclideanDoublePoint)) {
  65.             return false;
  66.         }
  67.         return Arrays.equals(point, ((EuclideanDoublePoint) other).point);
  68.     }

  69.     /**
  70.      * Get the n-dimensional point in integer space.
  71.      *
  72.      * @return a reference (not a copy!) to the wrapped array
  73.      */
  74.     public double[] getPoint() {
  75.         return point;
  76.     }

  77.     /** {@inheritDoc} */
  78.     @Override
  79.     public int hashCode() {
  80.         return Arrays.hashCode(point);
  81.     }

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

  87. }