File | Date | Author | Commit |
---|---|---|---|
.settings | 2019-03-26 |
![]() |
[62c358] Initial commit |
bin | 2019-03-26 |
![]() |
[62c358] Initial commit |
libs | 2019-03-26 |
![]() |
[62c358] Initial commit |
obj | 2019-03-26 |
![]() |
[62c358] Initial commit |
.cproject | 2019-03-26 |
![]() |
[62c358] Initial commit |
.project | 2019-03-26 |
![]() |
[62c358] Initial commit |
Defines.mk | 2019-03-26 |
![]() |
[62c358] Initial commit |
Makefile | 2019-03-26 |
![]() |
[62c358] Initial commit |
Patterns.mk | 2019-03-26 |
![]() |
[62c358] Initial commit |
README.md | 2019-03-26 |
![]() |
[62c358] Initial commit |
Targets.mk | 2019-03-26 |
![]() |
[62c358] Initial commit |
defs.hh | 2019-03-26 |
![]() |
[62c358] Initial commit |
main.cc | 2019-03-26 |
![]() |
[62c358] Initial commit |
mda.hh | 2019-03-26 |
![]() |
[62c358] Initial commit |
mda.zip | 2019-03-26 |
![]() |
[62c358] Initial commit |
Multidimensional Array (MDA) is a class for creating and using
multidimensional arrays.
A two dimensional array is just a one dimensional array of one dimensional arrays. Similarly, a three dimensional array is just a one dimensional array of two dimensional arrays. Or equally as well, a three dimensional array is just a two dimensional array of one dimensional arrays.
Storage for the array is based on a templated C++ STL vector class. The beauty of the STL vector class is that it automagically resizes itself when constructing or copying. Caveat Emptor.
The multidimensional arrays are indexed using unsigned integers.
The types that may stored may be any that provide a copy and assignment operator.
The MDA class does not implement matrix algebra operations.
This code is a significant variation of someone else's template implementation. I kind of remember it coming out of a book, but I can't find that
book in my personal library and I can't find it on the web either.
I apologize that I am not able to remember from who's implementation
I have derived mine.
C++11 now implements the array class but I wrote the first implementation around 2003 before C++11 was published.
Insofar as copyrights and whatnot, there are no restrictions
on the reuse or modification of this code for any purpose
whatsoever. There are no guarantees as to this code's correctness.
Usage:
You must create an array specification, that is, an array of unsigned integers where each integer specifies the size of successive dimensions.
For example, to create a MDA specification for a 2x5 array of floats:
vector<float> mdaspec{2,5};
The MDA specification is the most basic way of defining the nature of the MDA.
Having defined a MDA specification, create a 2 dimensional MDA as follows:
MDA<float,2> mda2d(mdaspec)</float,2>
This is interpreted as creating a multidimensional array of floats in two dimensions; the first dimension has 2 elements, the second dimension has 5 elements.
Example, to create a specification for a 3x4x5 array of integers;
vector<int> mdaspec{3,4,5};
Instantiate the 3 dimensional MDA of integers as follows:
MDA<int,3> mda3d(mdaspec);
One can use a greater dimension MDA specification to instantiate a lesser array, for example,
vector<float> mdaspec{3,4,5}
MDA<float,2> mda2d(mdaspec)
In this case only the first two [3,4} dimensions are used, ie, this creates
a MDA for storing floats in two dimensions; the first dimension has 3 elements and the 2nd has 4 elements. The 3rd dimension is ignored.
Create an empty 2 dimensional array and resize it;
vector<float> mdaspec{5,3,10,7}
MDA<float,2> a2d; // empty array
a2d.resize( mdaspec ); // resizes using MDA specification</float,2></float>
This creates a MDA which stores floats in two dimensions; the 1st dimension has 5 elements, the second has 3 elements.
Example, assume we have an existing 2D array called a2d and want to create a copy.
MDA<float,2> a2dCopy(a2d); // copy by constructor
or
MDA<float,2> a2dCopy;
a2dCopy = a2d; // copy by assignment
Example, assume an existing 3D array called a3d and want to create a copy from a 2D subset.
MDA<float,2> a2d; // empty
a2d.copy(a3d[0]); // copy 2D subset of 3D's 1st element
Assign and retrieve as expected using unsigned integers.
float x = a2d[44][11];
a2d[20][10] = x;
An exception, IndexingException, is thrown for out of range indices.
The IndexingException class catches the exception which is raised if an
[] operator access is out of bounds. It simply prints out the
dimension being accessed, the index attempted, and the maximum
number of elements in that dimension.
Reducing will delete elements, while expanding will add elements with default values. Use the resize() member function, for example, given a MDA specification:
vector<unsigned int> mdaspec{6,8,10};
MDA<unsigned int,3> a3d(mdaspec);
Expand the 3rd dimension to 20 as follows:
mdaspec[2]=20;
a3d.resize(mdaspec);
a3d is now a 6x8x20 array.
To expand the array to 6x20x40:
mdaspec[1]=20;mdaspec[2]=40;
a3d.resize(mdaspec);
Reduce a3d to 3x3x3 as follows:
mdaspec[0] = 3; mdaspec[1] = 3; mdaspec[2] = 3;
a3d.resize(mdaspec);
A simple command line program (with Makefile) is provided to demonstrate
the usage and capabilities of the MDA class. Also the user should look at
the code in main.cc to familiarize themselves. I have used MDA for modeling
5 and 7 dimensional probability density functions and believe the efficiency
to be very reasonable.