use of com.yahoo.tensor.IndexedTensor in project vespa by vespa-engine.
the class Join method indexedGeneralJoin.
private Tensor indexedGeneralJoin(IndexedTensor a, IndexedTensor b, TensorType joinedType) {
DimensionSizes joinedSize = joinedSize(joinedType, a, b);
Tensor.Builder builder = Tensor.Builder.of(joinedType, joinedSize);
int[] aToIndexes = mapIndexes(a.type(), joinedType);
int[] bToIndexes = mapIndexes(b.type(), joinedType);
joinTo(a, b, joinedType, joinedSize, aToIndexes, bToIndexes, false, builder);
// joinTo(b, a, joinedType, joinedSize, bToIndexes, aToIndexes, true, builder);
return builder.build();
}
use of com.yahoo.tensor.IndexedTensor in project vespa by vespa-engine.
the class Reduce method evaluate.
@Override
public <NAMETYPE extends TypeContext.Name> Tensor evaluate(EvaluationContext<NAMETYPE> context) {
Tensor argument = this.argument.evaluate(context);
if (!dimensions.isEmpty() && !argument.type().dimensionNames().containsAll(dimensions))
throw new IllegalArgumentException("Cannot reduce " + argument + " over dimensions " + dimensions + ": Not all those dimensions are present in this tensor");
// Special case: Reduce all
if (dimensions.isEmpty() || dimensions.size() == argument.type().dimensions().size())
if (argument.type().dimensions().size() == 1 && argument instanceof IndexedTensor)
return reduceIndexedVector((IndexedTensor) argument);
else
return reduceAllGeneral(argument);
TensorType reducedType = type(argument.type());
// Reduce cells
Map<TensorAddress, ValueAggregator> aggregatingCells = new HashMap<>();
for (Iterator<Tensor.Cell> i = argument.cellIterator(); i.hasNext(); ) {
Map.Entry<TensorAddress, Double> cell = i.next();
TensorAddress reducedAddress = reduceDimensions(cell.getKey(), argument.type(), reducedType);
aggregatingCells.putIfAbsent(reducedAddress, ValueAggregator.ofType(aggregator));
aggregatingCells.get(reducedAddress).aggregate(cell.getValue());
}
Tensor.Builder reducedBuilder = Tensor.Builder.of(reducedType);
for (Map.Entry<TensorAddress, ValueAggregator> aggregatingCell : aggregatingCells.entrySet()) reducedBuilder.cell(aggregatingCell.getKey(), aggregatingCell.getValue().aggregatedValue());
return reducedBuilder.build();
}
use of com.yahoo.tensor.IndexedTensor in project vespa by vespa-engine.
the class TypedBinaryFormat method encode.
public static byte[] encode(Tensor tensor) {
GrowableByteBuffer buffer = new GrowableByteBuffer();
if (tensor instanceof MixedTensor) {
buffer.putInt1_4Bytes(MIXED_BINARY_FORMAT_TYPE);
new MixedBinaryFormat().encode(buffer, tensor);
} else if (tensor instanceof IndexedTensor) {
buffer.putInt1_4Bytes(DENSE_BINARY_FORMAT_TYPE);
new DenseBinaryFormat().encode(buffer, tensor);
} else {
buffer.putInt1_4Bytes(SPARSE_BINARY_FORMAT_TYPE);
new SparseBinaryFormat().encode(buffer, tensor);
}
buffer.flip();
byte[] result = new byte[buffer.remaining()];
buffer.get(result);
return result;
}
use of com.yahoo.tensor.IndexedTensor in project vespa by vespa-engine.
the class JsonReaderTestCase method testParsingOfIndexedTensorWithCells.
@Test
public void testParsingOfIndexedTensorWithCells() {
Tensor tensor = assertTensorField("{{x:0,y:0}:2.0,{x:1,y:0}:3.0}}", createPutWithTensor("{ " + " \"cells\": [ " + " { \"address\": { \"x\": \"0\", \"y\": \"0\" }, " + " \"value\": 2.0 }, " + " { \"address\": { \"x\": \"1\", \"y\": \"0\" }, " + " \"value\": 3.0 } " + " ]" + "}", "indexedtensorfield"), "indexedtensorfield");
// this matters for performance
assertTrue(tensor instanceof IndexedTensor);
}
Aggregations