Loading...
Searching...
No Matches
datapoint.C
Go to the documentation of this file.
1/*
2 * This file is part of the SPLINTER library.
3 * Copyright (C) 2012 Bjarne Grimstad (bjarne.grimstad@gmail.com).
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8*/
9
10#include "datapoint.h"
11
12namespace SPLINTER
13{
14
15DataPoint::DataPoint()
16{
17}
18
19DataPoint::DataPoint(double x, double y)
20{
21 setData(std::vector<double>(1, x), y);
22}
23
24DataPoint::DataPoint(std::vector<double> x, double y)
25{
26 setData(x, y);
27}
28
29DataPoint::DataPoint(DenseVector x, double y)
30{
31 std::vector<double> newX;
32
33 for (int i = 0; i < x.size(); i++)
34 {
35 newX.push_back(x(i));
36 }
37
38 setData(newX, y);
39}
40
41void DataPoint::setData(const std::vector<double>& x, double y)
42{
43 this->x = x;
44 this->y = y;
45}
46
47bool DataPoint::operator<(const DataPoint& rhs) const
48{
49 if (this->getDimX() != rhs.getDimX())
50 {
51 throw Exception("DataPoint::operator<: Cannot compare data points of different dimensions");
52 }
53
54 for (unsigned int i = 0; i < this->getDimX(); i++)
55 {
56 if (x.at(i) < rhs.getX().at(i))
57 {
58 return true;
59 }
60 else if (x.at(i) > rhs.getX().at(i))
61 {
62 return false;
63 }
64 }
65
66 return false;
67}
68
69/*
70* Computes Euclidean distance ||x-y||
71*/
72double dist(const std::vector<double> x, const std::vector<double> y)
73{
74 if (x.size() != y.size())
75 {
76 throw Exception("DataPoint::dist: Cannot measure distance between two points of different dimension");
77 }
78
79 double sum = 0.0;
80
81 for (unsigned int i = 0; i < x.size(); i++)
82 {
83 sum += (x.at(i) - y.at(i)) * (x.at(i) - y.at(i));
84 }
85
86 return std::sqrt(sum);
87}
88
89/*
90* Computes Euclidean distance ||x-y||
91*/
92double dist(const DataPoint x, const DataPoint y)
93{
94 return dist(x.getX(), y.getX());
95}
96
97bool dist_sort(const DataPoint x, const DataPoint y)
98{
99 std::vector<double> zeros(x.getDimX(), 0);
100 DataPoint origin(zeros, 0.0);
101 double x_dist = dist(x, origin);
102 double y_dist = dist(y, origin);
103 return (x_dist < y_dist);
104}
105
106} // namespace SPLINTER
bool dist_sort(const DataPoint x, const DataPoint y)
Definition datapoint.C:97
double dist(const std::vector< double > x, const std::vector< double > y)
Definition datapoint.C:72
label i
Definition pEqn.H:46