Homepage

Jannis Teunissen

User Tools

Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

blog:fortran_improvements [2018/04/25 12:46] (current)
Line 1: Line 1:
 +===== Ideas for Fortran improvements =====
 +
 +==== Default values for optional arguments ====
 +
 +Currently, default values for optional arguments have to be handled like this:
 +
 +<code fortran>
 +subroutine test(n_arg)
 +  integer, intent(in), optional :: n_arg ! Default is 10
 +  integer :: n_used
 +
 +  if (present(n_arg)) then
 +     n_used = n_arg
 +  else
 +     n_used = 10
 +  end if
 +  ... ! Do something with n_used
 +</code>
 +
 +It would be more convenient if this was possible:
 +<code fortran>
 +subroutine test(n)
 +  integer, intent(in), optional :: n = 10 ! Default is 10
 +  ...
 +</code>
 +
 +Note that this syntax is also used to initialize variables with the 'save'
 +attribute, but since that attribute does not apply to optional arguments it
 +should be fine.
 +
 +==== Multi-dimensional vector indexing ====
 +
 +Currently, you can use vectors for indexing. Given three vectors **a, b, c** of equal length, you can e.g. do:
 +<code fortran>
 +a(b(:)) = c
 +</code>
 +which is the same as writing
 +<code fortran>
 +a(b(1)) = c(1)
 +a(b(2)) = c(2)
 +...
 +</code>
 +
 +Now suppose we have a matrix **D**, and we have an index vector **ix**. Then
 +there is currently now way to do
 +
 +<code fortran>
 +ix = [1, 1]
 +D(ix) = 0 ! Illegal
 +</code>
 +
 +Instead, you have to write
 +<code fortran>
 +ix = [1, 1]
 +D(ix(1), ix(2)) = 0 ! Correct
 +</code>
 +
 +I suggest to add the following syntax rule: if a /n/-dimensional array is
 +indexed by a single vector of length /n/, then automatically treat is as an
 +index, so that both the above code snippets behave the same.
 +
 +==== Passing arrays of unknown dimension ====
 +
 +It would be great if something like this was supported:
 +
 +<code fortran>
 +subroutine example(my_array, ndim)
 +  integer, intent(in) :: ndim
 +  real, intent(in)    :: my_array(dim=ndim)
 +  integer             :: nx(ndim)
 +
 +  nx = shape(my_array)
 +  ...
 +end subroutine example
 +</code>
  

Page Tools