List of usage examples for org.apache.commons.math3.ml.distance ChebyshevDistance ChebyshevDistance
ChebyshevDistance
From source file:GeMSE.GS.Analysis.Clustering.DistanceMatrix.java
public double[][] GetDistanceMatrix(double[][] space, Metrics metric, ClusteringDomains clusteringDomain) { euclideanDistance = new EuclideanDistance(); manhattanDistance = new ManhattanDistance(); earthMoversDistance = new EarthMoversDistance(); chebyshevDistance = new ChebyshevDistance(); canberraDistance = new CanberraDistance(); double[][] rtv = null; int rowCount = space.length; int colCount = space[0].length; int rtvIndex = 0; switch (clusteringDomain) { case Rows:// w ww.j a v a2s .c o m rtv = new double[1][(int) Math.floor((Math.pow(rowCount, 2) - rowCount) / 2f)]; for (int i = 0; i < rowCount - 1; i++) for (int j = i + 1; j < rowCount; j++) // TODO: replace the following mess with the factory method. switch (metric) { case EuclideanDistance: rtv[0][rtvIndex++] = euclideanDistance.compute(space[i], space[j]); break; case ManhattanDistance: rtv[0][rtvIndex++] = manhattanDistance.compute(space[i], space[j]); break; case EarthMoversDistance: rtv[0][rtvIndex++] = earthMoversDistance.compute(space[i], space[j]); break; case ChebyshevDistance: rtv[0][rtvIndex++] = chebyshevDistance.compute(space[i], space[j]); break; case CanberraDistance: rtv[0][rtvIndex++] = canberraDistance.compute(space[i], space[j]); break; case PearsonCorrelationCoefficient: rtv[0][rtvIndex++] = ComputePCC(space[i], space[j]); break; } break; case Columns: rtv = new double[1][(int) Math.floor((Math.pow(colCount, 2) - colCount) / 2f)]; double[] col_i = new double[rowCount]; double[] col_j = new double[rowCount]; for (int i = 0; i < colCount - 1; i++) { for (int j = i + 1; j < colCount; j++) { for (int row = 0; row < rowCount; row++) { col_i[row] = space[row][i]; col_j[row] = space[row][j]; } // TODO: Replace the following mess with the factory pattern. switch (metric) { case EuclideanDistance: rtv[0][rtvIndex++] = euclideanDistance.compute(col_i, col_j); break; case ManhattanDistance: rtv[0][rtvIndex++] = manhattanDistance.compute(col_i, col_j); break; case EarthMoversDistance: rtv[0][rtvIndex++] = earthMoversDistance.compute(col_i, col_j); break; case ChebyshevDistance: rtv[0][rtvIndex++] = chebyshevDistance.compute(col_i, col_j); break; case CanberraDistance: rtv[0][rtvIndex++] = canberraDistance.compute(col_i, col_j); break; case PearsonCorrelationCoefficient: rtv[0][rtvIndex++] = ComputePCC(col_i, col_j); break; } } } break; } return rtv; }
From source file:org.apache.solr.client.solrj.io.eval.ChebyshevDistanceEvaluator.java
@Override public Object doWork(Object first, Object second) throws IOException { if (null == first) { throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - null found for the first value", toExpression(constructingFactory))); }/*from w w w . j ava 2 s .co m*/ if (null == second) { throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - null found for the second value", toExpression(constructingFactory))); } if (!(first instanceof List<?>)) { throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - found type %s for the first value, expecting a list of numbers", toExpression(constructingFactory), first.getClass().getSimpleName())); } if (!(second instanceof List<?>)) { throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - found type %s for the second value, expecting a list of numbers", toExpression(constructingFactory), first.getClass().getSimpleName())); } ChebyshevDistance distance = new ChebyshevDistance(); return distance.compute( ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray(), ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray()); }