Apache Lucene open-source search software
SOLR-11485: Add olsRegress, spline and derivative Stream Evaluators
Joel Bernstein8750e5f2a97e
diff --git a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java b/solr/core/src/java/org/apache/solr/handler/StreamHandler.javaindex 9446463c600..e3491f65398 100644--- a/solr/core/src/java/org/apache/solr/handler/StreamHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/StreamHandler.java@@ -221,7 +221,6 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,.withFunctionName("sequence", SequenceEvaluator.class).withFunctionName("addAll", AppendEvaluator.class).withFunctionName("append", AppendEvaluator.class)- .withFunctionName("residuals", ResidualsEvaluator.class).withFunctionName("plot", PlotStream.class).withFunctionName("normalDistribution", NormalDistributionEvaluator.class).withFunctionName("uniformDistribution", UniformDistributionEvaluator.class)@@ -259,7 +258,6 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,.withFunctionName("gammaDistribution", GammaDistributionEvaluator.class).withFunctionName("betaDistribution", BetaDistributionEvaluator.class).withFunctionName("polyfit", PolyFitEvaluator.class)- .withFunctionName("polyfitDerivative", PolyFitDerivativeEvaluator.class).withFunctionName("harmonicFit", HarmonicFitEvaluator.class).withFunctionName("loess", LoessEvaluator.class).withFunctionName("matrix", MatrixEvaluator.class)@@ -280,6 +278,10 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,.withFunctionName("corrPValues", CorrelationSignificanceEvaluator.class).withFunctionName("normalizeSum", NormalizeSumEvaluator.class).withFunctionName("geometricDistribution", GeometricDistributionEvaluator.class)+ .withFunctionName("olsRegress", OLSRegressionEvaluator.class)+ .withFunctionName("derivative", DerivativeEvaluator.class)+ .withFunctionName("spline", SplineEvaluator.class)+// Boolean Stream Evaluatorsdiff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/AbsoluteValueEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/AbsoluteValueEvaluator.javaindex b5cc1c78b1c..c6b94467a2f 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/AbsoluteValueEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/AbsoluteValueEvaluator.java@@ -44,8 +44,7 @@ public class AbsoluteValueEvaluator extends RecursiveNumericEvaluator implementsreturn ((List<?>)value).stream().map(innerValue -> doWork(innerValue));}else{- // we know it's a BigDecimal- return Math.abs(((BigDecimal)value).doubleValue());+ return Math.abs(((Number)value).doubleValue());}}}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/AnovaEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/AnovaEvaluator.javaindex c1d6d9395da..dec0655d338 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/AnovaEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/AnovaEvaluator.java@@ -48,7 +48,7 @@ public class AnovaEvaluator extends RecursiveNumericListEvaluator implements ManList<double[]> anovaInput = Arrays.stream(values)// for each List, convert to double[]- .map(value -> ((List<BigDecimal>)value).stream().mapToDouble(BigDecimal::doubleValue).toArray())+ .map(value -> ((List<Number>)value).stream().mapToDouble(Number::doubleValue).toArray())// turn into List<double[]>.collect(Collectors.toList());diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ArcCosineEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ArcCosineEvaluator.javaindex 7dfe9925056..57d7e7f91d1 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ArcCosineEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ArcCosineEvaluator.java@@ -45,8 +45,7 @@ public class ArcCosineEvaluator extends RecursiveNumericEvaluator implements Onereturn ((List<?>)value).stream().map(innerValue -> doWork(innerValue)).collect(Collectors.toList());}else{- // we know it's a BigDecimal- return Math.acos(((BigDecimal)value).doubleValue());+ return Math.acos(((Number)value).doubleValue());}}}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ArcSineEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ArcSineEvaluator.javaindex 0750621bdf6..a3554d9a2be 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ArcSineEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ArcSineEvaluator.java@@ -45,8 +45,7 @@ public class ArcSineEvaluator extends RecursiveNumericEvaluator implements OneVareturn ((List<?>)value).stream().map(innerValue -> doWork(innerValue)).collect(Collectors.toList());}else{- // we know it's a BigDecimal- return Math.asin(((BigDecimal)value).doubleValue());+ return Math.asin(((Number)value).doubleValue());}}}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ArcTangentEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ArcTangentEvaluator.javaindex 95901ee0e33..26ef73d7356 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ArcTangentEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ArcTangentEvaluator.java@@ -46,7 +46,7 @@ public class ArcTangentEvaluator extends RecursiveNumericEvaluator implements On}else{// we know it's a BigDecimal- return Math.atan(((BigDecimal)value).doubleValue());+ return Math.atan(((Number)value).doubleValue());}}}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CeilingEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CeilingEvaluator.javaindex 7f11900c5a8..761adb2a9b5 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CeilingEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CeilingEvaluator.java@@ -45,8 +45,7 @@ public class CeilingEvaluator extends RecursiveNumericEvaluator implements OneVareturn ((List<?>)value).stream().map(innerValue -> doWork(innerValue)).collect(Collectors.toList());}else{- // we know it's a BigDecimal- return Math.ceil(((BigDecimal)value).doubleValue());+ return Math.ceil(((Number)value).doubleValue());}}}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ConvolutionEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ConvolutionEvaluator.javaindex 0d1816aef76..1ec38f48ca5 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ConvolutionEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ConvolutionEvaluator.java@@ -51,8 +51,8 @@ public class ConvolutionEvaluator extends RecursiveNumericEvaluator implements Treturn Arrays.stream(MathArrays.convolve(- ((List)first).stream().mapToDouble(value -> ((BigDecimal)value).doubleValue()).toArray(),- ((List)second).stream().mapToDouble(value -> ((BigDecimal)value).doubleValue()).toArray()+ ((List)first).stream().mapToDouble(value -> ((Number)value).doubleValue()).toArray(),+ ((List)second).stream().mapToDouble(value -> ((Number)value).doubleValue()).toArray())).mapToObj(Double::new).collect(Collectors.toList());}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CorrelationEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CorrelationEvaluator.javaindex 481160c4490..8087248bf43 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CorrelationEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CorrelationEvaluator.java@@ -81,21 +81,21 @@ public class CorrelationEvaluator extends RecursiveObjectEvaluator implements Maif (type.equals(CorrelationType.pearsons)) {PearsonsCorrelation pearsonsCorrelation = new PearsonsCorrelation();return pearsonsCorrelation.correlation(- ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray(),- ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray()+ ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(),+ ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray());} else if (type.equals(CorrelationType.kendalls)) {KendallsCorrelation kendallsCorrelation = new KendallsCorrelation();return kendallsCorrelation.correlation(- ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray(),- ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray()+ ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(),+ ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray());} else if (type.equals(CorrelationType.spearmans)) {SpearmansCorrelation spearmansCorrelation = new SpearmansCorrelation();return spearmansCorrelation.correlation(- ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray(),- ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray()+ ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(),+ ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray());} else {return null;diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CosineEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CosineEvaluator.javaindex 4904d1b51bf..c301f19117d 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CosineEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CosineEvaluator.java@@ -45,8 +45,7 @@ public class CosineEvaluator extends RecursiveNumericEvaluator implements OneValreturn ((List<?>)value).stream().map(innerValue -> doWork(innerValue)).collect(Collectors.toList());}else{- // we know it's a BigDecimal- return Math.cos(((BigDecimal)value).doubleValue());+ return Math.cos(((Number)value).doubleValue());}}}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CosineSimilarityEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CosineSimilarityEvaluator.javaindex ea88400b7a6..37fa1cf09a2 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CosineSimilarityEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CosineSimilarityEvaluator.java@@ -46,8 +46,8 @@ public class CosineSimilarityEvaluator extends RecursiveNumericEvaluator implemethrow 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()));}- double[] d1 = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();- double[] d2 = ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();+ double[] d1 = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();+ double[] d2 = ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();return cosineSimilarity(d1, d2);}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CovarianceEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CovarianceEvaluator.javaindex 810ab330edd..9480578a7ab 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CovarianceEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CovarianceEvaluator.java@@ -41,8 +41,8 @@ public class CovarianceEvaluator extends RecursiveObjectEvaluator implements ManCovariance covariance = new Covariance();return covariance.covariance(- ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray(),- ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray()+ ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(),+ ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray());} else if(values.length == 1) {Matrix matrix = (Matrix) values[0];diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CubedRootEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CubedRootEvaluator.javaindex e041707e43e..c0613f8601a 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CubedRootEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/CubedRootEvaluator.java@@ -45,8 +45,7 @@ public class CubedRootEvaluator extends RecursiveNumericEvaluator implements Onereturn ((List<?>)value).stream().map(innerValue -> doWork(innerValue)).collect(Collectors.toList());}else{- // we know it's a BigDecimal- return Math.cbrt(((BigDecimal)value).doubleValue());+ return Math.cbrt(((Number)value).doubleValue());}}}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/DerivativeEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/DerivativeEvaluator.javanew file mode 100644index 00000000000..183a47babf0--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/DerivativeEvaluator.java@@ -0,0 +1,63 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.solr.client.solrj.io.eval;++import java.io.IOException;+import java.util.Locale;++import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;+import org.apache.commons.math3.analysis.UnivariateFunction;+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;++public class DerivativeEvaluator extends RecursiveObjectEvaluator implements OneValueWorker {+ protected static final long serialVersionUID = 1L;++ public DerivativeEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{+ super(expression, factory);+ }++ @Override+ public Object doWork(Object value) throws IOException {+ if (null == value) {+ throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - null found for the first value", toExpression(constructingFactory)));+ }++ if (!(value instanceof VectorFunction)) {+ throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - found type %s for the first value, expecting a FunctionVector", toExpression(constructingFactory), value.getClass().getSimpleName()));+ }++ VectorFunction vectorFunction = (VectorFunction) value;+ if(!(vectorFunction.getFunction() instanceof DifferentiableUnivariateFunction)) {+ throw new IOException("Cannot evaluate derivative from parameter.");+ }++ DifferentiableUnivariateFunction func = (DifferentiableUnivariateFunction)vectorFunction.getFunction();+ double[] x = (double[])vectorFunction.getFromContext("x");+ UnivariateFunction derfunc = func.derivative();+ double[] dvalues = new double[x.length];+ for(int i=0; i<x.length; i++) {+ dvalues[i] = derfunc.value(x[i]);+ }++ VectorFunction vf = new VectorFunction(derfunc, dvalues);+ vf.addToContext("x", x);+ vf.addToContext("y", vectorFunction.getFromContext("y"));++ return vf;+ }+}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/DescribeEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/DescribeEvaluator.javaindex 4509a8c056c..ca06cf6f22d 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/DescribeEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/DescribeEvaluator.java@@ -48,7 +48,7 @@ public class DescribeEvaluator extends RecursiveNumericEvaluator implements OneV// we know each value is a BigDecimal or a list of BigDecimalsDescriptiveStatistics descriptiveStatistics = new DescriptiveStatistics();- ((List<?>)value).stream().mapToDouble(innerValue -> ((BigDecimal)innerValue).doubleValue()).forEach(innerValue -> descriptiveStatistics.addValue(innerValue));+ ((List<?>)value).stream().mapToDouble(innerValue -> ((Number)innerValue).doubleValue()).forEach(innerValue -> descriptiveStatistics.addValue(innerValue));Map<String,Number> map = new HashMap<>();map.put("max", descriptiveStatistics.getMax());diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/DistanceEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/DistanceEvaluator.javaindex 96712da6a49..f8d92240803 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/DistanceEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/DistanceEvaluator.java@@ -84,27 +84,27 @@ public class DistanceEvaluator extends RecursiveObjectEvaluator implements ManyVif (type.equals(DistanceType.euclidean)) {EuclideanDistance euclideanDistance = new EuclideanDistance();return euclideanDistance.compute(- ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray(),- ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray()+ ((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 -> ((BigDecimal) value).doubleValue()).toArray(),- ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray()+ ((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 -> ((BigDecimal) value).doubleValue()).toArray(),- ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray()+ ((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 -> ((BigDecimal) value).doubleValue()).toArray(),- ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray()+ ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(),+ ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray());} else {return null;diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/DotProductEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/DotProductEvaluator.javaindex 3133bac7d1c..059849f0fe8 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/DotProductEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/DotProductEvaluator.java@@ -49,8 +49,8 @@ public class DotProductEvaluator extends RecursiveNumericEvaluator implements Twthrow 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()));}- RealVector v = new ArrayRealVector(((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray());- RealVector v2 = new ArrayRealVector(((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray());+ RealVector v = new ArrayRealVector(((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray());+ RealVector v2 = new ArrayRealVector(((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray());return v.dotProduct(v2);diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EBEAddEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EBEAddEvaluator.javaindex c1eec9b71a3..241d8181239 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EBEAddEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EBEAddEvaluator.java@@ -49,8 +49,8 @@ public class EBEAddEvaluator extends RecursiveNumericEvaluator implements TwoVal}double[] result = MathArrays.ebeAdd(- ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray(),- ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray()+ ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(),+ ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray());List<Number> numbers = new ArrayList();diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EBEDivideEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EBEDivideEvaluator.javaindex c457f68795a..a3abfa92825 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EBEDivideEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EBEDivideEvaluator.java@@ -49,8 +49,8 @@ public class EBEDivideEvaluator extends RecursiveNumericEvaluator implements Two}double[] result = MathArrays.ebeDivide(- ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray(),- ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray()+ ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(),+ ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray());List<Number> numbers = new ArrayList();diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EBEMultiplyEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EBEMultiplyEvaluator.javaindex b3617cdd37f..a9d3a41fc64 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EBEMultiplyEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EBEMultiplyEvaluator.java@@ -49,8 +49,8 @@ public class EBEMultiplyEvaluator extends RecursiveNumericEvaluator implements T}double[] result = MathArrays.ebeMultiply(- ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray(),- ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray()+ ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(),+ ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray());List<Number> numbers = new ArrayList();diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EBESubtractEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EBESubtractEvaluator.javaindex 2f2f0223bb3..2a0de32bff0 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EBESubtractEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EBESubtractEvaluator.java@@ -49,8 +49,8 @@ public class EBESubtractEvaluator extends RecursiveNumericEvaluator implements T}double[] result = MathArrays.ebeSubtract(- ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray(),- ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray()+ ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(),+ ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray());List<Number> numbers = new ArrayList();diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EmpiricalDistributionEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EmpiricalDistributionEvaluator.javaindex e89db56647e..ccf02e21d4d 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EmpiricalDistributionEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EmpiricalDistributionEvaluator.java@@ -45,7 +45,7 @@ public class EmpiricalDistributionEvaluator extends RecursiveNumericEvaluator imEmpiricalDistribution empiricalDistribution = new EmpiricalDistribution();- double[] backingValues = ((List<?>)value).stream().mapToDouble(innerValue -> ((BigDecimal)innerValue).doubleValue()).sorted().toArray();+ double[] backingValues = ((List<?>)value).stream().mapToDouble(innerValue -> ((Number)innerValue).doubleValue()).sorted().toArray();empiricalDistribution.load(backingValues);return empiricalDistribution;diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EnumeratedDistributionEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EnumeratedDistributionEvaluator.javaindex 4a8b7f79ed8..7e36e26a48b 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EnumeratedDistributionEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/EnumeratedDistributionEvaluator.java@@ -41,13 +41,13 @@ public class EnumeratedDistributionEvaluator extends RecursiveNumericEvaluator iif(values.length == 1) {List<Number> first = (List<Number>)values[0];- int[] samples = ((List) first).stream().mapToInt(value -> ((BigDecimal) value).intValue()).toArray();+ int[] samples = ((List) first).stream().mapToInt(value -> ((Number) value).intValue()).toArray();return new EnumeratedIntegerDistribution(samples);} else {List<Number> first = (List<Number>)values[0];List<Number> second = (List<Number>)values[1];- int[] singletons = ((List) first).stream().mapToInt(value -> ((BigDecimal) value).intValue()).toArray();- double[] probs = ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();+ int[] singletons = ((List) first).stream().mapToInt(value -> ((Number) value).intValue()).toArray();+ double[] probs = ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();return new EnumeratedIntegerDistribution(singletons, probs);}}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/FactorialEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/FactorialEvaluator.javaindex bd8f88d8559..e394495ef34 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/FactorialEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/FactorialEvaluator.java@@ -48,7 +48,7 @@ public class FactorialEvaluator extends RecursiveNumericEvaluator implements Onereturn ((List<?>)value).stream().map(innerValue -> doWork(innerValue)).collect(Collectors.toList());}else{- return CombinatoricsUtils.factorialDouble(((BigDecimal)value).intValue());+ return CombinatoricsUtils.factorialDouble(((Number)value).intValue());}}}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/FieldValueEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/FieldValueEvaluator.javaindex 066fcc9cbc1..fac42740bdf 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/FieldValueEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/FieldValueEvaluator.java@@ -72,8 +72,9 @@ public class FieldValueEvaluator extends SourceEvaluator {return list;} else if(value instanceof Matrix) {return value;- }- else if(value instanceof Iterable && !(value instanceof List<?>)){+ } else if(value instanceof VectorFunction) {+ return value;+ } else if(value instanceof Iterable && !(value instanceof List<?>)){Iterable<?> iter = (Iterable<?>)value;List<Object> list = new ArrayList<Object>();for(Object obj : iter){@@ -82,7 +83,11 @@ public class FieldValueEvaluator extends SourceEvaluator {return list;}}-++ if(value == null) {+ return fieldName;+ }+return value;}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/FindDelayEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/FindDelayEvaluator.javaindex f50a521baeb..58486ab6f6b 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/FindDelayEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/FindDelayEvaluator.java@@ -53,10 +53,10 @@ public class FindDelayEvaluator extends RecursiveNumericEvaluator implements Two}// Get first and second lists as arrays, where second is in reverse order- double[] firstArray = ((List)first).stream().mapToDouble(value -> ((BigDecimal)value).doubleValue()).toArray();+ double[] firstArray = ((List)first).stream().mapToDouble(value -> ((Number)value).doubleValue()).toArray();double[] secondArray = StreamSupport.stream(Spliterators.spliteratorUnknownSize(((LinkedList)((List)second).stream().collect(Collectors.toCollection(LinkedList::new))).descendingIterator(),- Spliterator.ORDERED), false).mapToDouble(value -> ((BigDecimal)value).doubleValue()).toArray();+ Spliterator.ORDERED), false).mapToDouble(value -> ((Number)value).doubleValue()).toArray();double[] convolution = MathArrays.convolve(firstArray, secondArray);double maxValue = -Double.MAX_VALUE;diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/FloorEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/FloorEvaluator.javaindex c6d8b848e92..26cd54fcc19 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/FloorEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/FloorEvaluator.java@@ -45,8 +45,7 @@ public class FloorEvaluator extends RecursiveNumericEvaluator implements OneValureturn ((List<?>)value).stream().map(innerValue -> doWork(innerValue)).collect(Collectors.toList());}else{- // we know it's a BigDecimal- return Math.floor(((BigDecimal)value).doubleValue());+ return Math.floor(((Number)value).doubleValue());}}}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/HarmonicFitEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/HarmonicFitEvaluator.javaindex 09ed8dfd12a..932c113bf30 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/HarmonicFitEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/HarmonicFitEvaluator.java@@ -49,7 +49,7 @@ public class HarmonicFitEvaluator extends RecursiveNumericEvaluator implements Mif(objects.length == 1) {//Only the y values passed- y = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();+ y = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();x = new double[y.length];for(int i=0; i<y.length; i++) {x[i] = i;@@ -58,8 +58,8 @@ public class HarmonicFitEvaluator extends RecursiveNumericEvaluator implements M} else if(objects.length == 2) {// x and y passedObject second = objects[1];- x = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();- y = ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();+ x = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();+ y = ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/HyperbolicCosineEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/HyperbolicCosineEvaluator.javaindex a9e7c0aa4c7..883aad267d2 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/HyperbolicCosineEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/HyperbolicCosineEvaluator.java@@ -45,8 +45,7 @@ public class HyperbolicCosineEvaluator extends RecursiveNumericEvaluator implemereturn ((List<?>)value).stream().map(innerValue -> doWork(innerValue)).collect(Collectors.toList());}else{- // we know it's a BigDecimal- return Math.cosh(((BigDecimal)value).doubleValue());+ return Math.cosh(((Number)value).doubleValue());}}}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/HyperbolicSineEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/HyperbolicSineEvaluator.javaindex c23f64e9442..e4d1fed9245 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/HyperbolicSineEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/HyperbolicSineEvaluator.java@@ -45,8 +45,7 @@ public class HyperbolicSineEvaluator extends RecursiveNumericEvaluator implementreturn ((List<?>)value).stream().map(innerValue -> doWork(innerValue)).collect(Collectors.toList());}else{- // we know it's a BigDecimal- return Math.sinh(((BigDecimal)value).doubleValue());+ return Math.sinh(((Number)value).doubleValue());}}}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/LoessEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/LoessEvaluator.javaindex 5824f61099b..31f6962fe39 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/LoessEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/LoessEvaluator.java@@ -17,11 +17,13 @@package org.apache.solr.client.solrj.io.eval;import java.io.IOException;-import java.math.BigDecimal;import java.util.List;import java.util.ArrayList;+import java.util.Map;import org.apache.commons.math3.analysis.interpolation.LoessInterpolator;+import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction;+import org.apache.solr.client.solrj.io.Tuple;import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionNamedParameter;import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;@@ -59,25 +61,45 @@ public class LoessEvaluator extends RecursiveNumericEvaluator implements ManyValif(objects.length == 1) {//Only the y values passed- y = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();+ y = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();x = new double[y.length];for(int i=0; i<y.length; i++) {x[i] = i;}} else if(objects.length == 2) {Object second = objects[1];- x = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();- y = ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();+ x = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();+ y = ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();}LoessInterpolator interpolator = new LoessInterpolator(bandwidth, robustIterations);double[] smooth = interpolator.smooth(x, y);- List list = new ArrayList();+ List<Number> list = new ArrayList();for(double yvalue : smooth) {list.add(yvalue);}- return list;+ PolynomialSplineFunction spline = interpolator.interpolate(x, y);++ VectorFunction vec = new VectorFunction(spline, list);+ vec.addToContext("x", x);+ vec.addToContext("y", y);++ return vec;+ }+++ public static class LoessRegressionTuple extends Tuple {+ private PolynomialSplineFunction spline;++ public LoessRegressionTuple(PolynomialSplineFunction spline, Map<?,?> map) {+ super(map);+ this.spline = spline;+ }++ public double predict(double value) {+ return spline.value(value);+ }}}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MeanDifferenceEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MeanDifferenceEvaluator.javaindex c93c97a4a90..ff9ddb1d877 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MeanDifferenceEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/MeanDifferenceEvaluator.java@@ -48,8 +48,8 @@ public class MeanDifferenceEvaluator extends RecursiveNumericEvaluator implement}return StatUtils.meanDifference(- ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray(),- ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray()+ ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(),+ ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray());}}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/NaturalLogEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/NaturalLogEvaluator.javaindex b59ba38f9b3..54b62a00bfc 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/NaturalLogEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/NaturalLogEvaluator.java@@ -45,8 +45,7 @@ public class NaturalLogEvaluator extends RecursiveNumericEvaluator implements Onreturn ((List<?>)value).stream().map(innerValue -> doWork(innerValue)).collect(Collectors.toList());}else{- // we know it's a BigDecimal- return Math.log(((BigDecimal)value).doubleValue());+ return Math.log(((Number)value).doubleValue());}}}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/OLSRegressionEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/OLSRegressionEvaluator.javanew file mode 100644index 00000000000..c708a144772--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/OLSRegressionEvaluator.java@@ -0,0 +1,123 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++package org.apache.solr.client.solrj.io.eval;++import java.io.IOException;+import java.util.ArrayList;+import java.util.List;+import java.util.Map;+import java.util.HashMap;++import org.apache.commons.math3.stat.regression.MultipleLinearRegression;+import org.apache.commons.math3.stat.regression.OLSMultipleLinearRegression;+import org.apache.solr.client.solrj.io.Tuple;+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;++public class OLSRegressionEvaluator extends RecursiveObjectEvaluator implements ManyValueWorker {+ protected static final long serialVersionUID = 1L;++ public OLSRegressionEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{+ super(expression, factory);+ }++ @Override+ public Object doWork(Object ... values) throws IOException {++ Matrix observations = null;+ List<Number> outcomes = null;++ if(values[0] instanceof Matrix) {+ observations = (Matrix)values[0];+ } else {+ throw new IOException("The first parameter for olsRegress should be the observation matrix.");+ }++ if(values[1] instanceof List) {+ outcomes = (List) values[1];+ } else {+ throw new IOException("The second parameter for olsRegress should be outcome array. ");+ }++ double[][] observationData = observations.getData();+ double[] outcomeData = new double[outcomes.size()];+ for(int i=0; i<outcomeData.length; i++) {+ outcomeData[i] = outcomes.get(i).doubleValue();+ }++ OLSMultipleLinearRegression multipleLinearRegression = (OLSMultipleLinearRegression)regress(observationData, outcomeData);++ Map map = new HashMap();++ map.put("regressandVariance", multipleLinearRegression.estimateRegressandVariance());+ map.put("regressionParameters", list(multipleLinearRegression.estimateRegressionParameters()));+ map.put("RSquared", multipleLinearRegression.calculateRSquared());+ map.put("adjustedRSquared", multipleLinearRegression.calculateAdjustedRSquared());+ map.put("residualSumSquares", multipleLinearRegression.calculateResidualSumOfSquares());++ try {+ map.put("regressionParametersStandardErrors", list(multipleLinearRegression.estimateRegressionParametersStandardErrors()));+ map.put("regressionParametersVariance", new Matrix(multipleLinearRegression.estimateRegressionParametersVariance()));+ } catch (Exception e) {+ //Exception is thrown if the matrix is singular+ }++ return new MultipleRegressionTuple(multipleLinearRegression, map);+ }++ private List<Number> list(double[] values) {+ List list = new ArrayList();+ for(double d : values) {+ list.add(d);+ }+ return list;+ }++ protected MultipleLinearRegression regress(double[][] observations, double[] outcomes) {+ OLSMultipleLinearRegression olsMultipleLinearRegression = new OLSMultipleLinearRegression();+ olsMultipleLinearRegression.newSampleData(outcomes, observations);+ return olsMultipleLinearRegression;+ }++ public static class MultipleRegressionTuple extends Tuple {++ private MultipleLinearRegression multipleLinearRegression;+++ public MultipleRegressionTuple(MultipleLinearRegression multipleLinearRegression, Map<?,?> map) {+ super(map);+ this.multipleLinearRegression = multipleLinearRegression;+ }++ public double predict(double[] values) {+ List<Number> weights = (List<Number>)get("regressionParameters");+ double prediction = 0.0;+ List<Number> predictors = new ArrayList();+ predictors.add(1.0D);+ for(double d : values) {+ predictors.add(d);+ }+ for(int i=0; i< predictors.size(); i++) {+ prediction += weights.get(i).doubleValue()*predictors.get(i).doubleValue();+ }++ return prediction;+ }+ }+}+diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PolyFitDerivativeEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PolyFitDerivativeEvaluator.javaindex 1aacb9ee90b..775693842cf 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PolyFitDerivativeEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PolyFitDerivativeEvaluator.java@@ -52,7 +52,7 @@ public class PolyFitDerivativeEvaluator extends RecursiveNumericEvaluator implemif(objects.length == 1) {//Only the y values passed- y = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();+ y = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();x = new double[y.length];for(int i=0; i<y.length; i++) {x[i] = i;@@ -62,18 +62,18 @@ public class PolyFitDerivativeEvaluator extends RecursiveNumericEvaluator implem// x, y and degree passedObject second = objects[1];- x = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();- y = ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();+ x = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();+ y = ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();degree = ((Number)objects[2]).intValue();} else if(objects.length == 2) {if(objects[1] instanceof List) {// x and y passedObject second = objects[1];- x = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();- y = ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();+ x = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();+ y = ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();} else {// y and degree passed- y = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();+ y = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();x = new double[y.length];for(int i=0; i<y.length; i++) {x[i] = i;diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PolyFitEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PolyFitEvaluator.javaindex 2c0bf2a3e45..05365658244 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PolyFitEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PolyFitEvaluator.java@@ -51,7 +51,7 @@ public class PolyFitEvaluator extends RecursiveNumericEvaluator implements ManyVif(objects.length == 1) {//Only the y values passed- y = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();+ y = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();x = new double[y.length];for(int i=0; i<y.length; i++) {x[i] = i;@@ -61,18 +61,18 @@ public class PolyFitEvaluator extends RecursiveNumericEvaluator implements ManyV// x, y and degree passedObject second = objects[1];- x = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();- y = ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();+ x = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();+ y = ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();degree = ((Number)objects[2]).intValue();} else if(objects.length == 2) {if(objects[1] instanceof List) {// x and y passedObject second = objects[1];- x = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();- y = ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();+ x = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();+ y = ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();} else {// y and degree passed- y = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();+ y = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();x = new double[y.length];for(int i=0; i<y.length; i++) {x[i] = i;diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PredictEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PredictEvaluator.javaindex 5d9368b975b..075a1b3745b 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PredictEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/PredictEvaluator.java@@ -17,10 +17,12 @@package org.apache.solr.client.solrj.io.eval;import java.io.IOException;+import java.util.ArrayList;import java.util.List;import java.util.Locale;import java.util.stream.Collectors;+import org.apache.commons.math3.analysis.UnivariateFunction;import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;@@ -32,28 +34,65 @@ public class PredictEvaluator extends RecursiveObjectEvaluator implements TwoVal}@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)));+ 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)));}- if(null == second){- throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null found for the second value",toExpression(constructingFactory)));+ if (null == second) {+ throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - null found for the second value", toExpression(constructingFactory)));}- if(!(first instanceof RegressionEvaluator.RegressionTuple)){- throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the first value, expecting a RegressionTuple",toExpression(constructingFactory), first.getClass().getSimpleName()));- }- if(!(second instanceof Number) && !(second instanceof List<?>)){- throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the second value, expecting a Number",toExpression(constructingFactory), first.getClass().getSimpleName()));++ if (!(first instanceof VectorFunction) && !(first instanceof RegressionEvaluator.RegressionTuple) && !(first instanceof OLSRegressionEvaluator.MultipleRegressionTuple)) {+ throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - found type %s for the first value, expecting a RegressionTuple", toExpression(constructingFactory), first.getClass().getSimpleName()));}-- RegressionEvaluator.RegressionTuple regressedTuple = (RegressionEvaluator.RegressionTuple)first;- if(second instanceof Number){- return regressedTuple.predict(((Number)second).doubleValue());+ if (!(second instanceof Number) && !(second instanceof List<?>) && !(second instanceof Matrix)) {+ throw new IOException(String.format(Locale.ROOT, "Invalid expression %s - found type %s for the second value, expecting a Number, Array or Matrix", toExpression(constructingFactory), first.getClass().getSimpleName()));}- else{- return ((List<?>)second).stream().map(value -> regressedTuple.predict(((Number)value).doubleValue())).collect(Collectors.toList());++ if (first instanceof RegressionEvaluator.RegressionTuple) {++ RegressionEvaluator.RegressionTuple regressedTuple = (RegressionEvaluator.RegressionTuple) first;+ if (second instanceof Number) {+ return regressedTuple.predict(((Number) second).doubleValue());+ } else {+ return ((List<?>) second).stream().map(value -> regressedTuple.predict(((Number) value).doubleValue())).collect(Collectors.toList());+ }++ } else if (first instanceof OLSRegressionEvaluator.MultipleRegressionTuple) {++ OLSRegressionEvaluator.MultipleRegressionTuple regressedTuple = (OLSRegressionEvaluator.MultipleRegressionTuple) first;+ if (second instanceof List) {+ List<Number> list = (List<Number>) second;+ double[] predictors = new double[list.size()];++ for (int i = 0; i < list.size(); i++) {+ predictors[i] = list.get(i).doubleValue();+ }++ return regressedTuple.predict(predictors);+ } else if (second instanceof Matrix) {++ Matrix m = (Matrix) second;+ double[][] data = m.getData();+ List<Number> predictions = new ArrayList();+ for (double[] predictors : data) {+ predictions.add(regressedTuple.predict(predictors));+ }+ return predictions;+ }++ } else if (first instanceof VectorFunction) {+ VectorFunction vectorFunction = (VectorFunction) first;+ UnivariateFunction univariateFunction = (UnivariateFunction)vectorFunction.getFunction();+ if (second instanceof Number) {+ double x = ((Number)second).doubleValue();+ return univariateFunction.value(x);+ } else {+ return ((List<?>) second).stream().map(value -> univariateFunction.value(((Number) value).doubleValue())).collect(Collectors.toList());+ }}++ return null;}-}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RankEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RankEvaluator.javaindex 3b193c73787..9a7665fbbfe 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RankEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RankEvaluator.java@@ -48,7 +48,7 @@ public class RankEvaluator extends RecursiveNumericEvaluator implements OneValuereturn Arrays.stream(rank.rank(((List<?>)value).stream().mapToDouble(innerValue -> ((Number)innerValue).doubleValue()).toArray())).mapToObj(Double::new).collect(Collectors.toList());}else{- return doWork(Arrays.asList((BigDecimal)value));+ return doWork(Arrays.asList((Number)value));}}}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RecursiveEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RecursiveEvaluator.javaindex 6c5ae413143..b94c5dc58d5 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RecursiveEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RecursiveEvaluator.java@@ -55,6 +55,8 @@ public abstract class RecursiveEvaluator implements StreamEvaluator, ValueWorkerprotected Object normalizeInputType(Object value){if(null == value){return null;+ } else if (value instanceof VectorFunction) {+ return value;}else if(value instanceof Double){if(Double.isNaN((Double)value)){@@ -97,8 +99,9 @@ public abstract class RecursiveEvaluator implements StreamEvaluator, ValueWorkerprotected Object normalizeOutputType(Object value) {if(null == value){return null;- }- else if(value instanceof BigDecimal){+ } else if (value instanceof VectorFunction) {+ return value;+ } else if(value instanceof BigDecimal){BigDecimal bd = (BigDecimal)value;if(bd.signum() == 0 || bd.scale() <= 0 || bd.stripTrailingZeros().scale() <= 0){try{diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RecursiveNumericEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RecursiveNumericEvaluator.javaindex a1cdeb594ae..948f003be57 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RecursiveNumericEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RecursiveNumericEvaluator.java@@ -34,10 +34,11 @@ public abstract class RecursiveNumericEvaluator extends RecursiveEvaluator {}public Object normalizeInputType(Object value) throws StreamEvaluatorException {- if(null == value){+ if(null == value) {return null;- }- else if(value instanceof Double){+ } else if (value instanceof VectorFunction) {+ return value;+ } else if(value instanceof Double){if(Double.isNaN((Double)value)){return Double.NaN;}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RegressionEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RegressionEvaluator.javaindex b38b6b3492b..35627497c82 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RegressionEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RegressionEvaluator.java@@ -17,7 +17,6 @@package org.apache.solr.client.solrj.io.eval;import java.io.IOException;-import java.math.BigDecimal;import java.util.HashMap;import java.util.List;import java.util.Locale;@@ -50,8 +49,8 @@ public class RegressionEvaluator extends RecursiveNumericEvaluator implements Twthrow 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()));}- List<?> l1 = (List<?>)first;- List<?> l2 = (List<?>)second;+ List<Number> l1 = (List<Number>)first;+ List<Number> l2 = (List<Number>)second;if(l2.size() < l1.size()){throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - first list (%d) has more values than the second list (%d)",toExpression(constructingFactory), l1.size(), l2.size()));@@ -59,22 +58,22 @@ public class RegressionEvaluator extends RecursiveNumericEvaluator implements TwSimpleRegression regression = new SimpleRegression();for(int idx = 0; idx < l1.size(); ++idx){- regression.addData(((BigDecimal)l1.get(idx)).doubleValue(), ((BigDecimal)l2.get(idx)).doubleValue());+ regression.addData(l1.get(idx).doubleValue(), l2.get(idx).doubleValue());}- Map<String,Number> map = new HashMap<>();+ Map<String, Object> map = new HashMap<>();map.put("slope", regression.getSlope());map.put("intercept", regression.getIntercept());map.put("R", regression.getR());map.put("N", regression.getN());- map.put("RSquare", regression.getRSquare());+ map.put("RSquared", regression.getRSquare());map.put("regressionSumSquares", regression.getRegressionSumSquares());map.put("slopeConfidenceInterval", regression.getSlopeConfidenceInterval());map.put("interceptStdErr", regression.getInterceptStdErr());map.put("totalSumSquares", regression.getTotalSumSquares());map.put("significance", regression.getSignificance());map.put("meanSquareError", regression.getMeanSquareError());-+return new RegressionTuple(regression, map);}@@ -90,5 +89,4 @@ public class RegressionEvaluator extends RecursiveNumericEvaluator implements Twreturn this.simpleRegression.predict(value);}}-}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ResidualsEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ResidualsEvaluator.javadeleted file mode 100644index 8c5ca349ae7..00000000000--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/ResidualsEvaluator.java+++ /dev/null@@ -1,83 +0,0 @@-/*- * Licensed to the Apache Software Foundation (ASF) under one or more- * contributor license agreements. See the NOTICE file distributed with- * this work for additional information regarding copyright ownership.- * The ASF licenses this file to You under the Apache License, Version 2.0- * (the "License"); you may not use this file except in compliance with- * the License. You may obtain a copy of the License at- *- * http://www.apache.org/licenses/LICENSE-2.0- *- * Unless required by applicable law or agreed to in writing, software- * distributed under the License is distributed on an "AS IS" BASIS,- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.- * See the License for the specific language governing permissions and- * limitations under the License.- */-package org.apache.solr.client.solrj.io.eval;--import java.io.IOException;-import java.util.ArrayList;-import java.util.Arrays;-import java.util.List;-import java.util.Locale;--import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;-import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;--public class ResidualsEvaluator extends RecursiveObjectEvaluator implements ManyValueWorker {- protected static final long serialVersionUID = 1L;-- public ResidualsEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{- super(expression, factory);- }-- @Override- public Object doWork(Object ... values) throws IOException{- if(3 != values.length){- throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - three values expected but found %d",toExpression(constructingFactory), values.length));- }-- if(Arrays.stream(values).filter(value -> null == value).count() > 0){- throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - null value found",toExpression(constructingFactory)));- }-- if(!(values[0] instanceof RegressionEvaluator.RegressionTuple)){- throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the first value, expecting a RegressionTuple",toExpression(constructingFactory), values[0].getClass().getSimpleName()));- }- if(!(values[1] instanceof List<?>)){- throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the second value, expecting a list",toExpression(constructingFactory), values[1].getClass().getSimpleName()));- }- if(!(values[2] instanceof List<?>)){- throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - found type %s for the third value, expecting a list",toExpression(constructingFactory), values[2].getClass().getSimpleName()));- }- if(((List<?>)values[1]).stream().filter(value -> !(value instanceof Number)).count() > 0){- throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - expecting a list of numbers for the second value",toExpression(constructingFactory)));- }- if(((List<?>)values[2]).stream().filter(value -> !(value instanceof Number)).count() > 0){- throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - expecting a list of numbers for the third value",toExpression(constructingFactory)));- }-- RegressionEvaluator.RegressionTuple regressedTuple = (RegressionEvaluator.RegressionTuple)values[0];- List<?> l1 = (List<?>)values[1];- List<?> l2 = (List<?>)values[2];-- if(l2.size() < l1.size()){- throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - first list (%d) has more values than the second list (%d)",toExpression(constructingFactory), l1.size(), l2.size()));- }-- List<Number> residuals = new ArrayList<>();- for(int idx = 0; idx < l1.size(); ++idx){- double value1 = ((Number)l1.get(idx)).doubleValue();- double value2 = ((Number)l2.get(idx)).doubleValue();-- double prediction = regressedTuple.predict(value1);- double residual = value2 - prediction;-- residuals.add(residual);- }-- return residuals;- }--}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RoundEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RoundEvaluator.javaindex 0cfb3d40186..9f506e07d2a 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RoundEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/RoundEvaluator.java@@ -45,8 +45,7 @@ public class RoundEvaluator extends RecursiveNumericEvaluator implements OneValureturn ((List<?>)value).stream().map(innerValue -> doWork(innerValue)).collect(Collectors.toList());}else{- // we know it's a BigDecimal- return Math.round(((BigDecimal)value).doubleValue());+ return Math.round(((Number)value).doubleValue());}}}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SineEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SineEvaluator.javaindex 542610044b7..134d378d48e 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SineEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SineEvaluator.java@@ -45,8 +45,7 @@ public class SineEvaluator extends RecursiveNumericEvaluator implements OneValuereturn ((List<?>)value).stream().map(innerValue -> doWork(innerValue)).collect(Collectors.toList());}else{- // we know it's a BigDecimal- return Math.sin(((BigDecimal)value).doubleValue());+ return Math.sin(((Number)value).doubleValue());}}}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SplineEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SplineEvaluator.javanew file mode 100644index 00000000000..18b7fd11be1--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SplineEvaluator.java@@ -0,0 +1,78 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.solr.client.solrj.io.eval;++import java.io.IOException;+import java.math.BigDecimal;+import java.util.HashMap;+import java.util.List;+import java.util.ArrayList;+import java.util.Map;++import org.apache.commons.math3.analysis.UnivariateFunction;+import org.apache.commons.math3.analysis.interpolation.SplineInterpolator;+import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction;+import org.apache.commons.math3.stat.regression.SimpleRegression;+import org.apache.solr.client.solrj.io.Tuple;+import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;+import org.apache.solr.client.solrj.io.stream.expr.StreamExpressionNamedParameter;+import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;++public class SplineEvaluator extends RecursiveNumericEvaluator implements ManyValueWorker {+ protected static final long serialVersionUID = 1L;++ public SplineEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{+ super(expression, factory);+ }++ @Override+ public Object doWork(Object... objects) throws IOException{++ Object first = objects[0];++ double[] x = null;+ double[] y = null;++ if(objects.length == 1) {+ //Only the y values passed+ y = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();+ x = new double[y.length];+ for(int i=0; i<y.length; i++) {+ x[i] = i;+ }+ } else if(objects.length == 2) {+ Object second = objects[1];+ x = ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();+ y = ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray();+ }++ SplineInterpolator interpolator = new SplineInterpolator();+ PolynomialSplineFunction spline = interpolator.interpolate(x, y);++ List<Number> list = new ArrayList();+ for(double xvalue : x) {+ list.add(spline.value(xvalue));+ }++ VectorFunction vec = new VectorFunction(spline, list);+ vec.addToContext("x", x);+ vec.addToContext("y", y);++ return vec;+ }++}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SquareRootEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SquareRootEvaluator.javaindex e20109e8663..0e8e427717f 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SquareRootEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SquareRootEvaluator.java@@ -45,8 +45,7 @@ public class SquareRootEvaluator extends RecursiveNumericEvaluator implements Onreturn ((List<?>)value).stream().map(innerValue -> doWork(innerValue)).collect(Collectors.toList());}else{- // we know it's a BigDecimal- return Math.sqrt(((BigDecimal)value).doubleValue());+ return Math.sqrt(((Number)value).doubleValue());}}}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SumDifferenceEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SumDifferenceEvaluator.javaindex 74028881004..0b8a51ff3ab 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SumDifferenceEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/SumDifferenceEvaluator.java@@ -48,8 +48,8 @@ public class SumDifferenceEvaluator extends RecursiveNumericEvaluator implements}return StatUtils.sumDifference(- ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray(),- ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray()+ ((List) first).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray(),+ ((List) second).stream().mapToDouble(value -> ((Number) value).doubleValue()).toArray());}}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TangentEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TangentEvaluator.javaindex eb7e4dcb69f..7b951cd56c4 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TangentEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TangentEvaluator.java@@ -45,8 +45,7 @@ public class TangentEvaluator extends RecursiveNumericEvaluator implements OneVareturn ((List<?>)value).stream().map(innerValue -> doWork(innerValue)).collect(Collectors.toList());}else{- // we know it's a BigDecimal- return Math.tan(((BigDecimal)value).doubleValue());+ return Math.tan(((Number)value).doubleValue());}}}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TimeDifferencingEvaluator.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TimeDifferencingEvaluator.javaindex bd26b79ebd5..6424d0a780f 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TimeDifferencingEvaluator.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/TimeDifferencingEvaluator.java@@ -18,7 +18,6 @@package org.apache.solr.client.solrj.io.eval;import java.io.IOException;-import java.math.BigDecimal;import java.util.List;import java.util.Locale;import java.util.stream.Collectors;@@ -42,7 +41,7 @@ public class TimeDifferencingEvaluator extends RecursiveNumericEvaluator implemeif (!(1 == values.length || values.length == 2)){throw new IOException(String.format(Locale.ROOT,"%s(...) only works with 1 or 2 values but %d were provided", constructingFactory.getFunctionName(getClass()), values.length));}- List<BigDecimal> timeseriesValues = (List<BigDecimal> )values[0];+ List<Number> timeseriesValues = (List<Number> )values[0];Number lagValue = 1;if(1 == values.length) {diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/VectorFunction.java b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/VectorFunction.javanew file mode 100644index 00000000000..1a21b1dfedc--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/io/eval/VectorFunction.java@@ -0,0 +1,56 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.solr.client.solrj.io.eval;++import java.math.BigDecimal;+import java.util.HashMap;+import java.util.List;+import java.util.ArrayList;+import java.util.Map;++public class VectorFunction extends ArrayList {++ protected static final long serialVersionUID = 1L;++ private Object function;+ private Map context = new HashMap();++ public VectorFunction(Object function, double[] results) {+ this.function = function;+ for(double d : results) {+ add(d);+ }+ }++ public VectorFunction(Object function, List<Number> values) {+ this.function = function;+ addAll(values);+ }++ public Object getFunction() {+ return this.function;+ }++ public void addToContext(Object key, Object value) {+ this.context.put(key, value);+ }++ public Object getFromContext(Object key) {+ return this.context.get(key);+ }++}diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.javaindex c9fa396a195..4b368c1a52a 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java@@ -6791,7 +6791,7 @@ public class StreamExpressionTest extends SolrCloudTestCase {List<Map> listg = (List<Map>)tuples.get(0).get("g");Map mapg = listg.get(0);double pctg = (double) mapg.get("pct");- assertEquals(pctg, .2, .02 );+ assertEquals(pctg, .2, .02);List<Map> listh = (List<Map>)tuples.get(0).get("h");Map maph = listh.get(0);@@ -7114,34 +7114,11 @@ public class StreamExpressionTest extends SolrCloudTestCase {assertEquals(out.get(6).doubleValue(),21.0,0.01);}- @Test- public void testResiduals() throws Exception {- String cexpr = "let(a=array(1,2,3,4,5,6), b=array(2,4,6,8,10,12), c=regress(a,b), tuple(res=residuals(c,a,a)))";- ModifiableSolrParams paramsLoc = new ModifiableSolrParams();- paramsLoc.set("expr", cexpr);- paramsLoc.set("qt", "/stream");- String url = cluster.getJettySolrRunners().get(0).getBaseUrl().toString()+"/"+COLLECTIONORALIAS;- TupleStream solrStream = new SolrStream(url, paramsLoc);- StreamContext context = new StreamContext();- solrStream.setStreamContext(context);- List<Tuple> tuples = getTuples(solrStream);- assertTrue(tuples.size() == 1);- List<Number> out = (List<Number>)tuples.get(0).get("res");- assertTrue(out.size() == 6);- assertTrue(out.get(0).intValue() == -1);- assertTrue(out.get(1).intValue() == -2);- assertTrue(out.get(2).intValue() == -3);- assertTrue(out.get(3).intValue() == -4);- assertTrue(out.get(4).intValue() == -5);- assertTrue(out.get(5).intValue() == -6);- }-@Testpublic void testPolyfit() throws Exception {String cexpr = "let(echo=true," +" a=array(0,1,2,3,4,5,6,7)," +- " fit=polyfit(a, 1)," +- " deriv=polyfitDerivative(a, 1))";+ " fit=polyfit(a, 1))";ModifiableSolrParams paramsLoc = new ModifiableSolrParams();paramsLoc.set("expr", cexpr);paramsLoc.set("qt", "/stream");@@ -7161,17 +7138,6 @@ public class StreamExpressionTest extends SolrCloudTestCase {assertTrue(out.get(5).intValue() == 5);assertTrue(out.get(6).intValue() == 6);assertTrue(out.get(7).intValue() == 7);-- out = (List<Number>)tuples.get(0).get("deriv");- assertTrue(out.size() == 8);- assertTrue(out.get(0).intValue() == 1);- assertTrue(out.get(1).intValue() == 1);- assertTrue(out.get(2).intValue() == 1);- assertTrue(out.get(3).intValue() == 1);- assertTrue(out.get(4).intValue() == 1);- assertTrue(out.get(5).intValue() == 1);- assertTrue(out.get(6).intValue() == 1);- assertTrue(out.get(7).intValue() == 1);}@@ -7179,7 +7145,8 @@ public class StreamExpressionTest extends SolrCloudTestCase {public void testLoess() throws Exception {String cexpr = "let(echo=true," +" a=array(0,1,2,3,4,5,6,7)," +- " fit=loess(a))";+ " fit=loess(a), " ++ " der=derivative(fit))";ModifiableSolrParams paramsLoc = new ModifiableSolrParams();paramsLoc.set("expr", cexpr);@@ -7192,14 +7159,66 @@ public class StreamExpressionTest extends SolrCloudTestCase {assertTrue(tuples.size() == 1);List<Number> out = (List<Number>)tuples.get(0).get("fit");assertTrue(out.size() == 8);- assertTrue(out.get(0).intValue() == 0);- assertTrue(out.get(1).intValue() == 1);- assertTrue(out.get(2).intValue() == 2);- assertTrue(out.get(3).intValue() == 3);- assertTrue(out.get(4).intValue() == 4);- assertTrue(out.get(5).intValue() == 5);- assertTrue(out.get(6).intValue() == 6);- assertTrue(out.get(7).intValue() == 7);+ assertEquals(out.get(0).doubleValue(), 0.0, 0.0);+ assertEquals(out.get(1).doubleValue(), 1.0, 0.0);+ assertEquals(out.get(2).doubleValue(), 2.0, 0.0);+ assertEquals(out.get(3).doubleValue(), 3.0, 0.0);+ assertEquals(out.get(4).doubleValue(), 4.0, 0.0);+ assertEquals(out.get(5).doubleValue(), 5.0, 0.0);+ assertEquals(out.get(6).doubleValue(), 6.0, 0.0);+ assertEquals(out.get(7).doubleValue(), 7.0, 0.0);++ List<Number> out1 = (List<Number>)tuples.get(0).get("der");+ assertTrue(out1.size() == 8);+ assertEquals(out1.get(0).doubleValue(), 1.0, 0.0);+ assertEquals(out1.get(1).doubleValue(), 1.0, 0.0);+ assertEquals(out1.get(2).doubleValue(), 1.0, 0.0);+ assertEquals(out1.get(3).doubleValue(), 1.0, 0.0);+ assertEquals(out1.get(4).doubleValue(), 1.0, 0.0);+ assertEquals(out1.get(5).doubleValue(), 1.0, 0.0);+ assertEquals(out1.get(6).doubleValue(), 1.0, 0.0);+ assertEquals(out1.get(7).doubleValue(), 1.0, 0.0);++ }++ @Test+ public void testSpline() throws Exception {+ String cexpr = "let(echo=true," ++ " a=array(0,1,2,3,4,5,6,7), b=array(1,70,90,10,78, 100, 1, 9)," ++ " fit=spline(a, b), " ++ " der=derivative(fit))";++ ModifiableSolrParams paramsLoc = new ModifiableSolrParams();+ paramsLoc.set("expr", cexpr);+ paramsLoc.set("qt", "/stream");+ String url = cluster.getJettySolrRunners().get(0).getBaseUrl().toString()+"/"+COLLECTIONORALIAS;+ TupleStream solrStream = new SolrStream(url, paramsLoc);+ StreamContext context = new StreamContext();+ solrStream.setStreamContext(context);+ List<Tuple> tuples = getTuples(solrStream);+ assertTrue(tuples.size() == 1);+ List<Number> out = (List<Number>)tuples.get(0).get("fit");+ assertTrue(out.size() == 8);+ assertEquals(out.get(0).doubleValue(), 1.0, 0.0001);+ assertEquals(out.get(1).doubleValue(), 70.0, 0.0001);+ assertEquals(out.get(2).doubleValue(), 90.0, 0.0001);+ assertEquals(out.get(3).doubleValue(), 10.0, 0.0001);+ assertEquals(out.get(4).doubleValue(), 78.0, 0.0001);+ assertEquals(out.get(5).doubleValue(), 100.0, 0.0001);+ assertEquals(out.get(6).doubleValue(), 1.0, 0.0001);+ assertEquals(out.get(7).doubleValue(), 9.0, 0.0001);++ List<Number> out1 = (List<Number>)tuples.get(0).get("der");++ assertTrue(out1.size() == 8);+ assertEquals(out1.get(0).doubleValue(), 72.06870491240123, 0.0001);+ assertEquals(out1.get(1).doubleValue(), 62.86259017519753, 0.0001);+ assertEquals(out1.get(2).doubleValue(),-56.519065613191344, 0.0001);+ assertEquals(out1.get(3).doubleValue(), -16.786327722432148, 0.0001);+ assertEquals(out1.get(4).doubleValue(), 87.66437650291996, 0.0001);+ assertEquals(out1.get(5).doubleValue(), -63.87117828924769, 0.0001);+ assertEquals(out1.get(6).doubleValue(), -63.17966334592923, 0.0001);+ assertEquals(out1.get(7).doubleValue(), 43.58983167296462, 0.0001);}@@ -7241,6 +7260,50 @@ public class StreamExpressionTest extends SolrCloudTestCase {assertEquals((double) out.get("f-ratio"), 0.24169D, .0001);}+ @Test+ public void testOlsRegress() throws Exception {+ String cexpr = "let(echo=true, a=array(8.5, 12.89999962, 5.199999809, 10.69999981, 3.099999905, 3.5, 9.199999809, 9, 15.10000038, 10.19999981), " ++ "b=array(5.099999905, 5.800000191, 2.099999905, 8.399998665, 2.900000095, 1.200000048, 3.700000048, 7.599999905, 7.699999809, 4.5)," ++ "c=array(4.699999809, 8.800000191, 15.10000038, 12.19999981, 10.60000038, 3.5, 9.699999809, 5.900000095, 20.79999924, 7.900000095)," ++ "d=array(85.09999847, 106.3000031, 50.20000076, 130.6000061, 54.79999924, 30.29999924, 79.40000153, 91, 135.3999939, 89.30000305)," ++ "e=transpose(matrix(a, b, c))," ++ "f=olsRegress(e, d)," ++ "g=predict(f, e))";+ ModifiableSolrParams paramsLoc = new ModifiableSolrParams();+ paramsLoc.set("expr", cexpr);+ paramsLoc.set("qt", "/stream");+ String url = cluster.getJettySolrRunners().get(0).getBaseUrl().toString()+"/"+COLLECTIONORALIAS;+ TupleStream solrStream = new SolrStream(url, paramsLoc);+ StreamContext context = new StreamContext();+ solrStream.setStreamContext(context);+ List<Tuple> tuples = getTuples(solrStream);+ assertTrue(tuples.size() == 1);+ Map regression = (Map)tuples.get(0).get("f");++ Number rsquared = (Number)regression.get("RSquared");++ assertEquals(rsquared.doubleValue(), 0.9667887860584002, .000001);++ List<Number> regressionParameters = (List<Number>)regression.get("regressionParameters");++ assertEquals(regressionParameters.get(0).doubleValue(), 7.676028542255028, .0001);+ assertEquals(regressionParameters.get(1).doubleValue(), 3.661604009261836, .0001);+ assertEquals(regressionParameters.get(2).doubleValue(), 7.621051256504592, .0001);+ assertEquals(regressionParameters.get(3).doubleValue(), 0.8284680662898674, .0001);++ List<Number> predictions = (List<Number>)tuples.get(0).get("g");++ assertEquals(predictions.get(0).doubleValue(), 81.56082305847914, .0001);+ assertEquals(predictions.get(1).doubleValue(), 106.40333675525883, .0001);+ assertEquals(predictions.get(2).doubleValue(), 55.23044372150484, .0001);+ assertEquals(predictions.get(3).doubleValue(), 120.97932137751451, .0001);+ assertEquals(predictions.get(4).doubleValue(), 49.90981180846799, .0001);+ assertEquals(predictions.get(5).doubleValue(), 32.53654268030196, .0001);+ assertEquals(predictions.get(6).doubleValue(), 77.59681482774931, .0001);+ assertEquals(predictions.get(7).doubleValue(), 103.43841512086125, .0001);+ assertEquals(predictions.get(8).doubleValue(), 138.88047884217636, .0001);+ assertEquals(predictions.get(9).doubleValue(), 85.86401719768607, .0001);+ }@Testpublic void testPlot() throws Exception {@@ -7269,8 +7332,6 @@ public class StreamExpressionTest extends SolrCloudTestCase {assertTrue(pair3.get(1).intValue() == 3);}--@Testpublic void testMovingAverage() throws Exception {String cexpr = "movingAvg(array(1,2,3,4,5,6,7), 4)";@@ -7893,7 +7954,7 @@ public class StreamExpressionTest extends SolrCloudTestCase {Map regression = (Map)tuple.get("regress");double slope = (double)regression.get("slope");double intercept= (double) regression.get("intercept");- double rSquare= (double) regression.get("RSquare");+ double rSquare= (double) regression.get("RSquared");assertTrue(slope == 2.0D);assertTrue(intercept == 0.0D);assertTrue(rSquare == 1.0D);