Skylark (Sketching Library)  0.1
/var/lib/jenkins/jobs/Skylark/workspace/sketch/RFT_data.hpp
Go to the documentation of this file.
00001 #ifndef SKYLARK_RFT_DATA_HPP
00002 #define SKYLARK_RFT_DATA_HPP
00003 
00004 #ifndef SKYLARK_SKETCH_HPP
00005 #error "Include top-level sketch.hpp instead of including individuals headers"
00006 #endif
00007 
00008 #include <vector>
00009 
00010 #include "../utility/randgen.hpp"
00011 
00012 namespace skylark { namespace sketch {
00013 
00014 
00026 template <template <typename> class KernelDistribution>
00027 struct RFT_data_t : public sketch_transform_data_t {
00028 
00029     typedef dense_transform_data_t<KernelDistribution> underlying_data_type;
00030     typedef sketch_transform_data_t base_t;
00031 
00032     RFT_data_t (int N, int S, double inscale, double outscale,
00033         base::context_t& context)
00034         : base_t(N, S, context, "RFT"), _inscale(inscale),
00035           _outscale(outscale) {
00036 
00037         context = build();
00038     }
00039 
00040     virtual
00041     boost::property_tree::ptree to_ptree() const {
00042         SKYLARK_THROW_EXCEPTION (
00043           base::sketch_exception()
00044               << base::error_msg(
00045                  "Do not yet support serialization of generic RFT transform"));
00046 
00047         return boost::property_tree::ptree();
00048     }
00049 
00050 protected:
00051     RFT_data_t (int N, int S, double inscale, double outscale,
00052         const base::context_t& context, std::string type)
00053         : base_t(N, S, context, type),  _inscale(inscale),
00054           _outscale(outscale) {
00055 
00056     }
00057 
00058     base::context_t build() {
00059 
00060         base::context_t ctx = base_t::build();
00061 
00062         _underlying_data = boost::shared_ptr<underlying_data_type>(new
00063             underlying_data_type(base_t::_N, base_t::_S, _inscale, ctx));
00064 
00065         const double pi = boost::math::constants::pi<double>();
00066         boost::random::uniform_real_distribution<double>
00067             distribution(0, 2 * pi);
00068         _shifts = ctx.generate_random_samples_array(base_t::_S, distribution);
00069         return ctx;
00070     }
00071 
00072     double _inscale;
00073     double _outscale; 
00074     boost::shared_ptr<underlying_data_type> _underlying_data;
00076     std::vector<double> _shifts; 
00077 };
00078 
00079 struct GaussianRFT_data_t :
00080         public RFT_data_t<bstrand::normal_distribution> {
00081 
00082     typedef RFT_data_t<bstrand::normal_distribution > base_t;
00083 
00085     struct params_t : public sketch_params_t {
00086 
00087         params_t(double sigma) : sigma(sigma) {
00088 
00089         }
00090 
00091         const double sigma;
00092     };
00093 
00094     GaussianRFT_data_t(int N, int S, double sigma,
00095         base::context_t& context)
00096         : base_t(N, S, 1.0 / sigma, std::sqrt(2.0 / S), context, "GaussianRFT"), 
00097           _sigma(sigma) {
00098 
00099         context = base_t::build();
00100     }
00101 
00102     GaussianRFT_data_t(int N, int S, const params_t& params,
00103         base::context_t& context)
00104         : base_t(N, S, 1.0 / params.sigma, std::sqrt(2.0 / S), context, "GaussianRFT"), 
00105           _sigma(params.sigma) {
00106 
00107         context = base_t::build();
00108     }
00109 
00110     GaussianRFT_data_t(const boost::property_tree::ptree &pt) :
00111         base_t(pt.get<int>("N"), pt.get<int>("S"),
00112             1.0 / pt.get<double>("sigma"),
00113             std::sqrt(2.0 / pt.get<double>("S")),
00114             base::context_t(pt.get_child("creation_context")), "GaussianRFT"),
00115         _sigma(pt.get<double>("sigma")) {
00116 
00117         base_t::build();
00118     }
00119 
00125     virtual
00126     boost::property_tree::ptree to_ptree() const {
00127         boost::property_tree::ptree pt;
00128         sketch_transform_data_t::add_common(pt);
00129         pt.put("sigma", _sigma);
00130         return pt;
00131     }
00132 
00133 protected:
00134     GaussianRFT_data_t(int N, int S, double sigma,
00135         const base::context_t& context, std::string type)
00136         : base_t(N, S, 1.0 / sigma, std::sqrt(2.0 / S), context, type),
00137           _sigma(sigma) {
00138 
00139     }
00140 
00141 private:
00142     const double _sigma;
00143 };
00144 
00145 struct LaplacianRFT_data_t :
00146         public RFT_data_t<bstrand::cauchy_distribution> {
00147 
00148     typedef RFT_data_t<bstrand::cauchy_distribution > base_t;
00149 
00151     struct params_t : public sketch_params_t {
00152 
00153         params_t(double sigma) : sigma(sigma) {
00154 
00155         }
00156 
00157         const double sigma;
00158     };
00159 
00160     LaplacianRFT_data_t(int N, int S, double sigma,
00161         base::context_t& context)
00162         : base_t(N, S, 1.0 / sigma, std::sqrt(2.0 / S), context, "LaplacianRFT"),
00163         _sigma(sigma) {
00164 
00165         context = base_t::build();
00166     }
00167 
00168     LaplacianRFT_data_t(int N, int S, const params_t& params,
00169         base::context_t& context)
00170         : base_t(N, S, 1.0 / params.sigma, std::sqrt(2.0 / S), context, "LaplacianRFT"),
00171         _sigma(params.sigma) {
00172 
00173         context = base_t::build();
00174     }
00175 
00176     LaplacianRFT_data_t(const boost::property_tree::ptree &pt) :
00177         base_t(pt.get<int>("N"), pt.get<int>("S"),
00178             1.0 / pt.get<double>("sigma"),
00179             std::sqrt(2.0 / pt.get<double>("S")),
00180             base::context_t(pt.get_child("creation_context")), "LaplacianRFT"),
00181         _sigma(pt.get<double>("sigma")) {
00182 
00183         base_t::build();
00184     }
00185 
00191     virtual
00192     boost::property_tree::ptree to_ptree() const {
00193         boost::property_tree::ptree pt;
00194         sketch_transform_data_t::add_common(pt);
00195         pt.put("sigma", _sigma);
00196         return pt;
00197     }
00198 
00199 protected:
00200 
00201     LaplacianRFT_data_t(int N, int S, double sigma,
00202         const base::context_t& context, std::string type)
00203         : base_t(N, S, 1.0 / sigma, std::sqrt(2.0 / S), context, type), 
00204           _sigma(sigma) {
00205 
00206     }
00207 
00208 private:
00209     const double _sigma;
00210 };
00211 
00212 } } 
00214 #endif