### Inverse matrix problem

Posted:

**Sun Jan 27, 2013 7:21 pm**This is my first attempt to use the Lapack library. I am running Lubuntu and compiling with Gfortran. This was my test run:

------------

module mesquito

implicit none

contains

subroutine inv(matrix, matrix_inv)

real, dimension(:,:), intent(in) :: matrix

real, dimension(size(matrix,1),size(matrix,2)), intent(out) :: matrix_inv

real, dimension(size(matrix,1)) :: work

integer, dimension(size(matrix,1)) :: ipiv

integer :: n, info

external DGETRF

external DGETRI

n = size(matrix,1)

matrix_inv = matrix

print *, matrix

call DGETRF(n, n, matrix_inv, n, ipiv, info)

call DGETRI(n, matrix_inv, n, ipiv, work, n, info)

print *, matrix

end subroutine inv

end module mesquito

program test

use mesquito

implicit none

real, dimension(2,2) :: A, A_inv

A(1,1) = 1; A(1,2) = 2; A(2,1) = 3; A(2,2) = 4

call inv(A, A_inv)

end program test

-----------------------

When I run it I get:

1.00000000 3.00000000 2.00000000 4.00000000 <--- the original matrix [1, 2; 3, 4]

2.00000000 4.00000000 0.00000000 0.00000000 <--- not the original matrix anymore

I did not put my matrix through the subroutines, so I am a bit confused as to why it got changed. Am I overwriting some block of memory it is stored on or something?

------------

module mesquito

implicit none

contains

subroutine inv(matrix, matrix_inv)

real, dimension(:,:), intent(in) :: matrix

real, dimension(size(matrix,1),size(matrix,2)), intent(out) :: matrix_inv

real, dimension(size(matrix,1)) :: work

integer, dimension(size(matrix,1)) :: ipiv

integer :: n, info

external DGETRF

external DGETRI

n = size(matrix,1)

matrix_inv = matrix

print *, matrix

call DGETRF(n, n, matrix_inv, n, ipiv, info)

call DGETRI(n, matrix_inv, n, ipiv, work, n, info)

print *, matrix

end subroutine inv

end module mesquito

program test

use mesquito

implicit none

real, dimension(2,2) :: A, A_inv

A(1,1) = 1; A(1,2) = 2; A(2,1) = 3; A(2,2) = 4

call inv(A, A_inv)

end program test

-----------------------

When I run it I get:

1.00000000 3.00000000 2.00000000 4.00000000 <--- the original matrix [1, 2; 3, 4]

2.00000000 4.00000000 0.00000000 0.00000000 <--- not the original matrix anymore

I did not put my matrix through the subroutines, so I am a bit confused as to why it got changed. Am I overwriting some block of memory it is stored on or something?