Today has been the beginning of Week 2 in Fortran for scientific Computing course. This had the introduction to arrays, mainly, along with elemental procedures. A word or two were mentioned about “pure functions” but those remain a mystery for now. But, here are some highlights.
Arrays
- Arrays in fortran may have particular rank (upto 15 in case of Fortran 2008).
| |
- Arrays are arranged columnwise in Fortran.
- Array can be indexed in slices in a similar manner to python
- Array functions may be
- Elemental - operate on each value in array. Example
sin(A)operates on each element. - Operate on array as a whole -
sum(A)adds all the elements in the A.
- Elemental - operate on each value in array. Example
- The 2nd category of array functions usually have the following optional parameters.
dim- The dimension along which to perform the operations. For example, forA(m,n), the operationsum(A, dim=1)will return a vector of lengthnwith each element being the sum ofmrows in that column.mask- The condition to consider. Should be passed a logical array of same size of the array in consideration.
| |
There is count function which counts the number of instances which are true in logical array.
| |
Procedures
In a subroutine, if an array dimension n is specified, that dimension is the minimum requirement. But, if the passed array is larger, the first n values alone are considered.
| |
Also, the fortran allows one to index outside the array but it still doesn’t throw error. I still haven’t figured out why the (1,3) element comes out to be 8. May be it will be covered tomorrow!
| |
Also, elemental functions can have either have intent(in) or value not intent(out) nor intent(inout).