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!

Recursion Fortran

Status
Not open for further replies.

Runninghigh

Programmer
Jun 20, 2015
5
I have a problem.

I can only a little bit fortran. So I write a program first in Java, then I try to translate it in Fortran.

First my Java-Prgram:

Java:
	public static void main(String[] args) 
	{
	int matrix_groesse=4;
	double [][] array=new double[matrix_groesse][matrix_groesse];
	
	array[0][0]=4;
	array[0][1]=1;
	array[0][2]=0;
	array[0][3]=1;
	//array[0][4]=-3;
	//array[0][5]=-4;
	//array[0][6]=-5;
	
	
	array[1][0]=-2; 
	array[1][1]=0;
	array[1][2]=-2;
	array[1][3]=2;
	//array[1][4]=-4;	
	//array[1][5]=-0;
	//array[1][6]=-1;	

	
	array[2][0]=-1;
	array[2][1]=1;
	array[2][2]=-4;
	array[2][3]=4;
	//array[4][4]=4;		
	//array[4][5]=1;
	//array[4][6]=-3;		

	
	array[3][0]=-5;
	array[3][1]=-4;
	array[3][2]=5;
	array[3][3]=0;	
	//array[2][4]=0;		
	//array[2][5]=1;	
	//array[2][6]=-4;		

	
	/*array[3][0]=-2; 
	array[3][1]=-4;
	array[3][2]=2;
	array[3][3]=-5;	
	array[3][4]=-1;		
	array[3][5]=2;	
	array[3][6]=0;		

	array[5][0]=3;
	array[5][1]=-3;
	array[5][2]=-3;
	array[5][3]=4;	
	array[5][4]=-2;		
	array[5][5]=-5;	
	array[5][6]=1;		

	array[6][0]=-2;
	array[6][1]=-5;
	array[6][2]=3;
	array[6][3]=-2;	
	array[6][4]=3;		
	array[6][5]=1;	
	array[6][6]=-5;*/		
	
	Zeitmesser.startMessung(1);
	System.out.println("Determinante = "+Double.toString(Formelsammlung.determinante(array,matrix_groesse)));
	Zeitmesser.stopMessung(1);
	System.out.println("Zeit: "+Double.toString(Zeitmesser.getErgebnis(1)/1000)+" Sekunde");
	}

public static double determinante(double [][] array) //2x2 Matrix => det = 0/0*1/1-0/1*1/0
{
	return (array[0][0]*array[1][1])-(array[0][1]*array[1][0]);
}

public static double[][] laplace(double [][] array,int groesse_matrix, int spalte) //reduce matrix nxn into a matrix n-1*n-1
{
	double [][] array_=new double[groesse_matrix-1][groesse_matrix-1];
	int k,l;
	
	k=0;
	l=0;
	for (int i=1;i<groesse_matrix;i++)//beginn mit zeile 1, da zeile 0 immer die ausgewählte Zeile ist
		{
			{
			for(int j=0;j<groesse_matrix;j++)
				{
				if(j!=spalte) //aktuelle spalte wird nicht berücksichtigt
					array_[k][l++]=array[i][j];
				}
			}
		k++;
		l=0;
		}
	
	return array_;
}

public static double determinante(double [][] array, int matrix_groesse)
{
	int vorzeichen=1;
	double [][] array2;
	double faktor;
	double zwischensumme=0;
	double endsumme=0;
	
	if (matrix_groesse==2) //terminated by 2x2 matrix, liefert determinante einer 2x2-matrix
		{
		return Formelsammlung.determinante(array);
		}
	else
		{
		for (int i=0;i<matrix_groesse;i++)
			{
			zwischensumme=0;
			array2=laplace(array, matrix_groesse,i);
			faktor=array[0][i]*vorzeichen;
			vorzeichen*=-1;

			if(faktor!=0)
				{
				zwischensumme=faktor*determinante(array2,matrix_groesse-1);
				}
			
			endsumme+=zwischensumme;
			}
		return endsumme;
		}
}

This program calculates the determinant of a nxn matrix by Laplace. This program works.
The methode public static double determinante(double [][] array, int matrix_groesse) is the recursive methode, the heart of the program.
Termination condition is the Size of Matrix -> 2x2.
This program solve all testing problems. For example the test you see in the program. Determinante=6

Now my Fortran-Program
Code:
program determinante

    double precision, dimension(:,:),allocatable::array		!Startmatrix mit groesse matrix_groesse
    integer::groesse_matrix						!Groesse der Startmatrix 
	integer ::i,j								!schleifenvariablen
	double precision ::ergebnis=0								!Ergebnis der Determinante für die Startmatrix
    character(len=10)::hilf						!Wird nur für die sichtbare Lösung aus Start der Exe-Datei benötigt

	! Öffnen der Eingabedatei, Einlesen
	!**************************************************************************!
	open(10,file='vierer.txt')
	read(10,*) groesse_matrix

	! Zuweisung der Matrixgrößen, Einlesen der Matrix und Schließen der Eingabedatei
	allocate(array(groesse_matrix,groesse_matrix))
            
	do i=1,groesse_matrix
		read(10,*) (array(i,j),j=1,groesse_matrix)
	end do
	close(10)      

!Darstellung der zu berechnenden Matrix
	print*, 'Berechnung der Determinanten von zweier bis siebener Matrix'
    print*, '==========================================================='
    print*
    print*, 'gewaehlte Matrixgroesse = ', groesse_matrix
	print*
	print*,'Matrix ='
    do i=1,groesse_matrix
      do j=1,groesse_matrix
		write(*,'(F7.1)',advance="no") array(i,j)
      end do
      print*
    end do
   	print*

    ergebnis=determinante_laplace(array,groesse_matrix)
    
!Freigeben des Speichers array
    deallocate(array)

!Ergebnisausgabe
    print*
    print*,'Determinante = ', ergebnis

!Unterbrechung damit das Programm mit sichtbaren Ergebnis auch bei Start der Exe-Datei angezeigt bleibt. Weiter mit beliebiger Eingabe und Return	
read *, hilf

contains
!Liefert die Determinante einer 2er Matrix zurück
function determinante_matrix_2(array)result(d)
	double precision, dimension(2,2) :: array
    double precision ::d
	d= (array(1,1)*array(2,2))-(array(1,2)*array(2,1))
end function determinante_matrix_2

!Reduziert eine quadratische Matrix nach dem laplaceschem Verfahren um 1 (5er Matrix zu einer 4er Matrix, 4er zu einer 3er Matrix, usw.
function laplace(array, groesse_matrix, spalte)result(array_)
	integer :: groesse_matrix,spalte
	double precision, dimension(groesse_matrix,groesse_matrix), intent(in) :: array
   
	double precision, dimension(groesse_matrix-1,groesse_matrix-1) :: array_

	integer:: k,l
    integer:: i,j
	
	k=1
	l=1
    
	do i=2,groesse_matrix!beginn mit zeile 2, da zeile 1 immer die ausgewählte Zeile ist (Kann man optimieren, in dem man die  Zeile oder Spalte raussucht mit den meisten Nullen)
		do j=1,groesse_matrix
				if(j/=spalte) then!aktuelle spalte wird nicht berücksichtigt
					array_(k,l)=array(i,j)
                    l=l+1
				end if
		end do
		k=k+1
		l=1
	end do

end function laplace

double precision recursive function determinante_laplace(array, groesse_matrix)result(det)
	integer, intent(in) :: groesse_matrix	
	double precision, dimension (groesse_matrix,groesse_matrix) :: array
	double precision, dimension (groesse_matrix-1,groesse_matrix-1) :: array2
    double precision :: det
	integer,save :: vorzeichen=1
	double precision :: faktor
	double precision :: zwischensumme
	double precision :: ergebnis=0
    double precision :: hilf=0
    integer::i !schleifenvariable

	if (groesse_matrix==2) then
    	hilf=determinante_matrix_2(array)
    	det=hilf
	else
		do i=1,groesse_matrix
			zwischensumme=0
			array2=laplace(array, groesse_matrix,i)
			faktor=array(1,i)*vorzeichen
			vorzeichen=vorzeichen*(-1)
			if(array(1,i)/=0) then
				zwischensumme=faktor*determinante_laplace(array2,groesse_matrix-1)
			end if	
			ergebnis=ergebnis+zwischensumme
		end do
        det=ergebnis
	end if

end function determinante_laplace

end program determinante

The recursive function is double precision recursive function determinante_laplace(array, groesse_matrix)result(det).
In my opinion this function is equal to the methode in java.

But it isn't so. The Fortran-program solve only 3x3 Matrix. Is the matrix higher, the result is wrong. Why?

I'am sorry for my bad english. I hope, you can understand my problem and can help to solve it.
 
Thanks for the example.

But I would like to know why my function does not work. It seems to be equal to my recursive in Java. In Java it works fine. In Fortran not. I want to understand why this is so.
 
Runninghigh said:
But I would like to know why my function does not work.
It seems to be equal to my recursive in Java.

Hallo Runninghigh,
Yes I tried your Java program and it worked fine.

There were two problems in your Fortran program in the recursive function determinante_laplace:
1. On the beginning the result was initialized, i.e.:
Code:
   double precision :: ergebnis=0
2. The sign, i.e. vorzeichen was not properly computed.
I changed the computation like in my older example
Code:
   vorzeichen = merge(1, -1, mod(i,2) .eq. 1)

Here is the corrected code which now works:
Code:
[COLOR=#a020f0]module[/color] determinante
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
[COLOR=#a020f0]contains[/color]  
  [COLOR=#a020f0]subroutine[/color] print_matrix(matrix)
    [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
    [COLOR=#2e8b57][b]double precision[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color](:,:), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: matrix
    [COLOR=#2e8b57][b]integer[/b][/color] :: msize([COLOR=#ff00ff]2[/color]), i, j, m, n
    msize [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]shape[/color](matrix)
    m [COLOR=#804040][b]=[/b][/color] msize([COLOR=#ff00ff]1[/color])
    n [COLOR=#804040][b]=[/b][/color] msize([COLOR=#ff00ff]2[/color])
    [COLOR=#804040][b]do[/b][/color] i[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color], m
      [COLOR=#804040][b]do[/b][/color] j[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color], n
        [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]'(f8.2)'[/color],[COLOR=#804040][b]advance[/b][/color] [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]'no'[/color]) matrix(i,j)
      [COLOR=#804040][b]end do[/b][/color]
      [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])
    [COLOR=#804040][b]end do[/b][/color]
    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])
  [COLOR=#a020f0]end subroutine[/color] print_matrix 

  [COLOR=#0000ff]! Liefert die Determinante einer 2er Matrix zurück[/color]
  [COLOR=#a020f0]function[/color] determinante_matrix_2(array) [COLOR=#a020f0]result[/color](d)
    [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
    [COLOR=#2e8b57][b]double precision[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color]([COLOR=#ff00ff]2[/color],[COLOR=#ff00ff]2[/color]) :: array
    [COLOR=#2e8b57][b]double precision[/b][/color] ::d
    d[COLOR=#804040][b]=[/b][/color] (array([COLOR=#ff00ff]1[/color],[COLOR=#ff00ff]1[/color])[COLOR=#804040][b]*[/b][/color]array([COLOR=#ff00ff]2[/color],[COLOR=#ff00ff]2[/color]))[COLOR=#804040][b]-[/b][/color](array([COLOR=#ff00ff]1[/color],[COLOR=#ff00ff]2[/color])[COLOR=#804040][b]*[/b][/color]array([COLOR=#ff00ff]2[/color],[COLOR=#ff00ff]1[/color]))
  [COLOR=#a020f0]end function[/color] determinante_matrix_2

  [COLOR=#0000ff]! Reduziert eine quadratische Matrix nach dem laplaceschem Verfahren um 1 [/color]
  [COLOR=#0000ff]! (5er Matrix zu einer 4er Matrix, 4er zu einer 3er Matrix, usw.[/color]
  [COLOR=#a020f0]function[/color] laplace(array, groesse_matrix, spalte) [COLOR=#a020f0]result[/color](array_)
    [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
    [COLOR=#2e8b57][b]integer[/b][/color] :: groesse_matrix,spalte
    [COLOR=#2e8b57][b]double precision[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color](groesse_matrix,groesse_matrix), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: array
    [COLOR=#2e8b57][b]double precision[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color](groesse_matrix[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color],groesse_matrix[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color]) :: array_
    [COLOR=#2e8b57][b]integer[/b][/color]:: k,l
    [COLOR=#2e8b57][b]integer[/b][/color]:: i,j
    k[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color]
    l[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color]
    [COLOR=#0000ff]! beginn mit zeile 2, da zeile 1 immer die ausgewählte Zeile ist [/color]
    [COLOR=#0000ff]! (Kann man optimieren, in dem man die Zeile oder Spalte raussucht mit den meisten Nullen)[/color]
    [COLOR=#804040][b]do[/b][/color] i[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]2[/color],groesse_matrix
      [COLOR=#804040][b]do[/b][/color] j[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color],groesse_matrix
        [COLOR=#804040][b]if[/b][/color](j[COLOR=#804040][b]/=[/b][/color]spalte) [COLOR=#804040][b]then[/b][/color] [COLOR=#0000ff]!aktuelle spalte wird nicht berücksichtigt[/color]
          array_(k,l)[COLOR=#804040][b]=[/b][/color]array(i,j)
          l[COLOR=#804040][b]=[/b][/color]l[COLOR=#804040][b]+[/b][/color][COLOR=#ff00ff]1[/color]
        [COLOR=#804040][b]end if[/b][/color]
      [COLOR=#804040][b]end do[/b][/color]
      k[COLOR=#804040][b]=[/b][/color]k[COLOR=#804040][b]+[/b][/color][COLOR=#ff00ff]1[/color]
      l[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color]
    [COLOR=#804040][b]end do[/b][/color]
  [COLOR=#a020f0]end function[/color] laplace

  [COLOR=#a020f0]recursive[/color] [COLOR=#a020f0]function[/color] determinante_laplace(array, groesse_matrix) [COLOR=#a020f0]result[/color](det)
    [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
    [COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: groesse_matrix
    [COLOR=#2e8b57][b]double precision[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color] (groesse_matrix,groesse_matrix) :: array
    [COLOR=#2e8b57][b]double precision[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color] (groesse_matrix[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color],groesse_matrix[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color]) :: array2
    [COLOR=#2e8b57][b]double precision[/b][/color] :: det
    [COLOR=#2e8b57][b]integer[/b][/color] :: vorzeichen
    [COLOR=#2e8b57][b]double precision[/b][/color] :: faktor
    [COLOR=#2e8b57][b]double precision[/b][/color] :: zwischensumme
    [COLOR=#2e8b57][b]double precision[/b][/color] :: ergebnis
    [COLOR=#2e8b57][b]double precision[/b][/color] :: hilf[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]0[/color]
    [COLOR=#2e8b57][b]integer[/b][/color]::i [COLOR=#0000ff]!schleifenvariable[/color]

    [COLOR=#804040][b]if[/b][/color] (groesse_matrix[COLOR=#804040][b]==[/b][/color][COLOR=#ff00ff]2[/color]) [COLOR=#804040][b]then[/b][/color]
      hilf[COLOR=#804040][b]=[/b][/color]determinante_matrix_2(array)
      det[COLOR=#804040][b]=[/b][/color]hilf
    [COLOR=#804040][b]else[/b][/color]
      [COLOR=#804040][b]do[/b][/color] i[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color],groesse_matrix
        zwischensumme[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]0[/color]
        array2[COLOR=#804040][b]=[/b][/color]laplace(array, groesse_matrix,i)
        [COLOR=#0000ff]! computing sign[/color]
        [COLOR=#804040][b]if[/b][/color] ([COLOR=#008080]mod[/color](i,[COLOR=#ff00ff]2[/color]) [COLOR=#804040][b].eq.[/b][/color] [COLOR=#ff00ff]1[/color]) [COLOR=#804040][b]then[/b][/color]
          vorzeichen [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color]
        [COLOR=#804040][b]else[/b][/color]
          vorzeichen [COLOR=#804040][b]=[/b][/color] [COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color]
        [COLOR=#804040][b]end if[/b][/color]
        [COLOR=#0000ff]! or shorter:  [/color]
        [COLOR=#0000ff]!vorzeichen = merge(1, -1, mod(i,2) .eq. 1)[/color]
        faktor[COLOR=#804040][b]=[/b][/color]array([COLOR=#ff00ff]1[/color],i) [COLOR=#804040][b]*[/b][/color] vorzeichen
        [COLOR=#804040][b]if[/b][/color](faktor[COLOR=#804040][b]/=[/b][/color][COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
          zwischensumme[COLOR=#804040][b]=[/b][/color]faktor [COLOR=#804040][b]*[/b][/color] determinante_laplace(array2,groesse_matrix[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color])
        [COLOR=#804040][b]end if[/b][/color]
        ergebnis[COLOR=#804040][b]=[/b][/color]ergebnis [COLOR=#804040][b]+[/b][/color] zwischensumme
      [COLOR=#804040][b]end do[/b][/color]
      det[COLOR=#804040][b]=[/b][/color]ergebnis
    [COLOR=#804040][b]end if[/b][/color]
  [COLOR=#a020f0]end function[/color] determinante_laplace
[COLOR=#a020f0]end module[/color] determinante

[COLOR=#a020f0]program[/color] runninghigh
  [COLOR=#a020f0]use[/color] determinante
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
  [COLOR=#2e8b57][b]double precision[/b][/color] :: A1([COLOR=#ff00ff]1[/color],[COLOR=#ff00ff]1[/color]), A2([COLOR=#ff00ff]2[/color], [COLOR=#ff00ff]2[/color]), A3([COLOR=#ff00ff]3[/color], [COLOR=#ff00ff]3[/color]), A4([COLOR=#ff00ff]4[/color],[COLOR=#ff00ff]4[/color]), A5([COLOR=#ff00ff]5[/color],[COLOR=#ff00ff]5[/color])
  [COLOR=#2e8b57][b]double precision[/b][/color] :: det 

  [COLOR=#0000ff]! matrix A2[/color]
  A2([COLOR=#ff00ff]1[/color],:)[COLOR=#804040][b]=[/b][/color]([COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]7[/color], [COLOR=#ff00ff]6[/color][COLOR=#804040][b]/[/b][/color])
  A2([COLOR=#ff00ff]2[/color],:)[COLOR=#804040][b]=[/b][/color]([COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]3[/color], [COLOR=#ff00ff]9[/color][COLOR=#804040][b]/[/b][/color])
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Matrix A2:'[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'----------'[/color]
  [COLOR=#008080]call[/color] print_matrix(A2)
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Determinant of A2:'[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'-----------------'[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) determinante_laplace(A2, [COLOR=#ff00ff]2[/color])
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])

  [COLOR=#0000ff]! matrix A3[/color]
  A3([COLOR=#ff00ff]1[/color],:)[COLOR=#804040][b]=[/b][/color]([COLOR=#804040][b]/-[/b][/color][COLOR=#ff00ff]2[/color], [COLOR=#ff00ff]2[/color], [COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]3[/color][COLOR=#804040][b]/[/b][/color])
  A3([COLOR=#ff00ff]2[/color],:)[COLOR=#804040][b]=[/b][/color]([COLOR=#804040][b]/-[/b][/color][COLOR=#ff00ff]1[/color], [COLOR=#ff00ff]1[/color],  [COLOR=#ff00ff]3[/color][COLOR=#804040][b]/[/b][/color])
  A3([COLOR=#ff00ff]3[/color],:)[COLOR=#804040][b]=[/b][/color]([COLOR=#804040][b]/[/b][/color] [COLOR=#ff00ff]2[/color], [COLOR=#ff00ff]0[/color], [COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color][COLOR=#804040][b]/[/b][/color])
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Matrix A3:'[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'----------'[/color]
  [COLOR=#008080]call[/color] print_matrix(A3)
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Determinant of A3:'[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'-----------------'[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) determinante_laplace(A3, [COLOR=#ff00ff]3[/color])
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])

  [COLOR=#0000ff]! matrix A4[/color]
  A4([COLOR=#ff00ff]1[/color],:)[COLOR=#804040][b]=[/b][/color]([COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]3[/color], [COLOR=#ff00ff]0[/color], [COLOR=#ff00ff]2[/color], [COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color][COLOR=#804040][b]/[/b][/color])
  A4([COLOR=#ff00ff]2[/color],:)[COLOR=#804040][b]=[/b][/color]([COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]1[/color], [COLOR=#ff00ff]2[/color], [COLOR=#ff00ff]0[/color], [COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]2[/color][COLOR=#804040][b]/[/b][/color])
  A4([COLOR=#ff00ff]3[/color],:)[COLOR=#804040][b]=[/b][/color]([COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]4[/color], [COLOR=#ff00ff]0[/color], [COLOR=#ff00ff]6[/color], [COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]3[/color][COLOR=#804040][b]/[/b][/color])
  A4([COLOR=#ff00ff]4[/color],:)[COLOR=#804040][b]=[/b][/color]([COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]5[/color], [COLOR=#ff00ff]0[/color], [COLOR=#ff00ff]2[/color],  [COLOR=#ff00ff]0[/color][COLOR=#804040][b]/[/b][/color])
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Matrix A4:'[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'----------'[/color]
  [COLOR=#008080]call[/color] print_matrix(A4)
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Determinant of A4:'[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'-----------------'[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) determinante_laplace(A4, [COLOR=#ff00ff]4[/color])
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])

  [COLOR=#0000ff]! matrix A5[/color]
  A5([COLOR=#ff00ff]1[/color],:)[COLOR=#804040][b]=[/b][/color]([COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]5[/color], [COLOR=#ff00ff]2[/color], [COLOR=#ff00ff]0[/color], [COLOR=#ff00ff]0[/color], [COLOR=#ff00ff]2[/color][COLOR=#804040][b]/[/b][/color])
  A5([COLOR=#ff00ff]2[/color],:)[COLOR=#804040][b]=[/b][/color]([COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]0[/color], [COLOR=#ff00ff]1[/color], [COLOR=#ff00ff]4[/color], [COLOR=#ff00ff]3[/color], [COLOR=#ff00ff]2[/color][COLOR=#804040][b]/[/b][/color])
  A5([COLOR=#ff00ff]3[/color],:)[COLOR=#804040][b]=[/b][/color]([COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]0[/color], [COLOR=#ff00ff]0[/color], [COLOR=#ff00ff]6[/color], [COLOR=#ff00ff]2[/color], [COLOR=#ff00ff]3[/color][COLOR=#804040][b]/[/b][/color])
  A5([COLOR=#ff00ff]4[/color],:)[COLOR=#804040][b]=[/b][/color]([COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]0[/color], [COLOR=#ff00ff]0[/color], [COLOR=#ff00ff]4[/color], [COLOR=#ff00ff]3[/color], [COLOR=#ff00ff]1[/color][COLOR=#804040][b]/[/b][/color])
  A5([COLOR=#ff00ff]5[/color],:)[COLOR=#804040][b]=[/b][/color]([COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]0[/color], [COLOR=#ff00ff]0[/color], [COLOR=#ff00ff]0[/color], [COLOR=#ff00ff]0[/color], [COLOR=#ff00ff]2[/color][COLOR=#804040][b]/[/b][/color])
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Matrix A5:'[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'----------'[/color]
  [COLOR=#008080]call[/color] print_matrix(A5)
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Determinant of A5:'[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'-----------------'[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) determinante_laplace(A5, [COLOR=#ff00ff]5[/color])
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])    
[COLOR=#a020f0]end program[/color] runninghigh

Compilation and running
Code:
$ gfortran runninghigh.f95 -o runninghig

$ runninghigh
 Matrix A2:
 ----------
    7.00    6.00
    3.00    9.00

 Determinant of A2:
 -----------------
   45.000000000000000

 Matrix A3:
 ----------
   -2.00    2.00   -3.00
   -1.00    1.00    3.00
    2.00    0.00   -1.00

 Determinant of A3:
 -----------------
   18.000000000000000

 Matrix A4:
 ----------
    3.00    0.00    2.00   -1.00
    1.00    2.00    0.00   -2.00
    4.00    0.00    6.00   -3.00
    5.00    0.00    2.00    0.00

 Determinant of A4:
 -----------------
   20.000000000000000

 Matrix A5:
 ----------
    5.00    2.00    0.00    0.00    2.00
    0.00    1.00    4.00    3.00    2.00
    0.00    0.00    6.00    2.00    3.00
    0.00    0.00    4.00    3.00    1.00
    0.00    0.00    0.00    0.00    2.00

 Determinant of A5:
 -----------------
   100.00000000000000




 
P.S.:
I forgot to remove the variable hilf
Code:
     double precision :: hilf
It's not needed, you can can assign the result of a function to det
Code:
...
    if (groesse_matrix==2) then
      det=determinante_matrix_2(array)
    else
...
 
Thank you, great work. Now it works. Very fine.

Hilf I used only for debugging, but in vain.

Can you tell me why it is in Fortran a mistake to initialize Ergebnis?
In Java the Compiler complains, then I write ergebnis+=zwischensumme, because ergebnis has no current value. So I initialize ergebnis with 0, the value in the beginning.
If the first time is reached , the line Ergebnis=Ergebnis+1 , which makes the Compiler in Fortran when ergebnis is not initialized ?
 
Can you tell me why it is in Fortran a mistake to initialize Ergebnis?
The same question I asked myself :)
I'm not sure ... but there must be a diffrence how the function behave in fortran and in Java.

If you initialize your variables in the declaration section of the function i.e. here
Code:
...
    [highlight]integer :: vorzeichen=1[/highlight]
    ...
    [highlight]double precision :: ergebnis=0[/highlight]
...
the results are false.

But, if you initialize your variables in the function body i.e.
Code:
...
    integer :: vorzeichen
    ...
    double precision :: ergebnis
    integer::i !schleifenvariable
    ...
    [highlight]vorzeichen=1[/highlight]
    [highlight]ergebnis=0[/highlight]
    if (groesse_matrix==2) then
      det=determinante_matrix_2(array)
    else
...
then the computation works as expected.

Maybe in the first case, the compiler thinks, that ergebnis and vorzeichen are constants and not variables?

So you can keep your formula
vorzeichen = vorzeichen * (-1)
too, i.e. the working function is
Code:
  [COLOR=#a020f0]recursive[/color] [COLOR=#a020f0]function[/color] determinante_laplace(array, groesse_matrix) [COLOR=#a020f0]result[/color](det)
    [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
    [COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: groesse_matrix
    [COLOR=#2e8b57][b]double precision[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color] (groesse_matrix,groesse_matrix) :: array
    [COLOR=#2e8b57][b]double precision[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color] (groesse_matrix[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color],groesse_matrix[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color]) :: array2
    [COLOR=#2e8b57][b]double precision[/b][/color] :: det
    [COLOR=#2e8b57][b]integer[/b][/color] :: vorzeichen
    [COLOR=#2e8b57][b]double precision[/b][/color] :: faktor
    [COLOR=#2e8b57][b]double precision[/b][/color] :: zwischensumme
    [COLOR=#2e8b57][b]double precision[/b][/color] :: ergebnis
    [COLOR=#2e8b57][b]integer[/b][/color]::i [COLOR=#0000ff]!schleifenvariable[/color]

    vorzeichen[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color]
    ergebnis[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]0[/color]
    [COLOR=#804040][b]if[/b][/color] (groesse_matrix[COLOR=#804040][b]==[/b][/color][COLOR=#ff00ff]2[/color]) [COLOR=#804040][b]then[/b][/color]
      det[COLOR=#804040][b]=[/b][/color]determinante_matrix_2(array)
    [COLOR=#804040][b]else[/b][/color]
      [COLOR=#804040][b]do[/b][/color] i[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color],groesse_matrix
        zwischensumme[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]0[/color]
        array2[COLOR=#804040][b]=[/b][/color]laplace(array, groesse_matrix,i)
        faktor[COLOR=#804040][b]=[/b][/color]array([COLOR=#ff00ff]1[/color],i) [COLOR=#804040][b]*[/b][/color] vorzeichen
        vorzeichen[COLOR=#804040][b]=[/b][/color]vorzeichen [COLOR=#804040][b]*[/b][/color] ([COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color])
        [COLOR=#804040][b]if[/b][/color](faktor[COLOR=#804040][b]/=[/b][/color][COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
          zwischensumme[COLOR=#804040][b]=[/b][/color]faktor [COLOR=#804040][b]*[/b][/color] determinante_laplace(array2,groesse_matrix[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color])
        [COLOR=#804040][b]end if[/b][/color]
        ergebnis[COLOR=#804040][b]=[/b][/color]ergebnis [COLOR=#804040][b]+[/b][/color] zwischensumme
      [COLOR=#804040][b]end do[/b][/color]
      det[COLOR=#804040][b]=[/b][/color]ergebnis
    [COLOR=#804040][b]end if[/b][/color]
  [COLOR=#a020f0]end function[/color] determinante_laplace

To answer your question exactly, I needed to install a debugger, because when I try to print out the values of some variables in the code, for example like here
Code:
  recursive function determinante_laplace(array, groesse_matrix) result(det)
    ...
    vorzeichen=1
    ergebnis=0
    [COLOR=red]write(*,*) ergebnis[/color]
    if (groesse_matrix==2) then
      det=determinante_matrix_2(array)
    else
      do i=1,groesse_matrix
    ...
then the programm execution seems to stop.
 
That's the solution I think. The Compiler thinks, that are constants, even though the keyword Parameter is missing.

On the idea that since the error might be , I would never have come.
 
The Compiler thinks, that are constants,...
It doesn't seem to be true with the constant, it was only my frustrated guess :)

But according to the link from salgerman, it seems that the variables ergebnis and vorzeichen were initialized only in the first function call and not on the subsequent recursive calls.
You can install a debugger and explore this fact.

 
This is really unexpected . I have checked two days ago with print*, individual values ​​and it was from matrix size 4x4 all nonsense , that was in ergebnis. I could not explain it.

Many thanks for the help. The problem is solve. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top