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
184
185
186
187
188
189
190
191
192
| #include <iostream>
#include<math.h>
using namespace std;
class matrix
{public:
matrix (int k);
matrix operator+(matrix M);
matrix operator-(matrix M);
matrix operator*(matrix M);
matrix operator*(float M);
bool operator==(matrix M);
bool proverka();
~matrix();
int INVERSE();
int SLAU();
void output_matrix();
void input_matrix();
private:
int n;
double **a;
double *b;
double *x;
double matrica_a;
double massiv_b;
double **y;
};
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
setlocale (LC_ALL, "Russian");
int c,N;
cout<<"N=";cin>>N;
matrix A(N), B(N), D(N),F(N);
A.input_matrix();
A.output_matrix();
B.input_matrix();
B.output_matrix();
D=B*A*3+(A-B)*A;
D.output_matrix();
if(D.proverka()) cout<<"Åäèíè÷íàÿ";
else cout<<"íå åäèíè÷íàÿ";
A.INVERSE();
A.output_matrix();
if(B==A)
cout<<"A ÿâëÿåòñÿ îáðàòíîé ìàòðèöå B";
else cout<<"A íå ÿâëÿåòñÿ îáðàòíîé ìàòðèöå B";
return 1;
}
matrix::matrix(int k)
{int i;
n=k;
a=new double*[n];
for(i=0;i<n;i++) a[i]=new double[n];
}
void matrix::input_matrix()
{int i,j;
cout<<"Ââåäèòå ìàòðèöó "<<endl;
for(i=0;i<n;i++)
for(j=0;j<n;j++) cin>>a[i][j];
}
void matrix::output_matrix()
{int i,j;
cout<<"Ìàòðèöà"<<endl;
for(i=0;i<n;cout<<endl,i++)
for(j=0;j<n;j++) cout<<a[i][j]<<"\t";
}
matrix matrix::operator+(matrix M)
{int i,j;
matrix c(n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
c.a[i][j]=a[i][j]+M.a[i][j];
return c;
}
matrix matrix::operator-(matrix M)
{int i,j;
matrix c(n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
c.a[i][j]=a[i][j]-M.a[i][j];
return c;
}
matrix matrix::operator *(matrix M)
{int i,j,k;
matrix c(n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=0,c.a[i][j]=0;k<n;k++)
c.a[i][j]+=a[i][k]*M.a[k][j];
return c;
}
matrix matrix::operator*(float M)
{int i,j;
matrix c(n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
c.a[i][j]=a[i][j]*M;
return c;
}
int matrix::SLAU()
{
int i,j,k,r;
double c,M,max,s;
double **a,*b;
a=new double *[n];
for(i=0;i<n;i++) a[i]=new double[n];
b=new double [n];
for(k=0;k<n;k++)
{
max=fabs(a[k][k]); r=k;
for(i=k+1;i<n;i++)
if (fabs(a[i][k])>max)
{
max=fabs(a[i][k]); r=i;
}
for(j=0;j<n;j++)
{
c=a[k][j]; a[k][j]=a[r][j]; a[r][j]=c;
}
c=b[k]; b[k]=b[r]; b[r]=c;
for(i=k+1;i<n;i++)
{
for(M=a[i][k]/a[k][k],j=k;j<n;j++)
a[i][j]-=M*a[k][j];
b[i]-=M*b[k];
}
}
if (a[n-1][n-1]==0)
if(b[n-1]==0)
return -1;
else return -2;
else
{
for(i=n-1;i>=0;i--)
{
for(s=0,j=i+1;j<n;j++) s+=a[i][j]*x[j];
x[i]=(b[i]-s)/a[i][i];
}
return 0;
}
for(i=0;i<n;i++)delete [] a[i];
delete [] a;
delete [] b;
}
int matrix::INVERSE()
{
int i,j,res;
double *b,*x;
b=new double [n];
x=new double [n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
if (j==i) b[j]=1;
else b[j]=0;
res=SLAU();
if (res!=0)
break;
else
for(j=0;j<n;j++) y[j][i]=x[j];
}
delete [] x;
delete [] b;
if (res!=0)
return -1;
else
return 0;
}
bool matrix::operator ==(matrix M)
{ int i, j; bool pr=true;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if (a[i][j]!=M.a[i][j]){ pr=false; break;}
}
}
return pr;
}
bool matrix::proverka()
{int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if (i=j&&a[i][j]!=1)return false;
else if(a[i][j]!=0) return false;
}
}
return true;
}
matrix::~matrix() { } |