fortran 中实现矩阵乘法

 

001、

fortran 中实现矩阵乘法

root@ubuntu26:/home/test01/fortran# ls
test01.f90
root@ubuntu26:/home/test01/fortran# cat test01.f90         ## 测试程序
program test01_fortran
    implicit none

    integer :: ay1(2,3) = reshape([1,2,0,4,2,3], [2,3])
    integer :: ay2(3,2) = reshape([2,5,2,1,1,4], [3,2])
    integer :: ay3(2,2); integer :: i,j,k

    print *, "output ay1: "; do i = 1,2; print *, ay1(i,:); end do
    print *, "output ay2: "; do i = 1,3; print *, ay2(i,:); end do

    do i = 1, 2
        do j = 1,2
            ay3(i,j) = 0
            do k = 1,3
                ay3(i,j) = ay3(i,j) + ay1(i,k) * ay2(k,j)
            end do
        end do
    end do

    print *, "output ay3: "
    do i = 1,2
        print *, "ay3(", i, ",:) = ", ay3(i,:)
    end do

end program test01_fortran
root@ubuntu26:/home/test01/fortran# gfortran test01.f90 -Wall -O2 -o kkk
root@ubuntu26:/home/test01/fortran# ls
kkk  test01.f90
root@ubuntu26:/home/test01/fortran# ./kkk
 output ay1:
           1           0           2
           2           4           3
 output ay2:
           2           1
           5           1
           2           4
 output ay3:
 ay3(           1 ,:) =            6           9
 ay3(           2 ,:) =           30          18

 

image

 。

 

posted @ 2026-11-06 09:56  小鲨鱼2018  阅读(2)  评论(0)    收藏  举报