STK++ 0.9.13
STK_ExprBaseVisitor.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff
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_EXPRBASEVISITOR_H
36#define STK_EXPRBASEVISITOR_H
37
43
44namespace STK
45{
46
61template<typename Derived>
62template<typename Visitor>
63inline typename Visitor::TypeConst ExprBase<Derived>::visit(Visitor& visitor) const
64{
66 Impl::run(this->asDerived(), visitor);
67 return visitor.result();
68}
69
70/* count the number of not-zero values in the expression */
71template<typename Derived>
72inline int ExprBase<Derived>::count() const
73{
75 return visit(visitor);
76}
77
78/* return true if one element is not zero */
79template<typename Derived>
80inline bool const ExprBase<Derived>::any() const
81{
83 return visit(visitor);
84}
85
86/* count the number of not-zero values in the expression */
87template<typename Derived>
88inline bool const ExprBase<Derived>::all() const
89{
91 return visit(visitor);
92}
93
94/* count the number values in the expression */
95template<typename Derived>
97{ return isFinite().count();}
98
99// general min elt
100template<typename Derived>
101inline typename hidden::Traits<Derived>::Type const ExprBase<Derived>::minElt( int& row, int& col) const
102{
104 visit(visitor);
105 row = visitor.row_;
106 col = visitor.col_;
107 return visitor.result();
108}
109
110template<typename Derived>
111inline typename hidden::Traits<Derived>::Type const ExprBase<Derived>::minEltSafe( int& row, int& col) const
112{
115 visit(visitor);
116 row = visitor.row_;
117 col = visitor.col_;
118 return visitor.result();
119}
120// general max elt
121template<typename Derived>
122inline typename hidden::Traits<Derived>::Type const ExprBase<Derived>::maxElt( int& row, int& col) const
123{
126 visit(visitor);
127 row = visitor.row_;
128 col = visitor.col_;
129 return visitor.result();
130}
131//
132template<typename Derived>
133inline typename hidden::Traits<Derived>::Type const ExprBase<Derived>::maxEltSafe( int& row, int& col) const
134{
137 visit(visitor);
138 row = visitor.row_;
139 col = visitor.col_;
140 return visitor.result();
142// min elt with one index
143template<typename Derived>
144inline typename hidden::Traits<Derived>::Type const ExprBase<Derived>::minElt( int& idx) const
145{
148 visit(visitor);
150 return visitor.result();
151}
152template<typename Derived>
154{
157 visit(visitor);
159 return visitor.result();
160}
161// max elt with one index
162template<typename Derived>
163inline typename hidden::Traits<Derived>::Type const ExprBase<Derived>::maxElt( int& idx) const
164{
167 visit(visitor);
169 return visitor.result();
170}
171template<typename Derived>
176 visit(visitor);
178 return visitor.result();
179}
180// min without index
181template<typename Derived>
186 return visit(visitor);
187}
188template<typename Derived>
195// max elt without index
196template<typename Derived>
198{
201 return visit(visitor);
202}
203template<typename Derived>
205{
208 return visit(visitor);
209}
210
211/* sum the values of all the array */
212template<typename Derived>
214{
216 return visit(visitor);
217}
218/* sum safely the values of all the array */
219template<typename Derived>
221{
223 return safe().visit(visitor);
225
226//--------- Start result with return type
227/* @return the norm of this*/
228template<typename Derived>
230{ return Type(std::sqrt(norm2()));}
231template<typename Derived>
233{ return static_cast<Type>(std::sqrt(safe().norm2()));}
234/* @return the square norm of this*/
235template<typename Derived>
237{ return square().sum();}
238template<typename Derived>
240{ return safe().square().sum();}
241/* @return the norm of this*/
242template<typename Derived>
244{ return abs().maxElt();}
245
246/* average the values of all the array */
247template<typename Derived>
251 return visit(visitor);
252}
253/* sum safely the values of all the array */
254template<typename Derived>
256{
258 return visit(visitor);
259}
260
261/* compute the variance of all the array */
262template<typename Derived>
264{
265 Type mu = mean();
266 return (*this-mu).square().mean();
267}
268/* compute the variance of all the array */
269template<typename Derived>
271{
272 Type mu = meanSafe();
273 return (*this-mu).square().meanSafe();
274}
275/* compute the variance with given mean of all the elements of this*/
276template<typename Derived>
278{ return (*this-mean).square().mean();}
279template<typename Derived>
281{ return (*this-mean).square().meanSafe();}
282
283/* @return the weighted sum of all the elements of this using a Visitor*/
284template<typename Derived>
285template<typename Rhs>
292template<typename Derived>
293template<typename Rhs>
300/* @return the norm of this*/
301template<typename Derived>
302template<typename Rhs>
304{
307 return static_cast<Type>(std::sqrt(wnorm2(weights)));
308}
309template<typename Derived>
310template<typename Rhs>
312{
315 return static_cast<Type>(std::sqrt(wnorm2Safe(weights)));
316}
317/* @return the square norm of this*/
318template<typename Derived>
319template<typename Rhs>
326template<typename Derived>
327template<typename Rhs>
334
335/* @return the weighted mean */
336template<typename Derived>
337template<typename Rhs>
339{
342 Type size = weights.sum();
343 if (size <= 0 || !STK::isFinite(size)) return Arithmetic<Type>::NA();
344 return wsum(weights)/size;
345}
346
347template<typename Derived>
348template<typename Rhs>
350{
353 Type size = weights.sumSafe();
354 if (size <= 0) return Arithmetic<Type>::NA();
355 return wsumSafe(weights)/size;
356}
357
358/* @return the variance of all the elements of this using a Visitor*/
359template<typename Derived>
360template<typename Rhs>
362{
365 Type mean = wmean(weights);
367 return (*this-mean).square().wmean(weights);
368}
369template<typename Derived>
370template<typename Rhs>
372{
375 Type mean = wmeanSafe(weights);
377 return (*this-mean).square().wmeanSafe(weights);
378}
379
380/* @return the variance with given mean of all the elements of this*/
381template<typename Derived>
382template<typename Rhs>
384{
387 return (*this-mean).square().wmean(weights);
388}
389template<typename Derived>
390template<typename Rhs>
392{
395 return (*this-mean).square().wmeanSafe(weights);
396}
397
398
399} // namespace STK
400
401#endif /* STK_EXPRBASEVISITOR_H */
In this file we define the appliers classes.
This file implement the slicing visitors in the namespace STK.
#define STK_STATIC_ASSERT_POINT_OR_VECTOR_ONLY(EXPR)
#define STK_STATIC_ASSERT_ONE_DIMENSION_ONLY(EXPR)
In this file we implement the selector of the visitor and apply pattern.
In this file we implement the different visitors allowing unrolling of the visit.
In this file we define the Visitors classes.
Type const varianceSafe() const
Type const norm() const
bool const any() const
check if there is any non-zero element in an expression.
Type const sum() const
Type const wmean(ExprBase< Rhs > const &weights) const
bool const all() const
check if all the elements in an expression are not zero.
Type const wnorm2(ExprBase< Rhs > const &weights) const
Type const meanSafe() const
Type const minElt() const
Type const wnorm2Safe(ExprBase< Rhs > const &weights) const
Type const variance() const
int count() const
compute the value of non-zero element in an expression.
Type const sumSafe() const
hidden::Traits< Derived >::Type Type
int nbAvailableValues() const
Visitor::TypeConst visit(Visitor &visitor) const
Visit the container using a constant visitor.
Type const maxElt() const
Type const minEltSafe() const
Type const wmeanSafe(ExprBase< Rhs > const &weights) const
Type const norm2() const
Type const normInf() const
Type const wnormSafe(ExprBase< Rhs > const &weights) const
Type const wvarianceSafe(ExprBase< Rhs > const &weights) const
Type const wnorm(ExprBase< Rhs > const &weights) const
Type const normSafe() const
Type const wvariance(ExprBase< Rhs > const &weights) 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
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
bool isFinite(Type const &x)
utility method allowing to know if a value is a finite value
Real dot(ExprBase< Container1D1 > const &x, ExprBase< Container1D2 > const &y)
Compute the dot product.
hidden::SliceVisitorSelector< Derived, hidden::MeanVisitor, Arrays::by_col_ >::type_result mean(Derived const &A)
If A is a row-vector or a column-vector then the function will return the usual mean value of the vec...
hidden::SliceVisitorSelector< Derived, hidden::MeanSafeVisitor, Arrays::by_col_ >::type_result meanSafe(Derived const &A)
If A is a row-vector or a column-vector then the function will return the usual mean value of the vec...
The namespace STK is the main domain space of the Statistical ToolKit project.
static Type NA()
Adding a Non Available (NA) special number.
Visitor checking if all the elements of an array are different from zero.
Visitor checking if at least, one element of an array is different from zero.
Visitor counting the number of not-zero element in an array This visitor can be used in conjunction w...
utility class for getting the result from a visitor acting on a vector or a point.
Visitor computing safely the maximal coefficient of the Array.
Visitor computing the maximal coefficient of the Array.
Visitor computing safely the max of all the coefficients of the Array.
Visitor computing the max of all the coefficients of the Array.
Visitor computing safely the mean of all the coefficients of the Array.
Visitor computing the mean of all the coefficients of the Array.
Visitor computing safely the minimal coefficient with its value and indexes.
Visitor computing the min coefficient with its value and coordinates.
Visitor computing safely the min of all the coefficients of the Array.
Visitor computing the min of all the coefficients of the Array.
Visitor computing the sum of all the coefficients of the Array.
If< is2D_ &&unrollRows_ &&unrollCols_, ArrayImpl, HelperImpl >::Result Impl