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

Join 105,412 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,759 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! 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 - 04: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 - 11:40 AM
Post #2


DIC Maniac

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: 8/20/08 05:37AM

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