aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/tensor/TensorDifference.java
blob: ceb003b161553d102f6418091405b6f196281ec4 (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
// 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 difference between two tensors, see {@link Tensor#subtract}
 *
 * @author bratseth
 */
class TensorDifference {

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

    public TensorDifference(Tensor a, Tensor b) {
        this.dimensions = TensorOperations.combineDimensions(a, b);
        cells.putAll(a.cells());
        for (Map.Entry<TensorAddress, Double> bCell : b.cells().entrySet())
            cells.put(bCell.getKey(), a.cells().getOrDefault(bCell.getKey(), 0d) - bCell.getValue());
    }

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

}