1 /* -*- C++ -*- ------------------------------------------------------------
3 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
5 The Configurable Math Library (CML) is distributed under the terms of the
6 Boost Software License, v1.0 (see cml/LICENSE for details).
8 *-----------------------------------------------------------------------*/
10 * @brief Expressions to extract a row or column of a matrix.
13 #ifndef matrix_rowcol_h
14 #define matrix_rowcol_h
16 #include <cml/vector/vector_expr.h>
17 #include <cml/matrix/matrix_expr.h>
27 typedef MatrixRowOp
<ExprT
> expr_type
;
29 /* Record ary-ness of the expression: */
30 typedef unary_expression expr_ary
;
32 /* Copy the expression by value into higher-up expressions: */
33 typedef expr_type expr_const_reference
;
35 typedef typename
ExprT::value_type value_type
;
36 typedef vector_result_tag result_tag
;
37 typedef typename
ExprT::size_tag size_tag
;
39 /* Store the expression traits: */
40 typedef ExprTraits
<ExprT
> expr_traits
;
42 /* Get the reference type: */
43 typedef typename
expr_traits::const_reference expr_reference
;
45 /* Get the result vector type: */
46 typedef typename
expr_traits::result_type::row_vector_type result_type
;
48 /* Get the temporary type: */
49 typedef typename
result_type::temporary_type temporary_type
;
54 /** Record result size as an enum. */
55 enum { array_size
= result_type::array_size
};
60 /** Return the expression size as a pair. */
61 matrix_size
size() const {
62 return expr_traits().rows(m_expr
);
65 /** Return reference to contained expression. */
66 expr_reference
expression() const { return m_expr
; }
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
);
76 /** Construct from the subexpression to store. */
77 explicit MatrixRowOp(const ExprT
& expr
, size_t row
)
78 : m_expr(expr
), m_row(row
) {}
80 /** Copy constructor. */
81 MatrixRowOp(const expr_type
& e
)
82 : m_expr(e
.m_expr
), m_row(e
.m_row
) {}
87 expr_reference m_expr
;
93 /* Cannot be assigned to: */
94 expr_type
& operator=(const expr_type
&);
97 /** Expression traits class for MatrixRowOp<>. */
99 struct ExprTraits
< MatrixRowOp
<ExprT
> >
101 typedef MatrixRowOp
<ExprT
> expr_type
;
102 typedef ExprT arg_type
;
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
;
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(); }
115 template<class ExprT
>
120 typedef MatrixColOp
<ExprT
> expr_type
;
122 /* Record ary-ness of the expression: */
123 typedef unary_expression expr_ary
;
125 /* Copy the expression by value into higher-up expressions: */
126 typedef expr_type expr_const_reference
;
128 typedef typename
ExprT::value_type value_type
;
129 typedef vector_result_tag result_tag
;
130 typedef typename
ExprT::size_tag size_tag
;
132 /* Store the expression traits: */
133 typedef ExprTraits
<ExprT
> expr_traits
;
135 /* Get the reference type: */
136 typedef typename
expr_traits::const_reference expr_reference
;
138 /* Get the result vector type: */
139 typedef typename
expr_traits::result_type::col_vector_type result_type
;
141 /* Get the temporary type: */
142 typedef typename
result_type::temporary_type temporary_type
;
147 /** Record result size as an enum. */
148 enum { array_size
= result_type::array_size
};
153 /** Return the expression size as a pair. */
154 matrix_size
size() const {
155 return expr_traits().cols(m_expr
);
158 /** Return reference to contained expression. */
159 expr_reference
expression() const { return m_expr
; }
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
);
169 /** Construct from the subexpression to store. */
170 explicit MatrixColOp(const ExprT
& expr
, size_t col
)
171 : m_expr(expr
), m_col(col
) {}
173 /** Copy constructor. */
174 MatrixColOp(const expr_type
& e
)
175 : m_expr(e
.m_expr
), m_col(e
.m_col
) {}
180 expr_reference m_expr
;
186 /* Cannot be assigned to: */
187 expr_type
& operator=(const expr_type
&);
190 /** Expression traits class for MatrixColOp<>. */
191 template<class ExprT
>
192 struct ExprTraits
< MatrixColOp
<ExprT
> >
194 typedef MatrixColOp
<ExprT
> expr_type
;
195 typedef ExprT arg_type
;
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
;
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(); }
210 /* Define the row and column operators in the cml namespace: */
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
)
217 typedef et::MatrixRowOp
< matrix
<E
,AT
,BO
,L
> > ExprT
;
218 return et::VectorXpr
<ExprT
>(ExprT(expr
,i
));
221 /** Matrix row operator taking an et::MatrixXpr operand.
223 * The parse tree is automatically compressed by hoisting the MatrixXpr's
224 * subexpression into the subexpression of the MatrixRowOp.
227 et::VectorXpr
< et::MatrixRowOp
<XprT
> >
228 row(const et::MatrixXpr
<XprT
>& expr
, size_t i
)
230 typedef et::MatrixRowOp
<XprT
> ExprT
;
231 return et::MatrixXpr
<ExprT
>(ExprT(expr
.expression(),i
));
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
)
239 typedef et::MatrixColOp
< matrix
<E
,AT
,BO
,L
> > ExprT
;
240 return et::VectorXpr
<ExprT
>(ExprT(expr
,i
));
243 /** Matrix col operator taking an et::MatrixXpr operand.
245 * The parse tree is automatically compressed by hoisting the MatrixXpr's
246 * subexpression into the subexpression of the MatrixColOp.
249 et::VectorXpr
< et::MatrixColOp
<XprT
> >
250 col(const et::MatrixXpr
<XprT
>& expr
, size_t i
)
252 typedef et::MatrixColOp
<XprT
> ExprT
;
253 return et::VectorXpr
<ExprT
>(ExprT(expr
.expression(),i
));
260 // -------------------------------------------------------------------------