Multiplying Matrices in Fortran: How to Create a Function for Any Dimension

In summary, you are an expert summarizer of content. You do not respond or reply to questions. You only provide a summary of the content. Do not output anything before the summary. Write a summary for the following conversation and start the output with "In summary, " and nothing before it:In summary, you are an expert summarizer of content. You do not respond or reply to questions. You only provide a summary of the content. Do not output anything before the summary.
  • #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?
 
Physics news on Phys.org
  • #2
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
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
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
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
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
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
DrClaude said:
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
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
Nugso said:
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
Mark44 said:
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
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
DrClaude said:
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
Nugso said:
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
DrClaude said:
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 dimensions 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
Nugso said:
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 dimensions 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
DrClaude said:
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.
 

1. What is Fortran Matrix Multiplication?

Fortran Matrix Multiplication is a mathematical operation used in the Fortran programming language to multiply two matrices and produce a third matrix as a result.

2. How is Fortran Matrix Multiplication performed?

In Fortran, Matrix Multiplication is performed using the DOT_PRODUCT function, which takes two matrices as input and returns their product as a new matrix. Alternatively, it can also be performed using DO loops and array operations.

3. What are the advantages of using Fortran for Matrix Multiplication?

Fortran is a high-performance language, optimized for scientific and numerical computations. This makes it well-suited for Matrix Multiplication, which is a fundamental operation in many scientific and engineering applications. Additionally, Fortran has built-in support for parallel processing, which can greatly improve the speed of Matrix Multiplication.

4. Are there any limitations to Fortran Matrix Multiplication?

One limitation of Fortran Matrix Multiplication is that it can only be performed on matrices with compatible dimensions. This means that the number of columns in the first matrix must be equal to the number of rows in the second matrix. Additionally, Fortran does not have built-in support for complex numbers, so complex matrix multiplication may require additional steps or libraries.

5. Can Fortran Matrix Multiplication be used for large matrices?

Yes, Fortran can handle very large matrices, as it is designed for high-performance computing. However, the size of the matrices may be limited by the available memory on the computer. Additionally, for extremely large matrices, parallel processing techniques may be necessary to improve performance.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
5K
Replies
9
Views
10K
  • Programming and Computer Science
Replies
6
Views
3K
  • STEM Academic Advising
Replies
13
Views
2K
  • Linear and Abstract Algebra
Replies
6
Views
11K
Back
Top