Multi-dimensional Arrays

Definition

Multi-dimensional arrays are instances of the class Array. Class Array is a template class: Array<T, N, Allocator>. As for vectors, T is the type of the elements to be stored (e.g. double), N is the number of dimensions. The current implementation is limited to N below or equal to 9. Allocator defines the way memory is managed. It is close to STL allocators. See the section "Allocators" for further details.

Declaration

There is a default Allocator (see the section "Allocators"). It means that the last template parameter may be omitted. Then a multi-dimensional array of integers may be declared thanks to the line:

Array<int, 5> A;

This defines an array of size 0 x 0 x 0 x 0 x 0, that is to say an empty array. To define a 3D array of size 5 x 3 x 6 x 2 x 4, one may write:

Array<int, 5> A(5, 3, 6, 2, 4);

Use of multi-dimensional arrays

Only a few methods are available for multi-dimensional arrays because they are not the main concern of Seldon. Mainly, the access to elements is achieved through the operator(int, int, int, int, ...), and indices start at 0:

Array<double, 3> A(5, 6, 3);
A(0, 1, 3) = -3.1;
A(0, 0, 5) = 1.2 * A(0, 1, 3);

Methods related to multi-dimensional arrays are listed in the table below:

constructors
operators
GetLength returns the i-th dimension of the array
GetSize returns the number of elements stored in the array
GetDataSize returns the number of elements stored in the array
GetMemorySize returns the memory used to store the array
GetData returns the pointer to the elements stored in the array
Reallocate modifies the size of the array
Clear removes all elements of the array
Copy copies a multidimensional array
Fill sets elements to 0, 1, 2, etc or to a given value
Zero sets all the elements to 0
FillRand sets randomly elements of the array
Print displays the array
Write writes the array in a binary file
Read reads the array from a binary file



constructors of Array

Syntax :

  Array(int)
  Array(int, int)
  Array(int, int, int)
  Array(int, int, int, int)
  Array(int, int, int, int, int)
  Array(int, int, int, int, int, int)
  Array(int, int, int, int, int, int, int)
  Array(int, int, int, int, int, int, int, int)
  Array(int, int, int, int, int, int, int, int, int)

You can use a constructor with the dimensions of the multi-dimensional array.

Example :

Array<double, 4> A, B(5, 6, 3, 8);
// then you can modify entries of B
B(2, 0, 4, 1) = 2.5;

// you can use a copy constructor
Array<double, 4> C(B);

Related topics :

Reallocate
operator =

Location :

Class Array
Array.hxx Array.cxx

Operators of Array

Syntax :

  operator ()(int)
  operator ()(int, int)
  operator ()(int, int, int)
  operator ()(int, int, int, int)
  operator ()(int, int, int, int, int)
  operator ()(int, int, int, int, int, int)
  operator ()(int, int, int, int, int, int, int)
  operator ()(int, int, int, int, int, int, int, int)
  operator ()(int, int, int, int, int, int, int, int, int)
  operator = (const Array<T, N>& )

The access operator () can be used to modify A(i, j, k, m, ...).

Example :

Array<double, 4> A, B(5, 6, 3, 8);
// then you can modify entries of B
B(2, 0, 4, 1) = 2.5;

// you can use a copy constructor
Array<double, 4> C(B);

// or operator =
C = B;

Related topics :

Reallocate
Copy

Location :

Class Array
Array.hxx Array.cxx

GetLength

Syntax :

  int GetLength(int) const

This method returns a dimension of the array.

Example :

Array<double, 5> A, B(5, 6, 3, 8, 10);
// GetLength(2) should return 3
cout << "Third dimension of B " << B.GetLength(2) << endl;

Related topics :

Reallocate
GetSize

Location :

Class Array
Array.hxx Array.cxx

GetSize, GetDataSize

Syntax :

  int GetSize() const
  int GetDataSize() const

This method returns the number of elements effectively stored in the multi-dimensional array.

Example :

Array<double, 5> A, B(5, 6, 3, 4, 2);
// GetSize() should return 5*6*3*4*2 = 720
cout << "Number of elements of B " << B.GetSize() << endl;

Related topics :

Reallocate
GetLength

Location :

Class Array
Array.hxx Array.cxx

GetMemorySize

Syntax :

  size_t GetMemorySize() const

This method returns the memory used to store the array in bytes.

Example :

Array<double> A, B(5, 6, 3, 2, 8);
// GetMemorySize() should return 5*6*3*2*8*sizeof(double)
cout << "Memory used to store B " << B.GetMemorySize() << endl;

Related topics :

Reallocate
GetLength

Location :

Class Array
Array.hxx Array.cxx

GetData

Syntax :

  T* GetData() const

This method returns the pointer to the array storing all the elements. This method is a low-level routine and should be used cautiously.

Example :

Array<double, 5> A(5, 6, 3, 8, 7);
// If you wish to manipulate the vector containing all the elements of A :
Vector<double> V;
V.SetData(A.GetSize(), A.GetData());

// to avoid segmentation fault
V.Nullify();

Location :

Class Array
Array.hxx Array.cxx

Reallocate

Syntax :

  void Reallocate(int i, int j, int k, int m, ...);

This method resizes the multi-dimensional array with the new dimensions given in argument. If NewAlloc is used, the previous elements are lost, whereas they are kept if you are using MallocAlloc or CallocAlloc allocator.

Example :

Array<double, 5> A(5, 6, 3, 8, 7);
A.Fill();

// then changing the size of A
A.Reallocate(7, 2, 5, 10, 4);

Location :

Class Array
Array.hxx Array.cxx

Clear

Syntax :

  void Clear()

This method clears the multi-dimensional array.

Example :

// constructing a multi-dimensional array
int m = 10, n = 4, p = 7, k = 5, j = 3;
Array<double, 5> A(m, n, p, k, j);

// filling A
A(2, 0, 0, 1, 3) = 1.2;
// ...

// if you want to free the memory for other computations
A.Clear();

Location :

Class Array
Array.hxx Array.cxx

Copy

Syntax :

  void Copy(const Array<T>& )

This method copies a multi-dimensional array into the current object.

Example :

// constructing a multi-dimensional array
int m = 10, n = 4, p = 7, k = 6, j = 4;
Array<double, 5> A(m, n, p, k, j);
// filling A


// then you can copy this array into B
// you could also use operator =
Array<double, 5> B;
B.Copy(A);

Related topics :

operator
constructor

Location :

Class Array
Array.hxx Array.cxx

Fill

Syntax :

  void Fill()
  void Fill(const T& x)

This method fills the multi-dimensional array with 0, 1, 2, ... or with a given value.

Example :

// constructing a multi-dimensional array
int m = 2, n = 2, p = 4, k = 3;
Array<double, 4> A(m, n, p, k);
// filling A with 0, 1, 2, ...
A.Fill();
// A should be equal to
// A(0, 0, :, :) = |0, 1, 2|
//                 |3, 4, 5|
//                 |6, 7, 8|
//                 |9, 10, 11|
// A(0, 1, :, :) = |12, 13, 14|
//                 |15, 16, 17|
//                 |18, 19, 20|
//                 |21, 22, 23|
// A(1, 0, :, :) = |24, 25, 26|
//                 |27, 28, 29|
//                 |30, 31, 32|
//                 |33, 34, 35|
// A(1, 1, :, :) = |36, 37, 38|
//                 |39, 40, 41|
//                 |42, 43, 44|
//                 |45, 46, 47|


// you can also set all the entries to a same value
A.Fill(1.0);
// A should be equal to
// A(0, 0, :, :) = |1, 1, 1|
//                 |1, 1, 1|
//                 |1, 1, 1|
//                 |1, 1, 1|
// and also A(0, 1, :, :), A(1, 0, :, :) and A(1, 1, :, :)

Related topics :

Zero
FillRand

Location :

Class Array
Array.hxx Array.cxx

Zero

Syntax :

  void Zero()

This method initializes all the entries to 0.

Example :

// constructing a multi-dimensional array
int m = 3, n = 2, p = 4, k = 5;
Array<double, 4> A(m, n, p, k);
// A is not initialized, for example you could set all
// the values to 0
A.Zero();

Related topics :

Fill
FillRand

Location :

Class Array
Array.hxx Array.cxx

FillRand

Syntax :

  void FillRand()

This method fills the multi-dimensional array with random values.

Example :

// constructing a multi-dimensional array
int m = 3, n = 2, p = 4, k = 5;
Array<double, 4> A(m, n, p, k);
// A is not initialized, for example you could set 
// randomly the values
A.FillRand();

Related topics :

Fill
Zero

Location :

Class Array
Array.hxx Array.cxx

Print

Syntax :

  void Print()

This method displays the multi-dimensional array.

Example :

// constructing a multi-dimensional array
int m = 3, n = 2, p = 4, k = 5;
Array<double, 4> A(m, n, p, k);
A.FillRand();

cout << "A = " << endl;
A.Print();

Related topics :

Fill
Zero

Location :

Class Array
Array.hxx Array.cxx

Read

Syntax :

  void Read(string file_name);
  void Read(istream& in);
  void Read(string file_name, bool with_size);
  void Read(istream& in, bool with_size);

This method reads a multi-dimensional array from a file or an input stream (binary format). If with_size is set to false, the dimensions of the multi-dimensional array are not read in the file.

Example :

// constructing a multi-dimensional array
int m = 3, n = 2, p = 4, k = 5;
Array<double, 4> A(m, n, p, k), B;
A.FillRand();

// you can write it on a file
A.Write("test.dat");

// then read it
B.Read("test.dat");

Related topics :

Write
Reallocate

Location :

Class Array
Array.hxx Array.cxx

Write

Syntax :

  void Write(string file_name);
  void Write(ostream& in);
  void Write(string file_name, bool with_size);
  void Write(ostream& in, bool with_size);

This method writes a multi-dimensional array in a file or in an input stream (binary format). If with_size is set to false, the dimensions of the multi-dimensional array are not written in the file.

Example :

// constructing a multi-dimensional array
int m = 3, n = 2, p = 4, k = 5;
Array<double, 4> A(m, n, p, k), B;
A.FillRand();

// you can write it on a file
A.Write("test.dat");

// then read it
B.Read("test.dat");

Related topics :

Read
Reallocate

Location :

Class Array
Array.hxx Array.cxx