Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fortran problem matrix

Status
Not open for further replies.

ema91

Technical User
Nov 26, 2015
8
GB
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
 
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
 
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
 
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
 
Well...that is great practice and all; but, you do know about the Fortran intrinsic that does exactly all that, right?...matmul()
 
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.
 
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
 
Your problem on this thread has been solved. Raise that as another thread.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top