Multiplier deux matrices en fortran 90

Published: 10 mai 2017

DMCA.com Protection Status

Pour multiplier deux matrices en fortran 90 il existe la fonction MATMUL, illustration en reprenant l'exemple de wikipedia:

\begin{equation}
A = \left( \begin{array}{ccc}
1 & 2 & 0 \\
4 & 3 & -1
\end{array}\right)
\end{equation}

\begin{equation}
B = \left( \begin{array}{ccc}
5 & 1 \\
2 & 3 \\
3 & 4
\end{array}\right)
\end{equation}

\begin{equation}
R = A * B =
\left( \begin{array}{ccc}
1 & 2 & 0 \\
4 & 3 & -1
\end{array}\right)
*
\left( \begin{array}{ccc}
5 & 1 \\
2 & 3 \\
3 & 4
\end{array}\right)
=
\left( \begin{array}{ccc}
9 & 7 \\
23 & 9
\end{array}\right)
\end{equation}

program multiplying_two_arrays

implicit none

integer, dimension(2,3) :: MATRIX_A
integer, dimension(3,2) :: MATRIX_B
integer, dimension(2,2) :: RESULT

!----------------------------------------------------------------------------------------!
! Matrix A

MATRIX_A(1,1) = 1
MATRIX_A(1,2) = 2
MATRIX_A(1,3) = 0
MATRIX_A(2,1) = 4
MATRIX_A(2,2) = 3
MATRIX_A(2,3) = -1

!----------------------------------------------------------------------------------------!
! Matrix B

MATRIX_B(1,1) = 5
MATRIX_B(1,2) = 1
MATRIX_B(2,1) = 2
MATRIX_B(2,2) = 3
MATRIX_B(3,1) = 3
MATRIX_B(3,2) = 4

!----------------------------------------------------------------------------------------!
! R = A * B

RESULT = matmul(MATRIX_A,MATRIX_B)

!----------------------------------------------------------------------------------------!

write(6,*) RESULT(1,1),RESULT(1,2)
write(6,*) RESULT(2,1),RESULT(2,2)

!----------------------------------------------------------------------------------------!

end program multiplying_two_arrays

donne

       9           7
      23           9

Recherches associées