How to Implement Laplace Expansion in Fortran for Matrix Determinants?

Click For Summary
SUMMARY

This discussion focuses on implementing the Laplace expansion for calculating the determinant of an NxN matrix using Fortran 95. The user initially encounters an "undefined variable" error due to the improper declaration of the variable 'n' and the incorrect handling of array dimensions. After addressing these issues, the user transitions to a "nonconformant arrays" error, indicating further adjustments are needed in the recursive function for proper matrix manipulation. The final code structure includes a recursive function 'det' that now correctly takes 'n' as an argument, allowing for accurate determinant calculations.

PREREQUISITES
  • Fortran 95 programming language
  • Understanding of recursive functions
  • Matrix operations and manipulations
  • Debugging techniques for Fortran
NEXT STEPS
  • Explore Fortran 95 array handling and memory allocation techniques
  • Learn about recursive algorithms and their applications in numerical methods
  • Investigate error handling in Fortran to manage common compilation issues
  • Study advanced matrix operations, including LU decomposition and its implementation in Fortran
USEFUL FOR

Students and developers working on numerical analysis, particularly those needing to implement matrix operations in Fortran, as well as educators teaching linear algebra concepts through programming.

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
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K