K-Nearest Neighbor Graph Testing Library
C++ Python library that is able to import exisiting NN-structures; Implements Property Testing Algorithm that rejects with high probability if queries to given structure are epsilon-far from giving a K-Nearest Neighbor Graph
d_dim_tuple.hpp
1 /*
2 Copyright 2018 Dennis Rohde
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 
6 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 
8 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9 */
10 
11 #pragma once
12 
13 #include <iostream>
14 #include <limits>
15 #include <sstream>
16 #include <cmath>
17 
18 #include <boost/python/numpy.hpp>
19 
20 namespace np = boost::python::numpy;
21 namespace p = boost::python;
22 
23 template <typename T = double>
24 class Tuple : public std::vector<T> {
25  typedef unsigned long index_type;
26 
27 public:
28  typedef T value_type;
29  typedef std::vector<value_type> tuple_type;
30 
31  Tuple() : tuple_type{} {}
32  Tuple(const index_type d) : tuple_type(d) {}
33  Tuple(const T &in, const index_type d) : tuple_type(d, in) {}
34  Tuple(const tuple_type &in, const index_type d) : tuple_type{} {
35  if (in.size() < d) {
36  throw std::invalid_argument("Could not build d-tuple from vector of dimension "+std::to_string(in.size()));
37  }
38  this->assign(in.begin(), in.begin() + d);
39  }
40 
41  Tuple(const np::ndarray &in) : tuple_type{} {
42  auto dimensions = in.get_nd();
43  if (dimensions != 1 or in.get_dtype() != np::dtype::get_builtin<T>()) {
44  int status;
45  const std::type_info &ti = typeid(T);
46  std::cerr << "Need 1-dimensional numpy array of type " << abi::__cxa_demangle(ti.name(), 0, 0, &status) << " !" << std::endl;
47  return;
48  }
49  auto size = in.get_shape();
50  auto strides = in.get_strides();
51  auto data = in.get_data();
52  for (index_type i = 0; i < size[0]; ++i) {
53  (*this)[i] = *reinterpret_cast<const T*>(data + i);
54  }
55  }
56 
57  inline auto get(const index_type i) const {
58  if (i >= this->size()) return std::numeric_limits<T>::signaling_NaN();
59  return this->operator[](i);
60  }
61 
62  inline auto dimension() const {
63  return this->size();
64  }
65 
66  inline auto operator-(const Tuple<T> &o) const {
67  Tuple<T> result{*this, this->size()};
68  if (this->size() != o.size()) {
69  result = Tuple<T>(std::numeric_limits<T>::signaling_NaN(), this->size());
70  return result;
71  }
72  for (index_type i = 0; i < this->size(); ++i) {
73  result[i] -= o.get(i);
74  }
75  return result;
76  }
77 
78  inline auto operator*(const Tuple<T> &o) const {
79  if (this->size() != o.size()) return std::numeric_limits<T>::signaling_NaN();
80  T sum = 0;
81  for (index_type i = 0; i < this->size(); ++i) {
82  sum += get(i) * o.get(i);
83  }
84  return sum;
85  }
86 
87  inline auto operator==(const Tuple<T> &o) const {
88  if (this->size() != o.size()) {
89  std::cerr << "Can not compare tuples of different dimension!" << std::endl;
90  return false;
91  }
92  for (index_type i = 0; i < this->size(); ++i) {
93  if (std::fabs(get(i) - o.get(i)) < std::numeric_limits<T>::epsilon()) continue;
94  else return false;
95  }
96  return true;
97  }
98 
99  inline bool operator<(const Tuple<T> &o) const {
100  if (this->size() != o.size()) {
101  std::cerr << "Can not compare tuples of different dimension!" << std::endl;
102  return false;
103  }
104  for (index_type i = 0; i < this->size(); ++i) {
105  if (not (std::fabs(get(i) - o.get(i)) < std::numeric_limits<T>::epsilon())) {
106  if (o.get(i) - get(i) >= std::numeric_limits<T>::epsilon()) {
107  return true;
108  } else {
109  return false;
110  }
111  }
112  }
113  return false;
114  }
115 
116  inline auto as_str() const {
117  std::stringstream ss;
118  ss << *this;
119  return ss.str();
120  }
121 
122  inline auto repr() const {
123  std::stringstream ss;
124  ss << "Tuple of Dimension " << dimension() << ":" << std::endl;
125  ss << as_str();
126  return ss.str();
127  }
128 
129  inline auto as_ndarray() const {
130  np::dtype dt = np::dtype::get_builtin<T>();
131  p::list l;
132  np::ndarray result = np::array(l, dt);
133  for (const auto &elem: *this) {
134  l.append(elem);
135  }
136  result = np::array(l, dt);
137  return result;
138  }
139 
140  inline auto pbegin() const {
141  return this->begin();
142  }
143 
144  inline auto pend() const {
145  return this->end();
146  }
147 };
148 
149 template <typename T>
150 std::ostream& operator<<(std::ostream &out, const Tuple<T> &t) {
151  const auto d = t.size();
152  out << "(";
153  for (auto i = d-d; i < d-1; ++i) {
154  out << t.get(i);
155  out << ", ";
156  }
157  out << t.get(d-1) << ")";
158  return out;
159 }
Definition: d_dim_tuple.hpp:24