# How to Implement Laplace Expansion in Fortran for Matrix Determinants?

• Comp Sci
• kinbeat
In summary, the programmer is trying to solve a system of linear equations by recursively calculating the determinant. However, the compiler gives them an error because they did not declare a variable for the number of rows in the matrix.
kinbeat
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'
allocate(c(m,m))
print*, 'inserisci i coefficienti della matrice'

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?

Hi kinbeat.

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:

## 1. What is Laplace expansion in Fortran?

Laplace expansion in Fortran is a mathematical technique used to calculate the determinant of a square matrix. It involves breaking down the matrix into smaller submatrices and using a recursive formula to solve for the determinant.

## 2. How is Laplace expansion implemented in Fortran?

In Fortran, Laplace expansion can be implemented using a recursive function that calculates the determinant of a matrix by breaking it down into smaller submatrices. The function can be called repeatedly until the determinant is calculated for the entire matrix.

## 3. What are the advantages of using Laplace expansion in Fortran?

Laplace expansion in Fortran allows for the calculation of determinants of large matrices without having to manually calculate each element. It is also a useful tool in solving systems of linear equations and finding inverse matrices.

## 4. Are there any limitations to using Laplace expansion in Fortran?

While Laplace expansion in Fortran is a powerful tool, it can be computationally expensive for large matrices. It also requires a significant amount of memory to store the submatrices, which can be a limitation for systems with limited memory.

## 5. Can Laplace expansion be used for non-square matrices in Fortran?

No, Laplace expansion can only be used for square matrices in Fortran as it relies on the recursive formula for calculating determinants, which only works for square matrices.

• Programming and Computer Science
Replies
5
Views
3K
• Programming and Computer Science
Replies
4
Views
965
• Engineering and Comp Sci Homework Help
Replies
2
Views
5K
• Engineering and Comp Sci Homework Help
Replies
8
Views
2K
• Engineering and Comp Sci Homework Help
Replies
13
Views
2K
• Engineering and Comp Sci Homework Help
Replies
7
Views
1K
• Programming and Computer Science
Replies
12
Views
1K
• Engineering and Comp Sci Homework Help
Replies
5
Views
5K
• Engineering and Comp Sci Homework Help
Replies
1
Views
3K
• Engineering and Comp Sci Homework Help
Replies
7
Views
2K