STK++ 0.9.13
STK_ExprBase.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: 13 oct. 2012
28 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
35#ifndef STK_EXPRBASE_H
36#define STK_EXPRBASE_H
37
38namespace STK
39{
40// forward declaration
41template<class Derived> class ExprBase;
42template<class Derived> class ArrayBase;
43}
44
45
48#include "STK_ArraysTraits.h"
49#include "STK_Arrays_Util.h"
50#include "STK_ITContainer.h"
51#include "STK_IContainerRef.h"
61
62// forward declarations
63namespace STK
64{
65template<class Derived> class ArrayInitializer;
66
67namespace hidden
68{
69 template<typename Lhs, typename Rhs, int LStructure_, int RStructure_> struct ProductSelector;
70 template<typename Lhs, typename Rhs, int operatorType_> struct OperatorSelector;
71} // namespace hidden
72
73} // namespace STK
74
75
77#define DEFINE_BINARY_OPERATOR(OPERATORNAME, BINARYOPERATORNAME) \
78template<typename Rhs> \
79typename hidden::OperatorSelector<Derived, Rhs, Arrays::BINARYOPERATORNAME>::Result const \
80OPERATORNAME( ExprBase<Rhs> const& other) const;
81
83#define MAKE_UNARY_OPERATOR_NOARG(FUNCTION, FUNCTOR) \
84 inline UnaryOperator<FUNCTOR<Type>, Derived> FUNCTION() const \
85 { return UnaryOperator<FUNCTOR<Type>, Derived>(this->asDerived()); }
86
88#define MAKE_UNARY_OPERATOR_1ARG(FUNCTION, FUNCTOR) \
89 inline UnaryOperator<FUNCTOR<Type>, Derived> FUNCTION(Type const& value) const \
90 { return UnaryOperator<FUNCTOR<Type>, Derived>(this->asDerived(), FUNCTOR<Type>(value)); }
91
93#define MAKE_RESHAPE_OPERATOR(OPERATOR, SHAPE) \
94 inline OPERATOR##Operator< Derived> const SHAPE() const \
95 { return OPERATOR##Operator< Derived>(this->asDerived()); }
96
97
98namespace STK
99{
100
112template<class Derived>
113class ExprBase: public ITContainer<Derived, hidden::Traits<Derived>::structure_>
114{
115 public:
116 enum
117 {
123 };
127
128 protected:
130 inline ExprBase(): Base() {}
132 inline ~ExprBase() {}
133
134 public:
135 //--------------
136 // Start Visitors
140 template<typename Visitor>
141 typename Visitor::TypeConst visit(Visitor& visitor) const;
149 int count() const;
157 bool const any() const;
165 bool const all() const;
167 int nbAvailableValues() const;
168
173 Type const minElt( int& row, int& col) const;
178 Type const minEltSafe( int& row, int& col) const;
183 Type const maxElt( int& row, int& col) const;
188 Type const maxEltSafe( int& row, int& col) const;
194 Type const minElt( int& pos) const;
200 Type const minEltSafe( int& pos) const;
206 Type const maxElt( int& pos) const;
212 Type const maxEltSafe( int& pos) const;
216 Type const minElt() const;
220 Type const minEltSafe() const;
224 Type const maxElt() const;
228 Type const maxEltSafe() const;
229
231 Type const sum() const;
233 Type const sumSafe() const;
234
236 Type const norm() const;
238 Type const normSafe() const;
240 Type const norm2() const;
242 Type const norm2Safe() const;
244 Type const normInf() const;
245
247 Type const mean() const;
249 Type const meanSafe() const;
251 Type const variance() const;
253 Type const varianceSafe() const;
255 Type const variance(Type const& mean) const;
257 Type const varianceSafe(Type const& mean) const;
258
262 template<typename Rhs>
263 Type const wsum(ExprBase<Rhs> const& weights) const;
267 template<typename Rhs>
268 Type const wsumSafe(ExprBase<Rhs> const& weights) const;
272 template<typename Rhs>
273 Type const wnorm(ExprBase<Rhs> const& weights) const;
277 template<typename Rhs>
278 Type const wnormSafe(ExprBase<Rhs> const& weights) const;
282 template<typename Rhs>
283 Type const wnorm2(ExprBase<Rhs> const& weights) const;
287 template<typename Rhs>
292 template<typename Rhs>
293 Type const wmean(ExprBase<Rhs> const& weights) const;
297 template<typename Rhs>
298 Type const wmeanSafe(ExprBase<Rhs> const& weights) const;
302 template<typename Rhs>
303 Type const wvariance(ExprBase<Rhs> const& weights) const;
307 template<typename Rhs>
312 template<typename Rhs>
313 Type const wvariance(Type const& mean, ExprBase<Rhs> const& weights) const;
317 template<typename Rhs>
318 Type const wvarianceSafe(Type const& mean, ExprBase<Rhs> const& weights) const;
319 // Visitors terminated
320 //--------------------
321 // BinaryOperators
323 DEFINE_BINARY_OPERATOR(operator==,equalOp_)
325 DEFINE_BINARY_OPERATOR(operator!=,notEqualOp_)
327 DEFINE_BINARY_OPERATOR(operator>,greaterThanOp_)
329 DEFINE_BINARY_OPERATOR(operator<,lessThanOp_)
331 DEFINE_BINARY_OPERATOR(operator>=,greaterThanOrEqualOp_)
333 DEFINE_BINARY_OPERATOR(operator<=,lessThanOrEqualOp_)
334
335
336 DEFINE_BINARY_OPERATOR(operator+,sumOp_)
338 DEFINE_BINARY_OPERATOR(operator-,differenceOp_)
340 DEFINE_BINARY_OPERATOR(prod,productOp_)
342 DEFINE_BINARY_OPERATOR(operator/,divisionOp_)
344 DEFINE_BINARY_OPERATOR(operator%,moduloOp_)
345
346
350
351
352 DEFINE_BINARY_OPERATOR(operator&&,logicalAndOp_)
354 DEFINE_BINARY_OPERATOR(operator||,logicalOrOp_)
355
356
357 DEFINE_BINARY_OPERATOR(operator&,bitwiseAndOp_)
359 DEFINE_BINARY_OPERATOR(operator|,bitwiseOrOp_)
361 DEFINE_BINARY_OPERATOR(operator^,bitwiseXorOp_)
362 // BinaryOperators terminated
363 //--------------------
364 // UnaryOperators
399
400 // boolean operations
413
418
429
434
441
446
448 inline UnaryOperator<SafeOp<Type>, Derived> const safe(Type const value = Type()) const
449 { return UnaryOperator<SafeOp<Type>, Derived>(this->asDerived(), SafeOp<Type>(value)); }
450
451 // friends
453 inline friend UnaryOperator<SumWithOp<Type>, Derived> const
454 operator+(Type const& value, ExprBase<Derived> const& other)
455 { return other.asDerived() + value;}
457 inline friend UnaryOperator<SubstractToOp<Type>, Derived > const
459 { return UnaryOperator<SubstractToOp<Type>, Derived>(other.asDerived(), SubstractToOp<Type>(value));}
461 inline friend UnaryOperator< ProductWithOp<Type>, Derived> const
463 { return other.asDerived()*value; }
464
466 template<typename OtherType>
467 inline UnaryOperator<CastOp<Type, OtherType>, Derived> const cast() const
468 { return UnaryOperator<CastOp<Type, OtherType>, Derived>(this->asDerived());}
469
470 // pdf, lpdf, cdf, icdf
472 inline UnaryOperator<Law::PdfOp<Type>, Derived> pdf( Law::IUnivLaw<Type> const& law) const
473 { return UnaryOperator<Law::PdfOp<Type>, Derived>(this->asDerived(), Law::PdfOp<Type>(law));}
476 { return UnaryOperator<Law::LogPdfOp<Type>, Derived>(this->asDerived(), Law::LogPdfOp<Type>(law));}
478 inline UnaryOperator<Law::CdfOp<Type>, Derived> cdf( Law::IUnivLaw<Type> const& law) const
479 { return UnaryOperator<Law::CdfOp<Type>, Derived>(this->asDerived(), Law::CdfOp<Type>(law));}
482 { return UnaryOperator<Law::LogCdfOp<Type>, Derived>(this->asDerived(), Law::LogCdfOp<Type>(law));}
484 inline UnaryOperator<Law::CdfcOp<Type>, Derived> cdfc( Law::IUnivLaw<Type> const& law) const
485 { return UnaryOperator<Law::CdfcOp<Type>, Derived>(this->asDerived(), Law::CdfcOp<Type>(law));}
488 { return UnaryOperator<Law::LogCdfcOp<Type>, Derived>(this->asDerived(), Law::LogCdfcOp<Type>(law));}
490 inline UnaryOperator<Law::IcdfOp<Type>, Derived> icdf( Law::IUnivLaw<Type> const& law) const
491 { return UnaryOperator<Law::IcdfOp<Type>, Derived>(this->asDerived(), Law::IcdfOp<Type>(law));}
492
493 // extension
495 template< template<typename> class OtherOperator>
496 inline UnaryOperator<OtherOperator<Type>, Derived> const funct0() const
497 { return UnaryOperator<OtherOperator<Type>, Derived>(this->asDerived());}
499 template< template<typename> class OtherOperator>
500 inline UnaryOperator<OtherOperator<Type>, Derived> const funct1(Type const value) const
501 { return UnaryOperator<OtherOperator<Type>, Derived>(this->asDerived(), OtherOperator<Type>(value));}
502
503 // reshape operations
520
521 // slice operators
523 template<int Size_>
525 {
527 return SubVectorOperator<Derived, Size_>(this->asDerived(), I);
528 }
529
531 inline ColOperator<Derived> const col(int j) const
532 { return ColOperator<Derived> (this->asDerived(), j);}
534 inline RowOperator<Derived> const row(int i) const
535 { return RowOperator<Derived> (this->asDerived(), i);}
537 template<int Size_>
539 { return SubOperator<Derived, sizeRows_, Size_> (this->asDerived(), this->rows(), J);}
541 template<int Size_>
543 { return SubOperator<Derived, Size_, sizeCols_> (this->asDerived(), I, this->cols());}
544
546 template<int SizeRows_, int SizeCols_>
552
553 // dot operations
557 template<class Rhs>
559 dot(ExprBase<Rhs> const& other) const;
563 template<class Rhs>
565 dotSafe(ExprBase<Rhs> const& other) const;
566
568 template<typename Rhs>
570 operator*( ExprBase<Rhs> const& other) const;
571};
572
573} // namespace STK
574
575#undef DEFINE_BINARY_OPERATOR
576#undef MAKE_UNARY_OPERATOR_NOARG
577#undef MAKE_UNARY_OPERATOR_1ARG
578#undef MAKE_RESHAPE_OPERATOR
579
580#include "STK_ExprBaseVisitor.h"
581#include "STK_ExprBaseDot.h"
582#include "STK_ExprBaseProduct.h"
584
585
586#endif /* STK_EXPRBASE_H_ */
#define MAKE_RESHAPE_OPERATOR(OPERATOR, SHAPE)
utility macro allowing to construct unary operators
In this file we define the main traits class we use for the STK++ Containers.
In this file we define utilities functions and enum for the Array classes.
In this file we implement the Binary Implementation helper classes.
In this file we implement the BinaryOperator class.
In this file we implement the DotProduct class.
In this file we define the dot product and its particular cases.
In this file we define the matrix-matrix product and its particular cases.
In this file we define the Visitors for ExprBase.
#define MAKE_UNARY_OPERATOR_NOARG(FUNCTION, FUNCTOR)
utility macro allowing to construct unary operators
#define MAKE_UNARY_OPERATOR_1ARG(FUNCTION, FUNCTOR)
utility macro allowing to construct unary operators with one argument
#define DEFINE_BINARY_OPERATOR(OPERATORNAME, BINARYOPERATORNAME)
utility macro allowing to define binary operators
This is an internal header file, included by other Containers library headers.
This is an internal header file, included by other Containers library headers.
In this file we implement the distribution functors (pdf, lpdf, cdf, icdf) used by arrays.
In this file we define the interface base class IUnivLaw for all probabilities laws.
In this file we select the product method to use.
In this file we implement the ProductOperator class.
In this file we implement the DiagonalizeOperator, DiagonalGetterOperator, UpperTriangularizeOperator...
In this file we implement the RowOperator, ColOperator, SubVectorOperator and SubOperator classes.
#define STK_STATIC_ASSERT_ONE_DIMENSION_ONLY(EXPR)
#define STK_STATIC_ASSERT_TWO_DIMENSIONS_ONLY(EXPR)
In this file we implement the TransposeOperator class.
In this file we implement the UnaryOperator class.
base class for template evaluation expressions and visitors.
UnaryOperator< CosOp< Type >, Derived > cos() const
SubOperator< Derived, Size_, sizeCols_ > const row(TRange< Size_ > const &I) const
UnaryOperator< NegOp< Type >, Derived > neg() const
friend UnaryOperator< ProductWithOp< Type >, Derived > const operator*(Type const value, ExprBase< Derived > const &other)
UnaryOperator< TanOp< Type >, Derived > tan() const
UnaryOperator< LogOp< Type >, Derived > log() const
TransposeOperator< Derived > const transpose() const
Type const varianceSafe() const
Type const norm() const
UnaryOperator< Law::PdfOp< Type >, Derived > pdf(Law::IUnivLaw< Type > const &law) const
compute pdf values to this using distribution law given by user
ITContainer< Derived, structure_ > Base
friend UnaryOperator< SubstractToOp< Type >, Derived > const operator-(Type const value, ExprBase< Derived > const &other)
hidden::Traits< Derived >::TypeConst TypeConst
bool const any() const
check if there is any non-zero element in an expression.
Type const sum() const
UnaryOperator< AbsOp< Type >, Derived > abs() const
Type const wmean(ExprBase< Rhs > const &weights) const
UnaryOperator< Law::IcdfOp< Type >, Derived > icdf(Law::IUnivLaw< Type > const &law) const
compute inverse cumulative distribution function using distribution law given by user
hidden::OperatorSelector< Derived, Rhs, Arrays::maxOp_ >::Result const max(ExprBase< Rhs > const &other) const
RowOperator< Derived > const row(int i) const
bool const all() const
check if all the elements in an expression are not zero.
friend UnaryOperator< SumWithOp< Type >, Derived > const operator+(Type const &value, ExprBase< Derived > const &other)
Type const wnorm2(ExprBase< Rhs > const &weights) const
DiagonalizeOperator< Derived > const diagonalize() const
LowerTriangularizeOperator< Derived > const lowerTriangularize() const
DiagonalGetterOperator< Derived > const getDiagonal() const
Type const meanSafe() const
Type const minElt() const
Type const wnorm2Safe(ExprBase< Rhs > const &weights) const
Type const wvarianceSafe(Type const &mean, ExprBase< Rhs > const &weights) const
Type const variance() const
UnaryOperator< SafeOp< Type >, Derived > const safe(Type const value=Type()) const
Type const wvariance(Type const &mean, ExprBase< Rhs > const &weights) const
UnaryOperator< SqrtOp< Type >, Derived > sqrt() const
int count() const
compute the value of non-zero element in an expression.
Type const sumSafe() const
UnaryOperator< SafeInverseOp< Type >, Derived > safeInverse(Type const &value) const
UnaryOperator< CastOp< Type, OtherType >, Derived > const cast() const
UnaryOperator< CubeOp< Type >, Derived > cube() const
hidden::Traits< Derived >::Type Type
UnaryOperator< PowOp< Type >, Derived > pow(Type const &value) const
int nbAvailableValues() const
UnaryOperator< IsFiniteOp< Type >, Derived > isFinite() const
UpperTriangularizeOperator< Derived > const upperTriangularize() const
Visitor::TypeConst visit(Visitor &visitor) const
Visit the container using a constant visitor.
UnaryOperator< IsInfiniteOp< Type >, Derived > isInfinite() const
Type const maxElt() const
Type const minEltSafe() const
Type const wmeanSafe(ExprBase< Rhs > const &weights) const
Type const norm2() const
hidden::OperatorSelector< Derived, Rhs, Arrays::productOp_ >::Result const prod(ExprBase< Rhs > const &other) const
hidden::Promote< Type, typenameRhs::Type >::result_type const dotSafe(ExprBase< Rhs > const &other) const
UnaryOperator< AsinOp< Type >, Derived > asin() const
hidden::Promote< Type, typenameRhs::Type >::result_type const dot(ExprBase< Rhs > const &other) const
UnaryOperator< SquareOp< Type >, Derived > square() const
UnaryOperator< OtherOperator< Type >, Derived > const funct1(Type const value) const
Type const normInf() const
ColOperator< Derived > const col(int j) const
UnaryOperator< Law::LogPdfOp< Type >, Derived > lpdf(Law::IUnivLaw< Type > const &law) const
compute log-pdf values to this using distribution law given by user
SubOperator< Derived, sizeRows_, Size_ > const col(TRange< Size_ > const &J) const
SubVectorOperator< Derived, Size_ > const sub(TRange< Size_ > const &I) const
SubOperator< Derived, SizeRows_, SizeCols_ > const sub(TRange< SizeRows_ > const &I, TRange< SizeCols_ > const &J) const
Type const wnormSafe(ExprBase< Rhs > const &weights) const
UnaryOperator< OtherOperator< Type >, Derived > const funct0() const
Type const wvarianceSafe(ExprBase< Rhs > const &weights) const
UnaryOperator< AcosOp< Type >, Derived > acos() const
UnaryOperator< IsNaOp< Type >, Derived > isNA() const
UnaryOperator< Law::LogCdfOp< Type >, Derived > lcdf(Law::IUnivLaw< Type > const &law) const
compute log-cumulative distribution function of this using distribution law given by user
Type const wnorm(ExprBase< Rhs > const &weights) const
SymmetrizeOperator< Derived > const symmetrize() const
ExprBase()
Default constructor.
UnaryOperator< InverseOp< Type >, Derived > inverse() const
UnaryOperator< Law::LogCdfcOp< Type >, Derived > lcdfc(Law::IUnivLaw< Type > const &law) const
compute complementary cumulative distribution function of this using distribution law given by user
hidden::OperatorSelector< Derived, Rhs, Arrays::minOp_ >::Result const min(ExprBase< Rhs > const &other) const
UpperSymmetrizeOperator< Derived > const upperSymmetrize() const
UnaryOperator< Law::CdfcOp< Type >, Derived > cdfc(Law::IUnivLaw< Type > const &law) const
compute complementary cumulative distribution function of this using distribution law given by user
UnaryOperator< Law::CdfOp< Type >, Derived > cdf(Law::IUnivLaw< Type > const &law) const
compute cumulative distribution function of this using distribution law given by user
UnaryOperator< SinOp< Type >, Derived > sin() const
LowerSymmetrizeOperator< Derived > const lowerSymmetrize() const
Type const normSafe() const
~ExprBase()
destructor
Type const wvariance(ExprBase< Rhs > const &weights) const
UnaryOperator< ExpOp< Type >, Derived > exp() const
Type const wsum(ExprBase< Rhs > const &weights) const
Type const norm2Safe() const
Type const mean() const
Type const wsumSafe(ExprBase< Rhs > const &weights) const
Type const maxEltSafe() const
Specialized interface class for all arrays (can be either 2D arrays or 1D arrays).
Interface base class for all the univariate distributions.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Generic expression when the sub-part of an expression is accessed (specialization for vectors)
Index sub-vector region with fixed size.
Definition STK_Range.h:161
Generic expression when unary operator is applied to an expression.
The namespace STK is the main domain space of the Statistical ToolKit project.
Template functor which compute the absolute value of a number.
Template functor which compute the arc cosine of a number.
Template functor which compute the arc sine of a number.
Template functor to compute the bitwise and with a fixed value.
Template functor to compute the bitwise or with a fixed value.
Template functor to compute the bitwise xor with a fixed value.
Template functor which compute the cosine of a number.
Template functor which compute the cube of a number.
Template functor to add a number to a fixed value.
Template functor to divide a number by a fixed value.
Template functor to test if a number is equal to a fixed value.
Template functor which compute the exponential of a number.
Template functor to test if a number is greater or equal than a fixed value.
Template functor to test if a number is greater than a fixed value.
Template functor which compute the inverse of a number.
Template functor testing if a number is a finite value.
Template functor testing if a number is an infinite value.
Template functor testing if a number is a NA value.
Template functor to test if a number is less or equal than a fixed value.
Template functor to test if a number is less than a fixed value.
Template functor which compute the logarithm of a number.
Template functor computing logical and with a fixed value.
Template functor computing logical and with a fixed value.
Template functor to compute the minimum with a fixed value.
Template functor to compute the minimum between a fixed value.
Template functor to compute the minimum with a fixed value.
Template functor giving the not value.
Template functor to test if a number is different than a fixed value.
Template functor which compute the opposite of a number.
Template functor to raise a number to a power.
Template functor computing the product of a number by a fixed value.
Template functor which compute safely the inverse of a number.
Template functor which return a default value if the value is NA.
Template functor which compute the sine of a number.
Template functor which compute the square root of a number.
Template functor which compute the square of a number.
Template functor to add a number to a fixed value.
Template functor which compute the tan of a number.
Helper class to select the correct ProductReturnType.
If<(sizeof(Type1)>sizeof(Type2)), Type1, Type2 >::Result result_type