I am a very new user to Fortran 90. I am learning how to program. Currently, I am trying to create a program to do matrix multiplication. But, I am getting an error.
Program Matrix_Multiplication
Implicit None
Real, Dimension(2:2) :: A, B, C
Integer :: i, j, k
A = 0.0
B = 0.0
C = 0.0
do i = 1, 2
do j = 1, 2
Read (80, *) A
Read (90, *) B
Write (100, *) A, B
end do
end do
Call subC(A, B, C)
Write (110, *) C
End Program Matrix_Multiplication
Subroutine subC(A, B, C)
Implicit None
Real, Intent(IN) :: A, B
Integer :: i, j, k
Real, Intent(OUT) :: C
do i = 1, 2
do j = 1, 2
C = C(i, j) + (A(i, j)*B(j, i))
end do
end do
return
End Subroutine
On compiling:
C(i, j) = (A(i, k)*B(k, j)) 1 Error: Unclassifiable statement at (1)
As francescalus stated in his comment, A
, B
, and C
are declared as scalars inside the subroutine. Therefore, you cannot index them as arrays.
In this particular case I would rather use the intrinsic function matmul
instead of writing your own matrix-matrix multiplication:
Program Matrix_Multiplication
Implicit None
Real, Dimension (2,2) :: A,B,C
A=0.0
B=0.0
C=0.0
do i=1,2
do j=1,2
Read (80,*) A(j,i)
Read (90,*) B(j,i)
Write (100,*) A,B
end do
end do
C = matmul(A,B)
Write (110,*) C
End Program Matrix_Multiplication
For larger matrices there are highly optimized math-libraries out there. Using BLAS/LAPACK is highly recommended then. The correct subroutine for your example would be SGEMM.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With