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.
-
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 kittenCommented Nov 23, 2010 at 19:41
-
I need the code for a project– user517851Commented Nov 23, 2010 at 19:44
-
the inversion needs to be coded for nxn matrices, hence the problem– user517851Commented Nov 23, 2010 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.– duffymoCommented Nov 23, 2010 at 21:20
4 Answers
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.
-
Exactly, see 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.– ustunCommented Nov 23, 2010 at 18:53
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
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.
-
Ideally i would use mathematica but these are the only programs allowed for the code: c c++ fortran tksolver Commented Nov 23, 2010 at 18:34
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.