Reputation: 1
I am a newbie in fortran and i have to multiply matrices of different shapes with MATMUL() and the result is not what i expected...
Here is my fortran code:
integer, dimension(3,2) :: a
integer, dimension(2,2) :: b
integer :: i, j
a = reshape((/ 1, 1, 1, 1, 1, 1 /), shape(a))
b = MATMUL(a,TRANSPOSE(a))
do j = 1, 2
do i = 1, 2
print*, b(i, j)
end do
end do
I expected this matrix as a result:
b =
| 3 3 | , a 2x2 matrix
| 3 3 |
Instead, i got this error message:
matmlt.f90(9): error #6366: The shapes of the array expressions do not conform. [B] b = MATMUL(a,TRANSPOSE(a)) ------^
To make this code work properly i had to switch the MATMUL arguments like this:
b = MATMUL(TRANSPOSE(a), a)
And this way, i obtain what i was expecting at the beginning. But this is not intuitive.
On paper,
a =
| 1 1 1 |
| 1 1 1 |
transpose(a) =
| 1 1 |
| 1 1 |
| 1 1 |
a x transpose(a) =
| 3 3 |
| 3 3 |
and
transpose(a) x a =
| 2 2 2 |
| 2 2 2 |
| 2 2 2 |
What is wrong with my code?
Thank you.
Upvotes: 0
Views: 607
Reputation: 61
your matrix definition for the variable
integer, dimension(3,2) :: a
means, that you have 3 rows and 2 cols (different ofyour assumption). Subsequently
a=
|11|
|11|
|11|
and
transpose(a) =
|111|
|111|
matmul(a,transpose(a)) =
|2 2 2|
|2 2 2|
|2 2 2|
so your variable b should defined like
integer, dimension (3,3) :: b
instead of
integer, dimension (2,2) :: b
what is the reason of the
matmlt.f90(9): error #6366: The shapes of the array expressions do not conform. [B] b = MATMUL(a,TRANSPOSE(a)) ------^
Error
Upvotes: 2