]> Dogcows Code - chaz/yoink/blob - src/cml/matrix/matrix_rowcol.h
testing new non-autotools build system
[chaz/yoink] / src / cml / matrix / matrix_rowcol.h
1 /* -*- C++ -*- ------------------------------------------------------------
2
3 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
4
5 The Configurable Math Library (CML) is distributed under the terms of the
6 Boost Software License, v1.0 (see cml/LICENSE for details).
7
8 *-----------------------------------------------------------------------*/
9 /** @file
10 * @brief Expressions to extract a row or column of a matrix.
11 */
12
13 #ifndef matrix_rowcol_h
14 #define matrix_rowcol_h
15
16 #include <cml/vector/vector_expr.h>
17 #include <cml/matrix/matrix_expr.h>
18
19 namespace cml {
20 namespace et {
21
22 template<class ExprT>
23 class MatrixRowOp
24 {
25 public:
26
27 typedef MatrixRowOp<ExprT> expr_type;
28
29 /* Record ary-ness of the expression: */
30 typedef unary_expression expr_ary;
31
32 /* Copy the expression by value into higher-up expressions: */
33 typedef expr_type expr_const_reference;
34
35 typedef typename ExprT::value_type value_type;
36 typedef vector_result_tag result_tag;
37 typedef typename ExprT::size_tag size_tag;
38
39 /* Store the expression traits: */
40 typedef ExprTraits<ExprT> expr_traits;
41
42 /* Get the reference type: */
43 typedef typename expr_traits::const_reference expr_reference;
44
45 /* Get the result vector type: */
46 typedef typename expr_traits::result_type::row_vector_type result_type;
47
48 /* Get the temporary type: */
49 typedef typename result_type::temporary_type temporary_type;
50
51
52 public:
53
54 /** Record result size as an enum. */
55 enum { array_size = result_type::array_size };
56
57
58 public:
59
60 /** Return the expression size as a pair. */
61 matrix_size size() const {
62 return expr_traits().rows(m_expr);
63 }
64
65 /** Return reference to contained expression. */
66 expr_reference expression() const { return m_expr; }
67
68 /** Compute value at index i of the row vector. */
69 value_type operator[](size_t i) const {
70 return expr_traits().get(m_expr,m_row,i);
71 }
72
73
74 public:
75
76 /** Construct from the subexpression to store. */
77 explicit MatrixRowOp(const ExprT& expr, size_t row)
78 : m_expr(expr), m_row(row) {}
79
80 /** Copy constructor. */
81 MatrixRowOp(const expr_type& e)
82 : m_expr(e.m_expr), m_row(e.m_row) {}
83
84
85 protected:
86
87 expr_reference m_expr;
88 const size_t m_row;
89
90
91 private:
92
93 /* Cannot be assigned to: */
94 expr_type& operator=(const expr_type&);
95 };
96
97 /** Expression traits class for MatrixRowOp<>. */
98 template<class ExprT>
99 struct ExprTraits< MatrixRowOp<ExprT> >
100 {
101 typedef MatrixRowOp<ExprT> expr_type;
102 typedef ExprT arg_type;
103
104 typedef typename expr_type::value_type value_type;
105 typedef typename expr_type::expr_const_reference const_reference;
106 typedef typename expr_type::result_tag result_tag;
107 typedef typename expr_type::size_tag size_tag;
108 typedef typename expr_type::result_type result_type;
109 typedef expr_node_tag node_tag;
110
111 value_type get(const expr_type& v, size_t i) const { return v[i]; }
112 size_t size(const expr_type& e) const { return e.size(); }
113 };
114
115 template<class ExprT>
116 class MatrixColOp
117 {
118 public:
119
120 typedef MatrixColOp<ExprT> expr_type;
121
122 /* Record ary-ness of the expression: */
123 typedef unary_expression expr_ary;
124
125 /* Copy the expression by value into higher-up expressions: */
126 typedef expr_type expr_const_reference;
127
128 typedef typename ExprT::value_type value_type;
129 typedef vector_result_tag result_tag;
130 typedef typename ExprT::size_tag size_tag;
131
132 /* Store the expression traits: */
133 typedef ExprTraits<ExprT> expr_traits;
134
135 /* Get the reference type: */
136 typedef typename expr_traits::const_reference expr_reference;
137
138 /* Get the result vector type: */
139 typedef typename expr_traits::result_type::col_vector_type result_type;
140
141 /* Get the temporary type: */
142 typedef typename result_type::temporary_type temporary_type;
143
144
145 public:
146
147 /** Record result size as an enum. */
148 enum { array_size = result_type::array_size };
149
150
151 public:
152
153 /** Return the expression size as a pair. */
154 matrix_size size() const {
155 return expr_traits().cols(m_expr);
156 }
157
158 /** Return reference to contained expression. */
159 expr_reference expression() const { return m_expr; }
160
161 /** Compute value at index i of the col vector. */
162 value_type operator[](size_t i) const {
163 return expr_traits().get(m_expr,i,m_col);
164 }
165
166
167 public:
168
169 /** Construct from the subexpression to store. */
170 explicit MatrixColOp(const ExprT& expr, size_t col)
171 : m_expr(expr), m_col(col) {}
172
173 /** Copy constructor. */
174 MatrixColOp(const expr_type& e)
175 : m_expr(e.m_expr), m_col(e.m_col) {}
176
177
178 protected:
179
180 expr_reference m_expr;
181 const size_t m_col;
182
183
184 private:
185
186 /* Cannot be assigned to: */
187 expr_type& operator=(const expr_type&);
188 };
189
190 /** Expression traits class for MatrixColOp<>. */
191 template<class ExprT>
192 struct ExprTraits< MatrixColOp<ExprT> >
193 {
194 typedef MatrixColOp<ExprT> expr_type;
195 typedef ExprT arg_type;
196
197 typedef typename expr_type::value_type value_type;
198 typedef typename expr_type::expr_const_reference const_reference;
199 typedef typename expr_type::result_tag result_tag;
200 typedef typename expr_type::size_tag size_tag;
201 typedef typename expr_type::result_type result_type;
202 typedef expr_node_tag node_tag;
203
204 value_type get(const expr_type& v, size_t i) const { return v[i]; }
205 size_t size(const expr_type& e) const { return e.size(); }
206 };
207
208 } // namespace et
209
210 /* Define the row and column operators in the cml namespace: */
211
212 /** Matrix row operator taking a matrix operand. */
213 template<typename E, class AT, typename BO, typename L>
214 et::VectorXpr< et::MatrixRowOp< matrix<E,AT,BO,L> > >
215 row(const matrix<E,AT,BO,L>& expr, size_t i)
216 {
217 typedef et::MatrixRowOp< matrix<E,AT,BO,L> > ExprT;
218 return et::VectorXpr<ExprT>(ExprT(expr,i));
219 }
220
221 /** Matrix row operator taking an et::MatrixXpr operand.
222 *
223 * The parse tree is automatically compressed by hoisting the MatrixXpr's
224 * subexpression into the subexpression of the MatrixRowOp.
225 */
226 template<class XprT>
227 et::VectorXpr< et::MatrixRowOp<XprT> >
228 row(const et::MatrixXpr<XprT>& expr, size_t i)
229 {
230 typedef et::MatrixRowOp<XprT> ExprT;
231 return et::MatrixXpr<ExprT>(ExprT(expr.expression(),i));
232 }
233
234 /** Matrix col operator taking a matrix operand. */
235 template<typename E, class AT, typename BO, typename L>
236 et::VectorXpr< et::MatrixColOp< matrix<E,AT,BO,L> > >
237 col(const matrix<E,AT,BO,L>& expr, size_t i)
238 {
239 typedef et::MatrixColOp< matrix<E,AT,BO,L> > ExprT;
240 return et::VectorXpr<ExprT>(ExprT(expr,i));
241 }
242
243 /** Matrix col operator taking an et::MatrixXpr operand.
244 *
245 * The parse tree is automatically compressed by hoisting the MatrixXpr's
246 * subexpression into the subexpression of the MatrixColOp.
247 */
248 template<class XprT>
249 et::VectorXpr< et::MatrixColOp<XprT> >
250 col(const et::MatrixXpr<XprT>& expr, size_t i)
251 {
252 typedef et::MatrixColOp<XprT> ExprT;
253 return et::VectorXpr<ExprT>(ExprT(expr.expression(),i));
254 }
255
256 } // namespace cml
257
258 #endif
259
260 // -------------------------------------------------------------------------
261 // vim:ft=cpp
This page took 0.043712 seconds and 4 git commands to generate.