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