2

I need a code to determine the inverse of an nxn matrix A using row operations. I am having a ridiculous time writing this code. Anything helps, I am a grad student and havent taken a programming class in years. Thanks.

Amro
  • 123,847
  • 25
  • 243
  • 454
user517851
  • 21
  • 1
  • 2

4 Answers4

2

You might not really want the inverse. If you're trying to solve a system of equations you'd be better off using LU decomposition.

You don't say what language you'd like to write this app in. Java has Apache Commons Math; Python has NumPy; FORTRAN has LinPack. Pick a language and use a library; don't write it yourself.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Exactly, see http://www.johndcook.com/blog/2010/01/19/dont-invert-that-matrix/ for more. Though, given this is a homework question, he is probably asked for the inverse. – ustun Nov 23 '10 at 18:53
2
  SUBROUTINE MATINV(A,N)
  DIMENSION A(N,N)
  DO 1 I=1,N
     Z=A(I,I)
     A(I,I)=1.0
     DO 2 J=1,N
2       A(I,J)=A(1,J)/Z
     DO 1 K=1,N
        IF (K-I) 3,1,3
3          Z=A(K,I)
        A(K,I)=0.0
        DO 4 J=1,N
4          A(K,J)=A(K,J)-Z*A(I,J)
1 CONTINUE
  RETURN
  END
1

I suggest Scilab or MATLAB for matrix operations if you haven't taken a programming class in years.

If it is a one time calculation, look at WolframAlpha.

If using libraries is an option for C++, look at Armadillo.

ustun
  • 6,941
  • 5
  • 44
  • 57
  • Ideally i would use mathematica but these are the only programs allowed for the code: c c++ fortran tksolver – user517851 Nov 23 '10 at 18:34
0

Look at Numerical Recipies Online. The Fortran 77, Ansi C and Fortran 90 code is free and you can get pleanty of clues on how to best proceed.

John Alexiou
  • 28,472
  • 11
  • 77
  • 133