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 1461871 2013-03-27 22:01:25Z tn $
029     * @since 2.0
030     * @deprecated As of 3.2 (to be removed in 4.0),
031     * use {@link org.apache.commons.math3.ml.clustering.DoublePoint} instead
032     */
033    @Deprecated
034    public class EuclideanIntegerPoint implements Clusterable<EuclideanIntegerPoint>, Serializable {
035    
036        /** Serializable version identifier. */
037        private static final long serialVersionUID = 3946024775784901369L;
038    
039        /** Point coordinates. */
040        private final int[] point;
041    
042        /**
043         * Build an instance wrapping an integer array.
044         * <p>The wrapped array is referenced, it is <em>not</em> copied.</p>
045         * @param point the n-dimensional point in integer space
046         */
047        public EuclideanIntegerPoint(final int[] point) {
048            this.point = point;
049        }
050    
051        /**
052         * Get the n-dimensional point in integer space.
053         * @return a reference (not a copy!) to the wrapped array
054         */
055        public int[] getPoint() {
056            return point;
057        }
058    
059        /** {@inheritDoc} */
060        public double distanceFrom(final EuclideanIntegerPoint p) {
061            return MathArrays.distance(point, p.getPoint());
062        }
063    
064        /** {@inheritDoc} */
065        public EuclideanIntegerPoint centroidOf(final Collection<EuclideanIntegerPoint> points) {
066            int[] centroid = new int[getPoint().length];
067            for (EuclideanIntegerPoint p : points) {
068                for (int i = 0; i < centroid.length; i++) {
069                    centroid[i] += p.getPoint()[i];
070                }
071            }
072            for (int i = 0; i < centroid.length; i++) {
073                centroid[i] /= points.size();
074            }
075            return new EuclideanIntegerPoint(centroid);
076        }
077    
078        /** {@inheritDoc} */
079        @Override
080        public boolean equals(final Object other) {
081            if (!(other instanceof EuclideanIntegerPoint)) {
082                return false;
083            }
084            return Arrays.equals(point, ((EuclideanIntegerPoint) other).point);
085        }
086    
087        /** {@inheritDoc} */
088        @Override
089        public int hashCode() {
090            return Arrays.hashCode(point);
091        }
092    
093        /**
094         * {@inheritDoc}
095         * @since 2.1
096         */
097        @Override
098        public String toString() {
099            return Arrays.toString(point);
100        }
101    
102    }