SEARCH-11566: Add transpose Stream Evaluator to support transposing of matrices

This commit is contained in:
Joel Bernstein 2017-10-30 12:57:33 -04:00
parent 84fa17d42a
commit b7332f65b7
5 changed files with 91 additions and 0 deletions

View File

@ -269,6 +269,7 @@ public class StreamHandler extends RequestHandlerBase implements SolrCoreAware,
.withFunctionName("harmonicFit", HarmonicFitEvaluator.class)
.withFunctionName("loess", LoessEvaluator.class)
.withFunctionName("matrix", MatrixEvaluator.class)
.withFunctionName("transpose", TransposeEvaluator.class)
// Boolean Stream Evaluators

View File

@ -70,6 +70,8 @@ public class FieldValueEvaluator extends SourceEvaluator {
list.add(obj);
}
return list;
} else if(value instanceof Matrix) {
return value;
}
else if(value instanceof Iterable && !(value instanceof List<?>)){
Iterable<?> iter = (Iterable<?>)value;

View File

@ -29,6 +29,10 @@ public class Matrix implements Iterable {
this.data = data;
}
public double[][] getData() {
return this.data;
}
public Iterator iterator() {
return new MatrixIterator(data);
}

View File

@ -0,0 +1,52 @@
/*
* 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.linear.Array2DRowRealMatrix;
import org.apache.solr.client.solrj.io.stream.expr.StreamExpression;
import org.apache.solr.client.solrj.io.stream.expr.StreamFactory;
public class TransposeEvaluator extends RecursiveObjectEvaluator implements OneValueWorker {
protected static final long serialVersionUID = 1L;
public TransposeEvaluator(StreamExpression expression, StreamFactory factory) throws IOException{
super(expression, factory);
if(1 != containedEvaluators.size()){
throw new IOException(String.format(Locale.ROOT,"Invalid expression %s - expecting exactly 1 value but found %d",expression,containedEvaluators.size()));
}
}
@Override
public Object doWork(Object value) throws IOException{
if(null == value){
return null;
} else if(value instanceof Matrix) {
Matrix matrix = (Matrix) value;
double[][] data = matrix.getData();
Array2DRowRealMatrix amatrix = new Array2DRowRealMatrix(data);
Array2DRowRealMatrix tmatrix = (Array2DRowRealMatrix)amatrix.transpose();
return new Matrix(tmatrix.getData());
} else {
throw new IOException("matrix parameter expected for transpose function");
}
}
}

View File

@ -6065,6 +6065,38 @@ public class StreamExpressionTest extends SolrCloudTestCase {
assertEquals(array2.get(2).doubleValue(), 4.0, 0.0);
}
@Test
public void testTranspose() throws Exception {
String cexpr = "let(a=matrix(array(1,2,3), array(4,5,6)), b=transpose(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<List<Number>> out = (List<List<Number>>)tuples.get(0).get("b");
assertEquals(out.size(), 3);
List<Number> array1 = out.get(0);
assertEquals(array1.size(), 2);
assertEquals(array1.get(0).doubleValue(), 1.0, 0.0);
assertEquals(array1.get(1).doubleValue(), 4.0, 0.0);
List<Number> array2 = out.get(1);
assertEquals(array2.size(), 2);
assertEquals(array2.get(0).doubleValue(), 2.0, 0.0);
assertEquals(array2.get(1).doubleValue(), 5.0, 0.0);
List<Number> array3 = out.get(2);
assertEquals(array3.size(), 2);
assertEquals(array3.get(0).doubleValue(), 3.0, 0.0);
assertEquals(array3.get(1).doubleValue(), 6.0, 0.0);
}
@Test
public void testAddAll() throws Exception {
String cexpr = "addAll(array(1, 2, 3), array(4.5, 5.5, 6.5), array(7,8,9))";