I just learned Fortran recently and I am trying to compute the determinant of a matrix in Fortran. I was able to declare the matrix and print it out in Fortran. I know that if 2x2 matrix determinant would be mat(0,0) * mat(1,1) * mat(0,1) * mat(1,1). And if I want to calculate the determinant for a 3x3 matrix, I can reuse the logic in 2x2 matrix, right? But now I am trying to do a 5x5 matrix. I want to do it in a smart way instead of multiplying every element like "mat(0,0) * mat(1,1)... mat(5,5)"... How should I implement it without recursion?
Code:
program main
Implicit None
integer:: mat(5,5) ! Should I use Dimension() here?
integer::i,j,k
mat = reshape((/ 8, 9, 5, 4, 6 &
,-3, -2, 2, 1, 3 &
, 1, 4, -5, -3, 2 &
, 3, 2, 6, 1, 3 &
, 4, -1, -2, -3, 5 &
/),(/5,5/))
print , "The Matrix is: "
do i = 1, 5
print, (mat(j,i), j = 1, 5)
write(,)
end do
print*, "The determinant of this martrix is: "
print, det
function det(mat), result(D)
double::D
!I'm still trying to finish it....
end program main
See Question&Answers more detail:
os