3 ms·
You can of course use arrays of arrays if you like, but multidimensional arrays are indispensable for numerical work, especially linear algebra – matrices are 2
by StefanKarpinski 13y ago
You can of course use arrays of arrays if you like, but multidimensional arrays are indispensable for numerical work, especially linear algebra – matrices are 2D arrays after all. When doing numerical work in C, for example, you have to simulate multidimensional arrays by doing your own index calculations to translate back and forth between linear indexing and N-d indexing. Having language support for this is a must if you want to target numerical programming.
- RivieraKid 13y agoSo if I understand it correctly, it's for efficiency? What about some special treatment by the compiler, is something like that possible?
- StefanKarpinski 13y agoMemory layout mostly. For example, a 10x10 64-bit floating-point matrix should be stored as contiguous chunk of 100 inline 64-bit floats. This is quite different than an array of arrays, which is represented as an array of pointers to separate boxed 1-d array objects. The contiguous layout is not only far more efficient, but also is what all numerical libraries like LAPACK and FFTW expect. If numerical arrays were not laid out like that in Julia, then we wouldn't be able to do fast linear algebra or FFTs. Of course, sometimes you do want an array of arrays, such as when each array needs to have a different length or element type – which you can of course do. Which is why I'm a bit confused about how having support for multidimensional arrays is a problem. If you find them confusing, why not just ignore the feature and use arrays of arrays all the time? I'm also not clear on what "special treatment by the compiler" means.
- RivieraKid 13y ago> Of course, sometimes you do want an array of arrays, such as when each array needs to have a different length or element type – which you can of course do. Which is why I'm a bit confused about how having support for multidimensional arrays is a problem. If you find them confusing, why not just ignore the feature and use arrays of arrays all the time? The standard lib and 3rd party libs use multi-d arrays, so I can't avoid them. But I guess it takes some time to get used to. > I'm also not clear on what "special treatment by the compiler" means. I meant something like saving an array of arrays into a continuous memory block and somehow enforcing that the sub-arrays must have equal length. But now I realize that this is probably impossible...
- zwegner 13y agoI'm confused about your reference to C. C has multidimensional arrays, and all the elements are laid out contiguously in memory. No index calculations are needed, it's all done by the array indexing operation. If you have this code: int a[10][10]; then: &a[x][y] == ((char *)a + x * sizeof(a[0]) + y * sizeof(a[0][0])) where sizeof(a[0]) is == sizeof(int) * 10. quick edit: added a (char *) cast so the arithmetic doesn't multiply by sizeof(int) again
- StefanKarpinski 13y agoRight, I should have clarified. That works for statically allocated arrays but not for dynamically allocated ones. Since matrix dimensions are generally not known ahead of time, in real programs you almost always end up needing to simulate multiple indices using explicit index arithmetic.