0

In the simplest case, I want to create a 2*2 markov transition matrix as an input to a program, something like

  A = [0:977 , 0:023
       0:074 , 0:926 ]

That would be trivial in matlab, R or python, but it seems not that simple in low level language like fortran. I guess I cannot write something like

   REAL,DIMENSION(2,2) :: P
   P(1,1)= 0.977, P(1,2) = 0.023, P(2,1)= 0.074, P(2.2)=0.926

And I do some search, it's likely I will have to use read(*,*), but I'm not sure. So tell me how to do, thank you.

—————————— Supplementary——————————

Thank you @Fl.pf. for helpful guidence

A more specific example, I adjust a existing module to add two new parameters, both are in form of matrix

module parameters
implicit none
   REAL, PARAMETER      :: b = 0.99, d = 0.025, a = 0.36
   REAL, PARAMETER      :: klb = 0.01, inc = 0.025, kub = 45.0
   INTEGER, PARAMETER   :: length_grid_k = (kub-klb)/inc + 1
   INTEGER, PARAMETER :: length_z = 2
   REAL , PARAMETER     :: toler   = 1.e-4                      ! Numerical tolerance
   !REAL,DIMENSION(length_z,length_z) :: P           ! Trasition matrix of technology process
   REAL :: P(2,2)
   P(1,:) = (/0.977, 0.023/)
   P(2,:) = (/0.074, 0.926/)

   !REAL,DIMENSION(2)  :: y                          ! technology
   REAL :: y(2,1)
   y = (/1.25, 0.2/)

end module

As you can see, the P and y are matrice I add later on and where the error take place. The gfortran complier throw me error message like:

Unexpected assignment statement in module

So if I don't take the answer from @Fl.pf. for wrong, then I can only assign values to the matrix by 1) allocate space for the matrix by real, allocatable, dimension(:,:); 2) assign the value allocate(transition(n_tauchen,n_tauchen)). Am I right? But some code suggest alternatives, like the following snippet of code

program matrices

    implicit none
    integer :: i, j
    real*8 :: a(4), b(4)
    real*8 :: x(2, 4), y(4, 2), z(2, 2)

    ! initialize vectors and matrices
    a = (/(dble(5-i), i=1, 4)/)
    b = a+4d0
    x(1, :) = (/1d0, 2d0, 3d0, 4d0/)
    x(2, :) = (/5d0, 6d0, 7d0, 8d0/)
    y = transpose(x)
    z = matmul(x, y)   

It seems they assign values to matrix x directly. I'm a little bit confusing here , what's the difference?

zlqs1985
  • 509
  • 2
  • 8
  • 25
  • you could use `real, allocatable :: mat(:,:) allocate(mat(2,2)) mat(1,1) = 0.23123 ...` – Fl.pf. Mar 14 '16 at 07:43
  • Thank you @Fl.pf. for your helpful guidence. I add a more specific example – zlqs1985 Mar 14 '16 at 16:52
  • Have you had a look at the duplicate link at all? It contains all you need, really. – Vladimir F Героям слава Mar 14 '16 at 17:07
  • @VladimirF,hi, I want to know the difference, because I meet several writing styles on assigning values to matrix or multipledimension arrays, I want to know on which circumstance they are equivalent, and on which they are not. So my focus is slightly different from the link – zlqs1985 Mar 15 '16 at 01:56
  • remember to `deallocate(mat)` at the end look for a book about fortran and read about allocatables, arrays and such. – Fl.pf. Mar 17 '16 at 09:45
  • I have find the source of problem. I shall not place excute command in the parameter module – zlqs1985 Mar 17 '16 at 17:13

0 Answers0