1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
program trace
implicit none
real, dimension(9), parameter :: values = (/ 1, 2, 3, 1, 4, 9, 1, 8, 27 /)
integer, dimension(2), parameter :: profil2 = (/2, 2/)
integer, dimension(2), parameter :: profil3 = (/3, 3/)
real, dimension(2,2) :: A = reshape(values(1:4), profil2)
real, dimension(3,3) :: B = reshape(values, profil3)
integer :: i
call mprint(A)
print '("Tr(A) = ", g15.6)',tr(A)
print *
call mprint(B)
print '("Tr(B) = ", g15.6)',tr(B)
contains
real function tr(m)
real, dimension(:,:), intent(in) :: m ! Tableau de rang 2, de profil implicite
real :: s = 0
integer :: i, n
n = size(m,1) ! Taille de la matrice m selon la dimension 1
do i = 1, n
s = s + m(i,i)
end do
tr = s
end function tr
subroutine mprint(m)
real, dimension(:,:), intent(in) :: m ! Tableau de rang 2, de profil implicite
integer :: n
n = size(m,1) ! Taille de la matrice m selon la dimension 1 (m est supposee carree)
do i = 1, n
print *,m(i,:)
end do
end subroutine mprint
end program trace |
Partager