STK++ 0.9.13
Arrays Tutorial 6 : Using reference Arrays

This page explains how to create reference on existing arrays.

Creating reference to sub-arrays

Any slice of an existing array can be referenced by an other array and modified using this "reference" container.

ExampleOutput
#include "STKpp.h"
using namespace STK;
int main(int argc, char** argv)
{
ArraySquareX a(4); a << 1.,2.,3.,4.
, 1.,2.,3.,4.
, 1.,1.,1.,1.
, 1.,1.,1.,1.;
// create a reference
ArrayXX b(a.sub(Range(2), Range(3)), true);
b = -1.;
std::cout << "Modified a=\n" << a << "\n";
CArray<Real, 3, 4> c; c << 1.,2.,3.,4.
, 1.,2.,3.,4.
, 1.,1.,1.,1.;
// create a reference
CVector3 d(c.col(1), true);
d = -1.;
std::cout << "Modified c=\n" << c;
return 0;
}
This file include all the header files of the STK++ project.
specialization for the vector case.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Index sub-vector region: Specialization when the size is unknown.
Definition STK_Range.h:265
The namespace STK is the main domain space of the Statistical ToolKit project.
int main(int argc, char **argv)
Modified a=
-1 -1 -1 4
-1 -1 -1 4
 1  1  1 1
 1  1  1 1

Modified c=
1 -1 3 4
1 -1 3 4
1 -1 1 1

This method allow to get a safe access to some sub-part of a big array that you want to modify.

Creating reference to raw data

It is possible to map a single pointer Type* or a double pointer Type** in respectively CArrays and Array2Ds using the constructors.

Wrapper Constructors Examples
Wrap the data pointed by q in an Array with rows in the range I and columns in the range J.
Array2D<Type>( Type** q, Range const& I, Range const& J);

In this example we create an array of size (10, 5) and wrap it in an Array2.

Real** q = new Real*[10];
for (int j=0; j<10; j++) { q[j] = new Real[5];}
//wrap q. Note that q will not be freed when a is deleted
Array2D<Real> a(q, Range(0,10), Range(0,5)); //

Wrapper constructor. Wrap the data pointed by q in an Array with rows and columns of size m and n.
CArray<Type>( Type* q, int m, int n);

In this example we create an array of size (10, 5) and wrap it in an Array2D.

Real* q = new Real[10];
for (int j=0; j<10; j++) { q[j] = j;}
//wrap q in a fixed size array of size (2,5).
// Note that q will not be freed when a is deleted
CArray<Real, 2, 5> a(q, 2, 5);