1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran Matrix Multiplication

  1. May 13, 2013 #1

    Nugso

    User Avatar
    Gold Member

    Hi.


    Write an external function that takes two matrices, multiplies them and returns the resulting matrix.

    Matrices can be of any dimension.
    Print the resulting matrix

    The above is what I have to do.

    http://paste.servut.us/pgmm

    This is what I've done so far. But it seems like there're a few errors in it.

    C:\Users\Samsung\Desktop\Forti\Text1.f90
    C:\Users\Samsung\Desktop\Forti\Text1.f90(59): warning FOR4265: symbol B referenced but not set
    C:\Users\Samsung\Desktop\Forti\Text1.f90(59): warning FOR4265: symbol A referenced but not set
    C:\Users\Samsung\Desktop\Forti\Text1.f90(16): error FOR2942: symbol X is not permitted in a specification expression
    C:\Users\Samsung\Desktop\Forti\Text1.f90(16): error FOR2942: symbol Z is not permitted in a specification expression
    C:\Users\Samsung\Desktop\Forti\Text1.f90(16): error FOR2942: symbol X is not permitted in a specification expression
    Errors in declarations, no further processing for ODEV
    Error executing fl32.exe.
    Text1.exe - 3 error(s), 2 warning(s)


    The homework says it can be any dimesion, so I have to let the user enter the dimensions and I tried doing them with x,y,z,t. Then in order to multiply I did multiply all rows and columns with i,j,k. What am I doing wrong? Could anybody help please?
     
  2. jcsd
  3. May 13, 2013 #2

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    You have a problem declaring arrays A, B and C. The compiler doesn't have enough information to know how much memory to reserve when you have array dimensions which are uninitialized variables.

    The following should give you some ideas about correcting this problem:
    http://www.stanford.edu/class/me200c/tutorial_90/07_arrays.html
     
  4. May 13, 2013 #3

    Nugso

    User Avatar
    Gold Member

    Hi SteamKing. Thanks for the link, but I still have some problems understanding it. I actually tried to declare the arrays by the powerpoint file my teacher has sent.

    http://paste.servut.us/uwgf ( This version failed)

    http://paste.servut.us/osbc ( This version failed as well)


    I really don't see how I have a problem declaring arrays. :(
     
  5. May 13, 2013 #4

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    In the both versions, the arguments of FUNCTION MATRIX cannot each be enclosed by parentheses.
    It should be like this: FUNCTION MATRIX (A, B, C). Your function definition also lacks any executable statements.

    'I really don't see how I have a problem declaring arrays.'

    Your program won't be right until the compiler tells you it is right (by compiling without errors).
     
  6. May 13, 2013 #5

    Nugso

    User Avatar
    Gold Member

    Thanks for the reply. I again put all of them in the same paranthes as you have suggested. But what do you mean by lacking executable statement? ( I think I know what they're. Like do, if etc)

    http://paste.servut.us/ok7k

    This one is a working program which calculates the determinant and I'm trying to use the same structure while declaring the function definition. I don't see what is wrong. Why is mine not working but this one is?
     
  7. May 13, 2013 #6

    DrClaude

    User Avatar

    Staff: Mentor

    You cannot declare arrays as

    real::a(x,y),b(z,t),c(x,t)

    when the compiler does not know the value of x,y,z, and t. Either the size is a parameter, as in the deneme program, or you have to declare them as allocatable, then allocate them when the sizes are known. That allocation does not take place in the function interface, as you wrongly tried, but in the main program itself, after the values of x,y,z, and t have been read.
     
  8. May 13, 2013 #7

    DrClaude

    User Avatar

    Staff: Mentor

    I just realized that you didn't write a function, but a program with an interface declaration to a function you didn't write (see SteamKing's comment). Your code should start by something like

    subroutine matrix_multiply(a,b,c)

    and it should not read anything from standard input. You can then create a program to test that subroutine, and that could read in values from standard input, but you should also set values for the matrices themselves, otherwise you won't know if your routine works or not.
     
  9. May 13, 2013 #8

    Nugso

    User Avatar
    Gold Member

    Thanks for the reply DrClaude. So I'm guessing I should do it something like this;

    http://paste.servut.us/bar9 ( Still working on though, just wanted to show if I'm doing it right)
     
  10. May 13, 2013 #9

    Nugso

    User Avatar
    Gold Member

    http://paste.servut.us/6739

    Did this. No error. But when I compile it I get this; http://i.imgur.com/H9FqAfx.png

    The homework says that it should print the resulting matrix. But I have no idea what the compiler did and another thing that I wonder is the user did not even enter the inputs(elements of matrixes) only dimensions.
     
  11. May 13, 2013 #10

    Mark44

    Staff: Mentor

    Your program needs to initialize the elements of the two matrices that make up the product. The output you got is a result of garbage values (uninitialized values) in a and b.

    BTW, it would be better if you pasted your code here rather than making us open a web page somewhere.
     
  12. May 13, 2013 #11

    Nugso

    User Avatar
    Gold Member

    Thanks for the suggestion and reply. Could you please tell me how I do initialize the elements of the two matrices? I'm trying to figure out but since the dimensions are not known, I don't know how many and what to put.
     
  13. May 13, 2013 #12

    DrClaude

    User Avatar

    Staff: Mentor

    You're going in the right direction.

    The output you get is random, which is normal since the matrices are not initialized (the values in the arrays are random). The reason only 4 values are printed out is that you made a mistake in the indices in the final print.

    Note that everything depends only on 3 sizes, not 4 (there is a constraint as to which matrices can multiply which other). The 3 allocate statements can be combined into one. But mostly, I repeat that you have not created a function (or subroutine), but only have a main program. As it stands, the use of an interface is superfluous. The actual matrix multiplication should not be in the main program. Check the program deneme if you want an example.
     
  14. May 13, 2013 #13

    Nugso

    User Avatar
    Gold Member

    I'm figuring out how to initialize it. Normally I'd do it as; reshape((/1,2,3,4,5,6/),(2,3)) but this time I don't know how many and what integers to add.
     
  15. May 13, 2013 #14

    DrClaude

    User Avatar

    Staff: Mentor

    If you're going to ask for the dimensions on the input line, I guess you have to ask for the values also.
     
  16. May 13, 2013 #15

    Nugso

    User Avatar
    Gold Member

    Yes, but it's not something I can add to reshape function. ( At least I can't think of any way to do so) For example, the user entered the value of dimesions as 2,3 and 4,5. Then the program, I think, should ask for 26 integers and then has to add all into reshape. Guess I'm missing something..
     
  17. May 13, 2013 #16

    DrClaude

    User Avatar

    Staff: Mentor

    Just assign them directly to the array. I would use a loop, such that for instance if the dimensions are 2 x 3, the user would input

    2 4 5
    4 7 1

    with one line per row.
     
  18. May 13, 2013 #17

    Nugso

    User Avatar
    Gold Member

    Thank you all for the replies. I guess I'm too tired and have to rest a bit. I'll work on it tomorrow and I'll be sure asking a few more questions.


    Thanks once again. My brain does not seem to be working.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted