Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
-> Matrix Inverse <-
01-01-2010, 05:55 AM
Post: #1
Thumbs Up -> Matrix Inverse <-
I present this program to all pro9ramming crew,specially active ones.and also good manner ones.
this program will calculate the inverse of a matrix given by you,and will check if it is the correct answer or not(if you wish).for more info about matrices and inverse of them visit : en.wikipedia.org/wiki/Matrix_(mathematics)
Code:
Program Matrixx_Inverse(input,output);
  (*This Program will Calculate the inverse of a matrix*)
  Uses CRT;
  Type
    Mat=Array[1..30,1..30] of real;
  Var
    check,counter1,D,sign,counter,help,counter2,q,p,i,j,M,ii,jj,iii,Rows:Intege​r;
    Matrix4,Matrix,Matrix3,Matrix2:mat;
    sum,K,Det,Det2,temp:Real;
    matrix22:array[1..225] of real;
  Procedure ReadingMatrix(Var Matrix:Mat;Rows:integer);
    Var
      ii,jj:integer;
      Element:real;
    Begin
      Writeln(' Please enter the elements of your ',Rows,' X ',Rows,' Matrix');
      For ii:=1 To Rows Do
        Begin
          For jj:=1 To Rows Do
            Begin
              Read(Element);
              Matrix[ii,jj]:=Element;
            End;
        End;
    End;
  Procedure PrintingMatrix(Matrix:Mat;Rows:integer);
      Var
        ii,jj:integer;
        Element:real;
    Begin
      Writeln;
      Textcolor(10);
      For ii:=1 To Rows Do
        Begin
          For jj:=1 To Rows Do
            Begin
              Write(Matrix[ii,jj]:5:2);
            End;
          Writeln;
        End;
    End;
  Procedure Determinant(Matrix:Mat;Rowss:Integer;Var Multi:Real);
    Var
      IIII,JJJJ,LLLL,MMMM:Integer;
    Begin
      For LLLL:=Rowss DownTo 2 Do
          Begin
            For IIII:=1 tO LLLL-1 Do
              Begin
                If Matrix[IIII,LLLL]=0 Then
                  Begin
                  End
                Else
                  If Matrix[LLLL,LLLL]=0 Then
                    Begin
                      For MMMM:=Rowss DownTo 1 Do
                        Begin
                          Matrix[LLLL,MMMM]:=Matrix[iiii,MMMM]+Matrix[LLLL,MMMM];
                        End;
                    End;
                  K:=0;
                  K:=(-1)*Matrix[IIII,LLLL]/Matrix[LLLL,LLLL];
                  For JJJJ:=Rowss DownTo 1 Do
                    Begin
                      Matrix[IIII,JJJJ]:=K*Matrix[LLLL,JJJJ]+Matrix[IIII,JJJJ];
                    End;
              End;
          End;
        Multi:=1;
        For D:=1 To Rowss Do
          Begin
            Multi:=Multi*Matrix[D,D];
          End;
    End;
Begin
  CLRSCR;
  textcolor(10);
  writeln('PLEASE REFER TO  WWW.PRO9RAMMING.COM  IF YOU USE THIS CODE,ENJOY!');
  writeln;
  Textcolor(yellow);
  writeln('Please enter number of rows of your matrix which you want the inverse of it');
  Readln(Rows);
  ReadingMatrix(Matrix,Rows);
  Determinant(Matrix,Rows,Det2);
  If Det2=0 Then
    Begin
      Textcolor(red);
      writeln;
      Writeln('Determinant of your matrix is Zero');
      Writeln('Can`t do this,sorry');
      delay(1000);
      Textcolor(white);
      writeln;
      Writeln('CODED BY ALIREZA_M');
      Repeat
      until keypressed;
      Halt;
    End
  Else
    Sign:=1;
    For I:=1 To Rows Do
      Begin
        For J:=1 To Rows Do
          Begin
            For P:=1 To Rows Do
              Begin
                For Q:=1 To Rows Do
                  Begin
                    If (P<>I) And (Q<>J) Then
                      Begin
                        counter:=counter+1;
                        Matrix22[Counter]:=Matrix[P,Q];
                      End;
                  End;
              End;
            counter:=1;
            For ii:=1 To rows-1 Do
              For jj:=1 to rows-1 Do
                Begin
                  matrix2[ii,jj]:=matrix22[counter];
                  counter:=counter+1;
                end;
            For iii:=1 To i+J Do
              Begin
                Sign:=Sign*(-1);
              End;
            Determinant(Matrix2,Rows-1,Det);
            Matrix3[I,J]:=(Sign*Det)/Det2;
            sign:=1;
            counter:=0;
          End;
      End;
  For i:=1 To rows Do
    Begin
      For J:=1 To Rows Do
        Begin
          If J>I THEN
            Begin
              temp:=Matrix3[i,j];
              Matrix3[i,j]:=Matrix3[j,i];
              matrix3[j,i]:=temp;
            end;
        End;
    End;
  Textcolor(Green);
  Writeln('Here`s the inverse of your matrix ->');
  writeln;
  PrintingMatrix(Matrix3,Rows);
  Writeln;
  textcolor(3);
  writeln('  If you want to check the answer ( multiplication of ');
  writeln('inverse matrix to the original matrix should give you ');
  writeln('a matrix which the elements of main diagon should be 1');
  writeln('others should be 0  then       ->             Press  1');
  Writeln('If you don`t want to check the answer Then -> Press  2');
  Writeln;
  Read(check);
  Case check of
    1: Begin
         writeln;
         For p:=1 To rows Do
           Begin
             For i:=1 To rows Do
               Begin
                 For j:=1 To Rows Do
                   Begin
                     Sum:=sum+Matrix[p,j]*matrix3[j,i];
                   End;
                 Matrix4[p,I]:=Sum;
                 sum:=0;
               End;
           End;
         printingmatrix(matrix4,rows);
         Textcolor(White);
         Delay(1500);
         writeln;
         Writeln('CODED BY ALIREZA_M');
         Repeat
         Until Keypressed;
       End;
    2: Begin
         Textcolor(White);
         Delay(100);
         Writeln('CODED BY ALIREZA_M');
         Repeat
         Until Keypressed;
       End;
  End;
End.

the length of the code is mainly for the respect to you(users) d^-^b

Gholamreza Takhti
who was he? find out
Find all posts by this user
Quote this message in a reply
01-01-2010, 06:14 AM (This post was last modified: 01-01-2010 06:15 AM by drdebcol.)
Post: #2
RE: -> Matrix Inverse <-
Well done !
Mathematically correct and in programming way it is very good and optimized.
So if determinant is zero, it is not possible to find inverse.
So when you multiplicate matrix with it's inverse you need to get Identity matrix. And when i said "multiplicate" i referred to Matrix multiplication :
http://en.wikipedia.org/wiki/Matrix_multiplication
Identity matrix I is a matrix which main diagonal elements are ones and others are zeros :
Code:
1 0 . . . 0
0 1 . . . 0
. . . . . .        
. . . . . .
0 0 . . . 1
In programming :
Code:
if i=j then
  a(i,j)=1
else
  a(i,j)=0
More about Identity matrix you have here :
http://en.wikipedia.org/wiki/Identity_matrix
Determinant of Identity matrix is 1 (det I =1).

This is just for others to see !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
01-01-2010, 06:38 AM (This post was last modified: 01-01-2010 06:49 AM by Alireza_M.)
Post: #3
RE: -> Matrix Inverse <-
(01-01-2010 06:14 AM)drdebcol Wrote:  Well done !
Mathematically correct and in programming way it is very good and optimized.
So if determinant is zero, it is not possible to find inverse.
So when you multiplicate matrix with it's inverse you need to get Identity matrix. And when i said "multiplicate" i referred to Matrix multiplication :
http://en.wikipedia.org/wiki/Matrix_multiplication
Identity matrix I is a matrix which main diagonal elements are ones and others are zeros :
Code:
1 0 . . . 0
0 1 . . . 0
. . . . . .        
. . . . . .
0 0 . . . 1
In programming :
Code:
if i=j then
  a(i,j)=1
else
  a(i,j)=0
More about Identity matrix you have here :
http://en.wikipedia.org/wiki/Identity_matrix
Determinant of Identity matrix is 1 (det I =1).

This is just for others to see !

Thanks for you really fast replies and descriptions,I think if you weren't in this forum some my programs would be left non replied.i appreciate that. Wink

Gholamreza Takhti
who was he? find out
Find all posts by this user
Quote this message in a reply
01-26-2010, 09:30 PM
Post: #4
RE: -> Matrix Inverse <-
Nice code. Smile
Find all posts by this user
Quote this message in a reply
01-26-2010, 10:44 PM
Post: #5
RE: -> Matrix Inverse <-
Thanks Bro Wink

Gholamreza Takhti
who was he? find out
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: