Under what conditions can arrays be C-contiguous and F-contiguous simultaneosly?
I can think of the following:
The 1D case that is trivially C- and F-contiguous.
Similarly, multi-dimensional arrays where all dimensions are singleton dimensions except for one.
Are there any others?
You got it. An array is both C and Fortran contiguous (i.e. is both row major and column major) when it has at most 1 dimension longer than 1. Basically, vectors and scalars, plus degenerate arrays with additional "unnecessary" dimensions.
Related
Ok, I have a some array A[4][4], and another A[16], which are both just different representations of each other. Now, I am given an elements position on the 2D array, but I have to access it from the 1D array. IE, if I'm told to access the element A[2][0] in my 1D array, how would I do that?
In this simple example, A[2][0] maps to A[8] since you are requesting the first element in the third group of four. Similarly, A[0][2] maps to A[2] since you are requesting the third element of the first group of four. In general (A[i][j]) you are requesting the (i*4+j)-th element of A.
In the even more general case, you are requesting the (i*width+j)-th element.
This depends on your programming language and choice of array type. Depending on your language, arrays are either stored in row-major order or column-major order:
Edit: Java does not have multidimensional arrays, as per the documentation: In Java, a multi-dimensional array is structured an array of arrays, i.e. an array whose elements are references to array objects. This means that each row can be of different length and therefore the storage format is neither row-major nor column-major.
Row-major order is used in C/C++, PL/I, Python, Speakeasy and others. Column-major order is used in Fortran, MATLAB, GNU Octave, R, Julia, Rasdaman, and Scilab.
In some languages, you can also choose the order (e.g. MATLAB)
For row-major order, A[2][0] would be at A[2*4+0] (where 4 is the size of one row):
offset = row*NUMCOLS + column
For column-major order, A[2][0] would be at A[0*4+2] (where 4 is the size of one column):
offset = row + column*NUMROWS
It really depends on your programming language!
In short:
I need to load multiple 2d numpy matrices - with different sizes - into one numpy matrix. First, I tried to load them as the 3rd dimension of the matrix but due to the various sizes it wasn't possible. It would be great if you could direct me how to do it, or even any other convenient solution.
More description:
I'm reading arrays from multiple csv files and loading them into numpy arrays. This is easy when the number of csv files are known and the arrays are at the same size. Whereas, I need to do this for any number of files at different sizes. So I thought I could use numpy's structured arrays, but I can't do it.
I think Numpy is going to require mathematically consistent matrices... which means all rows/columns in a 2D matrix have to have the same number of elements. Likewise, if you try to make a 3-dimensional matrix, each 2D matrix will have to have the same dimensions.
However, you could use a regular Python array of numpy matrices. If you do that, then you can dynamically add/remove individual matrices, and they can have varying dimensions.
Does anyone know how to do array matrix multiplication in matlab? i.e. I have two 3 dimensional arrays consisting of sets of matrices in the first 2 dimensions and I would like to multiply each matrix in the first array with the corresponding one in the second array. So, i.e. if
A=randn(3,3);
B=cat(3,A,A);
I would like [[operation]] such that
B[[operation]]B = cat(3,A*A, A*A)
done in efficient vector form.
Many thanks in advance.
I have used MULTIPROD from the Mathworks FileExchange for N-D array multiplication before. It is basically an extension of bsxfun to N-D arrays, and works quite nicely (and fast) - although the interface is a bit cumbersome.
When using arrays I generally use only a one or two dimensional array -- very rarely three or more. I'm just kind of curious, what are some interesting/practical uses for arrays with three or more dimensions? Have you ever used an array with four or more dimensions? I had a professor in college use a six dimensional array in a program he demonstrated in class...ever had more than this?
In scientific programming it can be quite common. We just start calling these higher dimensional arrays tensors. Scalars are 0-dimensional tensors, vectors are 1-dimensional tensors, matrices are 2-dimensional tensors, and after that we just call them d-dimensional tensors (d=3,4,5,6). Dot products are then called contractions over indices.
Where are they used? I use them in some of my physics simulations. For instance, one method for simulating electrons on a lattice (regular array of sites) uses a tensor with a different set of indices for each connection to a neighboring site. In a 2D square lattice (think sites in the center of each space on a chess board), that means that each tensor has four indices, one for each neighboring site, so it is a 4-dimensional tensor.
3 dimentions in your array is not that uncommon when dealing with 3D problems, like 3D tetris or such games.
This question already has answers here:
Closed 13 years ago.
Possible Duplicates:
Implementing a matrix, which is more efficient - using an Array of Arrays (2D) or a 1D array?
Performance of 2-dimensional array vs 1-dimensional array
I was looking at one of my buddy's molecular dynamics code bases the other day and he had represented some 2D data as a 1D array. So rather than having to use two indexes he only has to keep track of one but a little math is done to figure out what position it would be in if it were 2D. So in the case of this 2D array:
two_D = [[0, 1, 2],
[3, 4, 5]]
It would be represented as:
one_D = [0, 1, 2, 3, 4, 5]
If he needed to know what was in position (1,1) of the 2D array he would do some simple algebra and get 4.
Is there any performance boost gained by using a 1D array rather than a 2D array. The data in the arrays can be called millions of times during the computation.
I hope the explanation of the data structure is clear...if not let me know and I'll try to explain it better.
Thank you :)
EDIT
The language is C
For a 2-d Array of width W and height H you can represent it as a 1-d Array of length W*H where each index
(x,y)
where x is the column and y is the row, of the 2-d array is mapped to to the index
i=y*W + x
in the 1-D array. Similarily you can use the inverse mapping:
y = i / W
x = i % W
. If you make W a power of 2 (W=2^m), you can use the hack
y = i >> m;
x = (i & (W-1))
where this optimization is restricted only to the case where W is a power of 2. A compiler would most likely miss this micro-optimization so you'd have to implement it yourself.
Modulus is a slow operator in C/C++, so making it disappear is advantageous.
Also, with large 2-d arrays keep in mind that the computer stores them in memory as a 1-d array and basically figures out the indexes using the mappings I listed above.
Far more important than the way that you determine these mappings is how the array is accessed. There are two ways to do it, column major and row major. The way that you traverse is more important than any other factor because it determines if you are using caching to your advantage. Please read http://en.wikipedia.org/wiki/Row-major_order .
Take a look at Performance of 2-dimensional array vs 1-dimensional array
Often 2D arrays are implemented as 1D arrays. Sometimes 2D arrays are implemented by a 1D array of pointers to 1D arrays. The first case obviously has no performance penalty compared to a 1D array, because it is identical to a 1D array. The second case might have a slight performance penalty due to the extra indirection (and additional subtle effects like decreased cache locality).
It's different for each system what kind is used, so without information about what you're using there's really no way to be sure. I'd advise to just test the performance if it's really important to you. And if the performance isn't that important, then don't worry about it.
For C, 2D arrays are 1D arrays with syntactic sugar, so the performance is identical.
You didn't mention which language this is regarding or how the 2D array would be implemented. In C 2D arrays are actually implemented as 1D arrays where C automatically performs the arithmetic on the indices to acces the right element. So it would do what your friend does anyway behind the scenes.
In other languages a 2d array might be an array of pointers to the inner arrays, in which case accessing an element would be array lookup + pointer dereference + array lookup, which is probably slower than the index arithmetic, though it would not be worth optimizing unless you know that this is a bottleneck.
oneD_index = 3 * y + x;
Where x is the position within the row and y the position in the column. Instead of 3 you use your column width. This way you can convert your 2D coordinates to a 1D coordinate.