Skylark (Sketching Library)  0.1
/var/lib/jenkins/jobs/Skylark/workspace/sketch/RLT_Elemental.hpp
Go to the documentation of this file.
00001 #ifndef SKYLARK_RLT_ELEMENTAL_HPP
00002 #define SKYLARK_RLT_ELEMENTAL_HPP
00003 
00004 namespace skylark {
00005 namespace sketch {
00006 
00010 template <typename ValueType,
00011           template <typename> class InputType,
00012           template <typename> class KernelDistribution>
00013 struct RLT_t <
00014     InputType<ValueType>,
00015     elem::Matrix<ValueType>,
00016     KernelDistribution> :
00017         public RLT_data_t<KernelDistribution> {
00018     // Typedef value, matrix, transform, distribution and transform data types
00019     // so that we can use them regularly and consistently.
00020     typedef ValueType value_type;
00021     typedef InputType<value_type> matrix_type;
00022     typedef elem::Matrix<value_type> output_matrix_type;
00023     typedef RLT_data_t<KernelDistribution> data_type;
00024 private:
00025     typedef skylark::sketch::dense_transform_t <matrix_type,
00026                                                 output_matrix_type,
00027                                                 KernelDistribution>
00028     underlying_t;
00029 
00030 
00031 protected:
00035     RLT_t (int N, int S, base::context_t& context)
00036         : data_type (N, S, context) {
00037 
00038     }
00039 
00040 public:
00044     RLT_t(const RFT_t<matrix_type,
00045                       output_matrix_type,
00046                       KernelDistribution>& other)
00047         : data_type(other) {
00048 
00049     }
00050 
00054     RLT_t(const data_type& other_data)
00055         : data_type(other_data) {
00056 
00057     }
00058 
00062     template <typename Dimension>
00063     void apply (const matrix_type& A,
00064                 output_matrix_type& sketch_of_A,
00065                 Dimension dimension) const {
00066         try {
00067             apply_impl(A, sketch_of_A, dimension);
00068         } catch (std::logic_error e) {
00069             SKYLARK_THROW_EXCEPTION (
00070                 base::elemental_exception()
00071                     << base::error_msg(e.what()) );
00072         } catch(boost::mpi::exception e) {
00073             SKYLARK_THROW_EXCEPTION (
00074                 base::mpi_exception()
00075                     << base::error_msg(e.what()) );
00076         }
00077     }
00078 
00079 private:
00084     void apply_impl(const matrix_type& A,
00085         output_matrix_type& sketch_of_A,
00086         skylark::sketch::columnwise_tag tag) const {
00087 
00088         underlying_t underlying(*data_type::_underlying_data);
00089         underlying.apply(A, sketch_of_A, tag);
00090         for(int j = 0; j < base::Width(A); j++)
00091             for(int i = 0; i < data_type::_S; i++) {
00092                 value_type val = sketch_of_A.Get(i, j);
00093                 sketch_of_A.Set(i, j,
00094                     data_type::_outscale * std::exp(-val));
00095             }
00096     }
00097 
00102     void apply_impl(const matrix_type& A,
00103         output_matrix_type& sketch_of_A,
00104         skylark::sketch::rowwise_tag tag) const {
00105 
00106         // TODO verify sizes etc.
00107         underlying_t underlying(*data_type::_underlying_data);
00108         underlying.apply(A, sketch_of_A, tag);
00109         for(int j = 0; j < data_type::_S; j++)
00110             for(int i = 0; i < base::Height(A); i++) {
00111                 value_type val = sketch_of_A.Get(i, j);
00112                 sketch_of_A.Set(i, j,
00113                     data_type::_outscale * std::exp(-val));
00114 
00115             }
00116     }
00117 };
00118 
00119 } } 
00121 #endif // SKYLARK_RLT_ELEMENTAL_HPP