Direct Graphical Models  v.1.7.0
mathop.h
1 // Mathematical Operations
2 // Written by Sergey G. Kosov in 2017 for Project X
3 #pragma once
4 
5 #include "types.h"
6 #include "macroses.h"
7 
8 namespace DirectGraphicalModels
9 {
10  // ================================ Random Namespace ==============================
16  namespace mathop {
25  template <typename T>
26  inline bool isEqual(const Mat &a, const Mat &b)
27  {
28  // Assertions
29  DGM_ASSERT_MSG(a.size() == b.size(), "Size mismatch");
30  DGM_ASSERT_MSG(a.type() == b.type(), "Type mismatch");
31 
32  if (a.cols == 1) {
33  for (int j = 0; j < a.rows; j++)
34  if (a.at<T>(j, 0) != b.at<T>(j, 0)) return false;
35  }
36  else if (a.rows == 1) {
37  for (int i = 0; i < a.cols; i++) {
38  if (a.at<T>(0, i) != b.at<T>(0, i)) return false;
39  }
40  }
41  else {
42  for (int j = 0; j < a.rows; j++) {
43  const T *pa = a.ptr<T>(j);
44  const T *pb = b.ptr<T>(j);
45  for (int i = 0; i < a.cols; i++)
46  if(pa[i] != pb[i]) return false;
47  }
48  }
49  return true;
50  }
61  template <typename Targ = float, typename Tres = float>
62  inline Tres Euclidian(const Mat &a, const Mat &b)
63  {
64  // Assertions
65  DGM_ASSERT_MSG(a.size() == b.size(), "Size mismatch");
66  DGM_ASSERT_MSG(a.type() == b.type(), "Type mismatch");
67 
68  Tres res = 0;
69  if (a.cols == 1) {
70  for (int j = 0; j < a.rows; j++)
71  res += (static_cast<Tres>(a.at<Targ>(j, 0)) - static_cast<Tres>(b.at<Targ>(j, 0))) * (static_cast<Tres>(a.at<Targ>(j, 0)) - static_cast<Tres>(b.at<Targ>(j, 0)));
72  }
73  else if (a.rows == 1) {
74  for (int i = 0; i < a.cols; i++) {
75  res += (static_cast<Tres>(a.at<Targ>(0, i)) - static_cast<Tres>(b.at<Targ>(0, i))) * (static_cast<Tres>(a.at<Targ>(0, i)) - static_cast<Tres>(b.at<Targ>(0, i)));
76  }
77  }
78  else {
79  for (int j = 0; j < a.rows; j++) {
80  const Targ *pa = a.ptr<Targ>(j);
81  const Targ *pb = b.ptr<Targ>(j);
82  for (int i = 0; i < a.cols; i++)
83  res += (static_cast<Tres>(pa[i]) - static_cast<Tres>(pb[i])) * (static_cast<Tres>(pa[i]) - static_cast<Tres>(pb[i]));
84  }
85  }
86  res = sqrt(res);
87  return res;
88  }
89 
90  template <typename T>
91  inline T Euclidian(const std::vector<T> &a, const std::vector<T> &b)
92  {
93  T sum = 0;
94  for (size_t i = 0; i < a.size(); i++)
95  sum += (a[i] - b[i]) * (a[i] - b[i]);
96  return sqrt(sum);
97  }
98 
105  template<typename T>
106  bool ifOverlap(pair_mat_t &box1, pair_mat_t &box2)
107  {
108  for (int x = 0; x < box1.first.cols; x++) {
109  if (box1.first.at<T>(0, x) > box2.second.at<T>(0, x)) return false;
110  if (box1.second.at<T>(0, x) < box2.first.at<T>(0, x)) return false;
111  }
112  return true;
113  }
114  }
115 }
Tres Euclidian(const Mat &a, const Mat &b)
Calculates the Euclidian distance between argument matrices a and b.
Definition: mathop.h:62
bool isEqual(const Mat &a, const Mat &b)
Compares two argument matrices a and b.
Definition: mathop.h:26
bool ifOverlap(pair_mat_t &box1, pair_mat_t &box2)
Checks whether two regions box1 and box2 overlap each other.
Definition: mathop.h:106