1 | #ifndef _theplu_yat_normalizer_row_normalizer_ |
---|
2 | #define _theplu_yat_normalizer_rows_normalizer_ |
---|
3 | |
---|
4 | // $Id: RowNormalizer.h 1579 2008-10-14 20:00:43Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2008 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 "yat/utility/Matrix.h" |
---|
26 | #include "yat/utility/yat_assert.h" |
---|
27 | |
---|
28 | #include <stdexcept> |
---|
29 | |
---|
30 | namespace theplu { |
---|
31 | namespace yat { |
---|
32 | namespace normalizer { |
---|
33 | |
---|
34 | /** |
---|
35 | \brief Using a functor T to normalize each column. |
---|
36 | |
---|
37 | If the underlying normalizer_type allows normalization "in |
---|
38 | place", i.e., it allows input range and result range to be the |
---|
39 | same range, then it is safe to have same container for input as |
---|
40 | output. |
---|
41 | |
---|
42 | In the case of views and lookups it is more complicated. The |
---|
43 | assignment is done sequentially, so if the input container and |
---|
44 | result container have the same underlying data, the normalization |
---|
45 | is typically not safe. |
---|
46 | |
---|
47 | \since New in yat 0.5 |
---|
48 | |
---|
49 | \see ColumnNormalizer |
---|
50 | */ |
---|
51 | template<class T> |
---|
52 | class RowNormalizer |
---|
53 | { |
---|
54 | public: |
---|
55 | /** |
---|
56 | functor used to normalize each column |
---|
57 | */ |
---|
58 | typedef T normalizer_type; |
---|
59 | |
---|
60 | /** |
---|
61 | \brief Default constructor |
---|
62 | */ |
---|
63 | RowNormalizer(void) {} |
---|
64 | |
---|
65 | /** |
---|
66 | \brief Constructor taking a functor \a norm |
---|
67 | */ |
---|
68 | RowNormalizer(T norm) |
---|
69 | : normalizer_(norm) {} |
---|
70 | |
---|
71 | /** |
---|
72 | Each row in \a matrix is normalized using class T, and |
---|
73 | assigned to Matrix \a result. |
---|
74 | |
---|
75 | Template argument Container2D1 should be a class modelling the |
---|
76 | concept \ref concept_container_2d. The template argument |
---|
77 | Container2D2 should be a class modelling the concept \ref |
---|
78 | concept_mutable_container_2d. |
---|
79 | |
---|
80 | \note \a result must have same dimensions as \a matrix. |
---|
81 | */ |
---|
82 | template<class Container2D1, class Container2D2> |
---|
83 | void operator()(const Container2D1& matrix, |
---|
84 | Container2D2& result) const |
---|
85 | { |
---|
86 | using utility::yat_assert; |
---|
87 | yat_assert<std::runtime_error>(matrix.rows()==result.rows(), |
---|
88 | "RowNormalizer: rows mismatch"); |
---|
89 | yat_assert<std::runtime_error>(matrix.columns()==result.columns(), |
---|
90 | "RowNormalizer: columns mismatch"); |
---|
91 | for (size_t i=0; i<matrix.rows(); ++i) |
---|
92 | normalizer_(matrix.begin_row(i), matrix.end_row(i), |
---|
93 | result.begin_row(i)); |
---|
94 | } |
---|
95 | |
---|
96 | private: |
---|
97 | T normalizer_; |
---|
98 | }; |
---|
99 | |
---|
100 | }}} // end of namespace normalizer, yat and thep |
---|
101 | #endif |
---|