## 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

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

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

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

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

## RE: Fortran problem matrix

## RE: Fortran problem matrix

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