Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,470 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,406 people online right now. Registration is fast and FREE... Join Now!




Fortran: matrix multiplication problem

 
Reply to this topicStart new topic

Fortran: matrix multiplication problem

xrayspex
post 10 Jun, 2008 - 03:06 AM
Post #1


New D.I.C Head

*
Joined: 10 Jun, 2008
Posts: 2

hi all!

i'm trying to write a matrix multiplication code - it's posted below:

CODE

program matrixProduct

implicit none

real, parameter :: N = 3

real, dimension( N , N ) :: A
real, dimension( N ) :: b,c

A = (/1,2,3,4,5,6,7,8,9/)
b = (/11,12,13/)

c = matrixVectorProduct(A,b)

print *, 'A*b = ', c

    contains

    function matrixVectorProduct(A,b) result (c)
    implicit none

    real, dimension(:,:), intent(in) :: A
    real, dimension(:), intent(in) :: b
    real, dimension( size(b) ) :: c

    integer :: N , i
    
    N = size(b)
    i = 1

    do i = 1, N
        c = 0.0
        c = c + b(i) * A(:,i)
        print *,c
    end do


    end function matrixVectorProduct

end program matrixProduct


the problem is: when i try to run it, i get an error msg like this:
___________________________________________________
Compiling Fortran...
C:\Documents and Settings\mlicer\My Documents\matrixProduct.f90
C:\Documents and Settings\mlicer\My Documents\matrixProduct.f90(13) : Error: The shapes of the array expressions do not conform. [A]
A = (/1,2,3,4,5,6,7,8,9/)
^
Error executing df.exe.

matrixProduct.obj - 1 error(s), 0 warning(s)
___________________________________________________

so obviously i am doing something wrong with the matrix A and vector b. any ideas??
User is offlineProfile CardPM

Go to the top of the page

Louisda16th
post 17 Jul, 2008 - 10:40 AM
Post #2


 

Group Icon
Joined: 3 Aug, 2006
Posts: 1,790



Thanked 1 times

Dream Kudos: 755
My Contributions


Old post but here goes....
You have to initialize A like this:
CODE

A = RESHAPE ((/1,2,3,4,5,6,7,8,9/), (/3,3/))

Since it is a multi dimensional array.

Also the parameter N should be an integer so:
CODE

integer, parameter :: N = 3

This is because you are using it to define the dimensions of an array.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 02:04PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month