# Data statement for matrices

**URL:** <https://fortran-lang.discourse.group/t/data-statement-for-matrices/5226>\
**Category:** Help\
**Created:** [February 16, 2023, 11:44pm UTC](https://fortran-lang.discourse.group/t/data-statement-for-matrices/5226 "2023-02-16T23:44:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mary](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@mary](https://fortran-lang.discourse.group/u/mary)\
**Post date:** [February 16, 2023, 11:44pm UTC](https://fortran-lang.discourse.group/t/data-statement-for-matrices/5226/1 "2023-02-16T23:44:11Z")

</div>

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?

```auto
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

```

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [February 17, 2023, 2:13am UTC](https://fortran-lang.discourse.group/t/data-statement-for-matrices/5226/2 "2023-02-17T02:13:25Z")

</div>

> [@mary](#):
>
> 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 …

@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.

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [February 17, 2023, 2:51am UTC](https://fortran-lang.discourse.group/t/data-statement-for-matrices/5226/3 "2023-02-17T02:51:42Z")

</div>

> [@mary](#):
>
> I believe that an option such as order=(/2,1/) should be used, but I have not been able to find it …

@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:

```fortran
   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

```

```auto
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

```

---

<div class="post-metadata">

**Author:** ![urbanjost](https://avatars.discourse-cdn.com/v4/letter/u/0ea827/32.png) [@urbanjost](https://fortran-lang.discourse.group/u/urbanjost)\
**Post date:** [February 17, 2023, 3:39am UTC](https://fortran-lang.discourse.group/t/data-statement-for-matrices/5226/5 "2023-02-17T03:39:51Z")

</div>

You might want to look at [array constructors in Fortran Wiki](https://fortranwiki.org/fortran/show/array+constructors); which shows several ways to initialize an array, including DATA statements. Note the warnings about using DATA statements as well.

---

<div class="post-metadata">

**Author:** ![mary](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@mary](https://fortran-lang.discourse.group/u/mary)\
**Post date:** [February 17, 2023, 4:26pm UTC](https://fortran-lang.discourse.group/t/data-statement-for-matrices/5226/6 "2023-02-17T16:26:41Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![mary](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@mary](https://fortran-lang.discourse.group/u/mary)\
**Post date:** [February 17, 2023, 4:28pm UTC](https://fortran-lang.discourse.group/t/data-statement-for-matrices/5226/7 "2023-02-17T16:28:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [February 17, 2023, 6:50pm UTC](https://fortran-lang.discourse.group/t/data-statement-for-matrices/5226/8 "2023-02-17T18:50:15Z")

</div>

> [@mary](#):
>
> 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 …

@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

> <https://github.com/j3-fortran/fortran_proposals/issues/102#issue-529663843>
>
> As proposed by a reader at comp.lang.fortran: https://groups.google.com/d/msg/co…mp.lang.fortran/m6qz7hC4a7M/78maOb9uEgAJ
> (see the thread for further comments and discussion)
> 
> \`\`\`
> Adding matrix literals
> This is mostly a quality of life thing, but something like
> 
> \`\`\`
> \`\`\`Fortran
> INTEGER, DIMENSION(2, 2) :: A = \[1, 2; 3, 4\]
> \`\`\`
> \`\`\`
> representing the matrix
> / 1 2 \\
> \\ 3 4 /
> 
> is, to me, a lot more readable than
> \`\`\`
> \`\`\`Fortran
> INTEGER, DIMENSION(2, 2) :: A = TRANSPOSE(RESHAPE(\[1, 2, 3, 4\], \[2, 2\]))
> \`\`\`
> 
> \`\`\`
> This could even be extended to more complex arrays by, for instance, repeating
> semicolons, eg.
> \`\`\`
> 
> \`\`\`Fortran
> INTEGER, DIMENSION(2, 2, 2) :: B = \[1, 2; 3, 4;; 5, 6; 7, 8\]
> \`\`\`
> \`\`\`
> One issue with this syntax is of course that the values are given row major,
> which is different from the memory layout of Fortran an thus also the way
> RESHAPE works.
> 
> Alternatively \[1, 2; 3, 4\] could represent
> / 1 3 \\
> \\ 2 4 /
> \`\`\`

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.

---

<div class="post-metadata">

**Author:** ![mary](https://avatars.discourse-cdn.com/v4/letter/m/e99b99/32.png) [@mary](https://fortran-lang.discourse.group/u/mary)\
**Post date:** [February 19, 2023, 8:56pm UTC](https://fortran-lang.discourse.group/t/data-statement-for-matrices/5226/9 "2023-02-19T20:56:56Z")

</div>

Sure, I will have a look.
