STK++ 0.9.13
|
This page explains STK++'s visitors and appliers and how they can be used on the arrays and in expressions.
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.
Example | Output |
---|---|
#include "STKpp.h"
using namespace STK;
{
CArray22 a;
a << 0, 1,
2, 3;
return 0;
}
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 maxElt(int &row, int &col) const Definition STK_ExprBaseVisitor.h:122 Type const minElt(int &row, int &col) const Definition STK_ExprBaseVisitor.h:101 The MultidimRegression class allows to regress a multidimensional output variable among a multivariat... Definition STK_MultidimRegression.h:52 The namespace STK is the main domain space of the Statistical ToolKit project. | 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 |
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.
Example | Output |
---|---|
#include "STKpp.h"
using namespace STK;
{
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();
return 0;
}
UnaryOperator< IsNaOp< Type >, Derived > isNA() const Definition STK_ExprBase.h:368 | 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 |
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.
Example | Output |
---|---|
#include "STKpp.h"
using namespace STK;
{
CArray2X a(2, 5);
Law::Gamma law(1, 1.5);
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. Definition STK_ArrayBaseApplier.h:68 Derived & randUnif() set random values to this using a uniform law. Definition STK_ArrayBaseApplier.h:104 Derived & randGauss() set random values to this using a standard gaussian law. Definition STK_ArrayBaseApplier.h:113 Derived & rand(Law::IUnivLaw< Type > const &law) set random values to this using a distribution law given by the user. Definition STK_ArrayBaseApplier.h:122 | 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 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.
Example | Output |
---|---|
#include "STKpp.h"
using namespace STK;
{
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;
}
| 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 |