# FORTRAN 90: Matrix Multiplication

Page 1 of 1

## 2 Replies - 2482 Views - Last Post: 15 December 2011 - 04:26 PM

### #1 Louisda16th

• dream.in.assembly.code

Reputation: 15
• Posts: 1,967
• Joined: 03-August 06

# FORTRAN 90: Matrix Multiplication

Posted 29 September 2007 - 04:25 AM

Description: !Example Program
```PROGRAM MULMAT
IMPLICIT NONE
REAL, ALLOCATABLE, DIMENSION(:,:) :: MatA, MatB, MatC
INTEGER :: rowsA, colsA, rowsB, colsB, rowsC, colsC
INTEGER :: i,j, ErrCode

PRINT*,"Enter order of Matrix A (rows,columns):"
READ*,rowsA,colsA
ALLOCATE(MatA(rowsA,colsA))
PRINT*,"Enter elements of Matrix A row wise"
DO i = 1, rowsA
READ*,(MatA(i,j),j = 1, colsA)
END DO

PRINT*,"Enter order of Matrix B (rows,columns):"
READ*,rowsB,colsB
ALLOCATE(MatB(rowsB,colsB))
PRINT*,"Enter elements of Matrix B row wise"
DO i = 1, rowsB
READ*,(MatB(i,j),j = 1, colsB)
END DO
ALLOCATE(MatC(rowsA,colsB))
CALL MATRIXPRODUCT(MatA, rowsA, colsA, MatB, rowsB, colsB, MatC, rowsC, colsC, ErrCode)
IF (ErrCode == 1) THEN
PRINT*,"A x B = "
DO i = 1, rowsC
PRINT *,(MatC(i,j), j = 1, colsC)
END DO
ENDIF
END PROGRAM MULMAT

```

!Subroutine goes hereThis subroutine can multiply two matrices of any order (which match)
```!Subroutine to find product of two matrices
!Author : Louisda16th a.k.a Ashwith J. Rego
!Description:
!Product of two matrices is defined by
!c(i,j) = a(i,1)*b(1,j) + a(i,2)*b(2,j)+........+a(i,n)*b(n,j)
!
SUBROUTINE MATRIXPRODUCT(A, rowsA, colsA, B, rowsB, colsB, C, rowsC, colsC, ErrCode)
!Declarations
IMPLICIT NONE
INTEGER, INTENT(IN) :: rowsA, colsA, rowsB, colsB !Matrix Dimensions
INTEGER, INTENT(INOUT) :: rowsC, colsC, ErrCode !Product matrix dimensions
REAL, DIMENSION(rowsA,colsA) :: A !Matrix A
REAL, INTENT(IN), DIMENSION(rowsB,colsB) :: B !Matrix B
REAL, DIMENSION(rowsA,colsC) :: C !Matrix C

INTEGER :: i, j, k !Counters

IF (colsA /= rowsB) THEN !Two matrices can be multiplied if and only if the number of columns
PRINT*,"Error! Order of matrices donot match" !of the first matrix equals the number of rows of the second
ErrCode = -1 !ErrCode = -1 => Subroutine returned unsuccessfully
RETURN
ELSE
rowsC = rowsA
colsC = colsB
ENDIF

!Initialize product matrix to 0
DO i = 1, rowsC
DO j = 1, ColsC
C(i,j) = 0
END DO
END DO

!Find product as per above formula
DO i = 1, rowsA
DO j = 1, colsB
DO k = 1, colsA
C(i,j) = C(i,j) + A(i,k)*B(k,j)
END DO
END DO
END DO

!ErrCode = 1 => Subroutine returned successfully
ErrCode = 1

END SUBROUTINE MATRIXPRODUCT
```

Is This A Good Question/Topic? 0

## Replies To: FORTRAN 90: Matrix Multiplication

### #2 lcgiap

• New D.I.C Head

Reputation: 0
• Posts: 0
• Joined: 31-March 11

## Re: FORTRAN 90: Matrix Multiplication

Posted 31 March 2011 - 09:09 PM

hi... i copy the code into fotran but it is a run time error 112,reference to undefined variable, array element or function result. is there anything wrong with the code? anyone can help me? Thank you very much...
Was This Post Helpful? 0

### #3 dogs1231992

• New D.I.C Head

Reputation: 0
• Posts: 0
• Joined: 15-December 11

## Re: FORTRAN 90: Matrix Multiplication

Posted 15 December 2011 - 04:26 PM

at the 14 line of sub REAL, DIMENSION(rowsA,colsC) :: C !Matrix C colsC should be changed colsB
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }