STK++ 0.9.13
STK_ExprBaseFunctors.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff, Université Lille 1, Inria
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23 */
24
25/*
26 * Project: stkpp::Arrays
27 * created on: 27 sept. 2012
28 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
35#ifndef STK_EXPRBASEFUNCTORS_H
36#define STK_EXPRBASEFUNCTORS_H
37
38#include "STK_CArrayPoint.h"
39#include "STK_CArrayVector.h"
40
41namespace STK
42{
43
44// forward declaration
45template<typename Derived, typename Functor> struct ApplyFunctorByCol;
46template<typename Derived, typename Functor> struct ApplyFunctorByRow;
47template<typename Derived, typename Functor> struct ApplyFunctor;
48template<typename Derived, typename Functor> struct ApplyWeightedFunctorByCol;
49template<typename Derived, typename Functor> struct ApplyWeightedFunctorByRow;
50template<typename Derived, typename Functor> struct ApplyWeightedFunctor;
51
52namespace hidden
53{
60template<typename Derived, template<class> class Functor, bool isVector_>
62
63
68template<class Derived, template<class> class Functor>
83
88template<class Derived, template<class> class Functor>
99
105template<typename Derived, template<class> class Functor>
107{
108 enum
109 {
110 isVector_ = ( Derived::structure_ == int(Arrays::vector_)
111 || Derived::structure_ == int(Arrays::point_)
112 || Derived::structure_ == int(Arrays::number_)
113 )
114 };
115
116 typedef typename FunctorSelector<Derived, Functor, (bool)isVector_>::ColOp ColOp;
117 typedef typename FunctorSelector<Derived, Functor, (bool)isVector_>::ColWeightedOp ColWeightedOp;
118 typedef typename FunctorSelector<Derived, Functor, (bool)isVector_>::RowOp RowOp;
119 typedef typename FunctorSelector<Derived, Functor, (bool)isVector_>::RowWeightedOp RowWeightedOp;
120
121 typedef typename ColOp::Row Row;
122 typedef typename RowOp::Col Col;
123};
124
125} // namespace hidden
126
130template<typename Derived, typename Functor>
132{
133 typedef typename Derived::Type Type;
134 enum
135 {
136 sizeRows_ = Derived::sizeRows_,
137 sizeCols_ = Derived::sizeCols_
138 };
140
143 : lhs_(lhs.asDerived())
144 , res_(lhs_.cols())
145 {}
148 {
149 for (int j= lhs_.beginCols(); j < lhs_.endCols(); ++j)
150 { res_[j] = Functor(lhs_.col(j))();}
151 return res_;
152 }
154 Row operator()(bool option)
155 {
156 for (int j= lhs_.beginCols(); j < lhs_.endCols(); ++j)
157 { res_[j] = Functor(lhs_.col(j))( option);}
158 return res_;
159 }
161 template<class OtherRow>
162 Row operator()(OtherRow const& value, bool option)
163 {
164#ifdef STK_BOUNDS_CHECK
165 if (lhs_.cols() != value.range())
166 STKRUNTIME_ERROR_NO_ARG(ApplyFunctorByCol::operator(value,option),lhs_.cols()!=value.range());
167#endif
168 for (int j= lhs_.beginCols(); j < lhs_.endCols(); ++j)
169 { res_[j] = Functor(lhs_.col(j))(value[j], option);}
170 return res_;
171 }
172 protected:
173 Derived const& lhs_;
175};
176
181template<typename Derived, typename Functor>
183{
184 typedef typename Derived::Type Type;
185 enum
186 {
187 sizeRows_ = Derived::sizeRows_,
188 sizeCols_ = Derived::sizeCols_
189 };
191
194 : lhs_(lhs.asDerived())
195 , res_(lhs_.cols())
196 {}
197 template<class Weights>
199 {
200 for (int j= lhs_.beginCols(); j < lhs_.endCols(); ++j)
201 { res_[j] = Functor(lhs_.col(j))(w);}
202 return res_;
203 }
204 template<class Weights>
205 Row operator()(ExprBase<Weights> const& w, bool option)
206 {
207 for (int j= lhs_.beginCols(); j < lhs_.endCols(); ++j)
208 { res_[j] = Functor(lhs_.col(j))(w, option);}
209 return res_;
210 }
211 template<class Weights>
212 Row operator()(ExprBase<Weights> const& w, Type const& value)
213 {
214 for (int j= lhs_.beginCols(); j < lhs_.endCols(); ++j)
215 { res_[j] = Functor(lhs_.col(j))(w, value);}
216 return res_;
217 }
218 template< class Weights, typename OtherRow>
219 Row operator()(ExprBase<Weights> const& w, ExprBase<OtherRow> const& value, bool option)
220 {
221#ifdef STK_BOUNDS_CHECK
222 if (lhs_.cols() != value.range())
223 STKRUNTIME_ERROR_NO_ARG(ApplyWeightedFunctorByCol::operator(w,value,option),lhs_.cols()!=value.range());
224#endif
225 for (int j= lhs_.beginCols(); j < lhs_.endCols(); ++j)
226 { res_[j] = Functor(lhs_.col(j))(w, value[j], option);}
227 return res_;
228 }
229 protected:
230 Derived const& lhs_;
232};
233
237template<typename Derived, typename Functor>
239{
240 typedef typename Derived::Type Type;
241 enum
242 {
243 sizeRows_ = Derived::sizeRows_,
244 sizeCols_ = Derived::sizeCols_
245 };
247
250 : lhs_(lhs.asDerived())
251 , res_(lhs_.rows())
252 {}
254 {
255 for (int i= lhs_.beginRows(); i < lhs_.endRows(); ++i)
256 { res_[i] = Functor(lhs_.row(i))();}
257 return res_;
258 }
259 Col operator()(bool option)
260 {
261 for (int j= lhs_.beginRows(); j < lhs_.endRows(); ++j)
262 { res_[j] = Functor(lhs_.row(j))( option);}
263 return res_;
264 }
265 template<class OtherCol>
266 Col operator()(OtherCol const& value, bool option)
267 {
268#ifdef STK_BOUNDS_CHECK
269 if (lhs_.rows() != value.range()) STKRUNTIME_ERROR_NO_ARG(ApplyFunctorByRow::operator(value,option),lhs_.rows()!=value.rows());
270#endif
271 for (int j= lhs_.beginRows(); j < lhs_.endRows(); ++j)
272 { res_[j] = Functor(lhs_.row(j))(value[j], option);}
273 return res_;
274 }
275 protected:
276 Derived const& lhs_;
278};
279
283template<typename Derived, typename Functor>
285{
286 typedef typename Derived::Type Type;
287 enum
288 {
289 sizeRows_ = Derived::sizeRows_,
290 sizeCols_ = Derived::sizeCols_
291 };
293
296 : lhs_(lhs.asDerived())
297 , res_(lhs_.rows())
298 {}
299 template<typename Weights>
301 {
302 for (int i= lhs_.beginRows(); i < lhs_.endRows(); ++i)
303 { res_[i] = Functor(lhs_.row(i))(w);}
304 return res_;
305 }
306 template<typename Weights>
307 Col operator()(ExprBase<Weights> const& w, bool option)
308 {
309 for (int j= lhs_.beginRows(); j < lhs_.endRows(); ++j)
310 { res_[j] = Functor(lhs_.row(j))(w, option);}
311 return res_;
312 }
313 template<typename Weights, typename OtherCol>
314 Col operator()(ExprBase<Weights> const& w, ExprBase<OtherCol> const& value, bool option)
315 {
316#ifdef STK_BOUNDS_CHECK
317 if (lhs_.rows() != value.range()) STKRUNTIME_ERROR_NO_ARG(ApplyFunctorByRow::operator(w,value,option),lhs_.rows()!=value.rows());
318#endif
319 for (int j= lhs_.beginRows(); j < lhs_.endRows(); ++j)
320 { res_[j] = Functor(lhs_.row(j))(w, value[j], option);}
321 return res_;
322 }
323 protected:
324 Derived const& lhs_;
326};
327
331template<typename Derived, typename Functor>
333{
334 typedef typename Derived::Type Type;
335 typedef Type Col;
336 typedef Type Row;
338 inline ApplyFunctor( ExprBase<Derived> const& lhs) : lhs_(lhs.asDerived())
341 inline Type operator()() { return Functor(lhs_)();}
343 inline Type operator()(bool option) { return Functor(lhs_)(option);}
345 inline Type operator()(Type const& value, bool option)
346 { return Functor(lhs_)(value, option);}
347
348 protected:
349 Derived const& lhs_;
350};
351
355template<typename Derived, typename Functor>
357{
358 typedef typename Derived::Type Type;
359 typedef Type Col;
360 typedef Type Row;
362 inline ApplyWeightedFunctor( ExprBase<Derived> const& lhs): lhs_(lhs.asDerived())
365 template<typename Weights>
366 inline Type operator()(ExprBase<Weights> const& w) { return Functor(lhs_)(w);}
368 template<typename Weights>
369 inline Type operator()(ExprBase<Weights> const& w, bool option)
370 { return Functor(lhs_)(w, option);}
372 template<typename Weights>
373 inline Type operator()(ExprBase<Weights> const& w, Type const& value, bool option)
374 { return Functor(lhs_)(w, value, option);}
375
376 protected:
377 Derived const& lhs_;
378};
379
380
381} // namespace STK
382
383#endif /* STK_EXPRBASEVISITOR_H */
In this file we implement the final class CArrayPoint.
In this file we implement the final class CArrayVector.
#define STKRUNTIME_ERROR_NO_ARG(Where, Error)
Definition STK_Macros.h:138
#define STK_STATIC_ASSERT_POINT_OR_VECTOR_ONLY(EXPR)
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
@ number_
(1,1) matrix/vector/array/expression (like a number)
@ point_
row oriented vector/array/expression
@ vector_
column oriented vector/array/expression
The namespace STK is the main domain space of the Statistical ToolKit project.
class allowing to apply the Functor Functor on each columns of an expression.
CArrayPoint< Type, sizeCols_ > Row
ApplyFunctorByCol(ExprBase< Derived > const &lhs)
constructor
Row operator()(OtherRow const &value, bool option)
class allowing to apply the Functor Functor on each rows of an expression.
Col operator()(OtherCol const &value, bool option)
CArrayVector< Type, sizeRows_ > Col
ApplyFunctorByRow(ExprBase< Derived > const &lhs)
constructor
class allowing applying the functor Functor on a vector or row-vector
Type operator()()
apply without argument
Type operator()(Type const &value, bool option)
apply with a value and an option argument
ApplyFunctor(ExprBase< Derived > const &lhs)
constructor
Type operator()(bool option)
apply with an option argument
class allowing to apply the weighted Functor Functor on each columns of an expression.
Row operator()(ExprBase< Weights > const &w, Type const &value)
ApplyWeightedFunctorByCol(ExprBase< Derived > const &lhs)
constructor
Row operator()(ExprBase< Weights > const &w)
Row operator()(ExprBase< Weights > const &w, bool option)
Row operator()(ExprBase< Weights > const &w, ExprBase< OtherRow > const &value, bool option)
CArrayPoint< Type, sizeCols_ > Row
class allowing to apply the Functor Functor on each rows of an expression.
Col operator()(ExprBase< Weights > const &w, bool option)
Col operator()(ExprBase< Weights > const &w, ExprBase< OtherCol > const &value, bool option)
Col operator()(ExprBase< Weights > const &w)
ApplyWeightedFunctorByRow(ExprBase< Derived > const &lhs)
constructor
CArrayVector< Type, sizeRows_ > Col
class allowing applying the weighted functor Functor on a vector or row-vector
ApplyWeightedFunctor(ExprBase< Derived > const &lhs)
constructor
Type operator()(ExprBase< Weights > const &w, Type const &value, bool option)
apply with weight, a value and an option argument
Type operator()(ExprBase< Weights > const &w, bool option)
apply with weight and an option argument
Type operator()(ExprBase< Weights > const &w)
apply with weights
ApplyWeightedFunctorByRow< Derived, ByRowFunctor > RowWeightedOp
ApplyWeightedFunctorByCol< Derived, ByColFunctor > ColWeightedOp
ApplyFunctorByCol< Derived, ByColFunctor > ColOp
ApplyFunctorByRow< Derived, ByRowFunctor > RowOp
ApplyWeightedFunctor< Derived, ByColFunctor > ColWeightedOp
ApplyWeightedFunctor< Derived, ByRowFunctor > RowWeightedOp
, Utility class that will select the type of operator to apply.
FunctorSelector< Derived, Functor,(bool) isVector_ >::RowWeightedOp RowWeightedOp
FunctorSelector< Derived, Functor,(bool) isVector_ >::ColOp ColOp
FunctorSelector< Derived, Functor,(bool) isVector_ >::RowOp RowOp
FunctorSelector< Derived, Functor,(bool) isVector_ >::ColWeightedOp ColWeightedOp