# Calculate the inverse of a matrix 19*19

Hordur Kvaran hordurk at rhi.hi.is
Tue Dec 5 17:17:39 EST 1995

```In <49hrq8\$92b at astor.urv.es> Santi Garcia Vallve <vallve at argo.urv.es> writes:

>I am trying to make my own programs with fortran77 to predict protein
>folding types by distances functions that make allowances for amino acid
>interactions (Chou and Zhang, 1994 JBC). I need to calculate the inverse
>of a matrix 19*19 (19 rows and 19 columns), how can I make it? It is
>possible to do it with fortran?

Using Matlab conventions:

% inv(A) is read as: inverse of A
% Adj(A) is the adjoint of A which is a matrix defined by the transpose
% matrix of cofactors.  For a 3x3 matrix:

c21 c22 c23
c31 c32 c33]'   % the ' stands for transpose

% abs(A) is the absolute value of A or all eigenvalues multiplied together.

Cofactors:

Cij=(-1)^(i+j)*mij

where mij is the minor for rows i and j.  You can find the minor by crossing
out rows i and j in your matrix and taking the determinant of the matrix
left (18x18).

Lets say you have a 3x3 matrix A = [2  1  1
1  1 -1
2  1  3]

You need the matrix of cofactors but to find that you need to find the minors.
As an example lets find m31.  To do that you cross out row 3 and column 1
which leaves you with the matrix   [1  1
1 -1]

Take the determinant:  1*(-1) - 1*1=-2.  So minor m31 = -2
Calculate cofactor c31=(-1)^(i+j)*m31=(-1)^(3+1)*(-2)=-2.  So you now have
one of 9 cofactors.  Fill out the cofactor matrix:

C = [c11 c12 c13]  = [4  -5 -1]
[c21 c22 c23]    [-2  4  0]
[-2  c32 c33]    [-2  3  1]

abs(A)=2 so

inv(A)=Adj(A)/abs(A) = C'/abs(A) =  [4  -5 -1]T /  = [2 -1  -1
[-2  4  0] /-2   -5/2 2 3/2
[-2  3  1]/      -1/2 0 1/2]

Just scale it up for a 19x19 matrix :)
BTW There are other ways to do it that are more efficient but hey, this one
works and is fairly simple.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Hordur Kvaran 			|				 |
| University of Iceland 		|	"I can resist everything |
| Mechanical Engineering		|	 except temptation"	 |
| hordurk at rhi.hi.is			|		-Oscar Wilde	 |
| URL:  http://www.rhi.hi.is/~hordurk/	|				 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

PS.  I know.  Nothing better to do.  I dread going back to work on the
project I'm working on.

```