Data statement for matrices

I want to use the data statement to initialize matrices. In my opinion, one advantage of using this method is that it provides a compact way of initializing matrices. However, the way I used it in the example below resulted in an error in the calculation. Both Z1 and ZZ1 should give a matrix of size 2x1 equal to [7;13] (ZZ1 = [7;13], Z1 = [10;12]). I believe that an option such as order=(/2,1/) should be used, but I have not been able to find it. Can someone help?

program test_Multiplication
  implicit none
  integer :: Xp1(3,1), b1(2,1), IW1_1(2,3), Z1(2,1)
  integer :: XXp1(3,1), bb1(2,1), IIW1_1(2,3), ZZ1(2,1)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  ! with data statement !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  data Xp1(1:3,1) &
  /1, &
   2, &
   3/
  data IW1_1(1:2,1:3) &
  /1, 1, 1, &
   2, 2, 2/
  data b1(1:2,1) &
  /1, &
   1/
  
  Z1 = matmul(IW1_1,Xp1)+b1
  print*, 'Z1', Z1

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  ! without data statement !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  XXp1 = reshape( (/1, &
   2, &
   3 /), &
   shape(XXp1), order=(/2,1/) )
  
  IIW1_1 = reshape( (/ 1, 1, 1, &
   2, 2, 2/), &
   shape(IIW1_1), order=(/2,1/) )
  
  bb1 = reshape( (/1, &
   1/), &
   shape(bb1), order=(/2,1/) )

  ZZ1 = matmul(IIW1_1,XXp1)+bb1
  print*, 'ZZ1', ZZ1

end program test_Multiplication

@mary,

Please keep in mind objects initialized using DATA statements are imparted the SAVE attribute regardless of whether you have declared the object to have such an attribute. This is what is considered an “implied SAVE” variable. Note if the objects you plan to use with DATA statements are variables in a main program (like in your example above) or with module entities, then as is they have the SAVE attribute and therefore it makes no difference to use them with DATA, it’s a matter of choice and whether the semantics will be clear enough for you and your colleagues trying to maintain and extend the program.

However with local variables in subprograms the SAVE attribute can hinder thread safety, something to keep in mind as concurrent and parallel execution is of increasing interest.

2 Likes

@mary,

With DATA, should you remain keen on using it, you can control the ordering via the implied-do indices. You can take a look at below:

   blk1: block
      integer :: IW1_1(2,3), IIW1_1(2,3)
      data IW1_1(1:2,1:3) &
        /1, 1, 1, &
         2, 2, 2/
      IIW1_1 = reshape( (/ 1, 1, 1, &
      2, 2, 2/), &
      shape(IIW1_1), order=(/2,1/) )
      print *, "Per OP"
      print *, iw1_1(1,:), new_line(""), iw1_1(2,:)
      print *, iiw1_1(1,:), new_line(""), iiw1_1(2,:)
   end block blk1
   print *
   blk2: block
      integer :: d(2,3), r(2,3)
      data ((d(I,J),J=1,3),I=1,2) &  !<-- note the loop control via parenthesized indices
     /1, 1, 1, &
      2, 2, 2/
      r = reshape( &
      [ 1, 1, 1, &
        2, 2, 2 ], &
      shape(r), order=[2,1] )
      print *, "Per ordered DATA and ordered reshape"
      print *, d(1,:), new_line(""), d(2,:)
      print *, r(1,:), new_line(""), r(2,:)
   end block blk2
end
C:\temp>gfortran p.f90 -o p.exe

C:\temp>p.exe
 Per OP
           1           1           2
           1           2           2
           1           1           1
           2           2           2

 Per ordered DATA and ordered reshape
           1           1           1
           2           2           2
           1           1           1
           2           2           2
1 Like

You might want to look at array constructors in Fortran Wiki; which shows several ways to initialize an array, including DATA statements. Note the warnings about using DATA statements as well.

1 Like

Thank you @FortranFan for giving an example. I did not know that I can use implied-do indices for Data statement and what would be the correct way of using them (first columns, second rows).

1 Like

Thank you for sharing the link. It is really helpful.

@mary ,

Re: the use of matrix literals in code e.g., toward initializing objects of rank-2 and greater, please see this thread at the J3 Fortran proposals site at GitHub

If you plan to use Fortran further and have any suggestions or ideas or any input when it comes of initializing matrices generally in Fortran, please post your feedback at the above link. Your comments will be valuable to consider also should the J3 Fortran committee start to work on improving this in the language in a future revision of the standard.

1 Like

Sure, I will have a look.