- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I seem to find increasingly often that I need to (or atleast, I would find it very convenientto) pass arrays with a certain shape (e.g. a three-dimensionalm x n x q matrix) to a procedure that expects to receive data in a different shape (e.g. atwo-dimensional matrixwithdimensions m, n*q, or even a single vector with length m*n*q). Typically the reason for wanting to do thisis is so that the data can be processed more efficiently by applying an operation which is common to all elements over a single long vector rather than a lot of short vectors.
In C, this would be a piece of cake: you pass a pointer to the first element and whatever description of the data you want. In Fortran, the bounds-checking gets in the way; you simply can't pass a three-dimensional array to a function expecting a two-dimensional array, even if the arrays are exactly the same size and have the data laid out in exactly the same order.
On the face of it this is an application for the much-derided EQUIVALENCE statement, but even that isn't legal when applied to procedure arguments. I really haven't found a good solution, and am usually forced to just copy the data into a new temporary array of the correct shape before making the procedure call, and then copying it back afterwards, which seems ridiculously wasteful.
Is there a better, or even a "proper", way to do this?
Stephen.
In C, this would be a piece of cake: you pass a pointer to the first element and whatever description of the data you want. In Fortran, the bounds-checking gets in the way; you simply can't pass a three-dimensional array to a function expecting a two-dimensional array, even if the arrays are exactly the same size and have the data laid out in exactly the same order.
On the face of it this is an application for the much-derided EQUIVALENCE statement, but even that isn't legal when applied to procedure arguments. I really haven't found a good solution, and am usually forced to just copy the data into a new temporary array of the correct shape before making the procedure call, and then copying it back afterwards, which seems ridiculously wasteful.
Is there a better, or even a "proper", way to do this?
Stephen.
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You do this by passing the first element of the array, say, Z(1,1,1). The Fortran language allows you to pass this to a dummy argument of a different shape as long as Z is not an assumed-shape array.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Wow, that's neat. Thanks Steve.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page