blob: e42d25197e2363f70eba13fdd004c4d044acddad (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.tensor.functions;
import com.google.common.annotations.Beta;
import com.yahoo.tensor.DimensionSizes;
import com.yahoo.tensor.IndexedTensor;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;
import com.yahoo.tensor.evaluation.EvaluationContext;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
/**
* An indexed tensor whose values are generated by a function
*
* @author bratseth
*/
@Beta
public class Generate extends PrimitiveTensorFunction {
private final TensorType type;
private final Function<List<Integer>, Double> generator;
/**
* Creates a generated tensor
*
* @param type the type of the tensor
* @param generator the function generating values from a list of ints specifying the indexes of the
* tensor cell which will receive the value
* @throws IllegalArgumentException if any of the tensor dimensions are not indexed bound
*/
public Generate(TensorType type, Function<List<Integer>, Double> generator) {
Objects.requireNonNull(type, "The argument tensor type cannot be null");
Objects.requireNonNull(generator, "The argument function cannot be null");
validateType(type);
this.type = type;
this.generator = generator;
}
private void validateType(TensorType type) {
for (TensorType.Dimension dimension : type.dimensions())
if (dimension.type() != TensorType.Dimension.Type.indexedBound)
throw new IllegalArgumentException("A generated tensor can only have indexed bound dimensions");
}
@Override
public List<TensorFunction> functionArguments() { return Collections.emptyList(); }
@Override
public TensorFunction replaceArguments(List<TensorFunction> arguments) {
if ( arguments.size() != 0)
throw new IllegalArgumentException("Generate must have 0 arguments, got " + arguments.size());
return this;
}
@Override
public PrimitiveTensorFunction toPrimitive() { return this; }
@Override
public Tensor evaluate(EvaluationContext context) {
Tensor.Builder builder = Tensor.Builder.of(type);
IndexedTensor.Indexes indexes = IndexedTensor.Indexes.of(dimensionSizes(type));
for (int i = 0; i < indexes.size(); i++) {
indexes.next();
builder.cell(generator.apply(indexes.toList()), indexes.indexesForReading());
}
return builder.build();
}
private DimensionSizes dimensionSizes(TensorType type) {
DimensionSizes.Builder b = new DimensionSizes.Builder(type.dimensions().size());
for (int i = 0; i < b.dimensions(); i++)
b.set(i, type.dimensions().get(i).size().get());
return b.build();
}
@Override
public String toString(ToStringContext context) { return type + "(" + generator + ")"; }
}
|