SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
BallTree.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2014 Parijat Mazumdar
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are those
27  * of the authors and should not be interpreted as representing official policies,
28  * either expressed or implied, of the Shogun Development Team.
29  */
30 
32 
33 using namespace shogun;
34 
35 CBallTree::CBallTree(int32_t leaf_size, EDistanceType d)
36 : CNbodyTree(leaf_size,d)
37 {
38 }
39 
40 float64_t CBallTree::min_dist(bnode_t* node,float64_t* feat, int32_t dim)
41 {
42  float64_t dist=0;
43  SGVector<float64_t> center=node->data.center;
44  for (int32_t i=0;i<dim;i++)
45  dist+=add_dim_dist(center[i]-feat[i]);
46 
47  dist=actual_dists(dist);
48  return CMath::max(0.0,dist-node->data.radius);
49 }
50 
51 float64_t CBallTree::min_dist_dual(bnode_t* nodeq, bnode_t* noder)
52 {
53  float64_t dist=0;
54  SGVector<float64_t> center1=nodeq->data.center;
55  SGVector<float64_t> center2=noder->data.center;
56  for (int32_t i=0;i<center1.vlen;i++)
57  dist+=add_dim_dist(center1[i]-center2[i]);
58 
59  dist=actual_dists(dist);
60  return CMath::max(0.0,dist-nodeq->data.radius-noder->data.radius);
61 }
62 
63 float64_t CBallTree::max_dist_dual(bnode_t* nodeq, bnode_t* noder)
64 {
65  float64_t dist=0;
66  SGVector<float64_t> center1=nodeq->data.center;
67  SGVector<float64_t> center2=noder->data.center;
68  for (int32_t i=0;i<center1.vlen;i++)
69  dist+=add_dim_dist(center1[i]-center2[i]);
70 
71  dist=actual_dists(dist);
72  return (dist+nodeq->data.radius+noder->data.radius);
73 }
74 
75 void CBallTree::min_max_dist(float64_t* pt, bnode_t* node, float64_t &lower,float64_t &upper, int32_t dim)
76 {
77  float64_t dist=0;
78  SGVector<float64_t> center=node->data.center;
79  for (int32_t i=0;i<dim;i++)
80  dist+=add_dim_dist(center[i]-pt[i]);
81 
82  dist=actual_dists(dist);
83  lower=CMath::max(0.0,dist-node->data.radius);
84  upper=dist+node->data.radius;
85 }
86 
87 void CBallTree::init_node(bnode_t* node, index_t start, index_t end)
88 {
89  SGVector<float64_t> upper_bounds(m_data.num_rows);
90  SGVector<float64_t> lower_bounds(m_data.num_rows);
91 
93  for (int32_t i=0;i<m_data.num_rows;i++)
94  {
95  center[i]=m_data(i,m_vec_id[start]);
96  upper_bounds[i]=m_data(i,m_vec_id[start]);
97  lower_bounds[i]=m_data(i,m_vec_id[start]);
98  for (int32_t j=start+1;j<=end;j++)
99  {
100  float64_t data_pt=m_data(i,m_vec_id[j]);
101  upper_bounds[i]=CMath::max(upper_bounds[i],data_pt);
102  lower_bounds[i]=CMath::min(lower_bounds[i],data_pt);
103  center[i]+=data_pt;
104  }
105 
106  center[i]/=(end-start+1.f);
107  }
108 
109  float64_t radius=0;
110  for (int32_t i=start;i<=end;i++)
111  radius=CMath::max(distance(m_vec_id[i],center.vector,center.vlen),radius);
112 
113  node->data.radius=radius;
114  node->data.center=center;
115  node->data.start_idx=start;
116  node->data.bbox_upper=upper_bounds;
117  node->data.bbox_lower=lower_bounds;
118  node->data.end_idx=end;
119 }
The node of the tree structure forming a TreeMachine The node contains pointer to its parent and poin...
int32_t index_t
Definition: common.h:62
float64_t distance(index_t vec, float64_t *arr, int32_t dim)
Definition: NbodyTree.cpp:201
float64_t add_dim_dist(float64_t d)
Definition: NbodyTree.h:194
index_t num_rows
Definition: SGMatrix.h:376
SGMatrix< float64_t > m_data
Definition: NbodyTree.h:314
index_t vlen
Definition: SGVector.h:494
EDistanceType
Definition: Distance.h:32
double float64_t
Definition: common.h:50
static T max(T a, T b)
Definition: Math.h:168
This class implements genaralized tree for N-body problems like k-NN, kernel density estimation...
Definition: NbodyTree.h:50
float64_t actual_dists(float64_t dists)
Definition: NbodyTree.h:172
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
SGVector< index_t > m_vec_id
Definition: NbodyTree.h:317
static T min(T a, T b)
Definition: Math.h:157
CBallTree(int32_t leaf_size=1, EDistanceType d=D_EUCLIDEAN)
Definition: BallTree.cpp:35

SHOGUN Machine Learning Toolbox - Documentation