1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
| unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls, Spin, CategoryButtons, ExtCtrls;
type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
Label2: TLabel;
Button1: TButton;
GroupBox2: TGroupBox;
GroupBox3: TGroupBox;
Label3: TLabel;
Edit1: TEdit;
Button4: TButton;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label4: TLabel;
Button3: TButton;
Timer1: TTimer;
StringGrid1: TStringGrid;
StringGrid2: TStringGrid;
StringGrid5: TStringGrid;
StringGrid3: TStringGrid;
StringGrid4: TStringGrid;
procedure Button4Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
a,b,a2,a3,a4,e,Aret: array [0..3,0..3] of real;
s1,s2,s3,s4,p1,p2,p3,p4:real;
I: Integer;
j: Integer;
///возведение исходной матрицы в квадрат
begin
for I := 0 to 3 do
for j := 0 to 3 do
begin
a[i,j]:=strtofloat(stringgrid1.Cells[i,j]);
b[i,j]:=strtofloat(stringgrid1.Cells[i,j]);
end;
for j := 0 to 3 do
begin
a2[0,j]:=a[0,j]*b[0,0]+a[1,j]*b[0,1]+a[2,j]*b[0,2]+a[3,j]*b[0,3];
stringgrid2.Cells[0,j]:=floattostr(a2[0,j]);
a2[1,j]:=a[0,j]*b[1,0]+a[1,j]*b[1,1]+a[2,j]*b[1,2]+a[3,j]*b[1,3];
stringgrid2.Cells[1,j]:=floattostr(a2[1,j]);
a2[2,j]:=a[0,j]*b[2,0]+a[1,j]*b[2,1]+a[2,j]*b[2,2]+a[3,j]*b[2,3];
stringgrid2.Cells[2,j]:=floattostr(a2[2,j]);
a2[3,j]:=a[0,j]*b[3,0]+a[1,j]*b[3,1]+a[2,j]*b[3,2]+a[3,j]*b[3,3];
stringgrid2.Cells[3,j]:=floattostr(a2[3,j]);
end;
///возведение исходной матрицы в куб
for j := 0 to 3 do
begin
a3[0,j]:=a2[0,j]*b[0,0]+a2[1,j]*b[0,1]+a2[2,j]*b[0,2]+a2[3,j]*b[0,3];
stringgrid3.Cells[0,j]:=floattostr(a3[0,j]);
a3[1,j]:=a2[0,j]*b[1,0]+a2[1,j]*b[1,1]+a2[2,j]*b[1,2]+a2[3,j]*b[1,3];
stringgrid3.Cells[1,j]:=floattostr(a3[1,j]);
a3[2,j]:=a2[0,j]*b[2,0]+a2[1,j]*b[2,1]+a2[2,j]*b[2,2]+a2[3,j]*b[2,3];
stringgrid3.Cells[2,j]:=floattostr(a3[2,j]);
a3[3,j]:=a2[0,j]*b[3,0]+a2[1,j]*b[3,1]+a2[2,j]*b[3,2]+a2[3,j]*b[3,3];
stringgrid3.Cells[3,j]:=floattostr(a3[3,j]);
end;
///возведение исходной матрицы в четвертую степень
for j := 0 to 3 do
begin
a4[0,j]:=a3[0,j]*b[0,0]+a3[1,j]*b[0,1]+a3[2,j]*b[0,2]+a3[3,j]*b[0,3];
stringgrid4.Cells[0,j]:=floattostr(a4[0,j]);
a4[1,j]:=a3[0,j]*b[1,0]+a3[1,j]*b[1,1]+a3[2,j]*b[1,2]+a3[3,j]*b[1,3];
stringgrid4.Cells[1,j]:=floattostr(a4[1,j]);
a4[2,j]:=a3[0,j]*b[2,0]+a3[1,j]*b[2,1]+a3[2,j]*b[2,2]+a3[3,j]*b[2,3];
stringgrid4.Cells[2,j]:=floattostr(a4[2,j]);
a4[3,j]:=a3[0,j]*b[3,0]+a3[1,j]*b[3,1]+a3[2,j]*b[3,2]+a3[3,j]*b[3,3];
stringgrid4.Cells[3,j]:=floattostr(a4[3,j]);
end;
///нахождение коэффициентов характеристического многочлена
s1:=a[0,0]+a[1,1]+a[2,2]+a[3,3];
s2:=a2[0,0]+a2[1,1]+a2[2,2]+a2[3,3];
s3:=a3[0,0]+a3[1,1]+a3[2,2]+a3[3,3];
s4:=a4[0,0]+a4[1,1]+a4[2,2]+a4[3,3];
p1:=-s1;
p2:=-1/2*(s2+p1*s1);
p3:=-1/3*(s3+p1*s2+p2*s1);
p4:=-1/4*(s4+p1*s3+p2*s2+p3*s1);
edit1.Text:='L^4 '+floattostr(p1)+'L^3 '+floattostr(p2)+'L^2 '+floattostr(p3)+'L '+floattostr(p4);
///нахождение обратной матрицы
if p4<>0 then
for I := 0 to 3 do
for j := 0 to 3 do
begin
e[i,j]:=0;
e[i,i]:=1;
Aret[i,j]:=-1/p4*(a3[i,j]+p1*a2[i,j]+p2*a[i,j]+p3*e[i,j]);
stringgrid5.Cells[i,j]:=floattostrf(Aret[i,j],ffFixed,10,4);
end
else
if p3<>0 then
for I := 0 to 3 do
for j := 0 to 3 do
begin
Aret[i,j]:=-1/p3*(a2[i,j]+p1*a[i,j]+p2*e[i,j]);
stringgrid5.Cells[i,j]:=floattostrf(Aret[i,j],ffFixed,10,4);
end
else
if p2<>0 then
for I := 0 to 3 do
for j := 0 to 3 do
begin
Aret[i,j]:=-1/p2*(a[i,j]+p1*e[i,j]);
stringgrid5.Cells[i,j]:=floattostrf(Aret[i,j],ffFixed,10,4);
end
else
if p1<>0 then
for I := 0 to 3 do
for j := 0 to 3 do
begin
Aret[i,j]:=-1/p1*(e[i,j]);
stringgrid5.Cells[i,j]:=floattostrf(Aret[i,j],ffFixed,10,4);
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
close;
end;
///процедура очистки
procedure TForm1.Button4Click(Sender: TObject);
var
z,j: Integer;
begin
edit1.Text:='';
for j := 0 to 3 do
for z := 0 to 3 do
begin
stringGrid1.Cells[j,z]:='';
stringGrid2.Cells[j,z]:='';
stringGrid3.Cells[j,z]:='';
stringGrid4.Cells[j,z]:='';
stringGrid5.Cells[j,z]:='';
end;
end;
///управление активностью кнопок
procedure TForm1.Timer1Timer(Sender: TObject);
var
I,j: Integer;
begin
for I := 0 to 3 do
for j := 0 to 3 do
if stringgrid1.Cells[i,j]='' then
begin
button1.Enabled:=false;
end
else
begin
button1.Enabled:=true;
end;
end;
end. |