Note that there are some explanatory texts on larger screens.

plurals
  1. POFortran Matrix Multiplication isn't giving the right answers
    primarykey
    data
    text
    <p>I've got a matrix calculator program, but I'm getting the wrong answer for my dot product multiplier. </p> <p>Here's my multiply subroutine:</p> <pre><code>subroutine multiply(m1,m2,res,row1,row2,col1,col2) integer, intent(in) :: row1,row2,col1,col2 real, intent(in), dimension(row1,col1) :: m1 real, intent(in), dimension(row2,col2) :: m2 real, intent(out), dimension(row1,col2) :: res integer :: i,j,k do i = 1, col2 do j = 1, col1 res(j, i) = 0 enddo do j = 1, col1 do k = 1, row1 res(k, i) = res(k, i) + m1(k, j)*m2(j, i) enddo enddo enddo </code></pre> <p>And here's my output, just in case that's the problem.</p> <pre><code> subroutine output(r,c,matrix,name) integer, intent(in) :: r integer, intent(in):: c character(len=10) :: name real, intent(out), dimension(3,3) :: matrix integer i,j print *,name do i = 1, r write(*,"(100F6.1)") ( matrix(i,j), j=1,c ) enddo end subroutine </code></pre> <p>If it helps, it works excellently for a 3x3 matrix, but not for two rectangular matrix. Here's what happens when I do a 2x3 * 3x2 matrix. At this point, i'm desperate for help. Anything you could suggest would be great. Thanks!</p> <p><img src="https://i.stack.imgur.com/APC2W.png" alt="enter image description here"></p> <p>EDIT: Here's all of my code in its current state with your suggestions.</p> <pre><code> PROGRAM G6P5 integer :: r1,r2,c1,c2,i,j,k,s real :: input real, dimension (3,3) :: mat1, mat2, rmat write (*,*) 'Please make a selection:' write (*,*) 'Enter 1 to add matrices' write (*,*) 'Enter 2 to subtract matrices' write (*,*) 'Enter 3 to multiply matrices' write (*,*) 'Enter 4 to transpose a matrix' write (*,*) 'Enter 5 to quit' read *, s select case (s) case (1) print *, 'Enter # of rows &amp; columns (1-10) (ex. 3 3 = 3x3)' read *, r1,c1 print *, 'Matrix 1:' call fillmatrix(r1,c1,mat1) r2 = r1 c2 = c1 print *, 'Matrix 2:' call fillmatrix(r2,c2,mat2) call output(r1,c1,mat1,'Matrix 1: ') call output(r2,c2,mat2,'Matrix 2: ') rmat = mat1+mat2 call output(r1,c1,rmat,'Sum: ') case (2) print *, 'Enter # of rows &amp; columns (1-10) (ex. 3 3 = 3x3)' read *, r1,c1 print *, 'Matrix 1:' call fillmatrix(r1,c1,mat1) r2 = r1 c2 = c1 print *, 'Matrix 2:' call fillmatrix(r2,c2,mat2) rmat = mat1-mat2 call output(r1,c1,mat1,'Matrix 1: ') call output(r2,c2,mat2,'Matrix 2: ') call output(r1,c1,rmat,'Sum: ') case (3) print *, 'Enter # of rows &amp; columns for matrix 1' print *, '(1 through 10, ex: 3 3 = 3x3)' read *, r1,c1 print *, 'Matrix 1:' call fillmatrix(r1,c1,mat1) print *, 'Enter # of rows &amp; columns for matrix 2' print *, '(1 through 10, ex: 3 3 = 3x3)' read *, r2,c2 print *, 'Matrix 2:' call fillmatrix(r2,c2,mat2) if (c1.eq.r2) then call multiply(mat1,mat2,rmat,r1,r2,c1,c2) call output(r1,c1,mat1,'Matrix 1: ') call output(r2,c2,mat2,'Matrix 2: ') call output(r1,c2,rmat,'Product: ') end if case (4) print *, 'Enter # of rows &amp; columns for matrix 1' print *, '(1 through 10, ex: 3 3 = 3x3)' read *, r1,c1 print *, 'Matrix 1:' call fillmatrix(r1,c1,mat1) call transpose(mat1,rmat,r1,c1) call output(r1,c1,rmat,'Transpose:') case (5) print *,'5' case default print *,'default' end select ! call fillmatrix(rows,columns,mat1) ! write (*,*) matrix1 END PROGRAM subroutine fillmatrix(r,c,matrix) integer, intent(in) :: r integer, intent(in):: c real, intent(out), dimension(3,3) :: matrix integer i,j do i=1,r do j = 1,c write (*,'(A,I2,A,I2,A)') 'Enter value (',i,',',j,').' read*, matrix(i,j) enddo enddo end subroutine subroutine multiply(m1,m2,res,row1,row2,col1,col2) integer, intent(in) :: row1,row2,col1,col2 real, intent(in), dimension(row1,col1) :: m1 real, intent(in), dimension(row2,col2) :: m2 real, intent(out), dimension(row1,col2) :: res integer :: i,j,k res = 0 do i = 1, row1 do j = 1, col2 do k = 1, col1 ! col1 must equal row2 res(i, j) = res(i, j) + m1(i, k)*m2(k, j) enddo ! k enddo ! j enddo ! i end subroutine subroutine transpose(m1,res,row,col) integer, intent(in) :: row,col real, intent(in), dimension(row,col) :: m1 real, intent(out), dimension(row,col) :: res integer :: i,j,k do i = 1,col do j = 1,row res(i,j) = m1(j,i) enddo enddo end subroutine subroutine output(r,c,matrix,name) integer, intent(in) :: r integer, intent(in):: c character(len=10) :: name real, intent(in), dimension(r,c) :: matrix integer i,j print *,name do i = 1, r write(*,"(100F6.1)") ( matrix(i,j), j=1,c ) enddo end subroutine </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload