[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

Inversion of matrix size NxN by Pascal programming-code ( or by mathematical formulas )

Posted on 2009-12-26
Medium Priority
1,501 Views
I did search the Internet by Google and by Yahoo!
For now I found only explanations on how to invert matrices 2x2, 3x3 an 4x4 size, but based on these explanations, I can not make an algorithm for Pascal ....
1. I need a program code for Inverting matrices size NxN
or
2. I need some mathematical formulas for Inverting matrices size NxN, which I could make working programm-Pascal-code ( an algorithm ).
Thanks.
0
Question by:esoftbg
• 6
• 5
• 2

LVL 23

Expert Comment

ID: 26126190
Hi esoftgb
Here there's a .Net Matrix Library http://www.bluebit.gr/NET/ used to develop an online calculator that can do matrix inversion up to 30x30
http://www.bluebit.gr/matrix-calculator/
Hope it helps
0

LVL 3

Expert Comment

ID: 26126671
Hello, the most simple algorithm I usually employ when I need to invert matrix is applying Gauss Elimination (see http://en.wikipedia.org/wiki/Gaussian_elimination) to the following set of matrices:

( A[NxN] | E[NxN] ) --> ( E[NxN] | B[NxN] )

Here A - is your initial matrix, E is the matrix, which consists of 1 in diagonal, B = inverted A.

For example:

1  0  0
1  1  1
1  0  1

To invert this matrix write in a row this matrix with E matrix
A             E
1  0  0   |  1  0  0
1  1  1   |  0  1  0  subtracting third line multiplied by 1 from the second line
1  0  1   |  0  0  1    II = II - III*1

1  0  0   |  1  0  0
0  1  0   |  0  1 -1   subtracting first line multiplied by 1 from the third line:
1  0  1   |  0  0  1      III = III - I*1

E             B
1  0  0   |  1  0  0
0  1  0   |  0  1 -1
0  0  1   | -1  0  1

So, now B is inverted A. The possible operations are: multiplying and dividing lines by number, subtracting and adding lines. Swapping lines. The main rule, you should follow: all operations should be applied to both matrices. When first matrix becomes an E, the second becomes inverted A.
0

LVL 3

Expert Comment

ID: 26126688
significant note: when I told "lines" I meant rows, not columns!
0

LVL 12

Author Comment

ID: 26128612
Hi Ferruccio68,
I did install NML50_x64.exe, and NML50_x86.exe ....
After that I did try to run:
shortcut  .NET Matrix Libraries 5.0 (64-bit) Documentation
and shortcut .NET Matrix Libraries 5.0 Documentation
every time I did see:
"Microsoft Document Explorer cannot be shown because the specified help collection 'ms-help://BLUEBIT.NML50' is invalid."
0

LVL 12

Author Comment

ID: 26128627
Hello programmer78,
I found hundreds of explanations about inversing of matrices (N x N size) ....
I need working programm-pascal-code or mathematical formulas which I could use to develop programm-pascal-code ....
0

LVL 3

Expert Comment

ID: 26128636
Why don't you take algorithm implementation from the Wikipedia (the link I've posted)? The pseudo-code is very similar with Pascal. It shouldn't be a problem.
0

LVL 23

Expert Comment

ID: 26130305
@I did install NML50_x64.exe, and NML50_x86.exe ....
Why both? One is for 64 bits os and the other for 32 bits (x86). You should install just the right one for your OS
0

LVL 12

Author Comment

ID: 26130676
@Why both?
First I did install NML50_x64.exe under 64 bits Windows 7, but I did see message: "Microsoft Document Explorer cannot be shown because the specified help collection 'ms-help://BLUEBIT.NML50' is invalid."
Then I did decide to install NML50_x86.exe, because I know that the 32 bits applications should work on 64 bits Windows, but the result was the same ....
Now I will install NML50_x86.exe under 32 bits Windows XP on the Laptop ....
0

LVL 3

Expert Comment

ID: 26130749
esoftbg, the main problem is that you need the code with variable sized matrices. Anyway it will require to understand, how to represent these matrices since they hardly will be just a simple 2D arrays.

I think .NET libraries definitely won't help you since employing them from Pascal's program is additional problem (probably more complicated than inverting matrices). Porting them - is difficult as well, since C# and Pascal are very different.
0

LVL 12

Author Comment

ID: 26131308
Under 32 bits Windows XP, when I try to run the shortcut .NET Matrix Libraries 5.0 Documentation,
I see the message "dexplore.exe could not be found."
0

LVL 12

Author Comment

ID: 26309018
I found a pascal code capable of Invert matrix ( size NxN ) ....
So I don't need anymore this question.
0

LVL 3

Expert Comment

ID: 26311934
esoftbg, it would be pretty good, if you place this code here (some users in the future may face with the similar problems).
0

LVL 12

Accepted Solution

esoftbg earned 0 total points
ID: 26314730
The pascal code for inversion of square matrix ( N x N ):
[code]
type
Tmatrix = array[1..MaxN, 1..MaxN] of Double;

procedure matinv(a:Tmatrix; var ainv: Tmatrix; n:integer);
var
i,j,k:          integer;
e:             Tmatrix;
max:        Double;
begin
algebrerr:=false;
{ creating single matrix }
for i:=1 to n do
for j:=1 to n do
e[i,j]:=0.0;
for i:=1 to n do
e[i,i]:=1.0;
{ Conversion matrix to triangle }
for i:=1 to n do
{1} begin
max:=abs(a[i,i]);k:=i;
for j:=succ(i) to n do
if abs(a[j,i])>max then
{2}    begin
max:=abs(a[j,i]);k:=j
{2}    end;
if max<1E-10 then begin algebrerr:=true;exit end;
if k<>i then
{2}    begin
for  j:=i to n do
{3}      begin
max:=a[k,j];
a[k,j]:=a[i,j];
a[i,j]:=max;
{3}      end;
for j:=1 to n do
{3}    begin
max:=e[k,j];
e[k,j]:=e[i,j];
e[i,j]:=max;
{3}    end;
{2}   end;
for j:=succ(i) to n do
a[i,j]:=a[i,j]/a[i,i];
for k:=1 to n do
e[i,k]:=e[i,k]/a[i,i];
for j:=succ(i) to n do
{2}    begin
for k:=succ(i) to n do
a[j,k]:=a[j,k]-a[i,k]*a[j,i];
for k:=1 to n do
e[j,k]:=e[j,k]-e[i,k]*a[j,i];
{2}    end;
{1} end;
{ ainv calculation}
for k:=1 to n do
{1} begin
ainv[n,k]:=e[n,k];
for i:=pred(n) downto 1 do
{2}   begin
max:=e[i,k];
for j:=succ(i) to n do
max:=max-a[i,j]*ainv[j,k];
ainv[i,k]:=max;
{2}   end;
{1}  end;
end;
[/code]
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Guide to the PMT, FV, IPMT and PPMT Functions In MS Excel we have the PMT, FV, IPMT and PPMT functions, which do a fantastic job for interest rate calculations.  But what if you don't have Excel ? This article is for programmers looking to reâ€¦
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that pâ€¦
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201â€¦
Suggested Courses
Course of the Month19 days, 21 hours left to enroll