15 #ifndef PAAL_FREQUENT_DIRECTIONS_HPP
16 #define PAAL_FREQUENT_DIRECTIONS_HPP
21 #include <boost/numeric/bindings/lapack/gesvd.hpp>
22 #include <boost/numeric/ublas/detail/matrix_assign.hpp>
23 #include <boost/numeric/ublas/matrix.hpp>
24 #include <boost/numeric/ublas/matrix_expression.hpp>
26 #include <boost/range/algorithm/copy.hpp>
27 #include <boost/range/distance.hpp>
28 #include <boost/range/size.hpp>
36 template <
typename InputRow,
39 void copy_row_to_matrix(InputRow &&input_row, DestRow &dest_matrix_row,
42 assert(boost::size(dest_matrix_row) == boost::size(input_row));
43 dest_matrix_row = input_row;
46 template <
typename InputRow,
48 void copy_row_to_matrix(InputRow &&input_row, DestRow &dest_matrix_row,
52 assert(boost::distance(dest_matrix_row) ==
53 std::distance(std::begin(input_row),
54 std::end(input_row)));
55 std::copy(std::begin(input_row),
57 std::begin(dest_matrix_row));
60 template <
typename InputRow,
62 void copy_row_to_matrix(InputRow &&input_row, DestRow &dest_matrix_row,
65 for (
auto it = std::begin(input_row); it != std::end(input_row); ++it) {
66 assert(it.index2() <= dest_matrix_row.size());
67 dest_matrix_row(it.index2()) = *it;
72 template <
typename InputRow,
74 void copy_row_to_matrix(InputRow &&input_row, DestRow &dest_matrix_row) {
75 copy_row_to_matrix(std::forward<InputRow>(input_row), dest_matrix_row,
76 typename data_structures::is_sparse_row<InputRow>::type(),
77 typename std::is_assignable<DestRow,InputRow>::type());
91 template <
typename Matrix>
94 std::size_t m_actual_size;
95 std::size_t m_compress_size;
98 using coordinate_t =
typename matrix_types::coordinate_t;
102 template<
class Archive>
103 void serialize(Archive & ar,
const unsigned int version) {
105 ar & m_compress_size;
116 : m_sketch(std::move(matrix)), m_actual_size(0),
117 m_compress_size(std::min(compress_size,
matrix_types::num_columns(m_sketch))) {
118 assert(m_compress_size >= 0);
119 assert(m_compress_size < matrix_types::num_rows(m_sketch));
127 return m_actual_size == other.m_actual_size &&
128 m_compress_size == other.m_compress_size &&
129 boost::numeric::ublas::detail::expression_type_check(m_sketch, other.m_sketch);
133 template <
typename MatrixData>
135 for (
auto row = matrix.begin1(); row != matrix.end1(); ++row) {
141 template <
typename InputRow>
143 if(m_actual_size == matrix_types::num_rows(m_sketch))
147 typename matrix_types::matrix_row_t dest_row{m_sketch, m_actual_size++};
148 detail::copy_row_to_matrix(std::forward<InputRow>(input_row), dest_row);
153 template <
typename RowRange>
155 for(
auto const & row : row_range)
165 auto rows_count = matrix_types::num_rows(m_sketch);
166 auto columns_count = matrix_types::num_columns(m_sketch);
167 auto min_dimension = std::min(rows_count, columns_count);
169 typename matrix_types::matrix_column_major_t u{rows_count,min_dimension}, vt{min_dimension,columns_count};
170 typename matrix_types::vector_t sigma{min_dimension};
172 typename matrix_types::matrix_column_major_t sketch{std::move(m_sketch)};
173 boost::numeric::bindings::lapack::gesvd(sketch, sigma, u, vt);
175 coordinate_t
const delta = m_compress_size < min_dimension ? sigma[m_compress_size] * sigma[m_compress_size] : coordinate_t{};
177 for (
auto &sigma_i : sigma) {
178 sigma_i = std::sqrt(std::max(sigma_i * sigma_i - delta, coordinate_t{}));
181 typename matrix_types::matrix_diagonal_t s_diagonal{rows_count, min_dimension, 0, 0, std::move(sigma.data())};
182 m_sketch = boost::numeric::ublas::prod(s_diagonal, vt);
184 m_actual_size = m_compress_size;
193 return std::make_pair(std::cref(m_sketch), m_actual_size);
199 template <
typename Matrix>
206 template <
typename Matrix>
212 template <
typename CoordinateType>
214 boost::numeric::ublas::matrix<CoordinateType> matrix{rows_count, columns_count, CoordinateType{}};
219 template <
typename CoordinateType>
221 boost::numeric::ublas::matrix<CoordinateType> matrix{rows_count, columns_count, CoordinateType{}};
227 #endif // PAAL_FREQUENT_DIRECTIONS_HPP
Traits class for matrix related types.
Represents sketch of matrix.
void update(MatrixData &&matrix)
Adds new data in matrix form.
frequent_directions()
default constructor, only for serialization purpose
void update_row(InputRow &&input_row)
Adds one new row.
void update_range(RowRange &&row_range)
Adds new rows.
void compress()
Compress sketch.
frequent_directions(Matrix matrix, std::size_t const compress_size)
Creates sketch.
bool operator==(frequent_directions const &other) const
operator==
auto make_frequent_directions(Matrix matrix)
make for frequent_directions
void serialize(Archive &ar, const unsigned int version)
serialize
std::pair< Matrix const &, std::size_t > get_sketch()