Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matrix multiplication program: Error: Unclassifiable statement at (1)

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)

like image 601
Aditya Gupta Avatar asked Jan 24 '15 11:01

Aditya Gupta


1 Answers

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.

like image 124
Alexander Vogt Avatar answered Nov 22 '22 11:11

Alexander Vogt