Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
877 views
in Technique[技术] by (71.8m points)

fortran - 将矩阵插入fortran(Inserting matrices into fortran)

How to insert matrices into fortran program?

(如何在fortran程序中插入矩阵?)

I have a script:

(我有一个脚本:)

program matrix

implicit none
integer, parameter :: size = 20
real, dimension(1:size,1:size) :: M1, M2, M
integer :: a, b, c, i, j

print *,'Insert size of matrix: a x b, b x c'

read(*,*) a, b, c


print *, 'Insert matrix 1'
do i = 1, a
  read(*,*) (M1(i,j), j=1,b)
end do

print *, M1
end program

When I run the program, I get the following:

(运行该程序时,我得到以下信息:)

 Insert size of matrix: a x b, b x c
2
2
2
 Insert matrix 1
1
2
3
0
   1.00000000       3.00000000      -9.23812943E+22   4.56206728E-41   1.40129846E-45   0.00000000      -7.45794612E+22   4.56206728E-41  -9.23928235E+22   4.56206728E-41   0.00000000       4.56206728E-41  -3.43290277E-17   4.59163468E-41  -7.46993155E+22   4.56206728E-41   2.80110774E-37   0.00000000      -9.23812943E+22   4.56206728E-41   2.00000000       0.00000000      -7.45794612E+22   4.56206728E-41  -9.23928235E+22   4.56206728E-41   0.00000000       4.56206728E-41  -3.43292395E-17   4.59163468E-41  -3.78086916E+22   4.56206728E-41   1.02033236E-38   0.00000000      -5.63054167E+22   4.56206728E-41  -5.61578697E+22   4.56206728E-41  -3.76465891E+22   4.56206728E-41   2.80259693E-45   4.56206728E-41   0.00000000       4.56206728E-41  -3.43294513E-17   4.59163468E-41   4.20389539E-45   4.56206728E-41  -3.43293983E-17   4.59163468E-41   0.00000000       4.56206728E-41  -9.24416786E+22   4.56206728E-41   0.00000000       0.00000000       9.80908925E-45   0.00000000      -9.23928235E+22   4.56206728E-41  -3.43296630E-17   4.59163468E-41   3.28687239E-22   0.00000000      -9.23890045E+22   4.56206728E-41  -3.43299542E-17   4.59163468E-41  -9.24453535E+22   4.56206728E-41  -9.23812943E+22   4.56206728E-41   0.00000000       0.00000000      -7.45824156E+22   4.56206728E-41   2.80259693E-45   0.00000000      -9.24453535E+22   4.56206728E-41   1.40129846E-45   0.00000000       0.00000000       0.00000000       1.40129846E-45   0.00000000      -9.23812943E+22   4.56206728E-41  -5.63054167E+22   4.56206728E-41  -7.45824156E+22   1.40129846E-45  -3.43293983E-17   4.59163468E-41  -3.43294513E-17   4.59163468E-41  -9.23890045E+22   4.56206728E-41   0.00000000       0.00000000       1.40129846E-45   0.00000000      -9.23812943E+22   4.56206728E-41              NaN   0.00000000      -7.45824156E+22   1.40129846E-45  -3.76901029E+22   4.56206728E-41  -9.23928235E+22   4.56206728E-41  -9.23890045E+22   4.56206728E-41   0.00000000       0.00000000       1.40129846E-45   0.00000000      -9.23812943E+22   4.56206728E-41              NaN   0.00000000      -7.46709924E+22   1.40129846E-45  -3.77340400E+22   4.56206728E-41  -7.43476564E+22   4.56206728E-41  -3.43335805E-17   4.59163468E-41  -1.11285239E+33   3.09168480E-41  -3.43343217E-17   4.59163468E-41   0.00000000       0.00000000       0.00000000       0.00000000      -7.46709924E+22   4.56206728E-41   1.40129846E-45   0.00000000       0.00000000       0.00000000      -9.27450813E+33   3.09168480E-41  -3.76901029E+22   4.56206728E-41  -3.43333688E-17   4.59163468E-41  -7.48105274E+22   4.56206728E-41   1.40129846E-45   0.00000000      -1.11294152E+33   3.09168480E-41   4.34402524E-44   0.00000000      -5.65814243E+22   4.56206728E-41   4.20389539E-45   0.00000000      -5.60198794E+22   4.56206728E-41  -5.60198794E+22   4.56206728E-41  -7.48105274E+22   4.56206728E-41   1.40129846E-45   0.00000000       1.01397116E-38   0.00000000       8.40779079E-45   0.00000000       1.13000708E-41   9.18340949E-41   3.36311631E-44   0.00000000      -9.27497359E+33   3.09168480E-41   0.00000000       0.00000000      -7.48105274E+22   4.56206728E-41   2.00000000       0.00000000       0.00000000       0.00000000      -3.43332100E-17   4.59163468E-41   1.13000708E-41   9.18340949E-41  -7.43579697E+22   4.56206728E-41  -9.27497359E+33   3.09168480E-41   0.00000000       0.00000000                  NaN              NaN   1.75162308E-43   1.76563607E-43   1.77964905E-43   1.79366203E-43  -5.59999195E+22   4.56206728E-41   1.13000708E-41   9.18340949E-41   0.00000000       0.00000000       0.00000000       0.00000000       0.00000000                  NaN   0.00000000                  NaN   1.75162308E-43   1.76563607E-43   1.77964905E-43   1.79366203E-43  -5.59999195E+22   4.56206728E-41  -5.59999195E+22   4.56206728E-41   1.96181785E-44   0.00000000       1.96181785E-44   0.00000000       0.00000000       0.00000000       0.00000000       0.00000000                  NaN              NaN              NaN              NaN   5.60519386E-45   5.60519386E-45   5.60519386E-45   5.60519386E-45  -5.60090708E+22   4.56206728E-41  -5.60090708E+22   4.56206728E-41  -5.65814243E+22   4.56206728E-41   6.46234854E-27   0.00000000      -5.65814243E+22   4.56206728E-41   6.46234854E-27   0.00000000      -3.86449200E+22   4.56206728E-41   2.35098870E-38   0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       2.80259693E-45   0.00000000       1.96181785E-44  -0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       2.80259693E-45   0.00000000       1.96181785E-44  -0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000      -9.38126010E-25  -6.45877724E-35   0.00000000       0.00000000      -9.38126010E-25  -6.45877724E-35  -5.65814243E+22   4.56206728E-41  -3.86429294E+22   4.56206728E-41  -5.65814243E+22   4.56206728E-41   2.35098870E-38   0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       2.52435490E-29   4.56206728E-41   0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       2.80259693E-45   0.00000000       1.96181785E-44  -0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       0.00000000       2.80259693E-45   0.00000000       1.96181785E-44  -0.00000000       0.00000000       0.00000000       0.00000000       0.00000000   

The inserting of matrix behave different from my expectation.

(矩阵的插入行为与我的预期不同。)

Where are all inserted elements?

(所有插入的元素都在哪里?)

It doesn't work when I want to multiply matrices.

(当我想将矩阵相乘时,它不起作用。)

How to insert it in better way?

(如何以更好的方式插入?)

EDIT - multiplying

(编辑 -乘法)

     SIZE of matrices a x b, b x c:
    2
    2
    2
        Matrix 1
    2
    0
    1
    3
printed matrix 1
       2.00000000       1.00000000       0.00000000       3.00000000    

Matrix 2
    1
    2
    3
    6

printed matrix 2
       1.00000000       3.00000000       2.00000000       6.00000000    

Their product:
       2.00000000       0.00000000    
       9.00000000       20.0000000  
  ask by Moe translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You declared a matrix of 20 x 20. But then you only read 2x2 elements.

(您声明了20 x 20的矩阵。但是,您仅读取2x2元素。)

So all the other elements are filled with garbage.

(因此,所有其他元素都充满了垃圾。)

When you do

(当你做)

print *, M1

Fortran does not know that you want to work only with 2x2 elements, it knows that you declared it 20x20 so it prints 20x20 elements.

(Fortran不知道您只想使用2x2元素,而是知道您将其声明为20x20,因此它会打印20x20元素。)

Most are not undefined.

(大多数不是不确定的。)

You can allocate the matrix just to the size you need.

(您可以将矩阵分配给所需的大小。)

   real, allocatable :: M1(:,:)

   read(*,*) a, b, c

   allocate(M1(a,b))

   print *, 'Insert matrix 1'

   do i = 1, a
     read(*,*) (M1(i,j), j=1,b)
   end do

   print *, M1

or you can just do

(或者你可以做)

  print *, M1(1:a, 1:b)

and the fixed size array.

(和固定大小的数组。)

But it is uglier.

(但这很丑。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...