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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
| #include "stdafx.h"
#include "CMatrix.h"
CMatrix::CMatrix()
{
n_rows = 1;
n_cols = 1;
array = new double*[n_rows];
for (int i = 0; i<n_rows; i++) array[i] = new double[n_cols];
for (int i = 0; i<n_rows; i++)
for (int j = 0; j<n_cols; j++) array[i][j] = 0;
}
//-------------------------------------------------------------------------------
CMatrix::CMatrix(int Nrow, int Ncol)
// Nrow - число строк
// Ncol - число столбцов
{
n_rows = Nrow;
n_cols = Ncol;
array = new double*[n_rows];
for (int i = 0; i < n_rows; i++) array[i] = new double[n_cols];
for (int i = 0; i < n_rows; i++)
for (int j = 0; j < n_cols; j++) array[i][j] = 0;
}
//---------------------------------------------------------------------------------
CMatrix::CMatrix(int Nrow) //Вектор
// Nrow - число строк
{
n_rows = Nrow;
n_cols = 1;
array = new double*[n_rows];
for (int i = 0; i<n_rows; i++) array[i] = new double[n_cols];
for (int i = 0; i<n_rows; i++)
for (int j = 0; j<n_cols; j++) array[i][j] = 0;
}
//---------------------------------------------------------------------------------
CMatrix::~CMatrix()
{
for (int i = 0; i<n_rows; i++) delete array[i];
delete array;
}
//---------------------------------------------------------------------------------
double &CMatrix::operator()(int i, int j)
// i - номер строки
// j - номер столбца
{
if ((i>n_rows - 1) || (j>n_cols - 1)) // проверка выхода за диапазон
{
TCHAR* error = _T("CMatrix::operator(int,int): выход индекса за границу диапазона ");
MessageBox(NULL, error, _T("Ошибка"), MB_ICONSTOP);
exit(1);
}
return array[i][j];
}
//---------------------------------------------------------------------------------
double &CMatrix::operator()(int i)
// i - номер строки для вектора
{
if (n_cols>1) // Число столбцов больше одного
{
TCHAR* error = TEXT("CMatrix::operator(int): объект не вектор - число столбцов больше 1 ");
MessageBox(NULL, error, TEXT("Ошибка"), MB_ICONSTOP);
exit(1);
}
if (i>n_rows - 1) // проверка выхода за диапазон
{
TCHAR* error = TEXT("CMatrix::operator(int): выход индекса за границу диапазона ");
MessageBox(NULL, error, TEXT("Ошибка"), MB_ICONSTOP);
exit(1);
}
return array[i][0];
}
//---------------------------------------------------------------------------------
CMatrix CMatrix::operator-()
// Оператор -M
{
CMatrix Temp(n_rows, n_cols);
for (int i = 0; i<n_rows; i++)
for (int j = 0; j<n_cols; j++) Temp(i, j) = -array[i][j];
return Temp;
}
//---------------------------------------------------------------------------------
CMatrix CMatrix::operator+(CMatrix& M)
// Оператор M1+M2
{
int bb = (n_rows == M.rows()) && (n_cols == M.cols());
if (!bb)
{
TCHAR* error = TEXT("CMatrix::operator(+): несоответствие размерностей матриц ");
MessageBox(NULL, error, TEXT("Ошибка"), MB_ICONSTOP);
exit(1);
}
CMatrix Temp(*this);
for (int i = 0; i<n_rows; i++)
for (int j = 0; j<n_cols; j++) Temp(i, j) += M(i, j);
return Temp;
}
//---------------------------------------------------------------------------------
CMatrix CMatrix::operator-(CMatrix& M)
// Оператор M1-M2
{
int bb = (n_rows == M.rows()) && (n_cols == M.cols());
if (!bb)
{
TCHAR* error = TEXT("CMatrix::operator(-): несоответствие размерностей матриц ");
MessageBox(NULL, error, TEXT("Ошибка"), MB_ICONSTOP);
exit(1);
}
CMatrix Temp(*this);
for (int i = 0; i<n_rows; i++)
for (int j = 0; j<n_cols; j++) Temp(i, j) -= M(i, j);
return Temp;
}
//---------------------------------------------------------------------------------
CMatrix CMatrix::operator*(CMatrix& M)
// Умножение на матрицу M
{
double sum;
int nn = M.rows();
int mm = M.cols();
CMatrix Temp(n_rows, mm);
if (n_cols == nn)
{
for (int i = 0; i<n_rows; i++)
for (int j = 0; j<mm; j++)
{
sum = 0;
for (int k = 0; k<n_cols; k++) sum += (*this)(i, k)*M(k, j);
Temp(i, j) = sum;
}
}
else
{
TCHAR* error = TEXT("CMatrix::operator*: несоответствие размерностей матриц ");
MessageBox(NULL, error, TEXT("Ошибка"), MB_ICONSTOP);
exit(1);
}
return Temp;
}
//---------------------------------------------------------------------------------
CMatrix CMatrix::operator=(const CMatrix& M)
// Оператор присваивания M1=M
{
if (this == &M) return *this;
int nn = M.rows();
int mm = M.cols();
if ((n_rows == nn) && (n_cols == mm))
{
for (int i = 0; i<n_rows; i++)
for (int j = 0; j<n_cols; j++) array[i][j] = M.array[i][j];
}
else // для ошибки размерностей
{
TCHAR* error = TEXT("CMatrix::operator=: несоответствие размерностей матриц");
MessageBox(NULL, error, TEXT("Ошибка"), MB_ICONSTOP);
exit(1);
}
return *this;
}
//---------------------------------------------------------------------------------
CMatrix::CMatrix(const CMatrix &M) // Конструктор копирования
{
n_rows = M.n_rows;
n_cols = M.n_cols;
array = new double*[n_rows];
for (int i = 0; i<n_rows; i++) array[i] = new double[n_cols];
for (int i = 0; i<n_rows; i++)
for (int j = 0; j<n_cols; j++) array[i][j] = M.array[i][j];
}
//---------------------------------------------------------------------------------
CMatrix CMatrix::operator+(double x)
// Оператор M+x, где M - матрица, x - число
{
CMatrix Temp(*this);
for (int i = 0; i<n_rows; i++)
for (int j = 0; j<n_cols; j++) Temp(i, j) += x;
return Temp;
}
//---------------------------------------------------------------------------------
CMatrix CMatrix::operator-(double x)
// Оператор M+x, где M - матрица, x - число
{
CMatrix Temp(*this);
for (int i = 0; i<n_rows; i++)
for (int j = 0; j<n_cols; j++) Temp(i, j) -= x;
return Temp;
}
//---------------------------------------------------------------------------------
CMatrix CMatrix::Transp()
// Возвращает матрицу,транспонированную к (*this)
{
CMatrix Temp(n_cols, n_rows);
for (int i = 0; i<n_cols; i++)
for (int j = 0; j<n_rows; j++) Temp(i, j) = array[j][i];
return Temp;
}
//---------------------------------------------------------------------------------
CMatrix CMatrix::GetRow(int k)
// Возвращает строку матрицы по номеру k
{
if (k>n_rows - 1)
{
TCHAR* error = TEXT("CMatrix::GetRow(int k): параметр k превышает число строк ");
MessageBox(NULL, error, TEXT("Ошибка"), MB_ICONSTOP);
exit(1);
}
CMatrix M(1, n_cols);
for (int i = 0; i<n_cols; i++)M(0, i) = (*this)(k, i);
return M;
}
//---------------------------------------------------------------------------------
CMatrix CMatrix::GetRow(int k, int n, int m)
// Возвращает подстроку из строки матрицы с номером k
// n - номер первого элемента в строке
// m - номер последнего элемента в строке
{
int b1 = (k >= 0) && (k<n_rows);
int b2 = (n >= 0) && (n <= m);
int b3 = (m >= 0) && (m<n_cols);
int b4 = b1 && b2&&b3;
if (!b4)
{
TCHAR* error = TEXT("CMatrix::GetRow(int k,int n, int m):ошибка в параметрах ");
MessageBox(NULL, error, TEXT("Ошибка"), MB_ICONSTOP);
exit(1);
}
int nCols = m - n + 1;
CMatrix M(1, nCols);
for (int i = n; i <= m; i++)M(0, i - n) = (*this)(k, i);
return M;
}
//---------------------------------------------------------------------------------
CMatrix CMatrix::GetCol(int k)
// Возвращает столбец матрицы по номеру k
{
if (k>n_cols - 1)
{
TCHAR* error = TEXT("CMatrix::GetCol(int k): параметр k превышает число столбцов ");
MessageBox(NULL, error, TEXT("Ошибка"), MB_ICONSTOP);
exit(1);
}
CMatrix M(n_rows, 1);
for (int i = 0; i<n_rows; i++)M(i, 0) = (*this)(i, k);
return M;
}
//---------------------------------------------------------------------------------
CMatrix CMatrix::GetCol(int k, int n, int m)
// Возвращает подстолбец из столбца матрицы с номером k
// n - номер первого элемента в столбце
// m - номер последнего элемента в столбце
{
int b1 = (k >= 0) && (k<n_cols);
int b2 = (n >= 0) && (n <= m);
int b3 = (m >= 0) && (m<n_rows);
int b4 = b1 && b2&&b3;
if (!b4)
{
TCHAR* error = TEXT("CMatrix::GetCol(int k,int n, int m):ошибка в параметрах ");
MessageBox(NULL, error, TEXT("Ошибка"), MB_ICONSTOP);
exit(1);
}
int nRows = m - n + 1;
CMatrix M(nRows, 1);
for (int i = n; i <= m; i++)M(i - n, 0) = (*this)(i, k);
return M;
}
//---------------------------------------------------------------------------------
CMatrix CMatrix::RedimMatrix(int NewRow, int NewCol)
// Изменяет размер матрицы с уничтожением данных
// NewRow - новое число строк
// NewCol - новое число столбцов
{
for (int i = 0; i<n_rows; i++) delete array[i];
delete array;
n_rows = NewRow;
n_cols = NewCol;
array = new double*[n_rows];
for (int i = 0; i<n_rows; i++) array[i] = new double[n_cols];
for (int i = 0; i<n_rows; i++)
for (int j = 0; j<n_cols; j++) array[i][j] = 0;
return (*this);
}
//---------------------------------------------------------------------------------
CMatrix CMatrix::RedimData(int NewRow, int NewCol)
// Изменяет размер матрицы с сохранением данных, которые можно сохранить
// NewRow - новое число строк
// NewCol - новое число столбцов
{
CMatrix Temp = (*this);
this->RedimMatrix(NewRow, NewCol);
int min_rows = Temp.rows()<(*this).rows() ? Temp.rows() : (*this).rows();
int min_cols = Temp.cols()<(*this).cols() ? Temp.cols() : (*this).cols();
for (int i = 0; i<min_rows; i++)
for (int j = 0; j<min_cols; j++) (*this)(i, j) = Temp(i, j);
return (*this);
}
//---------------------------------------------------------------------------------
CMatrix CMatrix::RedimMatrix(int NewRow)
// Изменяет размер матрицы с уничтожением данных
// NewRow - новое число строк
// NewCol=1
{
for (int i = 0; i<n_rows; i++) delete array[i];
delete array;
n_rows = NewRow;
n_cols = 1;
array = new double*[n_rows];
for (int i = 0; i<n_rows; i++) array[i] = new double[n_cols];
for (int i = 0; i<n_rows; i++)
for (int j = 0; j<n_cols; j++) array[i][j] = 0;
return (*this);
}
//---------------------------------------------------------------------------------
CMatrix CMatrix::RedimData(int NewRow)
// Изменяет размер матрицы с сохранением данных, которые можно сохранить
// NewRow - новое число строк
// NewCol=1
{
CMatrix Temp = (*this);
this->RedimMatrix(NewRow);
int min_rows = Temp.rows()<(*this).rows() ? Temp.rows() : (*this).rows();
for (int i = 0; i<min_rows; i++)(*this)(i) = Temp(i);
return (*this);
}
//----------------------------------------------------------------------------------
double CMatrix::MaxElement()
// Максимальное значение элементов матрицы
{
double max = (*this)(0, 0);
for (int i = 0; i<(this->rows()); i++)
for (int j = 0; j<(this->cols()); j++) if ((*this)(i, j)>max) max = (*this)(i, j);
return max;
}
//----------------------------------------------------------------------------------
double CMatrix::MinElement()
// Минимальное значение элементов матрицы
{
double min = (*this)(0, 0);
for (int i = 0; i<(this->rows()); i++)
for (int j = 0; j<(this->cols()); j++) if ((*this)(i, j)<min) min = (*this)(i, j);
return min;
} |