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.
Asked
Active
Viewed 5,855 times
2
-
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*... – dmckee --- ex-moderator kitten Nov 23 '10 at 19:41
-
I need the code for a project – user517851 Nov 23 '10 at 19:44
-
the inversion needs to be coded for nxn matrices, hence the problem – user517851 Nov 23 '10 at 21:04
-
1Not a problem - get a good library. You need not code it - unless that's the assignment. If that's true, get busy. – duffymo Nov 23 '10 at 21:20
4 Answers
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

BHASKAR GHOSH
- 21
- 1
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