-
Notifications
You must be signed in to change notification settings - Fork 17
Optional dimensioning in assumed-shape arrays #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks @ivan-pi for raising this issue. This is something I encounter very frequently myself. The explicit-shape arrays are actually more readable in this case: subroutine solve(A, b, n, k)
real, intent(inout) :: A(n,n)
real, intent(inout) :: b(n,k)
integer, intent(in) :: n, k
! perform factorization of A...
! perform back substitution for columns of b from 1 to k...
end subroutine Except that one has to pass |
I was annoyed by this today, while creating wrappers of some old least squares routines. I suppose that if assert statements (#70) were available then the current assumed-shape syntax would be sufficient. |
Here is how it would look like with an assert statement: subroutine solve(A, b)
real, intent(inout) :: A(:,:)
real, intent(inout) :: b(:,:)
assert(size(A,1) == size(A,2))
assert(size(A,1) == size(b,1))
! perform factorization of A...
! perform back substitution for columns of b from 1 to size(b,2)...
end subroutine |
Assumed-shape arrays are often used to make calling subroutines easier, i.e. by not having to explicitly pass the array size (or bounds). Say we have a function which solves a system of equations for multiple right-hand sides returning the solution in right hand size array. The subroutine might look something like this:
This interface is not optimal since, due to the rules of matrix multiplication, we actually know that the number of rows in
b
should be equal to the number of rows inA
. Therefore one might expect it to be possible to use the following interface:This way we could avoid the need for writing assertion statements to check if the first dimensions of A and B match, and forcing the user to check an error flag. Of course this problem can be avoided completely by using explicit shape arrays, defeating the entire point of having assumed-shape ones. A second (unattractive) option would be to use an assumed-size array, and explicitly pass only the number of columns in
b
:Could someone perhaps explain what is the reason that the second example above is not allowed and the compiler flags it as a "bad array specification"?
The text was updated successfully, but these errors were encountered: