Fortran Matrix Multiplication

  • Comp Sci
  • Thread starter Nugso
  • Start date
  • #1
Nugso
Gold Member
170
10
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?
 

Answers and Replies

  • #2
SteamKing
Staff Emeritus
Science Advisor
Homework Helper
12,796
1,667
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
 
  • #3
Nugso
Gold Member
170
10
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. :(
 
  • #4
SteamKing
Staff Emeritus
Science Advisor
Homework Helper
12,796
1,667
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).
 
  • #5
Nugso
Gold Member
170
10
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?
 
  • #6
DrClaude
Mentor
7,341
3,527
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.
 
  • #7
DrClaude
Mentor
7,341
3,527
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.
 
  • #8
Nugso
Gold Member
170
10
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.
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)
 
  • #9
Nugso
Gold Member
170
10
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.
 
  • #10
33,721
5,418
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.
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.
 
  • #11
Nugso
Gold Member
170
10
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.
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.
 
  • #12
DrClaude
Mentor
7,341
3,527
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.
 
  • #13
Nugso
Gold Member
170
10
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.
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.
 
  • #14
DrClaude
Mentor
7,341
3,527
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.
If you're going to ask for the dimensions on the input line, I guess you have to ask for the values also.
 
  • #15
Nugso
Gold Member
170
10
If you're going to ask for the dimensions on the input line, I guess you have to ask for the values also.
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..
 
  • #16
DrClaude
Mentor
7,341
3,527
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..
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.
 
  • #17
Nugso
Gold Member
170
10
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.
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.
 

Related Threads on Fortran Matrix Multiplication

Replies
2
Views
626
Replies
4
Views
24K
Replies
3
Views
4K
Replies
2
Views
6K
Replies
13
Views
1K
  • Last Post
Replies
7
Views
800
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
16
Views
2K
  • Last Post
Replies
0
Views
1K
Top