MCPcopy Create free account
hub / github.com/davisking/dlib / distance_squared

Function distance_squared

dlib/svm/sparse_vector.h:22–70  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

20
21 template <typename T, typename U>
22 typename T::value_type::second_type distance_squared (
23 const T& a,
24 const U& b
25 )
26 {
27 typedef typename T::value_type::second_type scalar_type;
28 typedef typename U::value_type::second_type scalar_typeU;
29 // Both T and U must contain the same kinds of elements
30 COMPILE_TIME_ASSERT((is_same_type<scalar_type, scalar_typeU>::value));
31
32 typename T::const_iterator ai = a.begin();
33 typename U::const_iterator bi = b.begin();
34
35 scalar_type sum = 0, temp = 0;
36 while (ai != a.end() && bi != b.end())
37 {
38 if (ai->first == bi->first)
39 {
40 temp = ai->second - bi->second;
41 ++ai;
42 ++bi;
43 }
44 else if (ai->first < bi->first)
45 {
46 temp = ai->second;
47 ++ai;
48 }
49 else
50 {
51 temp = bi->second;
52 ++bi;
53 }
54
55 sum += temp*temp;
56 }
57
58 while (ai != a.end())
59 {
60 sum += ai->second*ai->second;
61 ++ai;
62 }
63 while (bi != b.end())
64 {
65 sum += bi->second*bi->second;
66 ++bi;
67 }
68
69 return sum;
70 }
71
72// ------------------------------------------------------------------------------------
73

Callers 3

operator()Method · 0.85
operator()Method · 0.85
distanceFunction · 0.85

Calls 2

beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected