From 63f147f42efa956de3a176c2ae4e4b77754db296 Mon Sep 17 00:00:00 2001 From: Dongryeol Lee Date: Sat, 5 Jun 2010 19:41:11 +0000 Subject: [PATCH] Adding the metric object. --- .../gp_regression/bilinear_form_test.cc | 50 ++++++++++++++++ .../proximity_project/gen_metric_tree.h | 26 ++++----- .../dongryel/proximity_project/lmetric.h | 58 +++++++++++++++++++ .../dongryel/proximity_project/metric.h | 29 ++++++++++ .../dongryel/proximity_project/table.h | 53 +++++++++++++++++ 5 files changed, 201 insertions(+), 15 deletions(-) create mode 100644 fastlib/trunk/contrib/dongryel/proximity_project/lmetric.h create mode 100644 fastlib/trunk/contrib/dongryel/proximity_project/metric.h create mode 100644 fastlib/trunk/contrib/dongryel/proximity_project/table.h diff --git a/fastlib/trunk/contrib/dongryel/gp_regression/bilinear_form_test.cc b/fastlib/trunk/contrib/dongryel/gp_regression/bilinear_form_test.cc index 8505141ea9..a3769c0550 100644 --- a/fastlib/trunk/contrib/dongryel/gp_regression/bilinear_form_test.cc +++ b/fastlib/trunk/contrib/dongryel/gp_regression/bilinear_form_test.cc @@ -31,12 +31,62 @@ class BilinearFormTestSuite : public boost::unit_test_framework::test_suite { BilinearFormTest() { } + void RandomDataset(GenMatrix *random_dataset) { + } + void RunTests() { fprintf(stderr, "Running the tests:\n"); + // Generate a random table. + GenMatrix 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(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 op( + random_dataset, kernel, comm, map); + + // Make a Lanczos object, and run it. + fl::ml::BilinearFormEstimator 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 } }; diff --git a/fastlib/trunk/contrib/dongryel/proximity_project/gen_metric_tree.h b/fastlib/trunk/contrib/dongryel/proximity_project/gen_metric_tree.h index 133a5207c1..8f852756ab 100644 --- a/fastlib/trunk/contrib/dongryel/proximity_project/gen_metric_tree.h +++ b/fastlib/trunk/contrib/dongryel/proximity_project/gen_metric_tree.h @@ -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 TMetricTree *MakeGenMetricTree(Matrix& matrix, index_t leaf_size, - ArrayList *old_from_new = NULL, - ArrayList *new_from_old = NULL) { + std::vector *old_from_new = NULL, + std::vector *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 - (matrix, node, leaf_size, old_from_new_ptr); + tree_gen_metric_tree_private::SplitGenMetricTree( + 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; } diff --git a/fastlib/trunk/contrib/dongryel/proximity_project/lmetric.h b/fastlib/trunk/contrib/dongryel/proximity_project/lmetric.h new file mode 100644 index 0000000000..9c1b599498 --- /dev/null +++ b/fastlib/trunk/contrib/dongryel/proximity_project/lmetric.h @@ -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 +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(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 diff --git a/fastlib/trunk/contrib/dongryel/proximity_project/metric.h b/fastlib/trunk/contrib/dongryel/proximity_project/metric.h new file mode 100644 index 0000000000..1f6421dff0 --- /dev/null +++ b/fastlib/trunk/contrib/dongryel/proximity_project/metric.h @@ -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 diff --git a/fastlib/trunk/contrib/dongryel/proximity_project/table.h b/fastlib/trunk/contrib/dongryel/proximity_project/table.h new file mode 100644 index 0000000000..3880c629a5 --- /dev/null +++ b/fastlib/trunk/contrib/dongryel/proximity_project/table.h @@ -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 +class Table { + private: + TreeType *tree_; + + std::vector old_to_new_; + + std::vector 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