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++:
Before explaining the usage and differences between the different arrays, we first introduce some vocabulary. The terminology used in STK++ project for the arrays is the following:
The word point is borrowed from the statistical vocabulary where a row of a data array is often named a point.
The Array2D classes are very flexible if you need to add, insert, remove, resize,... quickly rows or columns to your container. On the other hand, the storing scheme of the the CArray classes allow you to used them easily with other linear algebra libraries (e.g. Lapack, Blas, ...).
Let us give you an introductory example:
Example: | Output: |
---|---|
#include "STKpp.h"
using namespace STK;
{
// create a matrix of Real of dynamic size (3,5)
Array2D<Real> a(3, 4); a << 1.,2.,3.,4.
, 1.,2.,3.,4.
, 1.,1.,1.,1.;
std::cout << "a=\n" << a;
// create a vector of Real of dynamic size 3 with all coefficients equal to 0
Array2DVector<Real> b(3, 0);
b[2] = 1.;
std::cout << "b=\n" << b;
// create an unitialized CArray of Real of fixed size (3,4)
// create a CArray of Real of dynamic size (3,4) with initial value -2.
CArray<Real> d(3, 4, -2.);
// compute c = -a - d + 3 (Id)
std::cout << "c=\n" << c;
// create an unitialized CVector of Real of fixed size 3
std::cout << "e=\n" << e;
// create an initialized CPoint of Real of fixed size 3
std::cout << "f= " << f;
return 0;
}
This file include all the header files of the STK++ project. 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= 1 2 3 4 1 2 3 4 1 1 1 1 b= 0 0 1 c= 4 3 2 1 4 3 2 1 4 4 5 4 e= -2 5 -2 f= -2 5 -2 |
The primary coefficient accessors and mutators in STK++ are the overloaded parenthesis operators. For matrices, the row index is always passed first.
The operator[] is also overloaded for index-based access in vectors, but keep in mind that C++ doesn't allow operator[] to take more than one argument. The operator[] is thus restricted to vectors/points/diagonal matrices. For vectors, just pass one index in a bracket.
Indexing starts at 0 by default. This behavior can be modify by defining the STKBASEARRAYS macro at compile time using the directive -DSTKBASEARRAYS=1. Enabling this macro allow user to get 1 based arrays like in FORTRAN. If you want to build code independent of the first index you should use the beginCols()
, beginRows(), endCols(), lastIdxCols(), endRows() and lastIdxRows() methods of the arrays and the begin()
, end(), lastIdx() methods of the Row-vectors, Column-Vectors, square matrices and diagonal matrices.
For example
Assume that c
, a,d are array of the same size and consider the line of code
It is an expression which involve matrix operations. All these expressions are encoded in an expression template and are completely inlined at compile time. That means there is no temporary objects created when these expressions are evaluated.
The Array2D family have only one mandatory template parameter: the type of the data that will be stored. On the other hand the CArray family have four template parameters:
UnknownSize
if it is not known at compile time),UnknownSize
if it is not known at compile time),Only the first parameter is mandatory.
Example: |
---|
#include "STKpp.h"
using namespace STK;
{
// Array2D constructors
Array2DDiagonal<Real> a(3); a= 1.; // same as Array2DDiagonal<Real> a(3, 1.);
Array2DSquare<Real> b(3); b = 0.; // same as Array2DSquare<Real> b(3, 0.);
Array2DLowerTriangular<Real> d(3, 3); d = -2.;// same as Array2DLowerTriangular<Real> d(3, 3, -2.);
// CArray constructors
CArrayPoint<Real> k(3); k = 6.; // same as CArrayPoint<Real> k(3, 6.);
return 0;
}
|
You can access rows, columns and sub-part of STK++ arrays easily. Here is an example:
Example: | Output: |
---|---|
#include "STKpp.h"
using namespace STK;
{
VectorX b(3, 0);
std::cout << "b=\n" << b;
b.sub(Range(2)) = 1.;
std::cout << "b=\n" << b << "\n";
ArrayXX a(3, 4); a << 1.,2.,3.,4.
, 1.,2.,3.,4.
, 1.,1.,1.,1.;
std::cout << "a=\n" << a;
a.col(1) = b;
a.row(1, Range(2,2)) = 0.;
std::cout << "a=\n" << a;
return 0;
}
Index sub-vector region: Specialization when the size is unknown. Definition STK_Range.h:265 | b= 0 0 0 b= 1 1 0 a= 1 2 3 4 1 2 3 4 1 1 1 1 a= 1 1 3 4 1 1 9 9 1 0 9 9 |
Applied to a vector/point/diagonal matrix the method sub require only one parameter: the Range of the data we want to access/mutate.
In the general case, you can use the following method in order to access/mutate columns/rows/part of an array:
For the Array2D classes (deriving form the IArray2D class), the operator () have been overloaded and you can also access to row/column/sub-part of the Array using
In some cases, you may want to conserve an access to some part of an array for some work. For this purpose, it is possible to create reference array, that is array that wrap (part of) another array.
Example: | Output: |
---|---|
#include "STKpp.h"
using namespace STK;
{
ArraySquareX a(4); a << 1.,2.,3.,4.
, 1.,2.,3.,4.
, 1.,1.,1.,1.
, 1.,1.,1.,1.;
// create a reference
b = -1.;
std::cout << "Modified a=\n" << a << "\n";
m.move(Stat::mean(a));
std::cout << "m= " << m;
return 0;
}
| Modified a= -1 -1 -1 4 -1 -1 -1 4 1 1 1 1 1 1 1 1 m= 0 0 0 2.5 |
The Stat::mean function return an Array2D by value. In order to avoid a useless copy, we use the move function. The following piece of code
perform the operations:
a
contains data, the memory is released, a
become the owner of the data contain by b
, b
become a reference.b
was a reference, then a
is also a reference.Many more examples can be found in the test files.