summaryrefslogtreecommitdiff
path: root/elki/src/main/java/de/lmu/ifi/dbs/elki/distance/distancefunction/set/HammingDistanceFunction.java
blob: 4d467db99110b457cf94674db6907d3bd6df17fc (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package de.lmu.ifi.dbs.elki.distance.distancefunction.set;

/*
 This file is part of ELKI:
 Environment for Developing KDD-Applications Supported by Index-Structures

 Copyright (C) 2015
 Ludwig-Maximilians-Universität München
 Lehr- und Forschungseinheit für Datenbanksysteme
 ELKI Development Team

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import de.lmu.ifi.dbs.elki.data.BitVector;
import de.lmu.ifi.dbs.elki.data.FeatureVector;
import de.lmu.ifi.dbs.elki.data.NumberVector;
import de.lmu.ifi.dbs.elki.data.type.SimpleTypeInformation;
import de.lmu.ifi.dbs.elki.data.type.TypeUtil;
import de.lmu.ifi.dbs.elki.distance.distancefunction.NumberVectorDistanceFunction;
import de.lmu.ifi.dbs.elki.utilities.documentation.Reference;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.AbstractParameterizer;

/**
 * Computes the Hamming distance of arbitrary vectors - i.e. counting, on how
 * many places they differ.
 * 
 * Reference:
 * <p>
 * R. W. Hamming<br />
 * Error detecting and error correcting codes<br />
 * Bell System technical journal, 29(2)
 * </p>
 * 
 * TODO: add a sparse (but not binary) optimized version?
 * 
 * @author Erich Schubert
 */
@Reference(authors = "R. W. Hamming", //
title = "Error detecting and error correcting codes", //
booktitle = "Bell System technical journal, 29(2)", //
url = "http://dx.doi.org/10.1002/j.1538-7305.1950.tb00463.x")
public class HammingDistanceFunction extends AbstractSetDistanceFunction<FeatureVector<?>> implements NumberVectorDistanceFunction<FeatureVector<?>> {
  /**
   * Static instance.
   */
  public static final HammingDistanceFunction STATIC = new HammingDistanceFunction();

  @Override
  public boolean isMetric() {
    return true;
  }

  @Override
  public double distance(FeatureVector<?> o1, FeatureVector<?> o2) {
    if(o1 instanceof BitVector && o2 instanceof BitVector) {
      return ((BitVector) o1).hammingDistance((BitVector) o2);
    }
    if(o1 instanceof NumberVector && o2 instanceof NumberVector) {
      return hammingDistanceNumberVector((NumberVector) o1, (NumberVector) o2);
    }
    final int d1 = o1.getDimensionality(), d2 = o2.getDimensionality();
    int differences = 0;
    int d = 0;
    for(; d < d1 && d < d2; d++) {
      Object v1 = o1.getValue(d), v2 = o2.getValue(d);
      final boolean n1 = isNull(v1), n2 = isNull(v2);
      if(n1 && n2) {
        continue;
      }
      if(v1 instanceof Double && Double.isNaN((Double) v1)) {
        continue;
      }
      if(v2 instanceof Double && Double.isNaN((Double) v2)) {
        continue;
      }
      // One must be set.
      if(n1 || n2 || !v1.equals(v2)) {
        ++differences;
      }
    }
    for(; d < d1; d++) {
      Object v1 = o1.getValue(d);
      if(!isNull(v1)) {
        if(v1 instanceof Double && Double.isNaN((Double) v1)) {
          continue;
        }
        ++differences;
      }
    }
    for(; d < d2; d++) {
      Object v2 = o2.getValue(d);
      if(!isNull(v2)) {
        if(v2 instanceof Double && Double.isNaN((Double) v2)) {
          continue;
        }
        ++differences;
      }
    }
    return differences;
  }

  @Override
  public double distance(NumberVector o1, NumberVector o2) {
    if(o1 instanceof BitVector && o2 instanceof BitVector) {
      return ((BitVector) o1).hammingDistance((BitVector) o2);
    }
    return hammingDistanceNumberVector(o1, o2);
  }

  /**
   * Version for number vectors.
   * 
   * @param o1 First vector
   * @param o2 Second vector
   * @return hamming distance
   */
  private double hammingDistanceNumberVector(NumberVector o1, NumberVector o2) {
    final int d1 = o1.getDimensionality(), d2 = o2.getDimensionality();
    int differences = 0;
    int d = 0;
    for(; d < d1 && d < d2; d++) {
      double v1 = o1.doubleValue(d), v2 = o2.doubleValue(d);
      if(v1 != v1 || v2 != v2) { /* NaN */
        continue;
      }
      if(v1 != v2) {
        ++differences;
      }
    }
    for(; d < d1; d++) {
      double v1 = o1.doubleValue(d);
      if(v1 != 0. && v1 == v1 /* not NaN */) {
        ++differences;
      }
    }
    for(; d < d2; d++) {
      double v2 = o2.doubleValue(d);
      if(v2 != 0. && v2 == v2 /* not NaN */) {
        ++differences;
      }
    }
    return differences;
  }

  @Override
  public SimpleTypeInformation<? super FeatureVector<?>> getInputTypeRestriction() {
    return TypeUtil.FEATURE_VECTORS;
  }

  /**
   * Parameterization class.
   * 
   * @author Erich Schubert
   * 
   * @apiviz.exclude
   */
  public static class Parameterizer extends AbstractParameterizer {
    @Override
    protected HammingDistanceFunction makeInstance() {
      return STATIC;
    }
  }
}