[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

# 2. Arrays

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 2.1 Getting started

Currently, Blitz++ provides a single array class, called `Array<T_numtype,N_rank>`. This array class provides a dynamically allocated N-dimensional array, with reference counting, arbitrary storage ordering, subarrays and slicing, flexible expression handling, and many other useful features.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.1.1 Template parameters

The `Array` class takes two template parameters:

- `T_numtype` is the numeric type to be stored in the array. `T_numtype` can be an integral type (`bool`, `char`, `unsigned char`, `short int`, `short unsigned int`, `int`, `unsigned int`, `long`, `unsigned long`), floating point type (`float`, `double`, `long double`), complex type (`complex<float>`, `complex<double>`, `complex<long double>`) or any user-defined type with appropriate numeric semantics.
- `N_rank` is the rank (or dimensionality) of the array. This should be a positive integer.

To use the `Array` class, include the header `<blitz/array.h>` and use the namespace `blitz`:

    #include <blitz/array.h>

    using namespace blitz;

    Array<int,1>    x;    // A one-dimensional array of int
    Array<double,2> y;    // A two-dimensional array of double
    .
    .
    Array<complex<float>, 12> z; // A twelve-dimensional array of complex<float>

When no constructor arguments are provided, the array is empty, and no memory is allocated. To create an array which contains some data, provide the size of the array as constructor arguments:

    Array<double,2> y(4,4);   // A 4x4 array of double

The contents of a newly-created array are garbage. To initialize the array, you can write:

    y = 0;

and all the elements of the array will be set to zero. If the contents of the array are known, you can initialize it using a comma-delimited list of values. For example, this code excerpt sets `y` equal to a 4x4 identity matrix:

    y = 1, 0, 0, 0,
        0, 1, 0, 0,
        0, 0, 1, 0,
        0, 0, 0, 1;

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.1.2 Array types

The `Array<T,N>` class supports a variety of arrays:

- Arrays of scalar types, such as `Array<int,1>` and `Array<float,3>`
- Complex arrays, such as `Array<complex<float>,2>`
- Arrays of user-defined types. If you have a class called `Polynomial`, then `Array<Polynomial,2>` is an array of `Polynomial` objects.
- Nested homogeneous arrays using `TinyVector` and `TinyMatrix`, in which each element is a fixed-size vector or array. For example, `Array<TinyVector<float,3>,3>` is a three-dimensional vector field.
- Nested heterogeneous arrays, such as `Array<Array<int,1>,1>`, in which each element is a variable-length array.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.1.3 A simple example

Here's an example program which creates two 3x3 arrays, initializes them, and adds them:

    #include <blitz/array.h>

    using namespace blitz;

    int main()
    {
        Array<float,2> A(3,3), B(3,3), C(3,3);

        A = 1, 0, 0,
            2, 2, 2,
            1, 0, 0;

        B = 0, 0, 7,
            0, 8, 0,
            9, 9, 9;

        C = A + B;

        cout << "A = " << A << endl
             << "B = " << B << endl
             << "C = " << C << endl;

        return 0;
    }

and the output:

    A = 3 x 3
    [         1         0         0
              2         2         2
              1         0         0 ]

    B = 3 x 3
    [         0         0         7
              0         8         0
              9         9         9 ]

    C = 3 x 3
    [         1         0         7
              2        10         2
             10         9         9 ]

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.1.4 Storage orders

Blitz++ is very flexible about the way arrays are stored in memory.

The default storage format is row-major, C-style arrays whose indices start at zero.

Fortran-style arrays can also be created. Fortran arrays are stored in column-major order, and have indices which start at one. To create a Fortran-style array, use this syntax: `Array<int,2> A(3, 3, fortranArray);` The last parameter, `fortranArray`, tells the `Array` constructor to use a fortran-style array format.

`fortranArray` is a global object which has an automatic conversion to type `GeneralArrayStorage<N>`. `GeneralArrayStorage<N>` encapsulates information about how an array is laid out in memory. By altering the contents of a `GeneralArrayStorage<N>` object, you can lay out your arrays any way you want: the dimensions can be ordered arbitrarily and stored in ascending or descending order, and the starting indices can be arbitrary.

Creating custom array storage formats is described in a later section (Array storage orders).

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 2.2 Public types

The `Array` class declares these public types:

- `T_numtype` is the element type stored in the array. For example, the type `Array<double,2>::T_numtype` would be `double`.
- `T_index` is a vector index into the array. The class `TinyVector` is used for this purpose.
- `T_array` is the array type itself (`Array<T_numtype,N_rank>`)
- `T_iterator` is an iterator type. NB: this iterator is not yet fully implemented, and is NOT STL compatible at the present time.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 2.3 Constructors

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.3.1 Default constructor

    Array();
    Array(GeneralArrayStorage<N_rank> storage)

The default constructor creates a C-style array of zero size. Any attempt to access data in the array may result in a run-time error, because there isn't any data to access!

An optional argument specifies a storage order for the array.

Arrays created using the default constructor can subsequently be given data by the `resize()`, `resizeAndPreserve()`, or `reference()` member functions.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.3.2 Creating an array from an expression

    Array(expression...)

You may create an array from an array expression. For example,

    Array<float,2> A(4,3), B(4,3);   // ...
    Array<float,2> C(A*2.0+B);

This is an explicit constructor (it will not be used to perform implicit type conversions). The newly constructed array will have the same storage format as the arrays in the expression. If arrays with different storage formats appear in the expression, an error will result. (In this case, you must first construct the array, then assign the expression to it).

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.3.3 Constructors which take extent parameters

    Array(int extent1);
    Array(int extent1, int extent2);
    Array(int extent1, int extent2, int extent3);
    ...
    Array(int extent1, int extent2, int extent3, ..., int extent11)

These constructors take arguments which specify the size of the array to be constructed. You should provide as many arguments as there are dimensions in the array.(1)

An optional last parameter specifies a storage format:

    Array(int extent1, GeneralArrayStorage<N_rank> storage);
    Array(int extent1, int extent2, GeneralArrayStorage<N_rank> storage);
    ...

For high-rank arrays, it may be convenient to use this constructor:

    Array(const TinyVector<int, N_rank>& extent);
    Array(const TinyVector<int, N_rank>& extent,
          GeneralArrayStorage<N_rank> storage);

The argument `extent` is a vector containing the extent (length) of the array in each dimension. The optional second parameter indicates a storage format. Note that you can construct `TinyVector<int,N>` objects on the fly with the `shape(i1,i2,...)` global function. For example, `Array<int,2> A(shape(3,5))` will create a 3x5 array.

A similar constructor lets you provide both a vector of base index values (lbounds) and extents:

    Array(const TinyVector<int, N_rank>& lbound,
          const TinyVector<int, N_rank>& extent);
    Array(const TinyVector<int, N_rank>& lbound,
          const TinyVector<int, N_rank>& extent,
          GeneralArrayStorage<N_rank> storage);

The argument `lbound` is a vector containing the base index value (or lbound) of the array in each dimension. The argument `extent` is a vector containing the extent (length) of the array in each dimension. The optional third parameter indicates a storage format. As with the above constructor, you can use the `shape(i1,i2,...)` global function to create the `lbound` and `extent` parameters.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.3.4 Constructors with Range arguments

These constructors allow arbitrary bases (starting indices) to be set:

    Array(Range r1);
    Array(Range r1, Range r2);
    Array(Range r1, Range r2, Range r3);
    ...
    Array(Range r1, Range r2, Range r3, ..., Range r11);

For example, this code:

    Array<int,2> A(Range(10,20), Range(20,30));

will create an 11x11 array whose indices are 10..20 and 20..30. An optional last parameter provides a storage order:

    Array(Range r1, GeneralArrayStorage<N_rank> storage);
    Array(Range r1, Range r2, GeneralArrayStorage<N_rank> storage);
    ...

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.3.5 Referencing another array

This constructor makes a shared view of another array's data:

    Array(Array<T_numtype, N_rank>& array);

After this constructor is used, both `Array` objects refer to the same data. Any changes made to one array will appear in the other array. If you want to make a duplicate copy of an array, use the `copy()` member function.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.3.6 Constructing an array from an expression

Arrays may be constructed from expressions, which are described in Expression evaluation order. The syntax is:

    Array(...array expression...);

For example, this code creates an array B which contains the square roots of the elements in A:

    Array<float,2> A(N,N);   // ...
    Array<float,2> B(sqrt(A));

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.3.7 Creating an array from pre-existing data

When creating an array using a pointer to already existing data, you have three choices for how Blitz++ will handle the data. These choices are enumerated by the enum type `preexistingMemoryPolicy`:

    enum preexistingMemoryPolicy {
      duplicateData,
      deleteDataWhenDone,
      neverDeleteData
    };

If you choose `duplicateData`, Blitz++ will create an array object using a copy of the data you provide. If you choose `deleteDataWhenDone`, Blitz++ will not create a copy of the data; and when no array objects refer to the data anymore, it will deallocate the data using `delete []`. Note that to use `deleteDataWhenDone`, your array data must have been allocated using the C++ `new` operator - for example, you cannot allocate array data using Fortran or `malloc`, then create a Blitz++ array from it using the `deleteDataWhenDone` flag. The third option is `neverDeleteData`, which means that Blitz++ will not never deallocate the array data. This means it is your responsibility to determine when the array data is no longer needed, and deallocate it. You should use this option for memory which has not been allocated using the C++ `new` operator.

These constructors create array objects from pre-existing data:

    Array(T_numtype* dataFirst, TinyVector<int, N_rank> shape,
          preexistingMemoryPolicy deletePolicy);
    Array(T_numtype* dataFirst, TinyVector<int, N_rank> shape,
          preexistingMemoryPolicy deletePolicy,
          GeneralArrayStorage<N_rank> storage);

The first argument is a pointer to the array data. It should point to the element of the array which is stored first in memory. The second argument indicates the shape of the array. You can create this argument using the `shape()` function. For example:

    double data[] = { 1, 2, 3, 4 };
    Array<double,2> A(data, shape(2,2), neverDeleteData);   // Make a 2x2 array

The `shape()` function takes N integer arguments and returns a `TinyVector<int,N>`.

By default, Blitz++ arrays are row-major. If you want to work with data which is stored in column-major order (e.g. a Fortran array), use the second version of the constructor:

    Array<double,2> B(data, shape(2,2), neverDeleteData,
                      FortranArray<2>());

This is a tad awkward, so Blitz++ provides the global object `fortranArray` which will convert to an instance of `GeneralArrayStorage<N_rank>`:

    Array<double,2> B(data, shape(2,2), neverDeleteData, fortranArray);

Another version of this constructor allows you to pass an arbitrary vector of strides:

    Array(T_numtype* _bz_restrict dataFirst, TinyVector<int, N_rank> shape,
          TinyVector<int, N_rank> stride,
          preexistingMemoryPolicy deletePolicy,
          GeneralArrayStorage<N_rank> storage = GeneralArrayStorage<N_rank>())

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.3.8 Interlacing arrays

For some platforms, it can be advantageous to store a set of arrays interlaced together in memory. Blitz++ provides support for this through the routines `interlaceArrays()` and `allocateArrays()`. An example:

    Array<int,2> A, B;
    interlaceArrays(shape(10,10), A, B);

The first parameter of `interlaceArrays()` is the shape for the arrays (10x10). The subsequent arguments are the set of arrays to be interlaced together. Up to 11 arrays may be interlaced. All arrays must store the same data type and be of the same rank. In the above example, storage is allocated so that `A(0,0)` is followed immediately by `B(0,0)` in memory, which is folloed by `A(0,1)` and `B(0,1)`, and so on.

A related routine is `allocateArrays()`, which has identical syntax:

    Array<int,2> A, B;
    allocateArrays(shape(10,10), A, B);

Unlike `interlaceArrays()`, which always interlaces the arrays, the routine `allocateArrays()` may or may not interlace them, depending on whether interlacing is considered advantageous for your platform. If the tuning flag `BZ_INTERLACE_ARRAYS` is defined in `<blitz/tuning.h>`, then the arrays are interlaced.

Note that the performance effects of interlacing are unpredictable: in some situations it can be a benefit, and in most others it can slow your code down substantially. You should only use `interlaceArrays()` after running some benchmarks to determine whether interlacing is beneficial for your particular algorithm and architecture.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.3.9 A note about reference counting

Blitz++ arrays use reference counting. When you create a new array, a memory block is allocated. The `Array` object acts like a handle for this memory block. A memory block can be shared among multiple `Array` objects - for example, when you take subarrays and slices. The memory block keeps track of how many `Array` objects are referring to it. When a memory block is orphaned - when no `Array` objects are referring to it - it automatically deletes itself and frees the allocated memory.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 2.4 Indexing, subarrays, and slicing

This section describes how to access the elements of an array. There are three main ways:

- Indexing obtains a single element
- Creating a subarray which refers to a smaller portion of an array
- Slicing to produce a smaller-dimensional view of a portion of an array

Indexing, subarrays and slicing all use the overloaded parenthesis `operator()`.

As a running example, we'll consider the three dimensional array pictured below, which has index ranges (0..7, 0..7, 0..7). Shaded portions of the array show regions which have been obtained by indexing, creating a subarray, and slicing.

Examples of array indexing, subarrays, and slicing.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.4.1 Indexing

There are two ways to get a single element from an array. The simplest is to provide a set of integer operands to `operator()`:

    A(7,0,0) = 5;
    cout << "A(7,0,0) = " << A(7,0,0) << endl;

This version of indexing is available for arrays of rank one through eleven. If the array object isn't `const`, the return type of `operator()` is a reference; if the array object is `const`, the return type is a value.

You can also get an element by providing an operand of type `TinyVector<int,N_rank>` where `N_rank` is the rank of the array object:

    TinyVector<int,3> index;
    index = 7, 0, 0;
    A(index) = 5;
    cout << "A(7,0,0) = " << A(index) << endl;

This version of `operator()` is also available in a const-overloaded version.

It's possible to use fewer than `N_rank` indices. However, missing indices are assumed to be zero, which will cause bounds errors if the valid index range does not include zero (e.g. Fortran arrays). For this reason, and for code clarity, it's a bad idea to omit indices.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.4.2 Subarrays

You can obtain a subarray by providing `Range` operands to `operator()`. A `Range` object represents a set of regularly spaced index values. For example,

    Array<int,3> B = A(Range(5,7), Range(5,7), Range(0,2));

The object B now refers to elements (5..7,5..7,0..2) of the array A.

The returned subarray is of type `Array<T_numtype,N_rank>`. This means that subarrays can be used wherever arrays can be: in expressions, as lvalues, etc. Some examples:

    // A three-dimensional stencil (used in solving PDEs)
    Range I(1,6), J(1,6), K(1,6);
    B = (A(I,J,K) + A(I+1,J,K) + A(I-1,J,K) + A(I,J+1,K)
     + A(I,J-1,K) + A(I,J+1,K) + A(I,J,K+1) + A(I,J,K-1)) / 7.0;

    // Set a subarray of A to zero
    A(Range(5,7), Range(5,7), Range(5,7)) = 0.;

The bases of the subarray are equal to the bases of the original array:

    Array<int,2> D(Range(1,5), Range(1,5));     // 1..5, 1..5
    Array<int,2> E = D(Range(2,3), Range(2,3)); // 1..2, 1..2

An array can be used on both sides of an expression only if the subarrays don't overlap. If the arrays overlap, the result may depend on the order in which the array is traversed.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.4.3 RectDomain and StridedDomain

The classes `RectDomain` and `StridedDomain`, defined in `blitz/domain.h`, offer a dimension-independent notation for subarrays.

`RectDomain` and `StridedDomain` can be thought of as a `TinyVector<Range,N>`. Both have a vector of lower- and upper-bounds; `StridedDomain` has a stride vector. For example, the subarray:

    Array<int,2> B = A(Range(4,7), Range(8,11));  // 4..7, 8..11

could be obtained using `RectDomain` this way:

    TinyVector<int,2> lowerBounds(4, 8);
    TinyVector<int,2> upperBounds(7, 11);
    RectDomain<2> subdomain(lowerBounds, upperBounds);

    Array<int,2> B = A(subdomain);

Here are the prototypes of `RectDomain` and `StridedDomain`.

    template<int N_rank>
    class RectDomain {

    public:
        RectDomain(const TinyVector<int,N_rank>& lbound,
            const TinyVector<int,N_rank>& ubound);

        const TinyVector<int,N_rank>& lbound() const;
        int lbound(int i) const;
        const TinyVector<int,N_rank>& ubound() const;
        int ubound(int i) const;
        Range operator[](int rank) const;
        void shrink(int amount);
        void shrink(int dim, int amount);
        void expand(int amount);
        void expand(int dim, int amount);
    };

    template<int N_rank>
    class StridedDomain {

    public:
        StridedDomain(const TinyVector<int,N_rank>& lbound,
            const TinyVector<int,N_rank>& ubound,
            const TinyVector<int,N_rank>& stride);

        const TinyVector<int,N_rank>& lbound() const;
        int lbound(int i) const;
        const TinyVector<int,N_rank>& ubound() const;
        int ubound(int i) const;
        const TinyVector<int,N_rank>& stride() const;
        int stride(int i) const;
        Range operator[](int rank) const;
        void shrink(int amount);
        void shrink(int dim, int amount);
        void expand(int amount);
        void expand(int dim, int amount);
    };

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.4.4 Slicing

A combination of integer and Range operands produces a slice. Each integer operand reduces the rank of the array by one. For example:

    Array<int,2> F = A(Range::all(), 2, Range::all());
    Array<int,1> G = A(2,            7, Range::all());

Range and integer operands can be used in any combination, for arrays up to rank 11.

Note: Using a combination of integer and Range operands requires a newer language feature (partial ordering of member templates) which not all compilers support. If your compiler does provide this feature, `BZ_PARTIAL_ORDERING` will be defined in `<blitz/config.h>`. If not, you can use this workaround:

    Array<int,3> F = A(Range::all(), Range(2,2), Range::all());
    Array<int,3> G = A(Range(2,2),   Range(7,7), Range::all());

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.4.5 More about Range objects

A `Range` object represents an ordered set of uniformly spaced integers. Here are some examples of using Range objects to obtain subarrays:

    #include <blitz/array.h>

    using namespace blitz;

    int main()
    {
        Array<int,1> A(7);
        A = 0, 1, 2, 3, 4, 5, 6;

        cout << A(Range::all())  << endl          // [ 0 1 2 3 4 5 6 ]
             << A(Range(3,5))    << endl          // [ 3 4 5 ]
             << A(Range(3,toEnd)) << endl         // [ 3 4 5 6 ]
             << A(Range(fromStart,3)) << endl     // [ 0 1 2 3 ]
             << A(Range(1,5,2)) << endl           // [ 1 3 5 ]
             << A(Range(5,1,-2)) << endl          // [ 5 3 1 ]
             << A(Range(fromStart,toEnd,2)) << endl;    // [ 0 2 4 6 ]

        return 0;
    }

The optional third constructor argument specifies a stride. For example, `Range(1,5,2)` refers to elements [1 3 5]. Strides can also be negative: `Range(5,1,-2)` refers to elements [5 3 1].

Note that if you use the same Range frequently, you can just construct one object and use it multiple times. For example:

    Range all = Range::all();
    A(0,all,all) = A(N-1,all,all);
    A(all,0,all) = A(all,N-1,all);
    A(all,all,0) = A(all,all,N-1);

Here's an example of using strides with a two-dimensional array:

    #include <blitz/array.h>

    using namespace blitz;

    int main()
    {
        Array<int,2> A(8,8);
        A = 0;

        Array<int,2> B = A(Range(1,7,3), Range(1,5,2));
        B = 1;

        cout << "A = " << A << endl;
        return 0;
    }

Here's an illustration of the `B` subarray:

Using strides to create non-contiguous subarrays.

And the program output:

    A = 8 x 8
    [         0         0         0         0         0         0         0
              0
              0         1         0         1         0         1         0
              0
              0         0         0         0         0         0         0
              0
              0         0         0         0         0         0         0
              0
              0         1         0         1         0         1         0
              0
              0         0         0         0         0         0         0
              0
              0         0         0         0         0         0         0
              0
              0         1         0         1         0         1         0
              0 ]

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.4.6 A note about assignment

The assignment operator (`=`) always results in the expression on the right-hand side (rhs) being copied to the lhs (i.e. the data on the lhs is overwritten with the result from the rhs). This is different from some array packages in which the assignment operator makes the lhs a reference (or alias) to the rhs. To further confuse the issue, the copy constructor for arrays does have reference semantics. Here's an example which should clarify things:

    Array<int,1> A(5), B(10);
    A = B(Range(0,4));               // Statement 1
    Array<int,1> C = B(Range(0,4));  // Statement 2

Statement 1 results in a portion of `B`'s data being copied into `A`. After Statement 1, both `A` and `B` have their own (nonoverlapping) blocks of data. Contrast this behaviour with that of Statement 2, which is not an assignment (it uses the copy constructor). After Statement 2 is executed, the array `C` is a reference (or alias) to `B`'s data.

So to summarize: If you want to copy the rhs, use an assignment operator. If you want to reference (or alias) the rhs, use the copy constructor (or alternately, the `reference()` member function in Member functions).

Very important: whenever you have an assignment operator (`=`, `+=`, `-=`, etc.) the lhs must have the same shape as the rhs. If you want the array on the left hand side to be resized to the proper shape, you must do so by calling the `resize` method, for example:

    A.resize(B.shape());    // Make A the same size as B
    A = B;

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.4.7 An example

    #include <blitz/array.h>

    using namespace blitz;

    int main()
    {
        Array<int,2> A(6,6), B(3,3);

        // Set the upper left quadrant of A to 5
        A(Range(0,2), Range(0,2)) = 5;

        // Set the upper right quadrant of A to an identity matrix
        B = 1, 0, 0,
            0, 1, 0,
            0, 0, 1;
        A(Range(0,2), Range(3,5)) = B;

        // Set the fourth row to 1
        A(3, Range::all()) = 1;

        // Set the last two rows to 0
        A(Range(4, Range::toEnd), Range::all()) = 0;

        // Set the bottom right element to 8
        A(5,5) = 8;

        cout << "A = " << A << endl;

        return 0;
    }

The output:

    A = 6 x 6
    [         5         5         5         1         0         0
              5         5         5         0         1         0
              5         5         5         0         0         1
              1         1         1         1         1         1
              0         0         0         0         0         0
              0         0         0         0         0         8 ]

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 2.5 Debug mode

The Blitz++ library has a debugging mode which is enabled by defining the preprocessor symbol `BZ_DEBUG`. For most compilers, the command line argument `-DBZ_DEBUG` should work.

In debugging mode, your programs will run very slowly. This is because Blitz++ is doing lots of precondition checking and bounds checking. When it detects something fishy, it will likely halt your program and display an error message.

For example, this program attempts to access an element of a 4x4 array which doesn't exist:

    #include <blitz/array.h>

    using namespace blitz;

    int main()
    {
        Array<complex<float>, 2> Z(4,4);

        Z = complex<float>(0.0, 1.0);

        Z(4,4) = complex<float>(1.0, 0.0);

        return 0;
    }

When compiled with `-DBZ_DEBUG`, the out of bounds indices are detected and an error message results:

    [Blitz++] Precondition failure: Module ../../blitz/array-impl.h line 1282
    Array index out of range: (4, 4)
    Lower bounds: 2 [          0         0 ]
    Length:       2 [          4         4 ]

    debug: ../../blitz/array-impl.h:1282: bool blitz::Array<T,
    N>::assertInRange(int, int) const [with P_numtype = std::complex<float>, int
    N_rank = 2]: Assertion `0' failed.

Precondition failures send their error messages to the standard error stream (`cerr`). After displaying the error message, `assert(0)` is invoked.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 2.6 Member functions

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.6.1 A note about dimension parameters

Several of the member functions take a dimension parameter which is an integer in the range 0 .. `N_rank`-1. For example, the method `extent(int n)` returns the extent (or length) of the array in dimension `n`.

These parameters are problematic:

- They make the code cryptic. Someone unfamiliar with the `reverse()` member function won't stand a chance of understanding what `A.reverse(2)` does.
- Some users are used to dimensions being 1 .. `N_rank`, rather than 0 .. `N_rank`-1. This makes dimension numbers inherently error-prone. Even though I'm a experienced C/C++ programmer, I still want to think of the first dimension as 1 - it doesn't make sense to talk about the "zeroth" dimension.

As a solution to this problem, Blitz++ provides a series of symbolic constants which you can use to refer to dimensions:

    const int firstDim    = 0;
    const int secondDim   = 1;
    const int thirdDim    = 2;
       .
       .
    const int eleventhDim = 10;

These symbols should be used in place of the numerals 0, 1, ... `N_rank`-1. For example:

    A.reverse(thirdDim);

This code is clearer: you can see that the parameter refers to a dimension, and it isn't much of a leap to realize that it's reversing the element ordering in the third dimension.

If you find `firstDim`, `secondDim`, ... aesthetically unpleasing, there are equivalent symbols `firstRank`, `secondRank`, `thirdRank`, ..., `eleventhRank`.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

#### Why stop at eleven?

The symbols had to stop somewhere, and eleven seemed an appropriate place to stop. Besides, if you're working in more than eleven dimensions your code is going to be confusing no matter what help Blitz++ provides.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.6.2 Member function descriptions

    const TinyVector<int, N_rank>&    base() const;
    int                               base(int dimension) const;

The base of a dimension is the first valid index value. A typical C-style array will have base of zero; a Fortran-style array will have base of one. The base can be different for each dimension, but only if you deliberately use a Range-argument constructor or design a custom storage ordering.

The first version returns a reference to the vector of base values. The second version returns the base for just one dimension; it's equivalent to the `lbound()` member function. See the note on dimension parameters such as `firstDim` above.

    Array<T,N>::iterator              begin();
    Array<T,N>::const_iterator        begin() const;

These functions return STL-style forward and input iterators, respectively, positioned at the first element of the array. Note that the array data is traversed in memory order (i.e. by rows for C-style arrays, and by columns for Fortran-style arrays). The `Array<T,N>::const_iterator` has these methods:

    const_iterator(const Array<T,N>&);
    T operator*() const;
    const T* [restrict] operator->() const;
    const_iterator& operator++();
    void operator++(int);
    bool operator==(const const_iterator<T,N>&) const;
    bool operator!=(const const_iterator<T,N>&) const;
    const TinyVector<int,N>& position() const;

Note that postfix ++ returns void (this is not STL-compliant, but is done for efficiency). The method `position()` returns a vector containing current index positions of the iterator. The `Array<T,N>::iterator` has the same methods as `const_iterator`, with these exceptions: `iterator& operator++(); T& operator*(); T* [restrict] operator->();` The `iterator` type may be used to modify array elements. To obtain iterator positioned at the end of the array, use the `end()` methods.

    int                               cols() const;
    int                               columns() const;

Both of these functions return the extent of the array in the second dimension. Equivalent to `extent(secondDim)`. See also `rows()` and `depth()`.

    Array<T_numtype, N_rank>          copy() const;

This method creates a copy of the array's data, using the same storage ordering as the current array. The returned array is guaranteed to be stored contiguously in memory, and to be the only object referring to its memory block (i.e. the data isn't shared with any other array object).

    const T_numtype* [restrict]       data() const;
          T_numtype* [restrict]       data();
    const T_numtype* [restrict]       dataZero() const;
          T_numtype* [restrict]       dataZero();
    const T_numtype* [restrict]       dataFirst() const;
          T_numtype* [restrict]       dataFirst();

These member functions all return pointers to the array data. The NCEG `restrict` qualifier is used only if your compiler supports it. If you're working with the default storage order (C-style arrays with base zero), you'll only need to use `data()`. Otherwise, things get complicated:

`data()` returns a pointer to the element whose indices are equal to the array base. With a C-style array, this means the element (0,0,...,0); with a Fortran-style array, this means the element (1,1,...,1). If `A` is an array object, `A.data()` is equivalent to (&A(A.base(firstDim), A.base(secondDim), ...)). If any of the dimensions are stored in reverse order, `data()` will not refer to the element which comes first in memory.

`dataZero()` returns a pointer to the element (0,0,...,0), even if such an element does not exist in the array. What's the point of having such a pointer? Say you want to access the element (i,j,k). If you add to the pointer the dot product of (i,j,k) with the stride vector (`A.stride()`), you get a pointer to the element (i,j,k).

`dataFirst()` returns a pointer to the element of the array which comes first in memory. Note however, that under some circumstances (e.g. subarrays), the data will not be stored contiguously in memory. You have to be very careful when meddling directly with an array's data.

Other relevant functions are: `isStorageContiguous()` and `zeroOffset()`.

    int                               depth() const;

Returns the extent of the array in the third dimension. This function is equivalent to `extent(thirdDim)`. See also `rows()` and `columns()`.

    int                               dimensions() const;

Returns the number of dimensions (rank) of the array. The return value is the second template parameter (`N_rank`) of the `Array` object. Same as `rank()`.

    RectDomain<N_rank>                domain() const;

Returns the domain of the array. The domain consists of a vector of lower bounds and a vector of upper bounds for the indices. NEEDS_WORK- need a section to explain methods of `RectDomain<N>`.

    Array<T,N>::iterator              end();
    Array<T,N>::const_iterator        end() const;

Returns STL-style forward and input iterators (respectively) for the array, positioned at the end of the array.

    int                               extent(int dimension) const;

The first version the extent (length) of the array in the specified dimension. See the note about dimension parameters such as `firstDim` in the previous section.

    Array<T_numtype2,N_rank>          extractComponent(T_numtype2,
                                      int componentNumber, int numComponents);

This method returns an array view of a single component of a multicomponent array. In a multicomponent array, each element is a tuple of fixed size. The components are numbered 0, 1, ..., `numComponents-1`. Example:

    Array<TinyVector<int,3>,2> A(128,128);  // A 128x128 array of int[3]

    Array<int,2> B = A.extractComponent(int(), 1, 3);

Now the B array refers to the 2nd component of every element in A. Note: for complex arrays, special global functions `real(A)` and `imag(A)` are provided to obtain real and imaginary components of an array. See the Global Functions section.

    void                              free();

This method resizes an array to zero size. If the array data is not being shared with another array object, then it is freed.

    bool                              isMajorRank(int dimension) const;

Returns true if the dimension has the largest stride. For C-style arrays (the default), the first dimension always has the largest stride. For Fortran-style arrays, the last dimension has the largest stride. See also `isMinorRank()` below and the note about dimension parameters such as `firstDim` in the previous section.

    bool                              isMinorRank(int dimension) const;

Returns true if the dimension does not have the largest stride. See also `isMajorRank()`.

    bool                              isRankStoredAscending(int dimension) const;

Returns true if the dimension is stored in ascending order in memory. This is the default. It will only return false if you have reversed a dimension using `reverse()` or have created a custom storage order with a descending dimension.

    bool                              isStorageContiguous() const;

Returns true if the array data is stored contiguously in memory. If you slice the array or work on subarrays, there can be skips - the array data is interspersed with other data not part of the array. See also the various `data..()` functions. If you need to ensure that the storage is contiguous, try `reference(copy())`.

    int                               lbound(int dimension) const;
    TinyVector<int,N_rank>            lbound() const;

The first version returns the lower bound of the valid index range for a dimension. The second version returns a vector of lower bounds for all dimensions. The lower bound is the first valid index value. If you're using a C-style array (the default), the lbound will be zero; Fortran-style arrays have lbound equal to one. The lbound can be different for each dimension, but only if you deliberately set them that way using a Range constructor or a custom storage ordering. This function is equivalent to `base(dimension)`. See the note about dimension parameters such as `firstDim` in the previous section.

    void                              makeUnique();

If the array's data is being shared with another Blitz++ array object, this member function creates a copy so the array object has a unique view of the data.

    int                               numElements() const;

Returns the total number of elements in the array, calculated by taking the product of the extent in each dimension. Same as `size()`.

    const TinyVector<int, N_rank>&    ordering() const;
    int                               ordering(int storageRankIndex) const;

These member functions return information about how the data is ordered in memory. The first version returns the complete ordering vector; the second version returns a single element from the ordering vector. The argument for the second version must be in the range 0 .. `N_rank`-1. The ordering vector is a list of dimensions in increasing order of stride; `ordering(0)` will return the dimension number with the smallest stride, and `ordering(N_rank-1)` will return the dimension number with largest stride. For a C-style array, the ordering vector contains the elements (`N_rank`-1, `N_rank`-2, ..., 0). For a Fortran-style array, the ordering vector is (0, 1, ..., `N_rank`-1). See also the description of custom storage orders in section Array storage orders.

    int                               rank() const;

Returns the rank (number of dimensions) of the array. The return value is equal to `N_rank`. Equivalent to `dimensions()`.

    void                              reference(Array<T_numtype,N_rank>& A);

This causes the array to adopt another array's data as its own. After this member function is used, the array object and the array `A` are indistinguishable - they have identical sizes, index ranges, and data. The data is shared between the two arrays.

    void                              reindexSelf(const TinyVector<int,N_rank>&);
    Array<T,N>                        reindex(const TinyVector<int,N_rank>&);

These methods reindex an array to use a new base vector. The first version reindexes the array, and the second just returns a reindexed view of the array, leaving the original array unmodified.

    void                              resize(int extent1, ...);
    void                              resize(const TinyVector<int,N_rank>&);

These functions resize an array to the specified size. If the array is already the size specified, then no memory is allocated. After resizing, the contents of the array are garbage. See also `resizeAndPreserve()`.

    void                              resizeAndPreserve(int extent1, ...);
    void                              resizeAndPreserve(const TinyVector<int,N_rank>&);

These functions resize an array to the specified size. If the array is already the size specified, then no change occurs (the array is not reallocated and copied). The contents of the array are preserved whenever possible; if the new array size is smaller, then some data will be lost. Any new elements created by resizing the array are left uninitialized.

    Array<T,N>                        reverse(int dimension);
    void                              reverseSelf(int dimension);

This method reverses the array in the specified dimension. For example, if `reverse(firstDim)` is invoked on a 2-dimensional array, then the ordering of rows in the array will be reversed; `reverse(secondDim)` would reverse the order of the columns. Note that this is implemented by twiddling the strides of the array, and doesn't cause any data copying. The first version returns a reversed "view" of the array data; the second version applies the reversal to the array itself.

    int                               rows() const;

Returns the extent (length) of the array in the first dimension. This function is equivalent to `extent(firstDim)`. See also `columns()`, and `depth()`.

    int                               size() const;

Returns the total number of elements in the array, calculated by taking the product of the extent in each dimension. Same as `numElements()`.

    const TinyVector<int, N_rank>&    shape() const;

Returns the vector of extents (lengths) of the array.

    const TinyVector<int, N_rank>&    stride() const;
    int                               stride(int dimension) const;

The first version returns the stride vector; the second version returns the stride associated with a dimension. A stride is the distance between pointers to two array elements which are adjacent in a dimension. For example, `A.stride(firstDim)` is equal to `&A(1,0,0) - &A(0,0,0)`. The stride for the second dimension, `A.stride(secondDim)`, is equal to `&A(0,1,0) - &A(0,0,0)`, and so on. For more information about strides, see the description of custom storage formats in Section Array storage orders. See also the description of parameters like `firstDim` and `secondDim` in the previous section.

    Array<T,N>                        transpose(int dimension1,
                                                int dimension2, ...);
    void                              transposeSelf(int dimension1,
                                                    int dimension2, ...);

These methods permute the dimensions of the array. The dimensions of the array are reordered so that the first dimension is `dimension1`, the second is `dimension2`, and so on. The arguments should be a permutation of the symbols `firstDim, secondDim, ...`. Note that this is implemented by twiddling the strides of the array, and doesn't cause any data copying. The first version returns a transposed "view" of the array data; the second version transposes the array itself.

    int                               ubound(int dimension) const;
    TinyVector<int,N_rank>            ubound() const;

The first version returns the upper bound of the valid index range for a dimension. The second version returns a vector of upper bounds for all dimensions. The upper bound is the last valid index value. If you're using a C-style array (the default), the ubound will be equal to the `extent(dimension)-1`. Fortran-style arrays will have ubound equal to `extent(dimension)`. The ubound can be different for each dimension. The return value of `ubound(dimension)` will always be equal to `lbound(dimension)+extent(dimension)-1`. See the note about dimension parameters such as `firstDim` in the previous section.

    int                               zeroOffset() const;

This function has to do with the storage of arrays in memory. You may want to refer to the description of the `data..()` member functions and of custom storage orders in Section Array storage orders for clarification. The return value of `zeroOffset()` is the distance from the first element in the array to the (possibly nonexistant) element `(0,0,...,0)`. In this context, "first element" returns to the element `(base(firstDim),base(secondDim),...)`.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 2.7 Global functions

    void                              allocateArrays(TinyVector<int,N>& shape,
                                                     Array<T,N>& A,
                                                     Array<T,N>& B, ...);

This function will allocate interlaced arrays, but only if interlacing is desirable for your architecture. This is controlled by the `BZ_INTERLACE_ARRAYS` flag in ``blitz/tuning.h'`. You can provide up to 11 arrays as parameters. Any views currently associated with the array objects are lost. Here is a typical use:

    Array<int,2> A, B, C;
    allocateArrays(shape(64,64),A,B,C);

If array interlacing is enabled, then the arrays are stored in memory like this: `A(0,0)`, `B(0,0)`, `C(0,0)`, `A(0,1)`, `B(0,1)`, ... If interlacing is disabled, then the arrays are allocated in the normal fashion: each array has its own block of memory. Once interlaced arrays are allocated, they can be used just like regular arrays.

    #include <blitz/array/convolve.h>
    Array<T,1>                        convolve(const Array<T,1>& B,
                                               const Array<T,1>& C);

This function computes the 1-D convolution of the arrays B and C: A[i] = sum(B[j] * C[i-j], j) If the array B has domain b_l \ldots b_h, and array C has domain c_l \ldots c_h, then the resulting array has domain a_l \ldots a_h, with l = b_l + c_l and a_h = b_h + c_h.

A new array is allocated to contain the result. To avoid copying the result array, you should use it as a constructor argument. For example: `Array<float,1> A = convolve(B,C);` The convolution is computed in the spatial domain. Frequency-domain transforms are not used. If you are convolving two large arrays, then this will be slower than using a Fourier transform.

Note that if you need a cross-correlation, you can use the convolve function with one of the arrays reversed. For example:

    Array<float,1> A = convolve(B,C.reverse());

Autocorrelation can be performed using the same approach.

    void                              cycleArrays(Array<T,N>& A, Array<T,N>& B);
    void                              cycleArrays(Array<T,N>& A, Array<T,N>& B,
                                                  Array<T,N>& C);
    void                              cycleArrays(Array<T,N>& A, Array<T,N>& B,
                                                  Array<T,N>& C, Array<T,N>& D);
    void                              cycleArrays(Array<T,N>& A, Array<T,N>& B,
                                                  Array<T,N>& C, Array<T,N>& D,
                                                  Array<T,N>& E);

These routines are useful for time-stepping PDEs. They take a set of arrays such as [`A,B,C,D`] and cyclically rotate them to [`B,C,D,A`]; i.e. the `A` array then refers to what was `B`'s data, the `B` array refers to what was `C`'s data, and the `D` array refers to what was `A`'s data. These functions operate in constant time, since only the handles change (i.e. no data is copied; only pointers change).

    void                         find(Array<TinyVector<int,Expr::rank>,1>& indices,
                                      const _bz_ArrayExpr<Expr>& expr);
    void                         find(Array<TinyVector<int,N>,1>& indices,
                                      const Array<bool,N>& exprVals);

This is an analogue to the Matlab `find()` method, which takes a boolean array expression or an array of bools and returns a 1d array of indices for all locations where the array or expression is true.

    Array<T,N>                        imag(Array<complex<T>,N>&);

This method returns a view of the imaginary portion of the array.

    void                              interlaceArrays(TinyVector<int,N>& shape,
                                                      Array<T,N>& A,
                                                      Array<T,N>& B, ...);

This function is similar to `allocateArrays()` above, except that the arrays are always interlaced, regardless of the setting of the `BZ_INTERLACE_ARRAYS` flag.

    Array<T,N>                        real(Array<complex<T>,N>&);

This method returns a view of the real portion of the array.

    TinyVector<int,1>                 shape(int L);
    TinyVector<int,2>                 shape(int L, int M);
    TinyVector<int,3>                 shape(int L, int M, int N);
    TinyVector<int,4>                 shape(int L, int M, int N, int O);
    ... [up to 11 dimensions]

These functions may be used to create shape parameters. They package the set of integer arguments as a `TinyVector` of appropriate length. For an example use, see `allocateArrays()` above.

    void                              swap(Array<T,N>& A, Array<T,N>& B);

This function swaps the storage of two arrays, just like the `std::swap()` function does for STL container types. This is a synonym for the two-argument version of `cycleArrays()` above.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 2.8 Inputting and Outputting Arrays

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.8.1 Output formatting

The current version of Blitz++ includes rudimentary output formatting for arrays. Here's an example:

    #include <blitz/array.h>

    using namespace blitz;

    int main()
    {
        Array<int,2> A(4,5,FortranArray<2>());
        firstIndex i;
        secondIndex j;
        A = 10*i + j;

        cout << "A = " << A << endl;

        Array<float,1> B(20);
        B = exp(-i/100.);

        cout << "B = " << endl << B << endl;

        return 0;
    }

And the output:

    A = 4 x 5
    [        11        12        13        14        15
             21        22        23        24        25
             31        32        33        34        35
             41        42        43        44        45 ]

    B =
    20
     [         1   0.99005  0.980199  0.970446  0.960789  0.951229  0.941765
       0.932394  0.923116  0.913931  0.904837  0.895834   0.88692  0.878095
       0.869358  0.860708  0.852144  0.843665   0.83527  0.826959  ]

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.8.2 Inputting arrays

Arrays may be restored from an istream using the `>>` operator. Note: you must know the dimensionality of the array being restored from the stream. The `>>` operator expects an array in the same input format as generated by the `<<` operator, namely:

- The size of the array, for example "32" for a 1-dimensional array of 32 elements, "12 x 64 x 128" for a 3-dimensional array of size 12x64x128.
- The symbol `'['` indicating the start of the array data
- The array elements, listed in memory storage order
- The symbol `']'` indicating the end of the array data

The operator prototype is:

    template<class T, int N>
    istream& operator>>(istream&, Array<T,N>&);

Here is an example of saving and restoring arrays from files. You can find this example in the Blitz++ distribution as ``examples/io.cpp'`.

    #include <blitz/array.h>
    #ifdef BZ_HAVE_STD
    	#include <fstream>
    #else
    	#include <fstream.h>
    #endif

    BZ_USING_NAMESPACE(blitz)

    const char* filename = "io.data";

    void write_arrays()
    {
        ofstream ofs(filename);
        if (ofs.bad())
        {
            cerr << "Unable to write to file: " << filename << endl;
            exit(1);
        }

        Array<float,3> A(3,4,5);
        A = 111 + tensor::i + 10 * tensor::j + 100 * tensor::k;
        ofs << A << endl;

        Array<float,2> B(3,4);
        B = 11 + tensor::i + 10 * tensor::j;
        ofs << B << endl;

        Array<float,1> C(4);
        C = 1 + tensor::i;
        ofs << C << endl;
    }

    int main()
    {
        write_arrays();

        ifstream ifs(filename);
        if (ifs.bad())
        {
            cerr << "Unable to open file: " << filename << endl;
            exit(1);
        }

        Array<float,3> A;
        Array<float,2> B;
        Array<float,1> C;

        ifs >> A >> B >> C;

        cout << "Arrays restored from file: " << A << B << C << endl;

        return 0;
    }

Note: The storage order and starting indices are not restored from the input stream. If you are restoring (for example) a Fortran-style array, you must create a Fortran-style array, and then restore it. For example, this code restores a Fortran-style array from the standard input stream:

    Array<float,2> B(fortranArray);
    cin >> B;

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

## 2.9 Array storage orders

Blitz++ is very flexible about the way arrays are stored in memory. Starting indices can be 0, 1, or arbitrary numbers; arrays can be stored in row major, column major or an order based on any permutation of the dimensions; each dimension can be stored in either ascending or descending order. An N dimensional array can be stored in N! 2^N possible ways.

Before getting into the messy details, a review of array storage formats is useful. If you're already familiar with strides and bases, you might want to skip on to the next section.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.9.1 Fortran and C-style arrays

Suppose we want to store this two-dimensional array in memory:

    [ 1 2 3 ]
    [ 4 5 6 ]
    [ 7 8 9 ]

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

#### Row major vs. column major

To lay the array out in memory, it's necessary to map the indices (i,j) into a one-dimensional block. Here are two ways the array might appear in memory:

    [ 1 2 3 4 5 6 7 8 9 ]
    [ 1 4 7 2 5 8 3 6 9 ]

The first order corresponds to a C or C++ style array, and is called row-major ordering: the data is stored first by row, and then by column. The second order corresponds to a Fortran style array, and is called column-major ordering: the data is stored first by column, and then by row.

The simplest way of mapping the indices (i,j) into one-dimensional memory is to take a linear combination.(2) Here's the appropriate linear combination for row major ordering:

    memory offset = 3*i + 1*j

And for column major ordering:

    memory offset = 1*i + 3*j

The coefficients of the (i,j) indices are called strides. For a row major storage of this array, the row stride is 3 - you have to skip three memory locations to move down a row. The column stride is 1 - you move one memory location to move to the next column. This is also known as unit stride. For column major ordering, the row and column strides are 1 and 3, respectively.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

#### Bases

To throw another complication into this scheme, C-style arrays have indices which start at zero, and Fortran-style arrays have indices which start at one. The first valid index value is called the base. To account for a non-zero base, it's necessary to include an offset term in addition to the linear combination. Here's the mapping for a C-style array with i=0..3 and j=0..3:

    memory offset =  0 + 3*i + 1*j

No offset is necessary since the indices start at zero for C-style arrays. For a Fortran-style array with i=1..4 and j=1..4, the mapping would be:

    memory offset = -4 + 3*i + 1*j

By default, Blitz++ creates arrays in the C-style storage format (base zero, row major ordering). To create a Fortran-style array, you can use this syntax:

    Array<int,2> A(3, 3, FortranArray<2>());

The third parameter, `FortranArray<2>()`, tells the `Array` constructor to use a storage format appropriate for two-dimensional Fortran arrays (base one, column major ordering).

A similar object, `ColumnMajor<N>`, tells the `Array` constructor to use column major ordering, with base zero:

    Array<int,2> B(3, 3, ColumnMajor<2>());

This creates a 3x3 array with indices i=0..2 and j=0..2.

In addition to supporting the 0 and 1 conventions for C and Fortran-style arrays, Blitz++ allows you to choose arbitrary bases, possibly different for each dimension. For example, this declaration creates an array whose indices have ranges i=5..8 and j=2..5:

    Array<int,2> A(Range(5,8), Range(2,5));

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.9.2 Creating custom storage orders

All `Array` constructors take an optional parameter of type `GeneralArrayStorage<N_rank>`. This parameter encapsulates a complete description of the storage format. If you want a storage format other than C or Fortran-style, you have two choices:

- You can create an object of type `GeneralArrayStorage<N_rank>`, customize the storage format, and use the object as a argument for the `Array` constructor.
- You can create your own storage format object which inherits from `GeneralArrayStorage<N_rank>`. This is useful if you will be using the storage format many times. This approach (inheriting from `GeneralArrayStorage<N_rank>`) was used to create the `FortranArray<N_rank>` objects. If you want to take this approach, you can use the declaration of `FortranArray<N_rank>` in `<blitz/array.h>` as a guide.

The next sections describe how to modify a `GeneralArrayStorage<N_rank>` object to suit your needs.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

#### In higher dimensions

In more than two dimensions, the choice of storage order becomes more complicated. Suppose we had a 3x3x3 array. To map the indices (i,j,k) into memory, we might choose one of these mappings:

    memory offset = 9*i + 3*j + 1*k
    memory offset = 1*i + 3*j + 9*k

The first corresponds to a C-style array, and the second to a Fortran-style array. But there are other choices; we can permute the strides (1,3,9) any which way:

    memory offset = 1*i + 9*j + 3*k
    memory offset = 3*i + 1*j + 9*k
    memory offset = 3*i + 9*j + 1*k
    memory offset = 9*i + 1*j + 3*k

For an N dimensional array, there are N! such permutations. Blitz++ allows you to select any permutation of the dimensions as a storage order. First you need to create an object of type `GeneralArrayStorage<N_rank>`:

    GeneralArrayStorage<3> storage;

`GeneralArrayStorage<N_rank>` contains a vector called `ordering` which controls the order in which dimensions are stored in memory. The `ordering` vector will contain a permutation of the numbers 0, 1, ..., N_rank-1. Since some people are used to the first dimension being 1 rather than 0, a set of symbols (firstDim, secondDim, ..., eleventhDim) are provided which make the code more legible.

The `ordering` vector lists the dimensions in increasing order of stride. You can access this vector using the member function `ordering()`. A C-style array, the default, would have:

    storage.ordering() = thirdDim, secondDim, firstDim;

meaning that the third index (k) is associated with the smallest stride, and the first index (i) is associated with the largest stride. A Fortran-style array would have:

    storage.ordering() = firstDim, secondDim, thirdDim;

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

#### Reversed dimensions

To add yet another wrinkle, there are some applications where the rows or columns need to be stored in reverse order.(3)

Blitz++ allows you to store each dimension in either ascending or descending order. By default, arrays are always stored in ascending order. The `GeneralArrayStorage<N_rank>` object contains a vector called `ascendingFlag` which indicates whether each dimension is stored ascending (`true`) or descending (`false`). To alter the contents of this vector, use the `ascendingFlag()` method:

    // Store the third dimension in descending order
    storage.ascendingFlag() = true, true, false;

    // Store all the dimensions in descending order
    storage.ascendingFlag() = false, false, false;

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

#### Setting the base vector

`GeneralArrayStorage<N_rank>` also has a `base` vector which contains the base index value for each dimension. By default, the base vector is set to zero. `FortranArray<N_rank>` sets the base vector to one.

To set your own set of bases, you have two choices:

- You can modify the `base` vector inside the `GeneralArrayStorage<N_rank>` object. The method `base()` returns a mutable reference to the `base` vector which you can use to set the bases.
- You can provide a set of `Range` arguments to the `Array` constructor.

Here are some examples of the first approach:

    // Set all bases equal to 5
    storage.base() = 5;

    // Set the bases to [ 1 0 1 ]
    storage.base() = 1, 0, 1;

And of the second approach:

    // Have bases of 5, but otherwise C-style storage
    Array<int,3> A(Range(5,7), Range(5,7), Range(5,7));

    // Have bases of [ 1 0 1 ] and use a custom storage
    Array<int,3> B(Range(1,4), Range(0,3), Range(1,4), storage);

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

#### Working simultaneously with different storage orders

Once you have created an array object, you will probably never have to worry about its storage order. Blitz++ should handle arrays of different storage orders transparently. It's possible to mix arrays of different storage orders in one expression, and still get the correct result.

Note however, that mixing different storage orders in an expression may incur a performance penalty, since Blitz++ will have to pay more attention to differences in indexing than it normally would.

You may not mix arrays with different domains in the same expression. For example, adding a base zero to a base one array is a no-no. The reason for this restriction is that certain expressions become ambiguous, for example:

    Array<int,1> A(Range(0,5)), B(Range(1,6));
    A=0;
    B=0;
    using namespace blitz::tensor;
    int result = sum(A+B+i);

Should the index `i` take its domain from array `A` or array `B`? To avoid such ambiguities, users are forbidden from mixing arrays with different domains in an expression.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

#### Debug dumps of storage order information

In debug mode (`-DBZ_DEBUG`), class `Array` provides a member function `dumpStructureInformation()` which displays information about the array storage:

    Array<float,4> A(3,7,8,2,FortranArray<4>());
    A.dumpStructureInformation(cerr);

The optional argument is an `ostream` to dump information to. It defaults to `cout`. Here's the output:

    Dump of Array<f, 4>:
    ordering_      = 4 [          0         1         2         3 ]
    ascendingFlag_ = 4 [          1         1         1         1 ]
    base_          = 4 [          1         1         1         1 ]
    length_        = 4 [          3         7         8         2 ]
    stride_        = 4 [          1         3        21       168 ]
    zeroOffset_    = -193
    numElements()  = 336
    isStorageContiguous() = 1

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

#### A note about storage orders and initialization

When initializing arrays with comma delimited lists, note that the array is filled in storage order: from the first memory location to the last memory location. This won't cause any problems if you stick with C-style arrays, but it can be confusing for Fortran-style arrays:

    Array<int,2> A(3, 3, FortranArray<2>());
    A = 1, 2, 3,
        4, 5, 6,
        7, 8, 9;
    cout << A << endl;

The output from this code excerpt will be:

    A = 3 x 3
             1         4         7
             2         5         8
             3         6         9

This is because Fortran-style arrays are stored in column major order.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

### 2.9.3 Storage orders example

    #include <blitz/array.h>

    BZ_USING_NAMESPACE(blitz)

    int main()
    {
        // 3x3 C-style row major storage, base zero
        Array<int,2> A(3, 3);

        // 3x3 column major storage, base zero
        Array<int,2> B(3, 3, ColumnMajorArray<2>());

        // A custom storage format:
        // Indices have range 0..3, 0..3
        // Column major ordering
        // Rows are stored ascending, columns stored descending
        GeneralArrayStorage<2> storage;
        storage.ordering() = firstRank, secondRank;
        storage.base() = 0, 0;
        storage.ascendingFlag() = true, false;

        Array<int,2> C(3, 3, storage);

        // Set each array equal to
        // [ 1 2 3 ]
        // [ 4 5 6 ]
        // [ 7 8 9 ]

        A = 1, 2, 3,
            4, 5, 6,
            7, 8, 9;

        cout << "A = " << A << endl;

        // Comma-delimited lists initialize in memory-storage order only.
        // Hence we list the values in column-major order to initialize B:

        B = 1, 4, 7, 2, 5, 8, 3, 6, 9;

        cout << "B = " << B << endl;

        // Array C is stored in column major, plus the columns are stored
        // in descending order!

        C = 3, 6, 9, 2, 5, 8, 1, 4, 7;

        cout << "C = " << C << endl;

        Array<int,2> D(3,3);
        D = A + B + C;

    #ifdef BZ_DEBUG
        A.dumpStructureInformation();
        B.dumpStructureInformation();
        C.dumpStructureInformation();
        D.dumpStructureInformation();
    #endif

        cout << "D = " << D << endl;

        return 0;
    }

And the output:

    A = 3 x 3
    [         1         2         3
              4         5         6
              7         8         9 ]

    B = 3 x 3
    [         1         2         3
              4         5         6
              7         8         9 ]

    C = 3 x 3
    [         1         2         3
              4         5         6
              7         8         9 ]

    Dump of Array<i, 2>:
    ordering_      = 2 [          1         0 ]
    ascendingFlag_ = 2 [          1         1 ]
    base_          = 2 [          0         0 ]
    length_        = 2 [          3         3 ]
    stride_        = 2 [          3         1 ]
    zeroOffset_    = 0
    numElements()  = 9
    isStorageContiguous() = 1
    Dump of Array<i, 2>:
    ordering_      = 2 [          0         1 ]
    ascendingFlag_ = 2 [          1         1 ]
    base_          = 2 [          0         0 ]
    length_        = 2 [          3         3 ]
    stride_        = 2 [          1         3 ]
    zeroOffset_    = 0
    numElements()  = 9
    isStorageContiguous() = 1
    Dump of Array<i, 2>:
    ordering_      = 2 [          0         1 ]
    ascendingFlag_ = 2 [          1         0 ]
    base_          = 2 [          0         0 ]
    length_        = 2 [          3         3 ]
    stride_        = 2 [          1        -3 ]
    zeroOffset_    = 6
    numElements()  = 9
    isStorageContiguous() = 1
    Dump of Array<i, 2>:
    ordering_      = 2 [          1         0 ]
    ascendingFlag_ = 2 [          1         1 ]
    base_          = 2 [          0         0 ]
    length_        = 2 [          3         3 ]
    stride_        = 2 [          3         1 ]
    zeroOffset_    = 0
    numElements()  = 9
    isStorageContiguous() = 1
    D = 3 x 3
    [         3         6         9
             12        15        18
             21        24        27 ]

[ << ] | [ >> ] |  |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

This document was generated by Charlie & on April, 23 2009 using texi2html 1.76.