[my-place] Refactoring: Tizen C++ convention - File names chenge.
[platform/core/context/context-provider.git] / src / my-place / visit-categer / Mahal.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "Mahal.h"
18 #include <math.h>
19 #include <Types.h>
20
21 ctx::num_t ctx::MahalModel::__distance(const std::vector<num_t> &v1, const std::vector<num_t> &v2, const std::vector<num_t> &m)
22 {
23         size_t n = v1.size();
24         if (m.size() != n * n) {
25                 _E("m.size() != n * n");
26                 return 0.0; // this value does not make any sense
27         }
28
29         std::vector<num_t> diff(n);
30         for (size_t i = 0; i < n; i++) {
31                 diff[i] = v2[i] - v1[i];
32         }
33
34         num_t dist2 = 0; // squared distance
35         for (size_t j = 0; j < n; j++) {
36                 for (size_t i = 0; i < n; i++) {
37                         dist2 += m[i * n + j] * diff[i] * diff[j];
38                 }
39         }
40         return sqrt(dist2);
41 }
42
43 ctx::num_t ctx::MahalModel::distance(const std::vector<ctx::num_t> &v)
44 {
45         return __distance(v, __mean, __sigma);
46 }