unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
type
TMatrix = class
A,B,C: array of array of real;
c1,b1: array of real;
z: array of integer;
N : integer ;
i,j,p: integer;
s,w:real;
e1,y,d,buf:real;
StringGrid1: TStringGrid;
StringGrid2: TStringGrid;
StringGrid3: TStringGrid;
Edit1: TEdit;
private
public
constructor Create;
Destructor Destroy;
Procedure Install(p:integer);
Procedure Print(StringGrid1,StringGrid2: TStringGrid);
Procedure ochis(StringGrid1,StringGrid2,StringGrid3: TStringGrid);
Procedure perem(StringGrid3: TStringGrid);
Procedure Transp(StringGrid3: TStringGrid);
Procedure Obr;
end;
implementation
uses Unit1;
Procedure TMatrix.Install(p:integer);
var
i,j: integer;
begin
N:=p;
SetLength(A,N,N);
SetLength(B,N,N);
SetLength(C,N,N);
For i:=0 to (N-1) do
For j:=0 to (N-1) do
A[i,j]:=Random(10)-Random(10);
For i:=0 to (N-1) do
For j:=0 to (N-1) do
B[i,j]:=Random(10)-Random(10);
end;
Procedure TMatrix.Print(StringGrid1,StringGrid2: TStringGrid);
var
i,j,k: integer;
begin
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid2.Cells[j,i]:=FloatToStr(A[i,j]);
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid1.Cells[j,i]:=floatToStr(B[i,j]);
end;
Procedure TMatrix.ochis(StringGrid1,StringGrid2,StringGrid3: TStringGrid);
var
i,j: integer;
begin
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid2.Cells[j,i]:=(' ');
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid1.Cells[j,i]:=(' ');
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid3.Cells[j,i]:=(' ');
end;
Procedure TMatrix.perem(StringGrid3: TStringGrid);
var
i,j,k: integer;
begin
for i:=0 to N-1 do
for j:=0 to N-1 do
begin
C[i,j]:=0;
for k:=0 to N-1 do
C[i,j]:=C[i,j]+(A[i,k]*B[k,j]);
end;
begin
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid3.Cells[j,i]:=FloatToStr(C[i,j]);
end;
end;
Procedure TMatrix.Transp(StringGrid3: TStringGrid);
var
i,j: integer;
begin
begin
For i:=0 to N-1 do
For j:=i+1 to N-1 do
begin
buf:=a[i,j]; a[i,j]:=a[j,i]; a[j,i]:=buf; end;
for i:=0 to N-1 do begin
for j:=0 to N-1 do
StringGrid3.Cells[j,i]:=floattostr(a[i,j]);
end;
end;
end;
Procedure TMatrix.Obr;
var
i,j,k: integer;
{begin
e1:=0.00001;
d:=1;
s:=0;
i:=1;
z:=i;
i:=i+1;
if i<=n then
begin
z:=i;
i:=i+1;
end
else
i:=1;
k:=i;
y:=a[i,j];
if i+1<=n then
begin
j:=i+1;
w:=a[i,j];
if abs(w)>abs(y)then
begin
begin
k:=j;
y:=w;
end;
j:=j+1;
end
else
j:=j+1;
if j<=n then
begin
w:=a[i,j];
if abs(w)>abs(y)then
begin
begin
k:=j;
y:=w;
end;
j:=j+1;
end
else
j:=j+1;
end
else
d:=d*y;
end
else
d:=d*y;
if abs(y)<=e1 then
begin
s:=1;
i:=n+1;
i:=i+1;
end
else
y:=1/y;
j:=1;
c1[j]:=a[j,k];
a[j,k]:=a[j,i];
a[j,i]:=-c1[j]*y;
b1[j]:=a[i,j]*y;
a[i,j]:=b1[j];
j:=j+1;
if j<=n then
begin
c1[j]:=a[j,k];
a[j,k]:=a[j,i];
a[j,i]:=c1[j]*y;
b1[j]:=a[i,j]*y;
a[i,j]:=b1[j];
j:=j+1;
end
else
j:=z;
z:=z[k];
z[k]:=j;
a[i,i]:=y;
k:=1;
if k<>i then
begin
j:=1;
if j<>i then
begin
a[k,j]:=a[k,i]-b1[j]*c1[k];
j:=j+1;
if j<=n then
begin
if j<>i then
begin
a[k,j]:=a[k,i]-b1[j]*c1[k];
j:=j+1;
end
else
k:=k+1;
end
else
k:=k+1;
if k<=n then
begin
if k<>i then
begin
j:=1;
if j<>i then
begin
a[k,j]:=a[k,i]-b1[j]*c1[k];
j:=j+1;
if j<=n then
begin
if j<>i then
begin
a[k,j]:=a[k,i]-b1[j]*c1[k];
j:=j+1;
end
else
k:=k+1;
end
else
k:=k+1;
end
else
i:=i+1;
end;
if i<=n then
begin
k:=i;
y:=a[i,j];
if i+1<=n then
begin
j:=i+1;
w:=a[i,j];
if abs(w)>abs(y)then
begin
begin
k:=j;
y:=w;
end;
j:=j+1;
end
else
j:=j+1;
if j<=n then
begin
w:=a[i,j];
if abs(w)>abs(y)then
begin
begin
k:=j;
y:=w;
end;
j:=j+1;
end
else
j:=j+1;
end
else
d:=d*y;
end
else
d:=d*y;
if abs(y)<=e1 then
begin
s:=1;
i:=n+1;
i:=i+1;
end
else
y:=1/y;
j:=1;
c1[j]:=a[j,k];
a[j,k]:=a[j,i];
a[j,i]:=c1[j]*y;
b1[j]:=a[i,j]*y;
a[i,j]:=b1[j];
j:=j+1;
if j<=n then
begin
c1[j]:=a[j,k];
a[j,k]:=a[j,i];
a[j,i]:=c1[j]*y;
b1[j]:=a[i,j]*y;
a[i,j]:=b1[j];
j:=j+1;
end
else
j:=z;
z:=z[k];
z[k]:=j;
a[i,i]:=y;
k:=1;
if k<>i then
begin
j:=1;
if j<>i then
begin
a[k,j]:=a[k,i]-b1[j]*c1[k];
j:=j+1;
if j<=n then
begin
if j<>i then
begin
a[k,j]:=a[k,i]-b1[j]*c1[k];
j:=j+1;
end
else
k:=k+1;
end
else
k:=k+1;
if k<=n then
begin
if k<>i then
begin
j:=1;
if j<>i then
begin
a[k,j]:=a[k,i]-b1[j]*c1[k];
j:=j+1;
if j<=n then
begin
if j<>i then
begin
a[k,j]:=a[k,i]-b1[j]*c1[k];
j:=j+1;
end
else
k:=k+1;
end
else
k:=k+1;
end
else
i:=i+1;
end
else
if s=0 then
begin
i:=1;
k:=z;
if k<>i then
begin
j:=1;
w:=a[i,j];
a[i,j]:=a[k,j];
a[k,j]:=w;
j:=j+1;
if j<=n then
begin
w:=a[i,j];
a[i,j]:=a[k,j];
a[k,j]:=w;
j:=j+1;
end
else
p:=z;
z:=z[k];
d:=-d;
k:=z;
if j<=n then
begin
w:=a[i,j];
a[i,j]:=a[k,j];
a[k,j]:=w;
j:=j+1;
end
else
p:=z;
z:=z[k];
d:=-d;
k:=z;
end
else
i:=i+1;
if i<=n then
begin
k:=z;
if k<>i then
begin
j:=1;
w:=a[i,j];
a[i,j]:=a[k,j];
a[k,j]:=w;
j:=j+1;
if j<=n then
begin
w:=a[i,j];
a[i,j]:=a[k,j];
a[k,j]:=w;
j:=j+1;
end
else
p:=z;
z:=z[k];
d:=-d;
k:=z;
if j<=n then
begin
w:=a[i,j];
a[i,j]:=a[k,j];
a[k,j]:=w;
j:=j+1;
end
else
p:=z;
z:=z[k];
d:=-d;
k:=z;
end
else
i:=i+1;
end
else
end
end; }
end;
constructor TMatrix.Create;
begin
inherited Create;
end;
destructor TMatrix.Destroy;
begin
inherited Destroy;
end;
end.
Обратная матрица
Доброго всем времени суток! Прошу помочь с задачкой. Найти обратную матрицу. В моей программе так же имеется умножение матриц и транспонирование. Умножение правильное, а вот в транспонировании я не уверен. Вот мой код.
См. публичные сообщения на своей страничке, там всё гораздо проще, чем то, что ты приводишь в своем коде.
Есть вроде бы стандартная функция, которая все это делает.
Цитата: Walain
Есть вроде бы стандартная функция, которая все это делает.
Думаю, что в стандарте этого нет... Может быть в библиотеке boost.
//update
Сори, забыл о каком языке идёт речь :D
Возможно, я что-то напутал в коде. Не затруднит ли Вас указать на ошибки?
Код:
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
type
TMatrix = class
A,B,C,tr: array of array of real;
c1,b1: array of real;
z: array of integer;
N : integer ;
i,j,p,t: integer;
s,w:real;
e1,y,d,buf:real;
StringGrid1: TStringGrid;
StringGrid2: TStringGrid;
StringGrid3: TStringGrid;
Edit1: TEdit;
private
public
constructor Create;
Destructor Destroy;
Procedure Install(p:integer);
Procedure Print(StringGrid1,StringGrid2: TStringGrid);
Procedure ochis(StringGrid1,StringGrid2,StringGrid3: TStringGrid);
Procedure perem(StringGrid3: TStringGrid);
Procedure Transp(StringGrid3: TStringGrid);
Procedure Ed (StringGrid1: TStringGrid);
Procedure Obr(StringGrid3: TStringGrid);
Procedure Prov(StringGrid1,StringGrid2,StringGrid3: TStringGrid);
end;
implementation
uses Unit1;
Procedure TMatrix.Install(p:integer);
var
i,j: integer;
begin
N:=p;
SetLength(A,N,N);
SetLength(B,N,N);
SetLength(C,N,N);
For i:=0 to (N-1) do
For j:=0 to (N-1) do
A[i,j]:=Random(10)-Random(10);
For i:=0 to (N-1) do
For j:=0 to (N-1) do
B[i,j]:=Random(10)-Random(10);
end;
Procedure TMatrix.Print(StringGrid1,StringGrid2: TStringGrid);
var
i,j,k: integer;
begin
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid2.Cells[j,i]:=FloatToStr(A[i,j]);
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid1.Cells[j,i]:=floatToStr(B[i,j]);
end;
Procedure TMatrix.ochis(StringGrid1,StringGrid2,StringGrid3: TStringGrid);
var
i,j: integer;
begin
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid2.Cells[j,i]:=(' ');
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid1.Cells[j,i]:=(' ');
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid3.Cells[j,i]:=(' ');
end;
Procedure TMatrix.perem(StringGrid3: TStringGrid);
var
i,j,k: integer;
begin
for i:=0 to N-1 do
for j:=0 to N-1 do
begin
C[i,j]:=0;
for k:=0 to N-1 do
C[i,j]:=C[i,j]+(A[i,k]*B[k,j]);
end;
begin
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid3.Cells[j,i]:=FloatToStr(C[i,j]);
end;
end;
Procedure TMatrix.Transp(StringGrid3: TStringGrid);
var
i,j: integer;
begin
begin
For i:=0 to N-1 do
For j:=i+1 to N-1 do
begin
buf:=a[i,j]; a[i,j]:=a[j,i]; a[j,i]:=buf; end;
for i:=0 to N-1 do begin
for j:=0 to N-1 do
StringGrid3.Cells[j,i]:=floattostr(a[i,j]);
end;
end;
end;
Procedure TMatrix.Ed(StringGrid1: TStringGrid);
var
i,j: integer;
begin
SetLength(c, n, n);
for i:=0 to n-1 do
for j:=0 to n-1 do
if i=j then
c[i, j]:=1
else c[i, j]:=0;
for i:=0 to N-1 do begin
for j:=0 to N-1 do
StringGrid1.Cells[j,i]:=floattostr(c[i,j]);
end;
end;
Procedure TMatrix.Obr(StringGrid3: TStringGrid);
var
i,j,k,t: integer;
begin
SetLength(c, n, n);
for i:=0 to n-1 do
for j:=0 to n-1 do
if i=j then
c[i, j]:=1
else c[i, j]:=0;
for t:=0 to n-1 do
for i:=t+1 to n-1 do begin
w:=a[i, t];
for j:=t to n-1 do
a[i, j]:=a[i,j]-w*(a[t, j]/a[t, t]);
for j:=0 to n-1 do
c[i, j]:=c[i, j]-w*(c[t,j]/a[t, t]);
end;
for t:=n-1 downto 1 do
for i:=t downto 1 do begin
w:=a[i-1,t];
for j:=t downto 0 do
a[i-1,j]:=a[i-1,j]-w*(a[t,j]/a[t,t]);
for j:=0 to n-1 do
c[i-1,j]:=c[i-1,j]-w*(c[t,j]/a[t,t]);
end;
for i:=0 to N-1 do begin
for j:=0 to N-1 do
StringGrid3.Cells[j,i]:=floattostr(c[i,j]);
end;
end;
Procedure TMatrix.Prov(StringGrid1,StringGrid2,StringGrid3: TStringGrid);
var
i,j,k: integer;
begin
For i:=0 to (N-1) do
For j:=0 to (N-1) do
A[i,j]:=strtoint(StringGrid2.Cells[j,i]);
{For i:=0 to (N-1) do
For j:=0 to (N-1) do
c[i,j]:=strtoint(StringGrid3.Cells[j,i]);}
for i:=0 to N-1 do
for k:=0 to N-1 do
begin
b[i,k]:=0;
for j:=0 to N-1 do
b[i,k]:=b[i,k]+(A[i,j]*c[j,k]);
end;
begin
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid1.Cells[j,i]:=FloatToStr(b[i,j]);
end;
end;
constructor TMatrix.Create;
begin
inherited Create;
end;
destructor TMatrix.Destroy;
begin
inherited Destroy;
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
type
TMatrix = class
A,B,C,tr: array of array of real;
c1,b1: array of real;
z: array of integer;
N : integer ;
i,j,p,t: integer;
s,w:real;
e1,y,d,buf:real;
StringGrid1: TStringGrid;
StringGrid2: TStringGrid;
StringGrid3: TStringGrid;
Edit1: TEdit;
private
public
constructor Create;
Destructor Destroy;
Procedure Install(p:integer);
Procedure Print(StringGrid1,StringGrid2: TStringGrid);
Procedure ochis(StringGrid1,StringGrid2,StringGrid3: TStringGrid);
Procedure perem(StringGrid3: TStringGrid);
Procedure Transp(StringGrid3: TStringGrid);
Procedure Ed (StringGrid1: TStringGrid);
Procedure Obr(StringGrid3: TStringGrid);
Procedure Prov(StringGrid1,StringGrid2,StringGrid3: TStringGrid);
end;
implementation
uses Unit1;
Procedure TMatrix.Install(p:integer);
var
i,j: integer;
begin
N:=p;
SetLength(A,N,N);
SetLength(B,N,N);
SetLength(C,N,N);
For i:=0 to (N-1) do
For j:=0 to (N-1) do
A[i,j]:=Random(10)-Random(10);
For i:=0 to (N-1) do
For j:=0 to (N-1) do
B[i,j]:=Random(10)-Random(10);
end;
Procedure TMatrix.Print(StringGrid1,StringGrid2: TStringGrid);
var
i,j,k: integer;
begin
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid2.Cells[j,i]:=FloatToStr(A[i,j]);
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid1.Cells[j,i]:=floatToStr(B[i,j]);
end;
Procedure TMatrix.ochis(StringGrid1,StringGrid2,StringGrid3: TStringGrid);
var
i,j: integer;
begin
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid2.Cells[j,i]:=(' ');
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid1.Cells[j,i]:=(' ');
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid3.Cells[j,i]:=(' ');
end;
Procedure TMatrix.perem(StringGrid3: TStringGrid);
var
i,j,k: integer;
begin
for i:=0 to N-1 do
for j:=0 to N-1 do
begin
C[i,j]:=0;
for k:=0 to N-1 do
C[i,j]:=C[i,j]+(A[i,k]*B[k,j]);
end;
begin
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid3.Cells[j,i]:=FloatToStr(C[i,j]);
end;
end;
Procedure TMatrix.Transp(StringGrid3: TStringGrid);
var
i,j: integer;
begin
begin
For i:=0 to N-1 do
For j:=i+1 to N-1 do
begin
buf:=a[i,j]; a[i,j]:=a[j,i]; a[j,i]:=buf; end;
for i:=0 to N-1 do begin
for j:=0 to N-1 do
StringGrid3.Cells[j,i]:=floattostr(a[i,j]);
end;
end;
end;
Procedure TMatrix.Ed(StringGrid1: TStringGrid);
var
i,j: integer;
begin
SetLength(c, n, n);
for i:=0 to n-1 do
for j:=0 to n-1 do
if i=j then
c[i, j]:=1
else c[i, j]:=0;
for i:=0 to N-1 do begin
for j:=0 to N-1 do
StringGrid1.Cells[j,i]:=floattostr(c[i,j]);
end;
end;
Procedure TMatrix.Obr(StringGrid3: TStringGrid);
var
i,j,k,t: integer;
begin
SetLength(c, n, n);
for i:=0 to n-1 do
for j:=0 to n-1 do
if i=j then
c[i, j]:=1
else c[i, j]:=0;
for t:=0 to n-1 do
for i:=t+1 to n-1 do begin
w:=a[i, t];
for j:=t to n-1 do
a[i, j]:=a[i,j]-w*(a[t, j]/a[t, t]);
for j:=0 to n-1 do
c[i, j]:=c[i, j]-w*(c[t,j]/a[t, t]);
end;
for t:=n-1 downto 1 do
for i:=t downto 1 do begin
w:=a[i-1,t];
for j:=t downto 0 do
a[i-1,j]:=a[i-1,j]-w*(a[t,j]/a[t,t]);
for j:=0 to n-1 do
c[i-1,j]:=c[i-1,j]-w*(c[t,j]/a[t,t]);
end;
for i:=0 to N-1 do begin
for j:=0 to N-1 do
StringGrid3.Cells[j,i]:=floattostr(c[i,j]);
end;
end;
Procedure TMatrix.Prov(StringGrid1,StringGrid2,StringGrid3: TStringGrid);
var
i,j,k: integer;
begin
For i:=0 to (N-1) do
For j:=0 to (N-1) do
A[i,j]:=strtoint(StringGrid2.Cells[j,i]);
{For i:=0 to (N-1) do
For j:=0 to (N-1) do
c[i,j]:=strtoint(StringGrid3.Cells[j,i]);}
for i:=0 to N-1 do
for k:=0 to N-1 do
begin
b[i,k]:=0;
for j:=0 to N-1 do
b[i,k]:=b[i,k]+(A[i,j]*c[j,k]);
end;
begin
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid1.Cells[j,i]:=FloatToStr(b[i,j]);
end;
end;
constructor TMatrix.Create;
begin
inherited Create;
end;
destructor TMatrix.Destroy;
begin
inherited Destroy;
end;
end.
Перемножение матриц у Вас в коде вроде бы сделано правильно, ошибок не обнаружил. Вот здесь описано как вычислять обратную матрицу в excel. Если перемножение не даст нужного результата, то выкладывайте проект целиком, т.к. только трассировкой можно быстро исправить ошибки.
Разобрался таки с кодом.
Вот, может кому понадобится.
Код:
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
type
matr=array of array of real;
TMatrix = class
A,B,C,tr: matr;
N : integer ;
i,j,p,t: integer;
s,w:real;
e1,y,d,buf,m,r,sum,max,inv,inv1:real;
result:boolean;
StringGrid1: TStringGrid;
StringGrid2: TStringGrid;
StringGrid3: TStringGrid;
Edit1: TEdit;
private
public
constructor Create;
Destructor Destroy;
Procedure Install(p:integer);
Procedure Print(StringGrid1,StringGrid2: TStringGrid);
Procedure ochis(StringGrid1,StringGrid2,StringGrid3: TStringGrid);
Procedure perem(StringGrid3: TStringGrid);
Procedure Transp(StringGrid3: TStringGrid);
Procedure Ed (StringGrid1: TStringGrid);
Procedure Obr(StringGrid3: TStringGrid);
Procedure Prov(StringGrid1,StringGrid2,StringGrid3: TStringGrid);
end;
implementation
uses Unit1;
Procedure TMatrix.Install(p:integer);
var
i,j: integer;
begin
N:=p;
SetLength(A,N,N);
SetLength(B,N,N);
SetLength(C,N,N);
For i:=0 to (N-1) do
For j:=0 to (N-1) do
A[i,j]:=Random(10)-Random(10);
For i:=0 to (N-1) do
For j:=0 to (N-1) do
B[i,j]:=Random(10)-Random(10);
end;
Procedure TMatrix.Print(StringGrid1,StringGrid2: TStringGrid);
var
i,j,k: integer;
begin
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid2.Cells[j,i]:=FloatToStr(A[i,j]);
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid1.Cells[j,i]:=floatToStr(B[i,j]);
end;
Procedure TMatrix.ochis(StringGrid1,StringGrid2,StringGrid3: TStringGrid);
var
i,j: integer;
begin
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid2.Cells[j,i]:=(' ');
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid1.Cells[j,i]:=(' ');
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid3.Cells[j,i]:=(' ');
end;
Procedure TMatrix.perem(StringGrid3: TStringGrid);
var
i,j,k: integer;
begin
for i:=0 to N-1 do
for j:=0 to N-1 do
begin
C[i,j]:=0;
for k:=0 to N-1 do
C[i,j]:=C[i,j]+(A[i,k]*B[k,j]);
end;
begin
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid3.Cells[j,i]:=FloatToStr(C[i,j]);
end;
end;
Procedure TMatrix.Transp(StringGrid3: TStringGrid);
var
i,j: integer;
begin
begin
For i:=0 to N-1 do
For j:=i+1 to N-1 do
begin
buf:=a[i,j]; a[i,j]:=a[j,i]; a[j,i]:=buf; end;
for i:=0 to N-1 do begin
for j:=0 to N-1 do
StringGrid3.Cells[j,i]:=floattostr(a[i,j]);
end;
end;
end;
Procedure TMatrix.Ed(StringGrid1: TStringGrid);
var
i,j: integer;
begin
SetLength(c, n, n);
for i:=0 to n-1 do
for j:=0 to n-1 do
if i=j then
c[i, j]:=1
else c[i, j]:=0;
for i:=0 to N-1 do begin
for j:=0 to N-1 do
StringGrid1.Cells[j,i]:=floattostr(c[i,j]);
end;
end;
Procedure TMatrix.Obr(StringGrid3: TStringGrid);
var
i,j,k,t: integer;
A_e,E:matr;
xe:array of Real;
function GaussSolveM(var A:matr; N:Integer; var X:array of Real):Boolean;
var
m,r,sum,max,inv,inv1:real;
i,j,imax,k:integer;
begin
result:=true;
For k:=0 to N-1 do begin
max:=0;
for i:=k to N do
if max<abs(A[i,k]) then
begin
max:=A[i,k];
imax:=i;
end;
if abs(A[imax,k])>0.000001 then begin
for j:=0 to N do begin
inv:=A[k,j];
A[k,j]:=A[imax,j];
A[imax,j]:=inv;
end;
inv1:=A[k,N+1];
A[k,N+1]:=A[imax,N+1];
A[imax,N+1]:=inv1;
end;
for i:=k+1 to N do begin
A[i,k]:=A[i,k]/A[k,k];
m:=A[i,k];
for j:=k+1 to N do
A[i,j]:=A[i,j]- m*A[k,j];
A[i,N+1]:=A[i,N+1]-m*A[k,N+1];
end;
end;
for i:=N downto 0 do
begin
sum:=0;
if i<N then
for j:=i+1 to N do
sum:=sum+A[i,j]*x[j];
r:=A[i,N+1]-sum;
If Abs(A[i,i])> 0.0000001 then
x:=r/A[i,i]
else
if abs(r)<=0.0000001 then
x:=0
else
result:=false;
end;
end;{gauss end}
begin {Obr main}
setlength(E,N,N);
setlength(xe,N);
setlength(A_e,N,N+1);
for i:=0 to N-1 do
for j:=0 to N-1 do
if i=j then E[i,j]:=1
else E[i,j]:=0;
for k:=0 to N-1 do begin
for i:=0 to N-1 do
for j:=0 to N-1 do
A_e[i,j]:=a[i,j];
for i:=0 to N-1 do
A_e[i,N]:=E[i,k];
if GaussSolveM(A_e,N-1,xe) then begin
for i:=0 to N-1 do
C[i,k]:=xe;
end
else begin
application.MessageBox('Корней нет!','Инфо!');
exit;
end;
end;
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid3.Cells[j,i]:=FloatToStr(c[i,j]);
end; {end obr}
Procedure TMatrix.Prov(StringGrid1,StringGrid2,StringGrid3: TStringGrid);
var
i,j,k: integer;
s:real;
begin
{For i:=0 to (N-1) do
For j:=0 to (N-1) do
A[i,j]:=strtoint(StringGrid2.Cells[j,i]);}
for i:=0 to N-1 do
for j:=0 to N-1 do
begin
S:=0;
for k:=0 to N-1 do
s:=s+(A[i,k]*c[k,j]);
b[i,j]:=s;
end;
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid1.Cells[j,i]:=FloatToStrF(b[i,j],ffExponent,2,2 );
end;
constructor TMatrix.Create;
begin
inherited Create;
end;
destructor TMatrix.Destroy;
begin
inherited Destroy;
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
type
matr=array of array of real;
TMatrix = class
A,B,C,tr: matr;
N : integer ;
i,j,p,t: integer;
s,w:real;
e1,y,d,buf,m,r,sum,max,inv,inv1:real;
result:boolean;
StringGrid1: TStringGrid;
StringGrid2: TStringGrid;
StringGrid3: TStringGrid;
Edit1: TEdit;
private
public
constructor Create;
Destructor Destroy;
Procedure Install(p:integer);
Procedure Print(StringGrid1,StringGrid2: TStringGrid);
Procedure ochis(StringGrid1,StringGrid2,StringGrid3: TStringGrid);
Procedure perem(StringGrid3: TStringGrid);
Procedure Transp(StringGrid3: TStringGrid);
Procedure Ed (StringGrid1: TStringGrid);
Procedure Obr(StringGrid3: TStringGrid);
Procedure Prov(StringGrid1,StringGrid2,StringGrid3: TStringGrid);
end;
implementation
uses Unit1;
Procedure TMatrix.Install(p:integer);
var
i,j: integer;
begin
N:=p;
SetLength(A,N,N);
SetLength(B,N,N);
SetLength(C,N,N);
For i:=0 to (N-1) do
For j:=0 to (N-1) do
A[i,j]:=Random(10)-Random(10);
For i:=0 to (N-1) do
For j:=0 to (N-1) do
B[i,j]:=Random(10)-Random(10);
end;
Procedure TMatrix.Print(StringGrid1,StringGrid2: TStringGrid);
var
i,j,k: integer;
begin
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid2.Cells[j,i]:=FloatToStr(A[i,j]);
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid1.Cells[j,i]:=floatToStr(B[i,j]);
end;
Procedure TMatrix.ochis(StringGrid1,StringGrid2,StringGrid3: TStringGrid);
var
i,j: integer;
begin
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid2.Cells[j,i]:=(' ');
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid1.Cells[j,i]:=(' ');
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid3.Cells[j,i]:=(' ');
end;
Procedure TMatrix.perem(StringGrid3: TStringGrid);
var
i,j,k: integer;
begin
for i:=0 to N-1 do
for j:=0 to N-1 do
begin
C[i,j]:=0;
for k:=0 to N-1 do
C[i,j]:=C[i,j]+(A[i,k]*B[k,j]);
end;
begin
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid3.Cells[j,i]:=FloatToStr(C[i,j]);
end;
end;
Procedure TMatrix.Transp(StringGrid3: TStringGrid);
var
i,j: integer;
begin
begin
For i:=0 to N-1 do
For j:=i+1 to N-1 do
begin
buf:=a[i,j]; a[i,j]:=a[j,i]; a[j,i]:=buf; end;
for i:=0 to N-1 do begin
for j:=0 to N-1 do
StringGrid3.Cells[j,i]:=floattostr(a[i,j]);
end;
end;
end;
Procedure TMatrix.Ed(StringGrid1: TStringGrid);
var
i,j: integer;
begin
SetLength(c, n, n);
for i:=0 to n-1 do
for j:=0 to n-1 do
if i=j then
c[i, j]:=1
else c[i, j]:=0;
for i:=0 to N-1 do begin
for j:=0 to N-1 do
StringGrid1.Cells[j,i]:=floattostr(c[i,j]);
end;
end;
Procedure TMatrix.Obr(StringGrid3: TStringGrid);
var
i,j,k,t: integer;
A_e,E:matr;
xe:array of Real;
function GaussSolveM(var A:matr; N:Integer; var X:array of Real):Boolean;
var
m,r,sum,max,inv,inv1:real;
i,j,imax,k:integer;
begin
result:=true;
For k:=0 to N-1 do begin
max:=0;
for i:=k to N do
if max<abs(A[i,k]) then
begin
max:=A[i,k];
imax:=i;
end;
if abs(A[imax,k])>0.000001 then begin
for j:=0 to N do begin
inv:=A[k,j];
A[k,j]:=A[imax,j];
A[imax,j]:=inv;
end;
inv1:=A[k,N+1];
A[k,N+1]:=A[imax,N+1];
A[imax,N+1]:=inv1;
end;
for i:=k+1 to N do begin
A[i,k]:=A[i,k]/A[k,k];
m:=A[i,k];
for j:=k+1 to N do
A[i,j]:=A[i,j]- m*A[k,j];
A[i,N+1]:=A[i,N+1]-m*A[k,N+1];
end;
end;
for i:=N downto 0 do
begin
sum:=0;
if i<N then
for j:=i+1 to N do
sum:=sum+A[i,j]*x[j];
r:=A[i,N+1]-sum;
If Abs(A[i,i])> 0.0000001 then
x:=r/A[i,i]
else
if abs(r)<=0.0000001 then
x:=0
else
result:=false;
end;
end;{gauss end}
begin {Obr main}
setlength(E,N,N);
setlength(xe,N);
setlength(A_e,N,N+1);
for i:=0 to N-1 do
for j:=0 to N-1 do
if i=j then E[i,j]:=1
else E[i,j]:=0;
for k:=0 to N-1 do begin
for i:=0 to N-1 do
for j:=0 to N-1 do
A_e[i,j]:=a[i,j];
for i:=0 to N-1 do
A_e[i,N]:=E[i,k];
if GaussSolveM(A_e,N-1,xe) then begin
for i:=0 to N-1 do
C[i,k]:=xe;
end
else begin
application.MessageBox('Корней нет!','Инфо!');
exit;
end;
end;
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid3.Cells[j,i]:=FloatToStr(c[i,j]);
end; {end obr}
Procedure TMatrix.Prov(StringGrid1,StringGrid2,StringGrid3: TStringGrid);
var
i,j,k: integer;
s:real;
begin
{For i:=0 to (N-1) do
For j:=0 to (N-1) do
A[i,j]:=strtoint(StringGrid2.Cells[j,i]);}
for i:=0 to N-1 do
for j:=0 to N-1 do
begin
S:=0;
for k:=0 to N-1 do
s:=s+(A[i,k]*c[k,j]);
b[i,j]:=s;
end;
For i:=0 to (N-1) do
For j:=0 to (N-1) do
StringGrid1.Cells[j,i]:=FloatToStrF(b[i,j],ffExponent,2,2 );
end;
constructor TMatrix.Create;
begin
inherited Create;
end;
destructor TMatrix.Destroy;
begin
inherited Destroy;
end;
end.