aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/tensor/TensorMin.java
blob: e389dea388357d42c16b9cb026b8f853c9a4e440 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.tensor;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Takes the min of each cell of two tensors, see {@link Tensor#min}
 *
 * @author bratseth
 */
class TensorMin {

    private final Set<String> dimensions;
    private final Map<TensorAddress, Double> cells = new HashMap<>();

    public TensorMin(Tensor a, Tensor b) {
        dimensions = TensorOperations.combineDimensions(a, b);
        cells.putAll(a.cells());
        for (Map.Entry<TensorAddress, Double> bCell : b.cells().entrySet()) {
            Double aValue = a.cells().get(bCell.getKey());
            if (aValue == null)
                cells.put(bCell.getKey(), bCell.getValue());
            else
                cells.put(bCell.getKey(), Math.min(aValue, bCell.getValue()));
        }
    }

    /** Returns the result of taking this sum */
    public Tensor result() { return new MapTensor(dimensions, cells); }

}