The Boost Multidimensional Array Library
(Boost.MultiArray)

Synopsis

The Boost Multidimensional Array Library provides a class template for
multidimensional arrays, as well as semantically equivalent
adaptors for arrays of contiguous data. The classes in this library
implement a common interface, formalized as a generic programming
concept. The interface design is in line with the precedent set by the
C++ Standard Library containers. Boost MultiArray is a more efficient
and convenient way to express N-dimensional arrays than existing
alternatives (especially the
std::vector<std::vector<...>> formulation
of N-dimensional arrays). The arrays provided by the library may be
accessed using the familiar syntax of native C++ arrays. Additional
features, such as resizing, reshaping, and creating views are
available (and described below).

Introduction

The C++ standard library provides several generic containers, but it
does not provide any multidimensional array types. The
std::vector class template can be used to implement
N-dimensional arrays, for example expressing a 2-dimensional array of
double elements using the type
std::vector<std::vector<double>>, but the
resulting interface is unwieldy and the memory overhead can be quite
high. Native C++ arrays (i.e. int arr[2][2][2];) do not
immediately interoperate well with the C++ Standard Library, and they
also lose information at function call boundaries (specifically the
extent of the last dimension). Finally, a dynamically allocated
contiguous block of elements can be treated as an array, though this
method requires manual bookkeeping that is error prone and obfuscates
the intent of the programmer.

The Boost MultiArray library enhances the C++ standard containers with
versatile multi-dimensional array abstractions. It includes a general
array class template and native array adaptors that support idiomatic
array operations and interoperate with C++ Standard Library containers
and algorithms. The arrays share a common interface, expressed as a
generic programming in terms of which generic array algorithms can be
implemented.

This document is meant to provide an introductory tutorial and user's
guide for the most basic and common usage patterns of MultiArray
components. The reference manual
provides more complete and formal documentation of library features.

MultiArray Components

multi_array is a container template. When instantiated, it
allocates space for the number of elements corresponding to the
dimensions specified at construction time. A multi_array may
also be default constructed and resized as needed.

multi_array_ref adapts an existing array of data to provide
the multi_array interface. multi_array_ref does not own the
data passed to it.

const_multi_array_ref is similar to multi_array_ref
but guarantees that the contents of the array are immutable. It can
thus wrap pointers of type T const*.

The three components exhibit very similar behavior. Aside from
constructor parameters, multi_array and
multi_array_ref export the same interface.
const_multi_array_ref provides only the constness-preserving
portions of the multi_array_ref interface.

Construction and Assignment

All of the non-const array types in this library provide assignment
operatorsoperator=(). Each of the array types multi_array,
multi_array_ref, subarray, and
array_view can be assigned from any
of the others, so long as their shapes match. The
const variants, const_multi_array_ref,
const_subarray, and const_array_view, can be the
source of a copy to an array with matching shape.
Assignment results in a deep (element by element) copy of the data
contained within an array.

Array View and Subarray Type Generators

In some situations, the use of nested generators for array_view and
subarray types is inconvenient. For example, inside a
function template parameterized upon array type, the extra
"template" keywords can be obfuscating. More likely though, some
compilers cannot handle templates nested within template parameters.
For this reason the type generators, subarray_gen,
const_subarray_gen, array_view_gen, and
const_array_view_gen are provided. Thus, the two typedefs
in the following example result in the same type:

Specifying Array Dimensions

When creating most of the Boost.MultiArray components, it is necessary
to specify both the number of dimensions and the extent of each
(boost::multi_array also provides a default constructor).
Though the number of dimensions is always specified as a template
parameter, two separate mechanisms have been provided to specify the
extent of each.

The first method involves passing a
Collection of extents to a
constructor, most commonly a boost::array. The constructor
will retrieve the beginning iterator from the container and retrieve N
elements, corresponding to extents for the N dimensions. This is
useful for writing dimension-independent code.

Example

The second method involves passing the constructor an extent_gen
object, specifying the matrix dimensions. The extent_gen type
is defined in the multi_array_types namespace and as a
member of every array type, but by default, the library constructs a
global extent_gen object boost::extents. In case of
concern about memory used by these objects, defining
BOOST_MULTI_ARRAY_NO_GENERATORS before including the library
header inhibits its construction.

Example

This can be useful for writing dimension-independent code, and under
some compilers may yield higher performance than operator[].

By default, both of the above element access methods perform range
checking. If a supplied index is out of the range defined for an
array, an assertion will abort the program. To disable range
checking (for performance reasons in production releases), define
the BOOST_DISABLE_ASSERTS preprocessor macro prior to
including multi_array.hpp in your application.

Creating Views

Boost.MultiArray provides the facilities for creating a sub-view of an
already existing array component. It allows you to create a sub-view that
retains the same number of dimensions as the original array or one
that has less dimensions than the original as well.

Sub-view creation occurs by placing a call to operator[], passing
it an index_gen type. The index_gen is populated by
passing index_range objects to its operator[].
The index_range and index_gen types are defined in
the multi_array_types namespace and as nested members of
every array type. Similar to boost::extents, the library by
default constructs the object boost::indices. You can
suppress this object by
defining BOOST_MULTI_ARRAY_NO_GENERATORS before including the
library header. A simple sub-view creation example follows.

An index_range object passed to a slicing operation will
inherit its start and/or finish value from the array being sliced if
you do not supply one. This conveniently prevents you from having to
know the bounds of the array dimension in certain cases. For example,
the default-constructed range will take the full extent of the
dimension it is used to specify.

Storage Ordering

Each array class provides constructors that accept a storage ordering
parameter. This is most
useful when interfacing with legacy codes that require an ordering
different from standard C, such as FORTRAN. The possibilities are
c_storage_order, fortran_storage_order, and
general_storage_order.

c_storage_order, which is the default, will store elements
in memory in the same order as a C array would, that is, the
dimensions are stored from last to first.

fortran_storage_order will store elements in memory in the same order
as FORTRAN would: from the first dimension to
the last. Note that with use of this parameter, the array
indices will remain zero-based.

Setting The Array Base

In some situations, it may be inconvenient or awkward to use an
array that is zero-based.
the Boost.MultiArray components provide two facilities for changing the
bases of an array. One may specify a pair of range values, with
the extent_range type, to
the extent_gen constructor in order to set the base value.

Changing an Array's Shape

The Boost.MultiArray arrays provide a reshape operation. While the
number of dimensions must remain the same, the shape of the array may
change so long as the total number of
elements contained remains the same.

Resizing an Array

The boost::multi_array class provides an element-preserving
resize operation. The number of dimensions must remain the same, but
the extent of each dimension may be increased and decreased as
desired. When an array is made strictly larger, the existing elements
will be preserved by copying them into the new underlying memory and
subsequently destructing the elements in the old underlying memory.
Any new elements in the array are default constructed. However, if
the new array size shrinks some of the dimensions, some elements will
no longer be available.

MultiArray Concept

Boost.MultiArray defines and uses the
MultiArray
concept. It specifies an interface for N-dimensional containers.

Test Cases

Boost.MultiArray comes with a suite of test cases meant to exercise
the features and semantics of the library. A description of the test
cases can be found here.

Related Work

boost::array
and std::vector are
one-dimensional containers of user data. Both manage their own
memory. std::valarray is a low-level
C++ Standard Library component
meant to provide portable high performance for numerical applications.
Blitz++ is
an array library developed by Todd
Veldhuizen. It uses
advanced C++ techniques to provide near-Fortran performance for
array-based numerical applications.
array_traits is a beta library, formerly distributed with
Boost, that provides a means to create iterators over native C++
arrays.
This library is analogous to
boost::array in that it augments C style N-dimensional
arrays, as boost::array does for C one-dimensional arrays.

Credits

Jeremy Siek
helped with the library and provided a sounding board for ideas,
advice, and assistance porting to Microsoft Visual C++.

Giovanni Bavestrelli
provided an early implementation of an
N-dimensional array which inspired feedback from the
Boost mailing list
members. Some design decisions in this work were based upon this
implementation and the comments it elicited.

Todd Veldhuizen wrote
Blitz++, which
inspired some aspects of this design. In addition, he supplied
feedback on the design and implementation of the library.

Jeremiah Willcock
provided feedback on the implementation and design of the
library and some suggestions for features.