Java examples for java.lang:Math Geometry Distance
Naive method for computing minimum distance - slower but needs less memory.
/*//from ww w. ja v a2s . co m * Java Information Dynamics Toolkit (JIDT) * Copyright (C) 2012, Joseph T. Lizier * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ //package com.java2s; public class Main { /** * Naive method for computing minimum distance - slower but needs less memory. * Made public for debugging only. O(d.n^2) speed * * @param observations * @return */ public static double[] computeMinEuclideanDistancesNaive( double[][] observations) { int numObservations = observations.length; int dimensions = observations[0].length; double[] distances = new double[numObservations]; for (int t = 0; t < numObservations; t++) { double minDistance = Double.POSITIVE_INFINITY; for (int t2 = 0; t2 < numObservations; t2++) { if (t == t2) { continue; } double thisDistance = 0.0; for (int d = 0; (d < dimensions) && (thisDistance < minDistance); d++) { double distanceOnThisVar = (observations[t][d] - observations[t2][d]); thisDistance += distanceOnThisVar * distanceOnThisVar; } // Now we need to sqrt the distance sum thisDistance = Math.sqrt(thisDistance); // Now check if this is a lower distance if (thisDistance < minDistance) { minDistance = thisDistance; } } distances[t] = minDistance; } return distances; } }