STK++ 0.9.13
STK_BinaryOperators.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: 17 oct. 2012
28 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
36#ifndef STK_BINARYOPERATORS_H
37#define STK_BINARYOPERATORS_H
38
39#define EGAL(arg1, arg2) ((arg1::structure_ == int(Arrays::arg2)))
40
41namespace STK
42{
43// forward declaration
44template<typename FunctorOp, typename Lhs, typename Rhs>
45class BinaryOperator;
46
47namespace hidden
48{
52template<typename FunctorOp, typename Lhs, typename Rhs>
54{
55 enum
56 {
57 // find the kind of binary operator and the Structure using helper class BinaryEltImpl
59
60 isLhs1D_ = EGAL(Lhs,vector_)||EGAL(Lhs,point_)||EGAL(Lhs,diagonal_),
61 isRhs1D_ = EGAL(Rhs,vector_)||EGAL(Rhs,point_)||EGAL(Rhs,diagonal_),
62
63 isRhs2D_ = EGAL(Rhs,array2D_)||EGAL(Rhs,square_)||EGAL(Rhs,diagonal_)
64 ||EGAL(Rhs,lower_triangular_)||EGAL(Rhs,upper_triangular_)
65 ||EGAL(Rhs,symmetric_)||EGAL(Rhs,lower_symmetric_)||EGAL(Rhs,upper_symmetric_),
66 isLhs2D_ = EGAL(Lhs,array2D_)||EGAL(Lhs,square_)||EGAL(Lhs,diagonal_)
67 ||EGAL(Lhs,lower_triangular_)||EGAL(Lhs,upper_triangular_)
68 ||EGAL(Lhs,symmetric_)||EGAL(Lhs,lower_symmetric_)||EGAL(Lhs,upper_symmetric_),
69
70
71 isRes0D_ = EGAL(Lhs,number_) && EGAL(Rhs,number_),
72 isRes1D_ = (EGAL(Lhs,vector_)||EGAL(Lhs,point_)) && (EGAL(Rhs,vector_)||EGAL(Rhs,point_)),
73 isRes2D_ = isLhs2D_ && isRhs2D_,
74
75 is1D1D_ = isLhs1D_ && isRhs1D_,
76
77 // get the structure from the helper class BinaryEltImpl
79 orient_ = Lhs::orient_, // preserve the Lhs storage orientation. Could be optimized ?
82 storage_ = (Lhs::storage_ == int(Arrays::dense_)) || (Rhs::storage_ == int(Arrays::dense_))
84
87 };
90 typedef typename FunctorOp::result_type Type;
91 typedef typename FunctorOp::result_type TypeConst;
92};
93
94} // end namespace hidden
95
96
115template<typename FunctorOp, typename Lhs, typename Rhs>
116class BinaryOperator: public ExprBase< BinaryOperator<FunctorOp, Lhs, Rhs> >
117 , public TRef<1>
118{
119 public:
123
126
127 enum
128 {
133
139
140 // All the valid cases for binary operators
142 };
147
150 inline BinaryOperator( Lhs const& lhs, Rhs const& rhs, FunctorOp const& func = FunctorOp())
152 {
153 // FIXME : not safe. Add more test in the 1D case at compile time (and runtime ?)
156 && (int(Rhs::sizeCols_) != UnknownSize)
157 && (int(Lhs::sizeCols_) != int(Rhs::sizeCols_))
158 && (isRes2D_)
159 ));
161 && (int(Rhs::sizeRows_) != UnknownSize)
162 && (int(Lhs::sizeRows_) != int(Rhs::sizeRows_))
163 && (isRes2D_)
164 ));
165#ifdef STK_BOUNDS_CHECK
166 if ((lhs.rows() != rhs.rows()) && (isRes2D_))
168 if (( lhs.cols() != rhs.cols()) && (isRes2D_))
170#endif
171 }
172
174 inline Lhs const& lhs() const { return lhs_; }
176 inline Rhs const& rhs() const { return rhs_; }
178 inline FunctorOp const& functor() const { return functor_; }
179
181 inline TypeConst elt2Impl(int i, int j) const { return EltImpl::elt2Impl(functor_, lhs_, rhs_, i, j);}
183 inline TypeConst elt1Impl(int i) const { return EltImpl::elt1Impl(functor_, lhs_, rhs_, i);}
185 inline TypeConst elt0Impl() const { return EltImpl::elt0Impl(functor_, lhs_, rhs_);}
187 inline RowRange const& rowsImpl() const { return RowsImpl::rowsImpl(lhs_, rhs_);}
189 inline ColRange const& colsImpl() const { return ColsImpl::colsImpl(lhs_, rhs_);}
190
191 protected:
192 Lhs const& lhs_;
193 Rhs const& rhs_;
195};
196
197
198} // namespace STK
199
200#undef EGAL
201
202#endif /* STK_BINARYOPERATORS_H */
#define EGAL(arg1, arg2)
#define STKRUNTIME_ERROR_2ARG(Where, Arg1, Arg2, Error)
Definition STK_Macros.h:120
#define STK_STATIC_ASSERT_ROWS_DIMENSIONS_MISMATCH(COND)
#define STK_STATIC_ASSERT_COLS_DIMENSIONS_MISMATCH(COND)
#define STK_STATIC_ASSERT_BINARY_OPERATOR_MISMATCH(COND)
Generic expression where a binary operator is applied to two expressions.
BinaryOperator(Lhs const &lhs, Rhs const &rhs, FunctorOp const &func=FunctorOp())
default constructor
ColRange const & colsImpl() const
TypeConst elt1Impl(int i) const
TypeConst elt0Impl() const
hidden::BinaryEltImpl< FunctorOp, Lhs, Rhs, Lhs::structure_, Rhs::structure_ > EltImpl
hidden::BinaryRowsImpl< Lhs, Rhs, EltImpl::sizeRows_, EltImpl::useForRows_ > RowsImpl
TypeConst elt2Impl(int i, int j) const
FunctorOp const & functor() const
RowRange const & rowsImpl() const
hidden::BinaryColsImpl< Lhs, Rhs, EltImpl::sizeCols_, EltImpl::useForCols_ > ColsImpl
Lhs const & lhs() const
hidden::Traits< BinaryOperator >::Type Type
Rhs const & rhs() const
hidden::Traits< BinaryOperator >::TypeConst TypeConst
TRange< sizeCols_ > ColRange
Type of the Range for the columns.
TRange< sizeRows_ > RowRange
Type of the Range for the rows.
base class for template evaluation expressions and visitors.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
@ sparse_
sparse matrix/vector/array/expression
@ dense_
dense matrix/vector/array/expression
const int UnknownSize
This value means that an integer is not known at compile-time, and that instead the value is stored i...
The namespace STK is the main domain space of the Statistical ToolKit project.
Base class for all referencing containers.
implement the access to the columns of the BinaryOperator Possible cases are:
Helper class giving the Structure of a binary operator.
implement the access to the rows of the BinaryOperator Possible cases are:
ColOperator< BinaryOperator< FunctorOp, Lhs, Rhs > > Col
RowOperator< BinaryOperator< FunctorOp, Lhs, Rhs > > Row