aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/ranking/LinearNormalizer.java
blob: 2cdba9d63614de90b9ebbc9fdc4a6b8571d1e465 (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 Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.ranking;

class LinearNormalizer extends Normalizer {

    LinearNormalizer(int maxSize) {
        super(maxSize);
    }

    void normalize() {
        double min = Float.MAX_VALUE;
        double max = -Float.MAX_VALUE;
        for (int i = 0; i < size; i++) {
            double val = data[i];
            if (val < Float.MAX_VALUE && val > -Float.MAX_VALUE) {
                min = Math.min(min, data[i]);
                max = Math.max(max, data[i]);
            }
        }
        double scale = 0.0;
        double midpoint = 0.0;
        if (max > min) {
            scale = 1.0 / (max - min);
            midpoint = (min + max) * 0.5;
        }
        for (int i = 0; i < size; i++) {
            double old = data[i];
            data[i] = 0.5 + scale * (old - midpoint);
        }
    }

    String normalizing() { return "linear"; }
}