aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo
diff options
context:
space:
mode:
authorLester Solbakken <lesters@oath.com>2019-02-12 14:56:36 +0100
committerLester Solbakken <lesters@oath.com>2019-02-12 14:56:36 +0100
commit08bf643ca8de76265205e45878b060f30aa5d187 (patch)
tree159a292a3f62ae3d7ca330437a7d84bc17979746 /vespajlib/src/main/java/com/yahoo
parent6cd73b95dcdcf95a07a726aab88147c2aa19a029 (diff)
Implement tensor modify applyTo in Java
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo')
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/Tensor.java20
1 files changed, 20 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/Tensor.java b/vespajlib/src/main/java/com/yahoo/tensor/Tensor.java
index 58ae508ea7c..8002990e5c6 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/Tensor.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/Tensor.java
@@ -93,6 +93,26 @@ public interface Tensor {
*/
Tensor withType(TensorType type);
+ /**
+ * Returns a new tensor where existing cells in this tensor have been
+ * modified according to the given operation and cells in the given map.
+ * Cells in the map outside of existing cells are thus ignored.
+ *
+ * @param op the modifying function
+ * @param cells the cells to modify
+ * @return a new tensor with modified cells
+ */
+ default Tensor modify(DoubleBinaryOperator op, Map<TensorAddress, Double> cells) {
+ Tensor.Builder builder = Tensor.Builder.of(type());
+ for (Iterator<Cell> i = cellIterator(); i.hasNext(); ) {
+ Cell cell = i.next();
+ TensorAddress address = cell.getKey();
+ double value = cell.getValue();
+ builder.cell(address, cells.containsKey(address) ? op.applyAsDouble(value, cells.get(address)) : value);
+ }
+ return builder.build();
+ }
+
// ----------------- Primitive tensor functions
default Tensor map(DoubleUnaryOperator mapper) {