STK++ 0.9.13
|
The containers/arrays you use in order to store and process the data in your application greatly influence the speed and the memory usage of your application.
STK++ proposes a large choice of containers/arrays and methods that you can used in conjunction with them.
There
are mainly two kinds of arrays you can use with STK++:
The special features of the Arrays2D family are described in the Using Array2D tutorial.
Data can be encapsulate in one of the following array
Type
is the type of the elements like double
, float
, etc. Orient
can be either Arrays::by_col_ (= 1, the default template) or Arrays::by_row_ (= 0) STK::UnknownSize
(the default template) It is possible to use constant arrays with all values equal to 1 using the type predefined in the
As usual, only the first template parameter is mandatory.
Constructors are detailed in the tutorial Arrays Constructors. After creation Arrays can be initialized using comma initializer
and elements can be accessed using the parenthesis (for arrays), the brackets (for vectors) and the elt
method
The whole array/vector can be initialized using a value, either at the construction or during execution by using
It is possible to access to a row, a column, a sub-part of an array using slicing operators.
For all the containers it is possible to get the status (reference or array owning its data) the range, the beginning, the end and the size using
For the Array2D family, it is also possible to get informations about the allocated memory of the containers. It can be interested in order to known if the container will have to reallocate the memory if you try to resize it.
The Array2D family of container allocate a small amount of supplementary memory so that in case of a @ resize, it is possible to expand the container without data copy.
Visitors and appliers visit/are applied to an array or vector (or expression) and are automatically unrolled if the array is of fixed (small) size.
Visitors | Appliers |
---|---|
int i= m.count(); // count the number of true values
bool f= m.any(); // is there any true value ?
bool f= m.all(); //
Type r= m.minElt(i,j); // can be v.minElt(i) or m.minElt()
Type r= m.maxElt(i,j); // can be v.maxElt(i) or m.maxElt()
Type r= m.minEltSafe(i,j); // can be v.minEltSafe(i) or m.minEltSafe()
Type r= m.maxEltSafe(i,j); // can be v.maxEltSafe(i) or m.maxEltSafe()
Type r= m.sum(), s= m.sumSafe();
Type r= m.mean(), s= m.meanSafe();
| m.randUnif(); // fill m with uniform random numbers between 0 and 1
m.randGauss(); // fill m with standardized Gaussian random numbers
m.setOnes(); // fill m with value 1
m.setValues(2);// fill m with value 2
m.setZeros(); // fill m with value 0
Law::Gamma law(1,2); // create a Gamma distribution
m.rand(law); // fill m with gamma(1,2) random numbers
|
The next methods use visitors in order to compute the result, eventually safely
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 a safe computation by row add SafeByRow.
All functors applied on an array by column will return by value an STK::Array2DPoint and a number if it is applied on a vector or a point.
All functors applied on an array by row will return by value an STK::Array2DVector and a number if it is applied on a vector or a point.
All the functors applied on arrays are currently in the namespace STK::Stat.
Functors by column | Functors by row |
---|---|
// could also be
// unbiased variance (division by n-1) when unbiased is true, false is the default
// fixed mean. Must be a vector/point for arrays and a number for vectors/points
// unbiased (false in examples below) has to be given
hidden::FunctorTraits< Derived, VarianceWithFixedMeanOp >::Row varianceWithFixedMean(Derived const &A, MeanType const &mean, bool unbiased) Compute the VarianceWithFixedMean(s) value(s) of A. Definition STK_Stat_Functors.h:912 hidden::FunctorTraits< Derived, VarianceSafeOp >::Row varianceSafe(Derived const &A, bool unbiased=false) Compute safely the variance(s) value(s) of A. Definition STK_Stat_Functors.h:869 hidden::FunctorTraits< Derived, MinSafeOp >::Row minSafe(Derived const &A) Compute safely the minimal(s) [weighted] value(s) of A. Definition STK_Stat_Functors.h:740 hidden::FunctorTraits< Derived, MaxOp >::Row max(Derived const &A) Compute the maximal(s) value(s) of A. Definition STK_Stat_Functors.h:753 hidden::FunctorTraits< Derived, SumOp >::Row sum(Derived const &A) Compute the sum of A. Definition STK_Stat_Functors.h:776 hidden::FunctorTraits< Derived, MaxSafeOp >::Row maxSafe(Derived const &A) Compute safely the maximal(s) value(s) of A. Definition STK_Stat_Functors.h:765 hidden::FunctorTraits< Derived, MeanOp >::Row mean(Derived const &A) Compute the mean(s) value(s) of A. Definition STK_Stat_Functors.h:799 hidden::FunctorTraits< Derived, VarianceOp >::Row variance(Derived const &A, bool unbiased=false) Compute the variance(s) value(s) of A. Definition STK_Stat_Functors.h:826 hidden::FunctorTraits< Derived, VarianceWithFixedMeanSafeOp >::Row varianceWithFixedMeanSafe(Derived const &A, MeanType const &mean, bool unbiased) Compute safely the VarianceWithFixedMean(s) value(s) of A. Definition STK_Stat_Functors.h:951 hidden::FunctorTraits< Derived, SumSafeOp >::Row sumSafe(Derived const &A) Compute safely the mean(s) value(s) of A. Definition STK_Stat_Functors.h:787 hidden::FunctorTraits< Derived, MeanSafeOp >::Row meanSafe(Derived const &A) Compute safely the mean(s) value(s) of A. Definition STK_Stat_Functors.h:811 hidden::FunctorTraits< Derived, MinOp >::Row min(Derived const &A) Compute the minimal(s) value(s) of A. Definition STK_Stat_Functors.h:726 hidden::FunctorTraits< Derived, MinOp >::Row minByCol(Derived const &A) Definition STK_Stat_Functors.h:726 hidden::FunctorTraits< Derived, MeanOp >::Row meanByCol(Derived const &A) Definition STK_Stat_Functors.h:799 hidden::FunctorTraits< Derived, MaxOp >::Row maxByCol(Derived const &A) Definition STK_Stat_Functors.h:753 hidden::FunctorTraits< Derived, MinSafeOp >::Row minSafeByCol(Derived const &A) Definition STK_Stat_Functors.h:740 hidden::FunctorTraits< Derived, MaxSafeOp >::Row maxSafeByCol(Derived const &A) Definition STK_Stat_Functors.h:765 hidden::FunctorTraits< Derived, MeanSafeOp >::Row meanSafeByCol(Derived const &A) Definition STK_Stat_Functors.h:811 hidden::FunctorTraits< Derived, SumOp >::Row sumByCol(Derived const &A) Definition STK_Stat_Functors.h:776 hidden::FunctorTraits< Derived, SumSafeOp >::Row sumSafeByCol(Derived const &A) Definition STK_Stat_Functors.h:787 | // unbiased variance (division by n-1) when unbiased is true, false is the default
// fixed mean. Must be a vector/point for arrays and a number for vectors/points
// unbiased (false in examples below) has to be given
hidden::FunctorTraits< Derived, MeanSafeOp >::Col meanSafeByRow(Derived const &A) Definition STK_Stat_Functors.h:811 hidden::FunctorTraits< Derived, SumOp >::Col sumByRow(Derived const &A) Definition STK_Stat_Functors.h:776 hidden::FunctorTraits< Derived, MeanOp >::Col meanByRow(Derived const &A) Definition STK_Stat_Functors.h:799 hidden::FunctorTraits< Derived, MaxOp >::Col maxByRow(Derived const &A) Definition STK_Stat_Functors.h:753 hidden::FunctorTraits< Derived, VarianceOp >::Col varianceByRow(Derived const &A, bool unbiased=false) Definition STK_Stat_Functors.h:846 hidden::FunctorTraits< Derived, VarianceSafeOp >::Col varianceSafeByRow(Derived const &A, bool unbiased=false) Definition STK_Stat_Functors.h:889 hidden::FunctorTraits< Derived, SumSafeOp >::Col sumSafeByRow(Derived const &A) Definition STK_Stat_Functors.h:787 hidden::FunctorTraits< Derived, MaxSafeOp >::Col maxSafeByRow(Derived const &A) Definition STK_Stat_Functors.h:765 hidden::FunctorTraits< Derived, VarianceWithFixedMeanOp >::Col varianceWithFixedMeanByRow(Derived const &A, MeanType const &mean, bool unbiased=false) Definition STK_Stat_Functors.h:932 hidden::FunctorTraits< Derived, VarianceWithFixedMeanSafeOp >::Col varianceWithFixedMeanSafeByRow(Derived const &A, MeanType const &mean, bool unbiased) Definition STK_Stat_Functors.h:971 hidden::FunctorTraits< Derived, MinSafeOp >::Col minSafeByRow(Derived const &A) Definition STK_Stat_Functors.h:740 hidden::FunctorTraits< Derived, MinOp >::Col minByRow(Derived const &A) Definition STK_Stat_Functors.h:726 |
Available operations on arrays/vectors and expressions are summarized in the next table. Many operations are similar to the operations implemented in Eigen library.
add, subtract, divide, multiply, etc. arrays term by term | m= m1+m2; m+= m1;
m= m1-m2; m-= m1;
m= m1/m2; m/= m1;
m= m1.prod(m2); // don't use m1*m2 if you want a product element by element
m= m1%m2; m%= m1; // work only with integer values
m= m1&&m2; m= m1||m2; // logical operations
m= m1&m2; m=m1|m2; m=m1^m2; // bitwise operations, work only with integers
|
add, subtract, divide, multiply, etc. by a number | m= m1+s; m= s+m1; m+= s;
m= m1-s; m= s-m1; m-= s;
m= m1/s; m= s/m1; m/= s;
m= m1*s; m= s*m1; m*= s;
m= m1%s; m= s%m1; m%= s; // work only with integers
bool f;
m= m1&&f; m= m1||f; // logical operations
int r;
m= m1&r; m=m1|r; m=m1^r; // bitwise operations, work only with integers
|
matrix by matrix/vector products | Real s = p2 * v1; // dot product
v= m2*v2; v= m1 * p2.transpose(); // get a vector
p= p2*m2; p= v1.transpose()*m2; // get a point (row-vector)
m= m2*m1; m = m1.transpose() * m; // matrix multiplication
m= m2.prod(m1.transpose()); // product element by element
m*=m1; // same as m = m * m1
|
Comparisons operators between m and an array or a value | Real s;
// count for each columns the number of true comparisons
hidden::SliceVisitorSelector< Derived, hidden::CountVisitor, Arrays::by_col_ >::type_result count(Derived const &A) If A is a row-vector or a column-vector then the function will return the usual count value of the ve... Definition STK_SlicingVisitors.h:369 |
Probabilities and related operations | Law::Normal l(0,1);
// these methods return an array of the same size as m
m.pdf(l); // compute pdf values to m using distribution l
m.lpdf(l); // compute log-pdf values to m using distribution l
m.cdf(l); // compute cdf values using distribution l
m.lcdf(l); // compute log-cdf values using distribution l
m.cdfc(l); // compute complementary cdf values using distribution l
m.lcdfc(l); // compute log-complementary cdf values using distribution l
m.icdf(l); // compute inverse cdf values using distribution l
|
Mathematical functions | m.isNa(); // boolean expression with true if m(i,j) is a NA value
m.isFinite(); // boolean expression with true if m(i,j) is a finite value
m.isInfinite(); // boolean expression with true if m(i,j) is an infinite value
m.neg(); // Type expression with !m(i,j)
m.abs(); // Type expression with abs(m(i,j))
m.sqrt(); // Type expression with sqrt(m(i,j))
m.log(); // Type expression with log(m(i,j))
m.exp(); // Type expression with exp(m(i,j))
m.pow(number); // Type expression with m(i,j)^number
m.square(); // Type expression with m(i,j)*m(i,j)
m.cube(); // Type expression with m(i,j)*m(i,j)*m(i,j)
m.inverse(); // Type expression with 1/m(i,j)
m.sin(); // Type expression with sin(m(i,j))
m.cos(); // Type expression with cos(m(i,j))
m.tan(); // Type expression with tan(m(i,j))
m.asin(); // Type expression with asin(m(i,j))
m.acos(); // Type expression with acos(m(i,j))
|
Miscellaneous functions | m.cast<OtherType>(); // expression with static_cast<OtherType>(m(i,j)))
m.funct0<Functor>(); // expression with Functor(m(i,j)))
m.funct1<Functor>(functor); // same thing with an instance of Functor given
|
There exists predefined typedef for the arrays that can be used. Hereafter we give a sample for the CArray and Array2D classes
CArrays | Array2D |
---|---|
// CArray with Real (double by default) data
typedef CArray<Real, UnknownSize, UnknownSize, Arrays::by_col_> CArrayXX;
typedef CArray<Real, UnknownSize, 2, Arrays::by_col_> CArrayX2;
typedef CArray<Real, UnknownSize, 3, Arrays::by_col_> CArrayX3;
typedef CArray<Real, 2, UnknownSize, Arrays::by_col_> CArray2X;
typedef CArray<Real, 3, UnknownSize, Arrays::by_col_by> CArray3X;
typedef CArray<Real, 2, 2, Arrays::by_col_> CArray22;
typedef CArray<Real, 3, 3, Arrays::by_col_> CArray33;
// CArray with double data (add d to the type name)
typedef CArray<double, UnknownSize, UnknownSize, Arrays::by_col_> CArrayXXd;
...
// CArray with int data (add i to the typename)
typedef CArray<int, UnknownSize, UnknownSize, Arrays::by_col_> CArrayXXi;
...
// Arrays with data stored by rows, the same as above with ByRow added
typedef CArray<Real, UnknownSize, UnknownSize, Arrays::by_row_> CArrayByRowXX;
...
// CArraySquare (like CArray with the same number of rows and columns)
typedef CArraySquare<Real, UnknownSize, Arrays::by_col_> CSquareX;
typedef CArraySquare<Real, 2, Arrays::by_col_> CSquare2;
...
typedef CArraySquare<int, 3, Arrays::by_col_> CSquare3i;
...
typedef CArraySquare<Real, UnknownSize, Arrays::by_row_> CSquareByRowX;
CArray< double, UnknownSize, UnknownSize, Arrays::by_col_ > CArrayXXd Definition STK_CArray.h:57 CArray< Real, UnknownSize, UnknownSize, Arrays::by_row_ > CArrayByRowXX Definition STK_CArray.h:72 CArraySquare< Real, UnknownSize, Arrays::by_col_ > CSquareX Definition STK_CArraySquare.h:50 CArray< int, UnknownSize, UnknownSize, Arrays::by_col_ > CArrayXXi Definition STK_CArray.h:64 CArraySquare< Real, UnknownSize, Arrays::by_row_ > CSquareByRowX Definition STK_CArraySquare.h:60 |
The predefined type STK::Real can be either double
(the default) or float
.
For the other kind of containers there exists also predefined types
CArrays family | Array2D family |
---|---|
typedef CArrayVector<Real, UnknownSize, Arrays::by_col_> CVectorX;
typedef CArrayVector<Real, 2, Arrays::by_col_> CVector2;
typedef CArrayVector<Real, 3, Arrays::by_col_> CVector3;
typedef CArrayVector<double, UnknownSize, Arrays::by_col_> CVectorXd;
typedef CArrayVector<double, 2, Arrays::by_col_> CVector2d;
typedef CArrayVector<double, 3, Arrays::by_col_> CVector3d;
typedef CArrayVector<int, UnknownSize, Arrays::by_col_> CVectorXi;
typedef CArrayVector<int, 2, Arrays::by_col_> CVector2i;
typedef CArrayVector<int, 3, Arrays::by_col_> CVector3i;
// CArrayPoint
typedef CArrayPoint<Real, UnknownSize, Arrays::by_col_> CPointX;
typedef CArrayPoint<Real, 2, Arrays::by_col_> CPoint2;
typedef CArrayPoint<Real, 3, Arrays::by_col_> CPoint3;
...
typedef CArrayPoint<int, 3, Arrays::by_col_> CPoint3i;
CArrayPoint< Real, UnknownSize, Arrays::by_col_ > CPointX Definition STK_CArrayPoint.h:51 CArrayVector< double, UnknownSize, Arrays::by_col_ > CVectorXd Definition STK_CArrayVector.h:50 CArrayVector< int, UnknownSize, Arrays::by_col_ > CVectorXi Definition STK_CArrayVector.h:53 CArrayVector< Real, UnknownSize, Arrays::by_col_ > CVectorX Definition STK_CArrayVector.h:47 | typedef Array2DPoint<Real> PointX;
typedef Array2DPoint<double> PointXd;
typedef Array2DVector<Real> VectorX;
typedef Array2DVector<double> VectorXd;
typedef Array2DDiagonal<Real> ArrayDiagonalX;
typedef Array2DDiagonal<int> ArrayDiagonalXi;
|