STK++ 0.9.13
Arrays Tutorial 4 : Using Visitors, Appliers and Functors

This page explains STK++'s visitors and appliers and how they can be used on the arrays and in expressions.

Visitors

A visitor is a constant function applied to an expression or array returning a single value. One of the most useful visitor is ExprBase::sum(), returning the sum of all the coefficients of a given expression or array.

Visitors can also be used to obtain the location of an element inside an Expression or Array. This is the case of ExprBase::maxElt(i,j), ExprBase::maxElt(i), ExprBase::minElt(i,j) and ExprBase::minElt(i), which can be used to find the location of the greatest or smallest coefficient in an Expression or an Array.

The minElt method should not be confunded with the min method which compute the minimum between two arrays.

ExampleOutput
#include "STKpp.h"
using namespace STK;
int main(int argc, char** argv)
{
a << 0, 1,
2, 3;
stk_cout << "a.sum()= " << a.sum() << _T("\n");
stk_cout << "a.minElt()= " << a.minElt() << _T("\n");
stk_cout << "a.maxElt()= " << a.maxElt() << _T("\n");
stk_cout << "(a > 0).count()= " << (a > 0).count() << _T("\n");
stk_cout << "(a > 0).all()= " << (a > 0).all() << _T("\n");
stk_cout << "(a > 0).any()= " << (a > 0).any() << _T("\n");
int i,j;
stk_cout << "a.minElt(i,j)= " << a.minElt(i,j) << _T("\n");
stk_cout << "i= " << i << ", j= " << j << _T("\n");
return 0;
}
#define stk_cout
Standard stk output stream.
#define _T(x)
Let x unmodified.
This file include all the header files of the STK++ project.
A CArray is a two dimensional array with a continuous storage like a C-array.
Definition STK_CArray.h:130
Type const sum() const
Type const maxElt(int &row, int &col) const
Type const minElt(int &row, int &col) const
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
The namespace STK is the main domain space of the Statistical ToolKit project.
int main(int argc, char **argv)
a.sum()= 6
a.minElt()= 0
a.maxElt()= 3
(a > 0).count()= 3
(a > 0).all()= 0
(a > 0).any()= 1
a.minElt(i,j)= 0
i= 0, j= 0

Slicing Visitors

A visitor can also be applied on each column/row of an Expression or Array by using global functions in the STK namespace. All the visitor can be used as slicing visitors except the minElt with arguments.

ExampleOutput
#include "STKpp.h"
using namespace STK;
int main(int argc, char** argv)
{
CArray2X a(2,4);
a << 0, 1, 2, 3
, 4, 5, 6, 7;
stk_cout << "min(a) = " << min(a);
stk_cout << "minByRow(a) = " << minByRow(a);
stk_cout << "mean(a) = " << mean(a);
stk_cout << "count(a > 0)= " << count(a > 0);
stk_cout << "all(a > 0) = " << all(a > 0);
stk_cout << "any(a > 0) = " << any(a > 0);
a(1,1) = a(1,3) = Arithmetic<Real>::NA();
stk_cout << "count(a.isNA())= " << count(a.isNA());
stk_cout << "countByRow(a.isNA())= " << countByRow(a.isNA());
return 0;
}
UnaryOperator< IsNaOp< Type >, Derived > isNA() const
Arithmetic properties of STK fundamental types.
min(a)      = 0 1 2 3
minByRow(a) = 0
4
mean(a) = 2 3 4 5
count(a > 0)= 1 2 2 2
all(a > 0)  = 0 1 1 1
any(a > 0)  = 1 1 1 1
count(a.isNA())= 0 1 0 1
countByRow(a.isNA())= 0
2

Appliers

An applier is like a visitor except that it can only be applied to arrays as it will modify the content of the array. Slicing appliers does not have any meaning and thus an applier can only be used on a whole expression. If you need to use an applier on a row or column use the col or row method.

ExampleOutput
#include "STKpp.h"
using namespace STK;
int main(int argc, char** argv)
{
CArray2X a(2, 5);
stk_cout << "a.randUnif() =\n" << a.randUnif();
stk_cout << "a.randGauss()=\n" << a.randGauss();
Law::Gamma law(1, 1.5);
stk_cout << "a.rand(law) =\n" << a.rand(law);
stk_cout << "a.setValue(1)=\n" << a.setValue(1);
stk_cout << "a+=2 =\n" << (a+=2);
stk_cout << "a*=2 =\n" << (a*=2);
stk_cout << "a/=2 =\n" << (a/=2);
stk_cout << "a-=2 =\n" << (a-=2);
return 0;
}
Derived & setValue(TypeConst value)
set a value to this container.
Derived & randUnif()
set random values to this using a uniform law.
Derived & randGauss()
set random values to this using a standard gaussian law.
Derived & rand(Law::IUnivLaw< Type > const &law)
set random values to this using a distribution law given by the user.
Gamma distribution law.
a.randUnif() =
 0.137596  0.88743  0.169496 0.368936 0.647935
0.0491916 0.203792 0.0451589 0.117904 0.866746
a.randGauss()=
-0.381627  1.89977   1.56497 1.48796 -0.136965
 0.993396 0.125887 -0.575037 2.36021  -1.04943
a.rand(law)  =
0.099326  0.817579 0.702912 0.937509 0.813065
  3.2304 0.0500886  2.33403  1.48067  3.72204
a.setValue(1)=
1 1 1 1 1
1 1 1 1 1
a+=2 =
3 3 3 3 3
3 3 3 3 3
a*=2 =
6 6 6 6 6
6 6 6 6 6
a/=2 =
3 3 3 3 3
3 3 3 3 3
a-=2 =
1 1 1 1 1
1 1 1 1 1

Functors

Functors can fundamentally be used in the same way that slicing visitors. The only difference is that they return an STK::Array2DPoint if they are applied by column or an STK::Array2DVector if they are applied by rows. It is possible to use the move method in order to store the results of the computation without data copy.

All the functors are in the STK::Stat namespace and compute the usual statistics by columns. If there is the possibility of missing/NaN values add the word Safe to the name of the functor. If the mean by row is needed, just add ByRow to the name of the functor. If you want both add SafeByRow.

ExampleOutput
#include "STKpp.h"
using namespace STK;
int main(int argc, char** argv)
{
CArray3X a(3,5); a.randGauss();
stk_cout << "Stat::mean(a)=\n" << Stat::mean(a);
stk_cout << "Stat::meanByRow(a)=\n"<< Stat::meanByRow(a);
stk_cout << "Stat::variance(a)=\n" << Stat::variance(a);
// compute the biased variance (divided by N=3) with mean fixed to 0
stk_cout << "Stat::varianceWithFixedMean(a,0,false)=\n"
<< Stat::varianceWithFixedMean(a, Const::VectorX(5)*0, false);
a(1,2) = a(2,2) = Arithmetic<Real>::NA();
stk_cout << "Stat::meanSafe(a)=\n" << Stat::meanSafe(a);
stk_cout << "Stat::meanSafeByRow(a)=\n" << Stat::meanSafeByRow(a);
return 0;
}
Define the constant point.
Stat::mean(a)=
-0.911327 0.0749711 1.16178 0.318061 -0.42087
Stat::meanByRow(a)=
 0.360791
-0.369855
 0.142633
Stat::variance(a)=
0.00584228 0.45772 0.472917 1.84638 0.821848
Stat::varianceWithFixedMean(a,0,false)=
0.00584228 0.45772 0.472917 1.84638 0.821848
Stat::meanSafe(a)=
-0.911327 0.0749711 1.76343 0.318061 -0.42087
Stat::meanSafeByRow(a)=
 0.360791
-0.842989
 0.128485