Calculating Inverse of a Large Matrix in FORTRAN 90

399 views
Skip to first unread message

boby mughal

unread,
Oct 19, 2016, 8:05:41 PM10/19/16
to gonum-dev
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

Brendan Tracey

unread,
Oct 20, 2016, 9:46:31 AM10/20/16
to gonum-dev
You misunderstand the purpose of this mailing list. The list is to help use the gonum library, a suite of scientific and numeric tools for the Go programming language.

If you'd like, it's very easy to compute a matrix inverse in Go, I posted some example code below. Note, however, it's usually a bad idea to compute an explicit matrix inverse.


package main

import (
    "fmt"
    "log"

    "github.com/gonum/matrix/mat64"
)

func main() {
    a := mat64.NewDense(7, 7, []float64{
        10, 20, 8, 9, -1, 3, 5,
        10, 9, 8, 9, -1, 3, 5,
        10, 20, -5, 9, -1, 3, 5,
        10, 20, 8, 18, -1, 3, 5,
        10, 20, 8, 9, 101, 3, 5,
        10, 20, 8, 9, -1, 7, 5,
        10, 20, 8, 9, -1, 3, 42,
    },
    )
    var b mat64.Dense
    if err := b.Inverse(a); err != nil {
        log.Fatal("a is singular")
    }

    var c mat64.Dense
    c.Mul(a, &b)

    fmt.Printf("%0.4v\n", mat64.Formatted(&c))
}
Reply all
Reply to author
Forward
0 new messages