Numerical Recipes Forum  

Go Back   Numerical Recipes Forum > Obsolete Editions Forum > Fortran 90 Programming with NR

Reply
 
Thread Tools Display Modes
  #1  
Old 04-18-2008, 10:05 PM
silveira13 silveira13 is offline
Registered User
 
Join Date: Apr 2008
Posts: 3
matrix multiplication

Dear friends,

I´d like to know how to perform a matrix multiplication with Fortran 90.

Thanks for answer!
Reply With Quote
  #2  
Old 04-18-2008, 11:17 PM
davekw7x davekw7x is offline
Registered User
 
Join Date: Jan 2008
Posts: 453
Quote:
Originally Posted by silveira13 View Post
...how to perform a matrix multiplication with Fortran 90
1. Get a book or other reference to Fortran 90.
2. Look up matmul:

Code:
      PROGRAM xmatmul

      IMPLICIT NONE

      INTEGER i,j
      INTEGER, PARAMETER :: M=2,N=3

      REAL :: a(M,N) = RESHAPE((/1.0,4.0,2.0,5.0,3.0,6.0/),(/M,N/))
      REAL :: b(N,M) = RESHAPE((/1.0,2.0,3.0,4.0,5.0,6.0/),(/N,M/))
      REAL :: c(M,M)

      write(*,*) 'Matrix [a]'
      do i=1,M
        write(*,1000) (a(i,j),j=1,N)
      enddo
      write(*,*)

      write(*,*) 'Matrix [b]'
      do i=1,N
        write(*,1000) (b(i,j),j=1,M)
      enddo
      write(*,*)

      c = matmul(a, b)
      write(*,*) 'Matrix [c] = [a] x [b]'
      do i = 1,M
        write(*,1000) (c(i,j),j=1,M)
      enddo

1000  FORMAT(1x,1P10E14.6)

      END PROGRAM xmatmul
Output:
Code:
 Matrix [a]
   1.000000E+00  2.000000E+00  3.000000E+00
   4.000000E+00  5.000000E+00  6.000000E+00

 Matrix [b]
   1.000000E+00  4.000000E+00
   2.000000E+00  5.000000E+00
   3.000000E+00  6.000000E+00

 Matrix [c] = [a] x [b]
   1.400000E+01  3.200000E+01
   3.200000E+01  7.700000E+01
Regards,

Dave
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 08:49 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.