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    
018    package org.apache.commons.math3.stat.clustering;
019    
020    import java.io.Serializable;
021    import java.util.Arrays;
022    import java.util.Collection;
023    
024    import org.apache.commons.math3.util.MathArrays;
025    
026    /**
027     * A simple implementation of {@link Clusterable} for points with integer coordinates.
028     * @version $Id: EuclideanIntegerPoint.java 1416643 2012-12-03 19:37:14Z tn $
029     * @since 2.0
030     */
031    public class EuclideanIntegerPoint implements Clusterable<EuclideanIntegerPoint>, Serializable {
032    
033        /** Serializable version identifier. */
034        private static final long serialVersionUID = 3946024775784901369L;
035    
036        /** Point coordinates. */
037        private final int[] point;
038    
039        /**
040         * Build an instance wrapping an integer array.
041         * <p>The wrapped array is referenced, it is <em>not</em> copied.</p>
042         * @param point the n-dimensional point in integer space
043         */
044        public EuclideanIntegerPoint(final int[] point) {
045            this.point = point;
046        }
047    
048        /**
049         * Get the n-dimensional point in integer space.
050         * @return a reference (not a copy!) to the wrapped array
051         */
052        public int[] getPoint() {
053            return point;
054        }
055    
056        /** {@inheritDoc} */
057        public double distanceFrom(final EuclideanIntegerPoint p) {
058            return MathArrays.distance(point, p.getPoint());
059        }
060    
061        /** {@inheritDoc} */
062        public EuclideanIntegerPoint centroidOf(final Collection<EuclideanIntegerPoint> points) {
063            int[] centroid = new int[getPoint().length];
064            for (EuclideanIntegerPoint p : points) {
065                for (int i = 0; i < centroid.length; i++) {
066                    centroid[i] += p.getPoint()[i];
067                }
068            }
069            for (int i = 0; i < centroid.length; i++) {
070                centroid[i] /= points.size();
071            }
072            return new EuclideanIntegerPoint(centroid);
073        }
074    
075        /** {@inheritDoc} */
076        @Override
077        public boolean equals(final Object other) {
078            if (!(other instanceof EuclideanIntegerPoint)) {
079                return false;
080            }
081            return Arrays.equals(point, ((EuclideanIntegerPoint) other).point);
082        }
083    
084        /** {@inheritDoc} */
085        @Override
086        public int hashCode() {
087            return Arrays.hashCode(point);
088        }
089    
090        /**
091         * {@inheritDoc}
092         * @since 2.1
093         */
094        @Override
095        public String toString() {
096            return Arrays.toString(point);
097        }
098    
099    }