INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Fortran problem matrix

Fortran problem matrix

(OP)
Hi! I'm doing a program for the product between a matrix and a vector, after i'll need also the product between two matrixes.
My compiler (silverfrost) doesn't work with this code but I think that it is all right :/

program matvett
!
! program for the product Mat x Vect
!
implicit none
integer nmax,mmax
parameter (nmax=20, mmax=20)
integer i,j, n,m
real A(nmax,mmax), xvet(mmax), yvet(nmax)
open(unit=8, file=C:\Users\ema\Desktop\ema.txt)
open(unit=9, file=C:\Users\ema\Desktop\risultato.txt)
read(8,*) n, m
write(9,*) 'Product matrix-vector'
write(9,*) 'Dimension matrix ', n, m
write(9,*) 'Matrix A:'
do i=1,n
read(8,*) (A(i,j),j=1,m)
write(9,*) ( A(i,j),j=1,m)
end do
write(9,*) 'Vector x:'
do i=1,m
read(8,*) xvet(i)
write(9,*) xvet(i)
end do
write(9,*) 'Vector product y:'
do i=1,n
! inizializziamo a 0. ciascuna componente del vettore prodotto
yvet(i)=0.0
!eseguiamo un ciclo do per j=1,m per calcolare le componenti
!del vettore prodotto
do j=1,m
yvet(i) = yvet(i) + A(i,j)*xvet(j)
end do
!scriviamo ciascuna componente sul file di risultati
write(9,*) yvet(i)
end do
close(8)
close(9)
end

RE: Fortran problem matrix

What means "doesn't work" ?

Is it a trouble at compile, link or run time ?

What is the exact message which shows the mistake ?

Fran├žois Jacq

RE: Fortran problem matrix

I tried to compile your program

The compilation fails because of the two wrong statements :

open(unit=8, file=C:\Users\ema\Desktop\ema.txt)
open(unit=9, file=C:\Users\ema\Desktop\risultato.txt)

The right syntax is :

open(unit=8, file="C:\Users\ema\Desktop\ema.txt")
open(unit=9, file="C:\Users\ema\Desktop\risultato.txt")


Fran├žois Jacq

RE: Fortran problem matrix

(OP)
Ok perfect! Now the program works. Sorry if I didn't write the error.
I have another question: if I'would like to do a product between to matrixes how the code will change?
I have done this code but I have an error at line 45

program matmatr
!
! programma per il calcolo del prodotto matrice vettore
!
implicit none
integer nmax,mmax,nmaxb, mmaxb, nmaxc, mmaxc
parameter (nmax=20, mmax=20)
parameter (nmaxb=20, mmaxb=20)
parameter (nmaxc=20, mmaxc=20)
integer i,j, n, m, k, z, x, y
real A(nmax,mmax), B(nmaxb, mmaxb), C(nmaxc, mmaxc)

open(unit=8, file='C:\Users\el1c15\Desktop\emaprova.txt')
open(unit=9, file='C:\Users\el1c15\Desktop\risultatoprova.txt')

read(8,*) n, m
write(9,*) 'Prodotto matrice-matrice'
write(9,*) 'dimensioni della matrice ', n, m
write(9,*) 'matrice A:'
read(8,*) k, z
write(9,*) 'Prodotto matrice-matrice'
write(9,*) 'dimensioni della matrice ', k, z
write(9,*) 'matrice B:'

!Matrix A
do i=1,n
read(8,*) (A(i,j),j=1,m)
write(9,*) ( A(i,j),j=1,m)
end do

!Matrix B
do x=1,k
read(8,*) (B(x,y),y=1,z)
write(9,*) ( B(x,y),y=1,z)
end do



!Product MatrixA x MatrixB
write(9,*) 'Matrice prodotto C:'
do i=1,n
do y=1,z
C(i,y)=0.0
do x=1,k
C(i,y)= C(i,y)+ A(i,j)* B(x,y)
end do
end do
write(9,*) C(i,y)
end do


close(8)
close(9)
end

RE: Fortran problem matrix

Well...that is great practice and all; but, you do know about the Fortran intrinsic that does exactly all that, right?...matmul()

RE: Fortran problem matrix

With Silverfrost, if you are running it from Visual Studio, you could highlight open and press F1 for the help on the open statement. You can still get to the help if you're using Plato but the indexing on the help doesn't work very well.

RE: Fortran problem matrix

(OP)
Ok I resolved my problems glasses here I attach the code (I don't know why but with MATMUL(A,B) the program did not work). I have another question for us, If you start this program you will see that datas of the Matrix C are in column, I'd like that these datas are write in matrix form (like the datas for Matrix A or Matrix B). I don't know how to do that :/

program matmatr
!
! programma per il calcolo del prodotto matrice vettore
!
implicit none
integer nmax,mmax,nmaxb, mmaxb, nmaxc, mmaxc
parameter (nmax=200, mmax=200)
parameter (nmaxb=200, mmaxb=200)
parameter (nmaxc=200, mmaxc=200)
integer i, j, n, m, z, y
real A(nmax,mmax), B(nmaxb, mmaxb), C(nmaxc, mmaxc)

open(unit=8, file='C:\Users\el1c15\Desktop\emaprova.txt')
open(unit=9, file='C:\Users\el1c15\Desktop\risultatoprova.txt')
write(9,*)
write(9,*) '***********************************'
write(9,*) '*** Product Matrix .DOT. Matrix ***'
write(9,*) '***********************************'
write(9,*)

write(*,*)
write(*,*) '***********************************'
write(*,*) '*** Product Matrix .DOT. Matrix ***'
write(*,*) '***********************************'
write(*,*)

!Read the dimension of the Matrix A
read(8,*) n, m
write(9,*) ' Row Column'
write(*,*) ' Row Column'
write(9,*) 'Dimension of Matrix A:', n, ' x',m
write(*,*) 'Dimension of Matrix A:', n, ' x',m


write(9,*)
write(*,*)

!Read the dimension of the matrix B
read(8,*) m, z
write(9,*) 'Dimension of Matrix B:', m,' x',z
write(*,*) 'Dimension of Matrix B:', m,' x',z

write(*,*)
write(9,*)
!Matrix A
write(9,*) 'Matrix A:'
write(*,*) 'Matrix A:'
do i=1,n
read(8,*) (A(i,j),j=1,m)
write(9,*) (A(i,j),j=1,m)
write(*,*) (A(i,j),j=1,m)
end do

write(9,*)
!Matrix B
write(9,*) 'Matrix B:'
write(*,*) 'Matrix B:'
do j=1,m
read(8,*) (B(j,y),y=1,z)
write(9,*) (B(j,y),y=1,z)
write(*,*) (B(j,y),y=1,z)
end do




write(9,*)
!Product MatrixA x MatrixB
write(9,*) 'Matrix product C:'
write(9,*) 'C(i,y)= C(i,y) + A(i,j) .DOT. B(j,y)'
write(9,*)
write(*,*) 'Matrix product C:'
write(*,*) 'C(i,y)= C(i,y) + A(i,j) .DOT. B(j,y)'
write(*,*)
do i=1,n
do y=1,z
C(i,y)=0.0
do j=1, m
C(i,y)=C(i,y)+ A(i,j) * B(j,y)
enddo
write (9,*) C(i,y)
write (*,*) C(i,y)
enddo
end do


close(8)
close(9)
end

RE: Fortran problem matrix

Your problem on this thread has been solved. Raise that as another thread.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close