Calculating Inverse of a Large Matrix in FORTRAN 90

4,394 views
Skip to first unread message

boby mughal

unread,
Oct 19, 2016, 6:35:20 PM10/19/16
to gg95
Hello Everyone !

First of all, I am glad to see this group for help in FORTRAN. And I hope to get valuable suggestions and help here.
I am working in FORTRAN 90 where I need to calculate Inverse of a 7x7 Matrix accurately. I have used a subroutine in main program which augmenting the matrix with Identity matrix and then perform calculations for Inverse determination by pivoting. But the problem is that most of the time I do not get the correct answer. I verify my inverse using fact that : matrix A multiplied by its Inverse = Identity Matrix . But it do not gives Identity matrix when I use the Inverse calculated by the subroutine. The code is attached here(named: MatInv.f90) .
I am working in Ubuntu 16.04 LTS. I have installed FORTRAN 90 and I compile my programs by command " gfortran porgram.f90 -o program" in terminal.

I searched on INTERNET that LAPACK is a library which can efficiently calculates inverse of large matrices. So following instruction in a video on YOUTUBE, I installed LAPACK by commands in terminal as follows:

1) tar zxvf lapack-3.6.0.gz
2) cd lapack-3.6.0

◇ gfortran
cp make.inc.example make.inc

4) make blaslib
5) make lapacklib
6) sudo ln -s $HOME/lapack-3.6.0/librefblas.a /usr/local/lib/libblas.a
7) sudo ln -s $HOME/lapack-3.6.0/liblapack.a /usr/local/lib/liblapack.a


(on checking in /usr/local/bin these libraries are present there)
Now I want to use the program which uses LAPACK to find inverse of a large matrix but I do not know how to compile the code using these libraries. After searching on INTERNET and using file matrix_inverse.f90 (attached named: inverse_mat.f90) when I use command :

gfortran my_program.f90 -llapack -lblas
It says:
(.text+0x20): undefined reference to `main'collect2: error: ld returned 1 exit status


I am completely stuck here. I do not know certain things as follows:

               (I). Whether I need to use LAPACK or not to find Inverse of a 7x7 matrix
               (II). Why my program (without LAPACK) do not give correct result for inverse. If the inverse is accurate it should satisfy condition:
                                                  matrix A multiplied by its Inverse = Identity Matrix
                     but it do not satisfy this condition.
               (III). If I use LAPACK, then how to accuratly use it to write and compile a program for inverse
               (IV). What is the reason of the error mentioned above
               (V). How to check whether I have installed LAPACK correctly or not
               (VI). How I can install and use LAPACK, step by step instruction.
               (VII). Finally, without knowing answers of all above questions, how can I find ACCURATE Inverse of a 7x7 martrix in FORTRAN 90?

     I shall be highly grateful for any suggestions and help.
Best Regards,
Mirza Younis Baig
MatInv.f90
inverse_mat.f90

Damian Rouson

unread,
Oct 19, 2016, 6:45:19 PM10/19/16
to gg...@googlegroups.com
On Oct 19, 2016, at 3:35 PM, boby mughal <mirzay...@gmail.com> wrote:

I have installed FORTRAN 90 and I compile my programs by command " gfortran porgram.f90 -o program" in terminal.

gfortran and g95 diverged many years ago (more than a decade ago if I recall correctly).  Questions for the gfrotran developers go to for...@gcc.gnu.org



6) sudo ln -s $HOME/lapack-3.6.0/librefblas.a /usr/local/lib/libblas.a
7) sudo ln -s $HOME/lapack-3.6.0/liblapack.a /usr/local/lib/liblapack.a


(on checking in /usr/local/bin these libraries are present there)
Now I want to use the program which uses LAPACK to find inverse of a large matrix but I do not know how to compile the code using these libraries. After searching on INTERNET and using file matrix_inverse.f90 (attached named: inverse_mat.f90) when I use command :

gfortran my_program.f90 -llapack -lblas

My guess is you’re missing the -L arguments that tell gfortran where to find these libraries. I think you want something like

gfortran my_program.f90 -L /usr/local/lib/ -L /usr/local/lib/libblas.a  -llapack -lblas

or possibly

gfortran my_program.f90 -L $HOME/lapack-3.6.0/ -L $HOME/lapack-3.6.0/ -llapack -lblas


It says:
(.text+0x20): undefined reference to `main'collect2: error: ld returned 1 exit status


I am completely stuck here. I do not know certain things as follows:

               (I). Whether I need to use LAPACK or not to find Inverse of a 7x7 matrix

I’m not a linear algebra expert, but I believe most experts would say that you it’s almost never a good idea to compute an actual inverse.  You might instead store a factorization of the matrix.  That question might be more appropriate for the LAPACK mailing list.

               (II). Why my program (without LAPACK) do not give correct result for inverse. If the inverse is accurate it should satisfy condition:
                                                  matrix A multiplied by its Inverse = Identity Matrix
                     but it do not satisfy this condition.
               (III). If I use LAPACK, then how to accuratly use it to write and compile a program for inverse
               (IV). What is the reason of the error mentioned above
               (V). How to check whether I have installed LAPACK correctly or not
               (VI). How I can install and use LAPACK, step by step instruction.
               (VII). Finally, without knowing answers of all above questions, how can I find ACCURATE Inverse of a 7x7 martrix in FORTRAN 90?

These are also questions for a LAPACK mailing list.

Damian

Adelson Oliveira

unread,
Oct 19, 2016, 8:11:48 PM10/19/16
to gg...@googlegroups.com
Your Linux distribution probably has a native package for installation of LAPACK and others Linear Algebra Packages. If you choose to install them your first attempt to compile your program would work nicely. I don't use Ubuntu, look for someone else help on it.

Anyway, if you finally decide not to search for a ubuntu's package for LAPACK, follow the suggestion for using -L options to tell the compiler where LAPACK routines can be found.

On the linear algebra side, you should follow the suggestion to factorize the matrix given above. That may be that your matrix is not invertible. LAPACK has routines for that purpuse. Look for references like SVD for example.

--
You received this message because you are subscribed to the Google Groups "gg95" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gg95+unsubscribe@googlegroups.com.
To post to this group, send email to gg...@googlegroups.com.
Visit this group at https://groups.google.com/group/gg95.
For more options, visit https://groups.google.com/d/optout.

jfh

unread,
Oct 26, 2016, 7:25:32 PM10/26/16
to gg95

You have already been told why it's usually a bad idea to use the inverse of a matrix and what to do instead. If you don't yet understand the numerical problems of linear algebra you may like what I used to convince a physics graduate student years ago, who thought that the inverse of a non-singular but ill-conditioned 4x4 matrix would solve his problem. It would, but only if the numbers in  it were all known exactly. Alas, the numbers in his original matrix were  found by experiment accurately to about 1 part in 10**6. I suggested changing a few of them by 1 part in 10**6 and see what happened to his inverse matrix. He was unhappy when he found numbers in it changing by over 50%, 1 part in 2, which is why his method was not already in use.  (He had done very well in all the courses in theoretical physics and relevant mathematics that the university offered except for the ones on numerical analysis, which he had not attempted.) So try changing some of the numbers in your 7x7 matrix by a small amount and see what happens to its inverse.
Reply all
Reply to author
Forward
0 new messages