2.1: Getting started |
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.
2.1.1: Template parameters |
The To use 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.
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;
2.1.2: Array types |
The
Array<T,N>
class supports a variety of arrays:
Array<int,1>
and Array<float,3>
Array<complex<float>,2>
Polynomial
, then Array<Polynomial,2>
is an array of
Polynomial
objects.
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.
Array<Array<int,1>,1>
, in
which each element is a variable-length array.
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; } |
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 |
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:
Creating custom array storage formats is described in a later section
(2.9).
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.
2.2: Public types |
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.
2.3: Constructors |
2.3.1: Default constructor |
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).
2.3.3: Constructors which take extent parameters |
2.3.4: Constructors with Range arguments |
2.3.5: Referencing another array |
This constructor makes a shared view of another array's data:
After this constructor is used, both Array(Array<T_numtype, N_rank>& array);
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.
2.3.6: Constructing an array from an expression |
Arrays may be constructed from expressions, which are described in 3. 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));
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
If you choose These constructors create array objects from pre-existing data:
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
The 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:
This is a tad awkward, so Blitz++ provides the global
object Another version of this constructor allows you to pass an arbitrary
vector of strides:
preexistingMemoryPolicy
:
enum preexistingMemoryPolicy {
duplicateData,
deleteDataWhenDone,
neverDeleteData
};
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.
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);
shape()
function. For example:
double data[] = { 1, 2, 3, 4 };
Array<double,2> A(data, shape(2,2), neverDeleteData); // Make a 2x2 array
shape()
function takes N integer arguments and returns a
TinyVector<int,N>
.
Array<double,2> B(data, shape(2,2), neverDeleteData,
FortranArray<2>());
fortranArray
which will convert to an
instance of GeneralArrayStorage<N_rank>
:
Array<double,2> B(data, shape(2,2), neverDeleteData, fortranArray);
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>())
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 The first parameter of A related routine is Unlike 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()
and allocateArrays()
.
An example:
Array<int,2> A, B;
interlaceArrays(shape(10,10), A, B);
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.
allocateArrays()
, which has identical syntax:
Array<int,2> A, B;
allocateArrays(shape(10,10), A, B);
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.
interlaceArrays()
after
running some benchmarks to determine whether interlacing
is beneficial for your particular algorithm and architecture.
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.
2.4: Indexing, subarrays, and slicing |
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.
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
You can also get an element by providing an operand
of type TinyVector<int,N_rank> where This version of It's possible to use fewer than 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.
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;
operator()
is also available in a const-overloaded
version.
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.
2.4.2: Subarrays |
You can obtain a subarray by providing The object B now refers to elements (5..7,5..7,0..2) of the
array A.
The returned subarray is of type The bases of the subarray are equal to the bases of the original array:
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.
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));
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.;
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
2.4.3: Slicing |
A combination of integer and Range operands produces a slice.
Each integer operand reduces the rank of the array by one. For
example:
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,
Array<int,2> F = A(Range::all(), 2, Range::all());
Array<int,1> G = A(2, 7, Range::all());
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());
2.4.4: More about Range objects |
2.4.5: 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:
Statement 1 results in a portion of 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 2.6.2).
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:
Array<int,1> A(5), B(10);
A = B(Range(0,4)); // Statement 1
Array<int,1> C = B(Range(0,4)); // Statement 2
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.
A.resize(B.shape()); // Make A the same size as B
A = B;
2.4.6: 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, 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 |
2.5: Debug mode |
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); // Since this is a C-style array, the valid index // ranges are 0..3 and 0..3 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.h line 1070 Array index out of range: (4, 4) Lower bounds: [ 0 0 ] Upper bounds: [ 3 3 ] Assertion failed: __EX, file ../../blitz/array.h, line 1070 IOT/Abort trap (core dumped) |
Precondition failures send their error messages to the standard
error stream (cerr
). After displaying the error message,
assert(0)
is invoked.
2.6: Member functions |
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:
reverse()
member function won't stand a chance of
understanding what A.reverse(2)
does.
As a solution to this problem, Blitz++ provides a series of symbolic constants which you can use to refer to dimensions:
These symbols should be used in place of the numerals
0, 1, ... N_rank - 1. For example:
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 const int firstDim = 0;
const int secondDim = 1;
const int thirdDim = 2;
.
.
const int eleventhDim = 10;
A.reverse(thirdDim);
firstDim
, secondDim
, ... aesthetically unpleasing,
there are equivalent symbols firstRank
, secondRank
,
thirdRank
, ..., eleventhRank
.
2.6.2: Member function descriptions |
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
Other relevant functions are:
Now the B array refers to the 2nd component of every element in A.
Note: for complex arrays, special global functions
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.
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.
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);
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 2.9.
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, ...);
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 2.9.
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 2.9
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),...)
.
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.
If the array B has domain bl..bh, and array C has domain cl..ch,
then the resulting array has domain al..ah, with al=bl+cl and
ah=bh+ch.
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:
Note that if you need a cross-correlation, you can use the
convolve function with one of the arrays reversed. For
example:
#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)
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.
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).
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.
2.8: Inputting and Outputting Arrays |
2.8.1: Output formatting |
The current version of Blitz++ includes rudimentary output formatting
for arrays. Here's an example:
And the output:
#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;
}
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 ]
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:
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!2N
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.
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 ]
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. (Taking a linear
combination is sufficient for dense, asymmetric arrays, such as
are provided by the Blitz++ Array
class.) 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.
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));
2.9.2: Creating custom storage orders |
All The next sections describe how to modify a 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:
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:
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
The 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:
Reversed dimensions To add yet another wrinkle, there are some applications where the
rows or columns need to be stored in reverse order. (For example,
certain bitmap formats store image rows from bottom to top rather than
top to bottom.)
Blitz++ allows you to store each dimension in either ascending or
descending order. By default, arrays are always stored in ascending
order. The Setting the base vector To set your own set of bases, you have two choices:
Here are some examples of the first approach:
And of the second approach:
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:
Should the index Debug dumps of storage order information In debug mode ( The optional argument is an 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:
The output from this code excerpt will be:
This is because Fortran-style arrays are stored in column
major order.
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:
GeneralArrayStorage<N_rank>
,
customize the storage format, and use the object as a argument
for the Array
constructor.
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.
GeneralArrayStorage<N_rank>
object to suit your needs.
memory offset = 9*i + 3*j + 1*k
memory offset = 1*i + 3*j + 9*k
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
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.
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;
storage.ordering() = firstDim, secondDim, thirdDim;
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;
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.
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.
Range
arguments to the
Array
constructor.
// Set all bases equal to 5
storage.base() = 5;
// Set the bases to [ 1 0 1 ]
storage.base() = 1, 0, 1;
// 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);
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);
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.
-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);
ostream
to dump information
to. It defaults to cout
. Here's the output:
Dump of Array<float, 4>:
ordering_ = [ 0 1 2 3 ]
ascendingFlag_ = [ 1111 ]
base_ = [ 1 1 1 1 ]
length_ = [ 3 7 8 2 ]
stride_ = [ 1 3 21 168 ]
zeroOffset_ = -193
numElements() = 336
storageContiguous = 1
Array<int,2> A(3, 3, FortranArray<2>());
A = 1, 2, 3,
4, 5, 6,
7, 8, 9;
cout << A << endl;
A = 3 x 3
1 4 7
2 5 8
3 6 9
2.9.3: Storage orders example |
/***************************************************************************** * storage.cpp Blitz++ Array custom storage orders example * * $Id: storage.cpp,v 1.1 2000/06/19 13:03:29 tveldhui Exp $ * * $Log: storage.cpp,v $ * Revision 1.1 2000/06/19 13:03:29 tveldhui * Initial source check-in; added files not usually released in the * distribution. * * Revision 1.1 1997/07/16 19:38:23 tveldhui * Update: Alpha release 0.2 (Arrays) * ***************************************************************************** */ #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 D = 3 x 3 3 6 9 12 15 18 21 24 27 |