SLIDE 15 Array syntax in modern Fortran enables a neat (and fast) way to express linear algebra operations 𝑧 = 𝐵𝑦 =
𝑘=1 𝑂
𝑏𝑘𝑦𝑘 ⟺
𝑧1 𝑧2 𝑧3 = 𝑏11 𝑏12 𝑏13 𝑏14 𝑏21 𝑏22 𝑏23 𝑏24 𝑏31 𝑏32 𝑏33 𝑏34 𝑦1 𝑦2 𝑦3 𝑦4
INTEGER :: m = 3, n = 4, i, j REAL :: A(m,n), x(n), y(m) ! Array syntax y=0.0 do j = 1, n y(:) = y(:) + A(:,j)*x(j) end do ! or, equivalently, with explicit ! loops y=0.0 do j = 1, n do i = 1, m y(i) = y(i) + A(i,j)*x(j) end do end do
Arrays in modern Fortran
53
Array initialization
Arrays can be initialized
– element by element – copied from another array – using single line data initialization statements – using the FORALL and WHERE statements
54
Array initialization
! Element-by-element initialization do j = 1, n idx(j) = j vector(j)=0 end do ! Initialization by copying from another array REAL :: to1(100,100), from1(100,100) REAL :: to2(100,100), from2(0:199,0:199) ... to1 = from1 to2(1:100, 1:100) = from2(0:199:2,0:199:2)
Every 2nd element
55
Array initialization with array constructors
integer :: idx(0:10) ! array constructor [ ] idx(0:10) = [0, (i, i = 1, 3), (0, j = 4, 10)] ! these can be used in setting variable values upon declaration integer :: values(3) = [11, 22, 33] ! equivalently, the older notation (/ /) idx(0:10) = (/ 0, (i, i = 1, 3), (0, j = 4, 10) /) ! or the archaic F77 way data idx / 0, 1, 2, 3, 7 * 0 /
56