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