Multiplication of two 2x2 matrices in Fortran

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 7K views
Parzeevahl
Messages
6
Reaction score
0
Homework Statement
Multiply two 2x2 matrices of your choice in Fortran and check that the code gives the correct result.
Relevant Equations
arrays and do loops
I have tried to do this using arrays and do loops:
Fortran:
program matrixmul
    implicit none
    real A(2, 2), B (2, 2), C (2, 2)
    integer i, j, k
    write (*, *) 'Input: First matrix'
    do i = 1, 2
    do j = 1, 2
    read (*, *) A (i, j)
    enddo
    enddo
    write (*, *) 'Input: Second matrix'
    do i = 1, 2
    do j = 1, 2
    read (*, *) B (i, j)
    enddo
    enddo
    do i = 1, 2
    do j = 1, 2
    do k = 1, 2
    C (i, j) = C (i, j) + A (i, k) * B (k, j)
    enddo
    enddo
    enddo
    write (*, *) C
    end

So, the code reads the matrices, element-by-element, from the user, and uses the matrix multiplication formula to give the results. But it is giving wrong results. I tried multiplying 2 unit matrices as a check, and the result is not a unit matrix. What went wrong?
 
Physics news on Phys.org
Parzeevahl said:
Homework Statement: Multiply two 2x2 matrices of your choice in Fortran and check that the code gives the correct result.
Homework Equations: arrays and do loops

I have tried to do this using arrays and do loops:
Fortran:
program matrixmul
    implicit none
    real A(2, 2), B (2, 2), C (2, 2)
    integer i, j, k
    write (*, *) 'Input: First matrix'
    do i = 1, 2
    do j = 1, 2
    read (*, *) A (i, j)
    enddo
    enddo
    write (*, *) 'Input: Second matrix'
    do i = 1, 2
    do j = 1, 2
    read (*, *) B (i, j)
    enddo
    enddo
    do i = 1, 2
    do j = 1, 2
    do k = 1, 2
    C (i, j) = C (i, j) + A (i, k) * B (k, j)
    enddo
    enddo
    enddo
    write (*, *) C
    end

So, the code reads the matrices, element-by-element, from the user, and uses the matrix multiplication formula to give the results. But it is giving wrong results. I tried multiplying 2 unit matrices as a check, and the result is not a unit matrix. What went wrong?
Out off topic, you should try to indent your code, it comes in handy for long programs.
 
  • Like
Likes   Reactions: berkeman
Parzeevahl said:
Fortran:
C (i, j) = C (i, j) + A (i, k) * B (k, j)
This line is very likely causing your error.
Before you start your do loops for multiplication, you should initialize the C array to all zero values. Otherwise each value of C(i, j) will be a garbage value, so each assignment statement in the innermost loop just adds a correct value to a garbage value, which results in a different garbage value.

Executing a statement like value = value + x is something you shouldn't do in any programming language if you haven't previously initialized value to some known value.

@archaic's suggestion of indenting each loop body is a good one. This makes your code easier for humans to read.
 
Last edited:
  • Like
Likes   Reactions: berkeman