Multiplication of two 2x2 matrices in Fortran

In summary: Not only will it help you spot errors, but it will help others who try to read your code. In addition to indenting each loop body, it is also good practice to indent the bodies of all the if statements, do loops, etc. that are nested inside your main loop. This makes the structure of your program easier to see.In summary, the code reads two 2x2 matrices from the user using arrays and do loops, and uses the matrix multiplication formula to give the results. However, the result is incorrect because the C array was not initialized to all zero values before the multiplication. This results in garbage values being added to the correct values, causing the incorrect result. Adding indentation to the code and initializing the C array beforehand
  • #1
Parzeevahl
6
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
  • #2
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 berkeman
  • #3
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 berkeman

What is the syntax for multiplying two 2x2 matrices in Fortran?

The syntax for multiplying two 2x2 matrices in Fortran is matmul(A,B), where A and B are the two matrices to be multiplied. It is important to note that both matrices must have dimensions of 2x2 for the multiplication to work.

What is the purpose of multiplying two 2x2 matrices in Fortran?

The purpose of multiplying two 2x2 matrices in Fortran is to combine the two matrices using a specific mathematical formula to produce a new matrix with different values. This is often used in scientific and mathematical applications to perform calculations and solve equations.

What are the rules for multiplying two 2x2 matrices in Fortran?

The rules for multiplying two 2x2 matrices in Fortran are as follows:

  • The number of columns in the first matrix must match the number of rows in the second matrix.
  • The resulting matrix will have the same number of rows as the first matrix and the same number of columns as the second matrix.
  • The order of multiplication matters, as matmul(A,B) is not the same as matmul(B,A).

Can two 2x2 matrices with different data types be multiplied in Fortran?

No, two matrices with different data types cannot be multiplied in Fortran. Both matrices must have the same data type, such as real or integer, for the multiplication to work.

Are there any built-in functions in Fortran for multiplying two 2x2 matrices?

Yes, there is a built-in function in Fortran specifically for multiplying two 2x2 matrices. It is the matmul function, which takes two matrices as input and returns the resulting matrix. This function is optimized for matrix multiplication and is the recommended method for multiplying matrices in Fortran.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
20
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
8K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
4
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
737
  • Programming and Computer Science
Replies
22
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
962
Back
Top