Comp Sci How to Implement Laplace Expansion in Fortran for Matrix Determinants?

Click For Summary
The discussion focuses on implementing Laplace expansion in Fortran to calculate the determinant of an NxN matrix using a recursive function. The user initially encounters a compiler error due to an undefined variable and later realizes that the variable 'n' was not declared. After modifying the code to include 'n' as an input parameter, they face a "nonconformant arrays" error related to matrix dimensions. The user clarifies that they aim to create matrix 'b' by excluding the i-th row and the first column from matrix 'a'. The conversation highlights the iterative debugging process in coding for matrix determinant calculation.
kinbeat
Messages
2
Reaction score
0
Hi, I'm dealing with an exam problem (which i have 8 days to solve, with no outside help limitations), which is to write a fortran95 program that solves linear systems.
the first part asks to find the determinant of a NxN matrix with the laplace expansion, implementing it as a recursive function.
this is what I've done so far
module prec
integer, parameter :: rk= selected_real_kind(6)
end module prec

module lap
use prec
implicit nonecontains
recursive function det(a) result (d)
real(kind=rk), intent(in), dimension(:,:) :: a
real(kind=rk), allocatable,dimension(:,:) :: b
real(kind=rk) :: d
integer ::i,n
n=0
b(1:i-1,1:n) = a(1:i-1,2:n)
b(i:n,1:n) =a(i+1:n,2:n)

if (size(a) > 2) then
do i=1,n
d= ((-1)**(i+1))*a(i,1)*det(b)
end do
else
d = a(1,1)*a(2,2)-a(1,2)*a(2,1)

end if
end function det
end module lap

program sistema
use lap
real(kind=rk), allocatable, dimension(:,:) :: c
real(kind=rk) :: z
integer ::j,m
print*,'Inserisci la dimensione della matrice'
read*,m
allocate(c(m,m))
print*, 'inserisci i coefficienti della matrice'
read*, c

do j=1,m
print*,c(j,:)
end do

z=det(c)

print*, z

end program sistema


but the compiler gives me this error:
error 112, reference to undefined variable, array element of function result (/undef)
LAP!DET - in file matrice.f95 at line 18 [+00aa]
main - in file matrice.f95 at line 51 [+0521]

what am i not seeing?
 
Physics news on Phys.org
Hi kinbeat. :welcome:

Code:
integer ::i,n
n=0
b(1:i-1,1:n) = a(1:i-1,2:n)
b(i:n,1:n) =a(i+1:n,2:n)

Does i have a value at the stage of being used on the 3rd line here?
 
ehi there!
yeah, i got a bit further now, i realized i didn't declare the n.
now i get the "nonconformant arrays" error, at that line, "
b(1:i-1,1:n) = a(1:i-1,2:n)what I'm trying to tell the compiler is that the matrix b is a minus the i row and first column.

now i changed that part so it looks like this:

recursive function det(a,n) result (d)
real(kind=rk), intent(in), dimension(:,:) :: a

real(kind=rk) :: d
integer ::i
integer, intent(in)::n
if (size(a) > 2) then
do i=1,n

b(1:i-1,1:n) = a(1:i-1,2:n)
b(i:n,1:n) =a(i+1:n,2:n)
d= ((-1)**(i+1))*a(i,1)*det(b,n)
end do
else
d = a(1,1)*a(2,2)-a(1,2)*a(2,1)

end if
 
Last edited:

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
6K