Skip to content

Commit

Permalink
dual distance methods added;minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mazumdarparijat committed Jul 16, 2014
1 parent 5911c80 commit 26f492d
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/shogun/distributions/KernelDensity.cpp
Expand Up @@ -28,7 +28,7 @@
* either expressed or implied, of the Shogun Development Team.
*/

#include <shogun/machine/KernelDensity.h>
#include <shogun/distributions/KernelDensity.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/multiclass/tree/KDTree.h>
#include <shogun/multiclass/tree/BallTree.h>
Expand Down
24 changes: 24 additions & 0 deletions src/shogun/multiclass/tree/BallTree.cpp
Expand Up @@ -48,6 +48,30 @@ float64_t CBallTree::min_dist(bnode_t* node,float64_t* feat, int32_t dim)
return CMath::max(0.0,dist-node->data.radius);
}

float64_t CBallTree::min_dist_dual(bnode_t* nodeq, bnode_t* noder)
{
float64_t dist=0;
SGVector<float64_t> center1=nodeq->data.center;
SGVector<float64_t> center2=noder->data.center;
for (int32_t i=0;i<center1.vlen;i++)
dist+=add_dim_dist(center1[i]-center2[i]);

dist=actual_dists(dist);
return CMath::max(0.0,dist-nodeq->data.radius-noder->data.radius);
}

float64_t CBallTree::max_dist_dual(bnode_t* nodeq, bnode_t* noder)
{
float64_t dist=0;
SGVector<float64_t> center1=nodeq->data.center;
SGVector<float64_t> center2=noder->data.center;
for (int32_t i=0;i<center1.vlen;i++)
dist+=add_dim_dist(center1[i]-center2[i]);

dist=actual_dists(dist);
return (dist+nodeq->data.radius+noder->data.radius);
}

void CBallTree::min_max_dist(float64_t* pt, bnode_t* node, float64_t &lower,float64_t &upper, int32_t dim)
{
float64_t dist=0;
Expand Down
16 changes: 16 additions & 0 deletions src/shogun/multiclass/tree/BallTree.h
Expand Up @@ -70,6 +70,22 @@ class CBallTree : public CNbodyTree
*/
float64_t min_dist(bnode_t* node,float64_t* feat, int32_t dim);

/** find minimum distance between 2 nodes
*
* @param nodeq node containing active query vectors
* @param noder node containing active training vectors
* @return min distance between 2 nodes
*/
virtual float64_t min_dist_dual(bnode_t* nodeq, bnode_t* noder);

/** find max distance between 2 nodes
*
* @param nodeq node containing active query vectors
* @param noder node containing active training vectors
* @return max distance between 2 nodes
*/
virtual float64_t max_dist_dual(bnode_t* nodeq, bnode_t* noder);

/** get min as well as max distance of a node from a point
*
* @param pt point whose distance is to be calculated
Expand Down
34 changes: 34 additions & 0 deletions src/shogun/multiclass/tree/KDTree.cpp
Expand Up @@ -54,6 +54,40 @@ float64_t CKDTree::min_dist(bnode_t* node,float64_t* feat, int32_t dim)
return actual_dists(dist);
}

float64_t CKDTree::min_dist_dual(bnode_t* nodeq, bnode_t* noder)
{
SGVector<float64_t> nodeq_lower=nodeq->data.bbox_lower;
SGVector<float64_t> nodeq_upper=nodeq->data.bbox_upper;
SGVector<float64_t> noder_lower=noder->data.bbox_lower;
SGVector<float64_t> noder_upper=noder->data.bbox_upper;
float64_t dist=0;
for(int32_t i=0;i<noder_lower.vlen;i++)
{
float64_t d1=nodeq_lower[i]-noder_upper[i];
float64_t d2=noder_lower[i]-nodeq_upper[i];
dist+=add_dim_dist(0.5*(d1+CMath::abs(d1)+d2+CMath::abs(d2)));
}

return actual_dists(dist);
}

float64_t CKDTree::max_dist_dual(bnode_t* nodeq, bnode_t* noder)
{
SGVector<float64_t> nodeq_lower=nodeq->data.bbox_lower;
SGVector<float64_t> nodeq_upper=nodeq->data.bbox_upper;
SGVector<float64_t> noder_lower=noder->data.bbox_lower;
SGVector<float64_t> noder_upper=noder->data.bbox_upper;
float64_t dist=0;
for(int32_t i=0;i<noder_lower.vlen;i++)
{
float64_t d1=CMath::abs(nodeq_lower[i]-noder_upper[i]);
float64_t d2=CMath::abs(noder_lower[i]-nodeq_upper[i]);
dist+=add_dim_dist(CMath::max(d1,d2));
}

return actual_dists(dist);
}

void CKDTree::min_max_dist(float64_t* pt, bnode_t* node, float64_t &lower,float64_t &upper, int32_t dim)
{
lower=0;
Expand Down
16 changes: 16 additions & 0 deletions src/shogun/multiclass/tree/KDTree.h
Expand Up @@ -70,6 +70,22 @@ class CKDTree : public CNbodyTree
*/
float64_t min_dist(bnode_t* node,float64_t* feat, int32_t dim);

/** find minimum distance between 2 nodes
*
* @param nodeq node containing active query vectors
* @param noder node containing active training vectors
* @return min distance between 2 nodes
*/
virtual float64_t min_dist_dual(bnode_t* nodeq, bnode_t* noder);

/** find max distance between 2 nodes
*
* @param nodeq node containing active query vectors
* @param noder node containing active training vectors
* @return max distance between 2 nodes
*/
virtual float64_t max_dist_dual(bnode_t* nodeq, bnode_t* noder);

/** get min as well as max distance of a node from a point
*
* @param pt point whose distance is to be calculated
Expand Down
8 changes: 0 additions & 8 deletions src/shogun/multiclass/tree/NbodyTree.cpp
Expand Up @@ -131,14 +131,6 @@ SGMatrix<index_t> CNbodyTree::get_knn_indices()
return SGMatrix<index_t>();
}

float64_t CNbodyTree::actual_dists(float64_t dists)
{
if (m_dist==D_MANHATTAN)
return dists;

return CMath::sqrt(dists);
}

void CNbodyTree::query_knn_single(CKNNHeap* heap, float64_t mdist, bnode_t* node, float64_t* arr, int32_t dim)
{
if (mdist>heap->get_max_dist())
Expand Down
26 changes: 24 additions & 2 deletions src/shogun/multiclass/tree/NbodyTree.h
Expand Up @@ -112,6 +112,22 @@ class CNbodyTree : public CTreeMachine<NbodyTreeNodeData>
*/
virtual float64_t min_dist(bnode_t* node,float64_t* feat, int32_t dim)=0;

/** find minimum distance between 2 nodes
*
* @param nodeq node containing active query vectors
* @param noder node containing active training vectors
* @return min distance between 2 nodes
*/
virtual float64_t min_dist_dual(bnode_t* nodeq, bnode_t* noder)=0;

/** find max distance between 2 nodes
*
* @param nodeq node containing active query vectors
* @param noder node containing active training vectors
* @return max distance between 2 nodes
*/
virtual float64_t max_dist_dual(bnode_t* nodeq, bnode_t* noder)=0;

/** initialize node
*
* @param node node to be initialized
Expand All @@ -120,7 +136,7 @@ class CNbodyTree : public CTreeMachine<NbodyTreeNodeData>
*/
virtual void init_node(bnode_t* node, index_t start, index_t end)=0;

/** get min as well max distance of a node from a point
/** get min as well as max distance of a node from a point
*
* @param pt point whose distance is to be calculated
* @param node node from which distances are to be calculated
Expand All @@ -135,7 +151,13 @@ class CNbodyTree : public CTreeMachine<NbodyTreeNodeData>
* @param dist distance value
* @return actual distance
*/
float64_t actual_dists(float64_t dist);
inline float64_t actual_dists(float64_t dists)
{
if (m_dist==D_MANHATTAN)
return dists;

return CMath::sqrt(dists);
}

/** distance between 2 vectors
*
Expand Down

0 comments on commit 26f492d

Please sign in to comment.