Adding the metric object.
This commit is contained in:
@@ -31,12 +31,62 @@ class BilinearFormTestSuite : public boost::unit_test_framework::test_suite {
|
||||
BilinearFormTest() {
|
||||
}
|
||||
|
||||
void RandomDataset(GenMatrix<double, false> *random_dataset) {
|
||||
}
|
||||
|
||||
void RunTests() {
|
||||
|
||||
fprintf(stderr, "Running the tests:\n");
|
||||
|
||||
// Generate a random table.
|
||||
GenMatrix<double, false> random_dataset;
|
||||
RandomDataset(&random_dataset);
|
||||
|
||||
// Generate a random kernel.
|
||||
// typedef fl::math::GaussianDotProduct< double, fl::math::LMetric<2> > KernelType;
|
||||
//fl::math::LMetric<2> metric;
|
||||
KernelType kernel;
|
||||
kernel.Init(math::Random<double>(1, 10), &metric);
|
||||
printf("Testing on the Gaussian kernel with the bandwidth of %g.\n",
|
||||
kernel.bandwidth() );
|
||||
|
||||
// Make a kernel matrix linear operator.
|
||||
#ifdef EPETRA_MPI
|
||||
Epetra_MpiComm comm(MPI_COMM_WORLD);
|
||||
#else
|
||||
Epetra_SerialComm comm;
|
||||
#endif
|
||||
Epetra_Map map(random_dataset.n_entries(), 0, comm);
|
||||
Anasazi::KernelLinearOperator<KernelType, false, false> op(
|
||||
random_dataset, kernel, comm, map);
|
||||
|
||||
// Make a Lanczos object, and run it.
|
||||
fl::ml::BilinearFormEstimator<fl::ml::InverseTransformation> bilinear;
|
||||
bilinear.Init(&op);
|
||||
|
||||
// A random intitial starting vector, and with it compute the
|
||||
// Lanczos tridiagonal matrix.
|
||||
Vector random_initial_vector;
|
||||
RandomVector_(random_dataset.n_entries(), &random_initial_vector);
|
||||
|
||||
// Test the log determinant computation.
|
||||
fl::ml::LogDeterminant log_determinant;
|
||||
log_determinant.Init(&op);
|
||||
log_determinant.set_max_num_iterations(3);
|
||||
|
||||
printf("Testing the log determinant: \n");
|
||||
printf("-----------------------------\n");
|
||||
printf("The ultra naive estimate should be %g.\n",
|
||||
log_determinant.NaiveCompute() );
|
||||
printf("The naive estimate is %g.\n",
|
||||
log_determinant.Compute() );
|
||||
printf("The Monte Carlo estimate is %g.\n",
|
||||
log_determinant.MonteCarloCompute() );
|
||||
|
||||
// Call MPI Finalize.
|
||||
#ifdef EPETRA_MPI
|
||||
MPI_Finalize();
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
// Copyright 2007 Georgia Institute of Technology. All rights reserved.
|
||||
// ABSOLUTELY NOT FOR DISTRIBUTION
|
||||
/**
|
||||
* @file tree/kdtree.h
|
||||
*
|
||||
* Tools for kd-trees.
|
||||
* @author Dongryeol Lee
|
||||
*
|
||||
* Eventually we hope to support KD trees with non-L2 (Euclidean)
|
||||
* metrics, like Manhattan distance.
|
||||
* @file tree/gen_metric_tree.h
|
||||
*
|
||||
* Tools for metric-trees.
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
|
||||
#ifndef TREE_GEN_METRIC_TREE_H
|
||||
@@ -17,13 +14,12 @@
|
||||
#include "general_spacetree.h"
|
||||
|
||||
#include "fastlib/base/common.h"
|
||||
#include "fastlib/col/arraylist.h"
|
||||
#include "fastlib/fx/fx.h"
|
||||
|
||||
#include "gen_metric_tree_impl.h"
|
||||
|
||||
/**
|
||||
* Regular pointer-style trees (as opposed to THOR trees).
|
||||
* Regular pointer-style trees.
|
||||
*/
|
||||
namespace proximity {
|
||||
|
||||
@@ -45,14 +41,14 @@ namespace proximity {
|
||||
*/
|
||||
template<typename TMetricTree>
|
||||
TMetricTree *MakeGenMetricTree(Matrix& matrix, index_t leaf_size,
|
||||
ArrayList<index_t> *old_from_new = NULL,
|
||||
ArrayList<index_t> *new_from_old = NULL) {
|
||||
std::vector<int> *old_from_new = NULL,
|
||||
std::vector<int> *new_from_old = NULL) {
|
||||
|
||||
TMetricTree *node = new TMetricTree();
|
||||
index_t *old_from_new_ptr;
|
||||
|
||||
if (old_from_new) {
|
||||
old_from_new->Init(matrix.n_cols());
|
||||
old_from_new->resize(matrix.n_cols());
|
||||
|
||||
for (index_t i = 0; i < matrix.n_cols(); i++) {
|
||||
(*old_from_new)[i] = i;
|
||||
@@ -66,11 +62,11 @@ TMetricTree *MakeGenMetricTree(Matrix& matrix, index_t leaf_size,
|
||||
|
||||
node->Init(0, matrix.n_cols());
|
||||
node->bound().center().Init(matrix.n_rows());
|
||||
tree_gen_metric_tree_private::SplitGenMetricTree<TMetricTree>
|
||||
(matrix, node, leaf_size, old_from_new_ptr);
|
||||
tree_gen_metric_tree_private::SplitGenMetricTree<TMetricTree>(
|
||||
matrix, node, leaf_size, old_from_new_ptr);
|
||||
|
||||
if (new_from_old) {
|
||||
new_from_old->Init(matrix.n_cols());
|
||||
new_from_old->resize(matrix.n_cols());
|
||||
for (index_t i = 0; i < matrix.n_cols(); i++) {
|
||||
(*new_from_old)[(*old_from_new)[i]] = i;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
/** @author Dongryeol Lee
|
||||
*
|
||||
* @file lmetric.h
|
||||
*
|
||||
* @brief Implements the general L_p metric object.
|
||||
*/
|
||||
|
||||
#ifndef CONTRIB_DONGRYEL_PROXIMITY_PROJECT_LMETRIC_H
|
||||
#define CONTRIB_DONGRYEL_PROXIMITY_PROJECT_LMETRIC_H
|
||||
|
||||
#include "fastlib/math/math_lib.h"
|
||||
#include "contrib/dongryel/proximity_project/metric.h"
|
||||
|
||||
namespace proximity_project {
|
||||
template<int t_pow>
|
||||
class LMetric: public virtual Metric {
|
||||
|
||||
public:
|
||||
double Distance(
|
||||
const Vector &first_point, const Vector &second_point) const {
|
||||
return math::Pow<1, t_pow>(
|
||||
this->DistanceIneq(first_point, second_point));
|
||||
}
|
||||
|
||||
double DistanceSq(
|
||||
const Vector &first_point, const Vector &second_point) const {
|
||||
return math::Pow<2, 1>(Distance(first_point, second_point));
|
||||
}
|
||||
|
||||
double DistanceIneq(
|
||||
const Vector &first_point, const Vector &second_point) const {
|
||||
return la::RawLMetric<t_pow>(first_point, second_point);
|
||||
}
|
||||
};
|
||||
|
||||
class LMetric<2>: public virtual Metric {
|
||||
|
||||
public:
|
||||
double Distance(
|
||||
const Vector &first_point, const Vector &second_point) const {
|
||||
|
||||
return math::Pow<1, 2>(DistanceIneq(a, b));
|
||||
}
|
||||
|
||||
double DistanceSq(
|
||||
const Vector &first_point, const Vector &second_point) const {
|
||||
|
||||
return la::RawLMetric<2>(first_point, second_point);
|
||||
}
|
||||
|
||||
double DistanceIneq(
|
||||
const Vector &first_point, const Vector &second_point) const {
|
||||
return this->Distance(first_point, second_point);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,29 @@
|
||||
/** @author Dongryeol Lee
|
||||
*
|
||||
* @file metric.h
|
||||
*
|
||||
* @brief The general metric that can be inherited from.
|
||||
*/
|
||||
|
||||
#ifndef CONTRIB_DONGRYEL_PROXIMITY_PROJECT_METRIC_H
|
||||
#define CONTRIB_DONGRYEL_PROXIMITY_PROJECT_METRIC_H
|
||||
|
||||
#include "fastlib/la/matrix.h"
|
||||
|
||||
namespace fl {
|
||||
namespace ml {
|
||||
class Metric {
|
||||
public:
|
||||
virtual double Distance(
|
||||
const Vector &first_point, const Vector &second_point) const = 0;
|
||||
|
||||
virtual double DistanceSq(
|
||||
const Vector &first_point, const Vector &second_point) const = 0;
|
||||
|
||||
virtual double DistanceIneq(
|
||||
const Vector &first_point, const Vector &second_point) const = 0;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,53 @@
|
||||
/** @author Dongryeol Lee
|
||||
*
|
||||
* @brief A thin wrapper on the Matrix class with the tree.
|
||||
*
|
||||
* @file table.h
|
||||
*/
|
||||
|
||||
#ifndef CONTRIB_DONGRYEL_PROXIMITY_PROJECT_TABLE_H
|
||||
#define CONTRIB_DONGRYEL_PROXIMITY_PROJECT_TABLE_H
|
||||
|
||||
namespace proximity_project {
|
||||
template<typename TreeType>
|
||||
class Table {
|
||||
private:
|
||||
TreeType *tree_;
|
||||
|
||||
std::vector<int> old_to_new_;
|
||||
|
||||
std::vector<int> new_to_old_;
|
||||
|
||||
public:
|
||||
|
||||
class TreeIterator {
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
Table() {
|
||||
tree_ = NULL;
|
||||
}
|
||||
|
||||
~Table() {
|
||||
if (tree_ != NULL) {
|
||||
delete tree_;
|
||||
tree_ = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
const TreeType *get_tree() const {
|
||||
return tree_;
|
||||
}
|
||||
|
||||
TreeType *get_tree() {
|
||||
return tree_;
|
||||
}
|
||||
|
||||
void IndexData() {
|
||||
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user