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.

4
  • Assuming you have been asked to write the inverter (rather than just needing to use one from a library) the first question is: can you perform the inversion by hand? If so, try coding the procedure, and then tell us where you get stuck... Commented Nov 23, 2010 at 19:41
  • I need the code for a project
    – user517851
    Commented Nov 23, 2010 at 19:44
  • the inversion needs to be coded for nxn matrices, hence the problem
    – user517851
    Commented Nov 23, 2010 at 21:04
  • 1
    Not a problem - get a good library. You need not code it - unless that's the assignment. If that's true, get busy.
    – duffymo
    Commented Nov 23, 2010 at 21:20

4 Answers 4

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.

1
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.

1
  • Ideally i would use mathematica but these are the only programs allowed for the code: c c++ fortran tksolver
    – user517851
    Commented Nov 23, 2010 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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.