001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.math3.stat.clustering;
018    
019    import java.io.Serializable;
020    import java.util.Collection;
021    import java.util.Arrays;
022    
023    import org.apache.commons.math3.util.MathArrays;
024    
025    /**
026     * A simple implementation of {@link Clusterable} for points with double coordinates.
027     * @version $Id: EuclideanDoublePoint.java 1413096 2012-11-23 22:34:05Z erans $
028     * @since 3.1
029     */
030    public class EuclideanDoublePoint implements Clusterable<EuclideanDoublePoint>, Serializable {
031    
032        /** Serializable version identifier. */
033        private static final long serialVersionUID = 8026472786091227632L;
034    
035        /** Point coordinates. */
036        private final double[] point;
037    
038        /**
039         * Build an instance wrapping an integer array.
040         * <p>
041         * The wrapped array is referenced, it is <em>not</em> copied.
042         *
043         * @param point the n-dimensional point in integer space
044         */
045        public EuclideanDoublePoint(final double[] point) {
046            this.point = point;
047        }
048    
049        /** {@inheritDoc} */
050        public EuclideanDoublePoint centroidOf(final Collection<EuclideanDoublePoint> points) {
051            final double[] centroid = new double[getPoint().length];
052            for (final EuclideanDoublePoint p : points) {
053                for (int i = 0; i < centroid.length; i++) {
054                    centroid[i] += p.getPoint()[i];
055                }
056            }
057            for (int i = 0; i < centroid.length; i++) {
058                centroid[i] /= points.size();
059            }
060            return new EuclideanDoublePoint(centroid);
061        }
062    
063        /** {@inheritDoc} */
064        public double distanceFrom(final EuclideanDoublePoint p) {
065            return MathArrays.distance(point, p.getPoint());
066        }
067    
068        /** {@inheritDoc} */
069        @Override
070        public boolean equals(final Object other) {
071            if (!(other instanceof EuclideanDoublePoint)) {
072                return false;
073            }
074            return Arrays.equals(point, ((EuclideanDoublePoint) other).point);
075        }
076    
077        /**
078         * Get the n-dimensional point in integer space.
079         *
080         * @return a reference (not a copy!) to the wrapped array
081         */
082        public double[] getPoint() {
083            return point;
084        }
085    
086        /** {@inheritDoc} */
087        @Override
088        public int hashCode() {
089            return Arrays.hashCode(point);
090        }
091    
092        /** {@inheritDoc} */
093        @Override
094        public String toString() {
095            return Arrays.toString(point);
096        }
097    
098    }