-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiltered_matrix.cpp
More file actions
223 lines (182 loc) · 5.16 KB
/
filtered_matrix.cpp
File metadata and controls
223 lines (182 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <iostream>
#include <vector>
#include <boost/iterator/permutation_iterator.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/range.hpp>
#include <boost/assign.hpp>
using boost::assign::operator+=;
#if 0
template<class T>
class Matrix {
private:
typedef std::vector< std::vector<T> > Data;
public:
typedef std::vector<T> Row;
typedef typename Data::const_iterator IteratorByRows;
Matrix()
{}
Matrix(size_t nRows, size_t nColumns)
: data_(nRows, Row(nColumns, static_cast<T>(0)))
{}
size_t nRows() const
{
return data_.size();
}
size_t nColumns() const
{
if (nRows == 0) {
return 0;
} else {
return data_.at(0).size();
}
}
IteratorByRows rowsBegin() const
{
return data_.begin();
}
IteratorByRows rowsEnd() const
{
return data_.end();
}
const Row& rowAt(size_t index) const
{
return data_.at(index);
}
void addRow(const Row& row)
{
if (!data_.empty()) {
assert(row.size() == nColumns());
}
data_.push_back(row);
}
private:
Data data_;
};
#endif
namespace ublas = boost::numeric::ublas;
typedef double ValueType;
typedef ublas::matrix<ValueType> Matrix;
typedef ublas::vector<ValueType> Vector;
typedef Matrix::const_iterator1 ConstIteratorByRows;
typedef Matrix::const_iterator2 ConstIteratorByColumns;
typedef boost::iterator_range<ConstIteratorByColumns> ColumnsRange;
typedef std::vector<size_t> IndicesVector;
typedef IndicesVector::const_iterator IndexIterator;
typedef boost::permutation_iterator<
ConstIteratorByRows,
IndexIterator
> SelectedRowsIteratorBase;
class SelectedRowsIterator : public SelectedRowsIteratorBase {
public:
SelectedRowsIterator()
: itByRows_()
{}
SelectedRowsIterator(ConstIteratorByRows itByRows, IndexIterator itIndex)
: SelectedRowsIteratorBase(itByRows, itIndex),
itByRows_(itByRows)
{}
ConstIteratorByRows rowsIterator() const
{
return itByRows_ + *(base());
}
private:
ConstIteratorByRows itByRows_;
};
typedef boost::iterator_range<SelectedRowsIterator> SelectedRowsRange;
void prepareIndices(IndicesVector* indices,
size_t maxPossibleIndex = std::numeric_limits<size_t>::max())
{
BOOST_ASSERT(!indices->empty());
std::sort(indices->begin(), indices->end());
indices->erase(std::unique(indices->begin(), indices->end()), indices->end());
BOOST_ASSERT(indices->back() < maxPossibleIndex);
}
class MatrixWithSelection {
public:
MatrixWithSelection(const Matrix& matrix, const IndicesVector& rowsIndices)
: matrix_(matrix), rowsIndices_(rowsIndices)
{
prepareIndices(&rowsIndices_);
}
void addRow(const Vector&)
{
// add row
}
SelectedRowsIterator rowBegin() const
{
return SelectedRowsIterator(matrix_.begin1(), rowsIndices_.begin());
}
SelectedRowsIterator rowEnd() const
{
return SelectedRowsIterator(matrix_.begin1(), rowsIndices_.end());
}
const Matrix& matrix() const
{
return matrix_;
}
private:
Matrix matrix_;
IndicesVector rowsIndices_;
};
Matrix filterColumns(const Matrix& matrix, IndicesVector indicesToSave)
{
if (indicesToSave.empty()) {
return Matrix();
}
prepareIndices(&indicesToSave);
Matrix filteredMatrix(matrix.size1(), indicesToSave.size());
// perform filtering
return filteredMatrix;
}
template<class Iterator>
std::ostream& printRange(std::ostream& os, Iterator begin, Iterator end)
{
// std::copy(begin(), end(),
// std::ostream_iterator<typename std::iterator_traits<Iterator>::value_type>(os, " "));
for (Iterator it(begin); it != end; ++it) {
os << *it << " ";
}
os << "\n";
return os;
}
template<class Range>
std::ostream& printRange(std::ostream& os, const Range& range)
{
return printRange(os, range.begin(), range.end());
}
namespace {
int main_()
{
Matrix matrix(4, 3);
std::vector<double> linearData;
for (size_t i = 0; i < matrix.size1() * matrix.size2(); ++i) {
matrix(i / matrix.size2(), i % matrix.size2()) = static_cast<double>(i);
}
IndicesVector indices;
indices += 0, 1, 3;
MatrixWithSelection matrixWithSelection(matrix, indices);
std::cout << "The whole matrix is: \n";
for (ConstIteratorByRows it(matrixWithSelection.matrix().begin1());
it != matrixWithSelection.matrix().end1();
++it)
{
printRange(std::cout, it);
}
std::cout << "\n";
for (SelectedRowsIterator it(matrixWithSelection.rowBegin());
it != matrixWithSelection.rowEnd();
++it)
{
const ConstIteratorByRows itRow = it.rowsIterator();
std::cout << "Row index = " << itRow.index1() << "; ";
printRange(std::cout, itRow);
if (itRow.index1() != 0) {
std::cout << "Previous row index is " << (itRow.index1() - 1) << std::endl;
std::cout << "Here it is: ";
printRange(std::cout, itRow - 1);
}
}
return 0;
}
}