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

Function test_normalize

dlib/test/sparse_vector.cpp:143–174  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

141// ----------------------------------------------------------------------------------------
142
143 void test_normalize()
144 {
145 using sparse_type = std::vector<std::pair<unsigned long,double>>;
146 const sparse_type a = {{0,3}, {2,4}};
147 const sparse_type n = normalize(a);
148 DLIB_TEST(n.size() == 2);
149 DLIB_TEST(n[0].first == 0 && std::abs(n[0].second - 0.6) < 1e-15);
150 DLIB_TEST(n[1].first == 2 && std::abs(n[1].second - 0.8) < 1e-15);
151 DLIB_TEST(a[0].second == 3);
152
153 sparse_type b = a;
154 normalize_inplace(b);
155 DLIB_TEST(b == n);
156
157 std::map<unsigned long,double> m = {{1,3}, {5,4}};
158 normalize_inplace(m);
159 DLIB_TEST(std::abs(m[1] - 0.6) < 1e-15 && std::abs(m[5] - 0.8) < 1e-15);
160
161 // A zero vector is returned as it is.
162 const sparse_type zero = {{3,0}};
163 DLIB_TEST(normalize(zero) == zero);
164 DLIB_TEST(normalize(sparse_type()).empty());
165
166 // Matrices still go to the dense normalize().
167 matrix<double,2,1> d = {3, 4};
168 DLIB_TEST(max(abs(normalize(d) - matrix<double,2,1>{0.6, 0.8})) < 1e-15);
169 normalize_inplace(d);
170 DLIB_TEST(max(abs(d - matrix<double,2,1>{0.6, 0.8})) < 1e-15);
171 matrix<double,0,1> z = zeros_matrix<double>(3,1);
172 normalize_inplace(z);
173 DLIB_TEST(z == zeros_matrix<double>(3,1));
174 }
175
176// ----------------------------------------------------------------------------------------
177

Callers 1

perform_testMethod · 0.85

Calls 6

absFunction · 0.85
normalize_inplaceFunction · 0.85
normalizeFunction · 0.50
maxFunction · 0.50
sizeMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected