1 | #ifndef _theplu_yat_utility_kernel_matrix_ |
---|
2 | #define _theplu_yat_utility_kernel_matrix_ |
---|
3 | |
---|
4 | // $Id: KernelMatrix.h 3190 2014-03-30 07:47:40Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2014 Peter Johansson |
---|
8 | |
---|
9 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
10 | |
---|
11 | The yat library is free software; you can redistribute it and/or |
---|
12 | modify it under the terms of the GNU General Public License as |
---|
13 | published by the Free Software Foundation; either version 3 of the |
---|
14 | License, or (at your option) any later version. |
---|
15 | |
---|
16 | The yat library is distributed in the hope that it will be useful, |
---|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
19 | General Public License for more details. |
---|
20 | |
---|
21 | You should have received a copy of the GNU General Public License |
---|
22 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "Container2DIterator.h" |
---|
26 | #include "StrideIterator.h" |
---|
27 | |
---|
28 | namespace theplu { |
---|
29 | namespace yat { |
---|
30 | namespace utility { |
---|
31 | |
---|
32 | /** |
---|
33 | \brief A KernelMatrix is a \ref concept_container_2d |
---|
34 | |
---|
35 | A matrix-like object constructed from two |
---|
36 | <a href="http://www.sgi.com/tech/stl/RandomAccessContainer.html"> |
---|
37 | |
---|
38 | RandomAccessContainer</a>s and Functor. Each row in the matrix |
---|
39 | correspond to an element in the RandomAccessRange1 and each |
---|
40 | column correspond to an element in RandomAccessRange2. Number of |
---|
41 | rows in the matrix equals number of element in RandomAccessRange1 |
---|
42 | and number of columns equals number of elements in |
---|
43 | RandomAccessRange2. An element in row \a row and column \a column |
---|
44 | is calculated by taking the element \a row in RandomAccessRange1 |
---|
45 | and element \a column in RandomAccessRange2, and passing these |
---|
46 | elements to BinaryFunctor. |
---|
47 | |
---|
48 | Type Requirements: |
---|
49 | - \c RandomAccessContainer1 must be a |
---|
50 | <a href="http://www.sgi.com/tech/stl/RandomAccessContainer.html"> |
---|
51 | RandomAccessContainer</a> |
---|
52 | - \c RandomAccessContainer2 must be a |
---|
53 | <a href="http://www.sgi.com/tech/stl/RandomAccessContainer.html"> |
---|
54 | RandomAccessContainer</a> |
---|
55 | - \c BinaryFunctor must be a |
---|
56 | <a href="http://www.sgi.com/tech/stl/AdaptableBinaryFunction.html"> |
---|
57 | AdaptableBinaryFunctor</a> |
---|
58 | - \c RandomAccessContainer1::reference must be convertible to |
---|
59 | \c BinaryFunctor::first_argument_type |
---|
60 | - \c RandomAccessContainer2::reference must be convertible to |
---|
61 | \c BinaryFunctor::second_argument_type |
---|
62 | */ |
---|
63 | template <class RandomAccessRange1, class RandomAccessRange2, |
---|
64 | class BinaryFunctor> |
---|
65 | class KernelMatrix |
---|
66 | { |
---|
67 | public: |
---|
68 | /// \c value_type is same as BinaryFunctor's result_type |
---|
69 | typedef typename BinaryFunctor::result_type value_type; |
---|
70 | |
---|
71 | /// \c const_reference is same as \c value_type |
---|
72 | typedef value_type const_reference; |
---|
73 | |
---|
74 | /// \brief \c const_iterator |
---|
75 | typedef Container2DIterator<const KernelMatrix, value_type, const_reference> |
---|
76 | const_iterator; |
---|
77 | |
---|
78 | /// \brief \c const_column_iterator |
---|
79 | typedef StrideIterator<const_iterator> const_column_iterator; |
---|
80 | |
---|
81 | /// \brief \c const_row_iterator_iterator |
---|
82 | typedef const_iterator const_row_iterator; |
---|
83 | |
---|
84 | /** |
---|
85 | \brief constructor |
---|
86 | |
---|
87 | Same as three-argument constructor, but using default |
---|
88 | constructor of class BinaryFunctor. |
---|
89 | */ |
---|
90 | KernelMatrix(const RandomAccessRange1& r1, const RandomAccessRange2& r2) |
---|
91 | : range1_(r1), range2_(r2) {} |
---|
92 | |
---|
93 | /** |
---|
94 | \brief constructor |
---|
95 | */ |
---|
96 | KernelMatrix(const RandomAccessRange1& r1, const RandomAccessRange2& r2, |
---|
97 | const BinaryFunctor& bf) |
---|
98 | : range1_(r1), range2_(r2), func_(bf) {} |
---|
99 | |
---|
100 | /** |
---|
101 | Iterator iterates along a row. When end of row is reached it |
---|
102 | jumps to beginning of next row. |
---|
103 | |
---|
104 | \return const_iterator pointing to upper-left element. |
---|
105 | */ |
---|
106 | const_iterator begin(void) const { return const_iterator(*this, 0, 0); } |
---|
107 | |
---|
108 | |
---|
109 | /** |
---|
110 | Iterator iterates along a column. |
---|
111 | |
---|
112 | \return const_iterator pointing to first element of column \a column. |
---|
113 | */ |
---|
114 | const_column_iterator begin_column(size_t column) const |
---|
115 | { |
---|
116 | return const_column_iterator(const_iterator(*this, 0, column), |
---|
117 | this->columns()); |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | /** |
---|
122 | Iterator iterates along a row. |
---|
123 | |
---|
124 | \return const_iterator pointing to first element of row \a row. |
---|
125 | */ |
---|
126 | const_row_iterator begin_row(size_t row) const |
---|
127 | { return const_row_iterator(*this, row, 0); } |
---|
128 | |
---|
129 | |
---|
130 | /** |
---|
131 | \return const_iterator pointing to end of matrix |
---|
132 | */ |
---|
133 | const_iterator end(void) const |
---|
134 | { return const_iterator(*this, rows(), 0); } |
---|
135 | |
---|
136 | |
---|
137 | /** |
---|
138 | \return const_iterator pointing to end of column \a i |
---|
139 | */ |
---|
140 | const_column_iterator end_column(size_t column) const |
---|
141 | { |
---|
142 | return const_column_iterator(const_iterator(*this, this->rows(), column), |
---|
143 | this->columns()); |
---|
144 | } |
---|
145 | |
---|
146 | |
---|
147 | /** |
---|
148 | \return const_iterator pointing to end of row \a i |
---|
149 | */ |
---|
150 | const_row_iterator end_row(size_t row) const |
---|
151 | { return const_row_iterator(*this, row, this->columns()); } |
---|
152 | |
---|
153 | |
---|
154 | /** |
---|
155 | Number of rows is defined by RandomAccessRange2::size(void) |
---|
156 | |
---|
157 | \return The number of columns in the matrix. |
---|
158 | */ |
---|
159 | typename RandomAccessRange2::size_type columns(void) const |
---|
160 | { return range2_.size(); } |
---|
161 | |
---|
162 | |
---|
163 | /** |
---|
164 | Number of rows is defined by RandomAccessRange1::size(void) |
---|
165 | |
---|
166 | \return The number of rows in the matrix. |
---|
167 | */ |
---|
168 | typename RandomAccessRange1::size_type rows(void) const |
---|
169 | { return range1_.size(); } |
---|
170 | |
---|
171 | |
---|
172 | /** |
---|
173 | \brief Element access operator. |
---|
174 | |
---|
175 | Element is calculated using BinaryFunctor passing |
---|
176 | \c row th element of RandomAccessRange1 and \c column th element in |
---|
177 | RandomAccessRange2. |
---|
178 | |
---|
179 | \return Reference to the element position (\a row, \a column). |
---|
180 | */ |
---|
181 | const_reference operator()(size_t row, size_t column) const |
---|
182 | { return func_(range1_[row], range2_[column]); } |
---|
183 | |
---|
184 | private: |
---|
185 | RandomAccessRange1 range1_; |
---|
186 | RandomAccessRange2 range2_; |
---|
187 | BinaryFunctor func_; |
---|
188 | }; |
---|
189 | |
---|
190 | }}} // of namespace utility, yat, and theplu |
---|
191 | |
---|
192 | #endif |
---|