List of usage examples for org.apache.commons.math3.ml.distance EarthMoversDistance compute
public double compute(double[] a, double[] b)
From source file:org.apache.solr.client.solrj.io.eval.DistanceEvaluator.java
@Override public Object doWork(Object... values) throws IOException { if (values.length == 2) { Object first = values[0]; Object second = values[1]; if (null == first) { throw new IOException( String.format(Locale.ROOT, "Invalid expression %s - null found for the first value", toExpression(constructingFactory))); }//w ww . ja v a2 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())); } if (type.equals(DistanceType.euclidean)) { EuclideanDistance euclideanDistance = new EuclideanDistance(); return euclideanDistance.compute( ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(), ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray()); } else if (type.equals(DistanceType.manhattan)) { ManhattanDistance manhattanDistance = new ManhattanDistance(); return manhattanDistance.compute( ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(), ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray()); } else if (type.equals(DistanceType.canberra)) { CanberraDistance canberraDistance = new CanberraDistance(); return canberraDistance.compute( ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(), ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray()); } else if (type.equals(DistanceType.earthMovers)) { EarthMoversDistance earthMoversDistance = new EarthMoversDistance(); return earthMoversDistance.compute( ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(), ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray()); } else { return null; } } else if (values.length == 1) { if (values[0] instanceof Matrix) { Matrix matrix = (Matrix) values[0]; if (type.equals(DistanceType.euclidean)) { EuclideanDistance euclideanDistance = new EuclideanDistance(); return distance(euclideanDistance, matrix); } else if (type.equals(DistanceType.canberra)) { CanberraDistance canberraDistance = new CanberraDistance(); return distance(canberraDistance, matrix); } else if (type.equals(DistanceType.manhattan)) { ManhattanDistance manhattanDistance = new ManhattanDistance(); return distance(manhattanDistance, matrix); } else if (type.equals(DistanceType.earthMovers)) { EarthMoversDistance earthMoversDistance = new EarthMoversDistance(); return distance(earthMoversDistance, matrix); } else { return null; } } else { throw new IOException( "distance function operates on either two numeric arrays or a single matrix as parameters."); } } else { throw new IOException( "distance function operates on either two numeric arrays or a single matrix as parameters."); } }
From source file:org.apache.solr.client.solrj.io.eval.EarthMoversDistanceEvaluator.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 ww.j a va 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())); } EarthMoversDistance distance = new EarthMoversDistance(); return distance.compute( ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray(), ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray()); }