- #1
Ha1234
- 1
- 0
Hi,
I have a project for a class in numerics, where I have to write a program with FORTRAN 90 that calculates the determinant of a matrix using Laplace.
The program itself works now, but I have added a function that finds the row/column with most zeroes and reorganizes the matrix, so that they are in the first column. When I try it with small matrices, it works just fine. However (starting at 4x4) I get the following error:
SIGSEGV:Segmentation fault-invalid memory reference.
Backtrace for this error:
#0 6f6117be
#1 6f695deb
#2 004010f8
I have read pretty much every discussion about this I could find, but either the solutions did not apply to my code or the explanation was too complicated for me to unterstand...This is the first program ever that I wrote, so I am not an expert.
This is the function (whithout this function it works fine, so I gues the problem is somewhere in the function):
I would really apprecaite it if someone had ideas what else I could be doing wrong!
Thank you!
I have a project for a class in numerics, where I have to write a program with FORTRAN 90 that calculates the determinant of a matrix using Laplace.
The program itself works now, but I have added a function that finds the row/column with most zeroes and reorganizes the matrix, so that they are in the first column. When I try it with small matrices, it works just fine. However (starting at 4x4) I get the following error:
SIGSEGV:Segmentation fault-invalid memory reference.
Backtrace for this error:
#0 6f6117be
#1 6f695deb
#2 004010f8
I have read pretty much every discussion about this I could find, but either the solutions did not apply to my code or the explanation was too complicated for me to unterstand...This is the first program ever that I wrote, so I am not an expert.
This is the function (whithout this function it works fine, so I gues the problem is somewhere in the function):
Fortran:
function sortieren_Matrix(A,n) result(A_getauscht)
implicit none
!Festlegen der Variablen
real A(n,n) !Matrix A
real A_getauscht(n,n) !Matrix mit vertauschten Zeilen und Spalten
real merk !Hilfsvariablen
integer n !Größe der Matrix
integer zaehl !Zählt die Nullen
integer merk1,merk2 !Hilfsvariablen
integer zaehl_max1,zaehl_max2 !Anzahl Nullen (1-Zeile,2-Spalte)
integer minus !negatives Vorzeichen
integer i,j !Laufindices
!Durchsuchen nach Zeile mit den meisten Nullen
zaehl_max1=0.0
do i=1,n
zaehl=0.0
do j=1,n
if(A(i,j) .eq. 0.0) then
zaehl=zaehl+1
end if
end do
if(zaehl .gt. zaehl_max1) then
zaehl_max1=zaehl
merk1=i !Merken, in welcher Zeile die meisten Nullen sind
end if
end do
!Durchsuchen nach Spalte mit den meisten Nullen
zaehl_max2=0.0
do i=1,n
zaehl=0.0
do j=1,n
if(A(j,i) .eq. 0.0) then
zaehl=zaehl+1
end if
end do
if(zaehl .gt. zaehl_max2) then
zaehl_max2=zaehl
merk2=i !Merken, in welcher Spalte die meisten Nullen sind
end if
end do
!neu Ordnen der Matrix, sodass die erste Spalte, die mit den meisten Nullen ist
if(zaehl_max1 .gt. zaehl_max2) then !sehen ob in Spalte oder Zeile die meisten Nullen sind
do i=1,n !Zeilen und Spalten vertauschen/transponieren
do j=1,n
A_getauscht(i,j)=A(j,i)
end do
end do
merk2=merk1
else
A_getauscht=A
end if
!Vorzeichen der Determinante positiv oder negativ?
minus=1
if(merk2 .ne. 1) then !Wenn bei einer Matrix Spalten oder Zeilen getauscht werden, ändert sich ihr Vrozeichen
minus=-1
end if
!Ordnen, sodass Nullen in erster Spalte
do i=1,n
merk=A_getauscht(i,1)
A_getauscht(i,1)=A_getauscht(i,merk2)
A_getauscht(i,merk2)=merk
end do
end function sortieren_Matrix
I would really apprecaite it if someone had ideas what else I could be doing wrong!
Thank you!