1 | //$$ submat.cpp submatrices |
---|
2 | |
---|
3 | // Copyright (C) 1991,2,3,4: R B Davies |
---|
4 | |
---|
5 | #include "include.h" |
---|
6 | |
---|
7 | #include "newmat.h" |
---|
8 | #include "newmatrc.h" |
---|
9 | |
---|
10 | #ifdef use_namespace |
---|
11 | namespace NEWMAT { |
---|
12 | #endif |
---|
13 | |
---|
14 | #ifdef DO_REPORT |
---|
15 | #define REPORT { static ExeCounter ExeCount(__LINE__,11); ++ExeCount; } |
---|
16 | #else |
---|
17 | #define REPORT {} |
---|
18 | #endif |
---|
19 | |
---|
20 | |
---|
21 | /****************************** submatrices *********************************/ |
---|
22 | |
---|
23 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
24 | GetSubMatrix& BaseMatrix::SubMatrix(int first_row, int last_row, int first_col, |
---|
25 | int last_col) const |
---|
26 | #else |
---|
27 | GetSubMatrix BaseMatrix::SubMatrix(int first_row, int last_row, int first_col, |
---|
28 | int last_col) const |
---|
29 | #endif |
---|
30 | { |
---|
31 | REPORT |
---|
32 | Tracer tr("SubMatrix"); |
---|
33 | int a = first_row - 1; int b = last_row - first_row + 1; |
---|
34 | int c = first_col - 1; int d = last_col - first_col + 1; |
---|
35 | if (a<0 || b<0 || c<0 || d<0) Throw(SubMatrixDimensionException()); |
---|
36 | // allow zero rows or columns |
---|
37 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
38 | GetSubMatrix* x = new GetSubMatrix(this, a, b, c, d, false); |
---|
39 | MatrixErrorNoSpace(x); |
---|
40 | return *x; |
---|
41 | #else |
---|
42 | return GetSubMatrix(this, a, b, c, d, false); |
---|
43 | #endif |
---|
44 | } |
---|
45 | |
---|
46 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
47 | GetSubMatrix& BaseMatrix::SymSubMatrix(int first_row, int last_row) const |
---|
48 | #else |
---|
49 | GetSubMatrix BaseMatrix::SymSubMatrix(int first_row, int last_row) const |
---|
50 | #endif |
---|
51 | { |
---|
52 | REPORT |
---|
53 | Tracer tr("SubMatrix(symmetric)"); |
---|
54 | int a = first_row - 1; int b = last_row - first_row + 1; |
---|
55 | if (a<0 || b<0) Throw(SubMatrixDimensionException()); |
---|
56 | // allow zero rows or columns |
---|
57 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
58 | GetSubMatrix* x = new GetSubMatrix(this, a, b, a, b, true); |
---|
59 | MatrixErrorNoSpace(x); |
---|
60 | return *x; |
---|
61 | #else |
---|
62 | return GetSubMatrix( this, a, b, a, b, true); |
---|
63 | #endif |
---|
64 | } |
---|
65 | |
---|
66 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
67 | GetSubMatrix& BaseMatrix::Row(int first_row) const |
---|
68 | #else |
---|
69 | GetSubMatrix BaseMatrix::Row(int first_row) const |
---|
70 | #endif |
---|
71 | { |
---|
72 | REPORT |
---|
73 | Tracer tr("SubMatrix(row)"); |
---|
74 | int a = first_row - 1; |
---|
75 | if (a<0) Throw(SubMatrixDimensionException()); |
---|
76 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
77 | GetSubMatrix* x = new GetSubMatrix(this, a, 1, 0, -1, false); |
---|
78 | MatrixErrorNoSpace(x); |
---|
79 | return *x; |
---|
80 | #else |
---|
81 | return GetSubMatrix(this, a, 1, 0, -1, false); |
---|
82 | #endif |
---|
83 | } |
---|
84 | |
---|
85 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
86 | GetSubMatrix& BaseMatrix::Rows(int first_row, int last_row) const |
---|
87 | #else |
---|
88 | GetSubMatrix BaseMatrix::Rows(int first_row, int last_row) const |
---|
89 | #endif |
---|
90 | { |
---|
91 | REPORT |
---|
92 | Tracer tr("SubMatrix(rows)"); |
---|
93 | int a = first_row - 1; int b = last_row - first_row + 1; |
---|
94 | if (a<0 || b<0) Throw(SubMatrixDimensionException()); |
---|
95 | // allow zero rows or columns |
---|
96 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
97 | GetSubMatrix* x = new GetSubMatrix(this, a, b, 0, -1, false); |
---|
98 | MatrixErrorNoSpace(x); |
---|
99 | return *x; |
---|
100 | #else |
---|
101 | return GetSubMatrix(this, a, b, 0, -1, false); |
---|
102 | #endif |
---|
103 | } |
---|
104 | |
---|
105 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
106 | GetSubMatrix& BaseMatrix::Column(int first_col) const |
---|
107 | #else |
---|
108 | GetSubMatrix BaseMatrix::Column(int first_col) const |
---|
109 | #endif |
---|
110 | { |
---|
111 | REPORT |
---|
112 | Tracer tr("SubMatrix(column)"); |
---|
113 | int c = first_col - 1; |
---|
114 | if (c<0) Throw(SubMatrixDimensionException()); |
---|
115 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
116 | GetSubMatrix* x = new GetSubMatrix(this, 0, -1, c, 1, false); |
---|
117 | MatrixErrorNoSpace(x); |
---|
118 | return *x; |
---|
119 | #else |
---|
120 | return GetSubMatrix(this, 0, -1, c, 1, false); |
---|
121 | #endif |
---|
122 | } |
---|
123 | |
---|
124 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
125 | GetSubMatrix& BaseMatrix::Columns(int first_col, int last_col) const |
---|
126 | #else |
---|
127 | GetSubMatrix BaseMatrix::Columns(int first_col, int last_col) const |
---|
128 | #endif |
---|
129 | { |
---|
130 | REPORT |
---|
131 | Tracer tr("SubMatrix(columns)"); |
---|
132 | int c = first_col - 1; int d = last_col - first_col + 1; |
---|
133 | if (c<0 || d<0) Throw(SubMatrixDimensionException()); |
---|
134 | // allow zero rows or columns |
---|
135 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
136 | GetSubMatrix* x = new GetSubMatrix(this, 0, -1, c, d, false); |
---|
137 | MatrixErrorNoSpace(x); |
---|
138 | return *x; |
---|
139 | #else |
---|
140 | return GetSubMatrix(this, 0, -1, c, d, false); |
---|
141 | #endif |
---|
142 | } |
---|
143 | |
---|
144 | void GetSubMatrix::SetUpLHS() |
---|
145 | { |
---|
146 | REPORT |
---|
147 | Tracer tr("SubMatrix(LHS)"); |
---|
148 | const BaseMatrix* bm1 = bm; |
---|
149 | GeneralMatrix* gm1 = ((BaseMatrix*&)bm)->Evaluate(); |
---|
150 | if ((BaseMatrix*)gm1!=bm1) |
---|
151 | Throw(ProgramException("Invalid LHS")); |
---|
152 | if (row_number < 0) row_number = gm1->Nrows(); |
---|
153 | if (col_number < 0) col_number = gm1->Ncols(); |
---|
154 | if (row_skip+row_number > gm1->Nrows() |
---|
155 | || col_skip+col_number > gm1->Ncols()) |
---|
156 | Throw(SubMatrixDimensionException()); |
---|
157 | } |
---|
158 | |
---|
159 | void GetSubMatrix::operator<<(const BaseMatrix& bmx) |
---|
160 | { |
---|
161 | REPORT |
---|
162 | Tracer tr("SubMatrix(<<)"); GeneralMatrix* gmx = 0; |
---|
163 | Try |
---|
164 | { |
---|
165 | SetUpLHS(); gmx = ((BaseMatrix&)bmx).Evaluate(); |
---|
166 | if (row_number != gmx->Nrows() || col_number != gmx->Ncols()) |
---|
167 | Throw(IncompatibleDimensionsException()); |
---|
168 | MatrixRow mrx(gmx, LoadOnEntry); |
---|
169 | MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip); |
---|
170 | // do need LoadOnEntry |
---|
171 | MatrixRowCol sub; int i = row_number; |
---|
172 | while (i--) |
---|
173 | { |
---|
174 | mr.SubRowCol(sub, col_skip, col_number); // put values in sub |
---|
175 | sub.Copy(mrx); mr.Next(); mrx.Next(); |
---|
176 | } |
---|
177 | gmx->tDelete(); |
---|
178 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
179 | delete this; |
---|
180 | #endif |
---|
181 | } |
---|
182 | |
---|
183 | CatchAll |
---|
184 | { |
---|
185 | if (gmx) gmx->tDelete(); |
---|
186 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
187 | delete this; |
---|
188 | #endif |
---|
189 | ReThrow; |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | void GetSubMatrix::operator=(const BaseMatrix& bmx) |
---|
194 | { |
---|
195 | REPORT |
---|
196 | Tracer tr("SubMatrix(=)"); GeneralMatrix* gmx = 0; |
---|
197 | // MatrixConversionCheck mcc; // Check for loss of info |
---|
198 | Try |
---|
199 | { |
---|
200 | SetUpLHS(); gmx = ((BaseMatrix&)bmx).Evaluate(); |
---|
201 | if (row_number != gmx->Nrows() || col_number != gmx->Ncols()) |
---|
202 | Throw(IncompatibleDimensionsException()); |
---|
203 | LoadAndStoreFlag lasf = |
---|
204 | ( row_skip == col_skip |
---|
205 | && gm->Type().IsSymmetric() |
---|
206 | && gmx->Type().IsSymmetric() ) |
---|
207 | ? LoadOnEntry+DirectPart |
---|
208 | : LoadOnEntry; |
---|
209 | MatrixRow mrx(gmx, lasf); |
---|
210 | MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip); |
---|
211 | // do need LoadOnEntry |
---|
212 | MatrixRowCol sub; int i = row_number; |
---|
213 | while (i--) |
---|
214 | { |
---|
215 | mr.SubRowCol(sub, col_skip, col_number); // put values in sub |
---|
216 | sub.CopyCheck(mrx); mr.Next(); mrx.Next(); |
---|
217 | } |
---|
218 | gmx->tDelete(); |
---|
219 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
220 | delete this; |
---|
221 | #endif |
---|
222 | } |
---|
223 | |
---|
224 | CatchAll |
---|
225 | { |
---|
226 | if (gmx) gmx->tDelete(); |
---|
227 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
228 | delete this; |
---|
229 | #endif |
---|
230 | ReThrow; |
---|
231 | } |
---|
232 | } |
---|
233 | |
---|
234 | void GetSubMatrix::operator<<(const Real* r) |
---|
235 | { |
---|
236 | REPORT |
---|
237 | Tracer tr("SubMatrix(<<Real*)"); |
---|
238 | SetUpLHS(); |
---|
239 | if (row_skip+row_number > gm->Nrows() || col_skip+col_number > gm->Ncols()) |
---|
240 | Throw(SubMatrixDimensionException()); |
---|
241 | MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip); |
---|
242 | // do need LoadOnEntry |
---|
243 | MatrixRowCol sub; int i = row_number; |
---|
244 | while (i--) |
---|
245 | { |
---|
246 | mr.SubRowCol(sub, col_skip, col_number); // put values in sub |
---|
247 | sub.Copy(r); mr.Next(); |
---|
248 | } |
---|
249 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
250 | delete this; |
---|
251 | #endif |
---|
252 | } |
---|
253 | |
---|
254 | void GetSubMatrix::operator=(Real r) |
---|
255 | { |
---|
256 | REPORT |
---|
257 | Tracer tr("SubMatrix(=Real)"); |
---|
258 | SetUpLHS(); |
---|
259 | MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip); |
---|
260 | // do need LoadOnEntry |
---|
261 | MatrixRowCol sub; int i = row_number; |
---|
262 | while (i--) |
---|
263 | { |
---|
264 | mr.SubRowCol(sub, col_skip, col_number); // put values in sub |
---|
265 | sub.Copy(r); mr.Next(); |
---|
266 | } |
---|
267 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
268 | delete this; |
---|
269 | #endif |
---|
270 | } |
---|
271 | |
---|
272 | void GetSubMatrix::Inject(const GeneralMatrix& gmx) |
---|
273 | { |
---|
274 | REPORT |
---|
275 | Tracer tr("SubMatrix(inject)"); |
---|
276 | SetUpLHS(); |
---|
277 | if (row_number != gmx.Nrows() || col_number != gmx.Ncols()) |
---|
278 | Throw(IncompatibleDimensionsException()); |
---|
279 | MatrixRow mrx((GeneralMatrix*)(&gmx), LoadOnEntry); |
---|
280 | MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip); |
---|
281 | // do need LoadOnEntry |
---|
282 | MatrixRowCol sub; int i = row_number; |
---|
283 | while (i--) |
---|
284 | { |
---|
285 | mr.SubRowCol(sub, col_skip, col_number); // put values in sub |
---|
286 | sub.Inject(mrx); mr.Next(); mrx.Next(); |
---|
287 | } |
---|
288 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
289 | delete this; |
---|
290 | #endif |
---|
291 | } |
---|
292 | |
---|
293 | void GetSubMatrix::operator+=(const BaseMatrix& bmx) |
---|
294 | { |
---|
295 | REPORT |
---|
296 | Tracer tr("SubMatrix(+=)"); GeneralMatrix* gmx = 0; |
---|
297 | // MatrixConversionCheck mcc; // Check for loss of info |
---|
298 | Try |
---|
299 | { |
---|
300 | SetUpLHS(); gmx = ((BaseMatrix&)bmx).Evaluate(); |
---|
301 | if (row_number != gmx->Nrows() || col_number != gmx->Ncols()) |
---|
302 | Throw(IncompatibleDimensionsException()); |
---|
303 | MatrixRow mrx(gmx, LoadOnEntry); |
---|
304 | MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip); |
---|
305 | // do need LoadOnEntry |
---|
306 | MatrixRowCol sub; int i = row_number; |
---|
307 | while (i--) |
---|
308 | { |
---|
309 | mr.SubRowCol(sub, col_skip, col_number); // put values in sub |
---|
310 | sub.Check(mrx); // check for loss of info |
---|
311 | sub.Add(mrx); mr.Next(); mrx.Next(); |
---|
312 | } |
---|
313 | gmx->tDelete(); |
---|
314 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
315 | delete this; |
---|
316 | #endif |
---|
317 | } |
---|
318 | |
---|
319 | CatchAll |
---|
320 | { |
---|
321 | if (gmx) gmx->tDelete(); |
---|
322 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
323 | delete this; |
---|
324 | #endif |
---|
325 | ReThrow; |
---|
326 | } |
---|
327 | } |
---|
328 | |
---|
329 | void GetSubMatrix::operator-=(const BaseMatrix& bmx) |
---|
330 | { |
---|
331 | REPORT |
---|
332 | Tracer tr("SubMatrix(-=)"); GeneralMatrix* gmx = 0; |
---|
333 | // MatrixConversionCheck mcc; // Check for loss of info |
---|
334 | Try |
---|
335 | { |
---|
336 | SetUpLHS(); gmx = ((BaseMatrix&)bmx).Evaluate(); |
---|
337 | if (row_number != gmx->Nrows() || col_number != gmx->Ncols()) |
---|
338 | Throw(IncompatibleDimensionsException()); |
---|
339 | MatrixRow mrx(gmx, LoadOnEntry); |
---|
340 | MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip); |
---|
341 | // do need LoadOnEntry |
---|
342 | MatrixRowCol sub; int i = row_number; |
---|
343 | while (i--) |
---|
344 | { |
---|
345 | mr.SubRowCol(sub, col_skip, col_number); // put values in sub |
---|
346 | sub.Check(mrx); // check for loss of info |
---|
347 | sub.Sub(mrx); mr.Next(); mrx.Next(); |
---|
348 | } |
---|
349 | gmx->tDelete(); |
---|
350 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
351 | delete this; |
---|
352 | #endif |
---|
353 | } |
---|
354 | |
---|
355 | CatchAll |
---|
356 | { |
---|
357 | if (gmx) gmx->tDelete(); |
---|
358 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
359 | delete this; |
---|
360 | #endif |
---|
361 | ReThrow; |
---|
362 | } |
---|
363 | } |
---|
364 | |
---|
365 | void GetSubMatrix::operator+=(Real r) |
---|
366 | { |
---|
367 | REPORT |
---|
368 | Tracer tr("SubMatrix(+= or -= Real)"); |
---|
369 | // MatrixConversionCheck mcc; // Check for loss of info |
---|
370 | Try |
---|
371 | { |
---|
372 | SetUpLHS(); |
---|
373 | MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip); |
---|
374 | // do need LoadOnEntry |
---|
375 | MatrixRowCol sub; int i = row_number; |
---|
376 | while (i--) |
---|
377 | { |
---|
378 | mr.SubRowCol(sub, col_skip, col_number); // put values in sub |
---|
379 | sub.Check(); // check for loss of info |
---|
380 | sub.Add(r); mr.Next(); |
---|
381 | } |
---|
382 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
383 | delete this; |
---|
384 | #endif |
---|
385 | } |
---|
386 | |
---|
387 | CatchAll |
---|
388 | { |
---|
389 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
390 | delete this; |
---|
391 | #endif |
---|
392 | ReThrow; |
---|
393 | } |
---|
394 | } |
---|
395 | |
---|
396 | void GetSubMatrix::operator*=(Real r) |
---|
397 | { |
---|
398 | REPORT |
---|
399 | Tracer tr("SubMatrix(*= or /= Real)"); |
---|
400 | // MatrixConversionCheck mcc; // Check for loss of info |
---|
401 | Try |
---|
402 | { |
---|
403 | SetUpLHS(); |
---|
404 | MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart, row_skip); |
---|
405 | // do need LoadOnEntry |
---|
406 | MatrixRowCol sub; int i = row_number; |
---|
407 | while (i--) |
---|
408 | { |
---|
409 | mr.SubRowCol(sub, col_skip, col_number); // put values in sub |
---|
410 | sub.Multiply(r); mr.Next(); |
---|
411 | } |
---|
412 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
413 | delete this; |
---|
414 | #endif |
---|
415 | } |
---|
416 | |
---|
417 | CatchAll |
---|
418 | { |
---|
419 | #ifdef TEMPS_DESTROYED_QUICKLY |
---|
420 | delete this; |
---|
421 | #endif |
---|
422 | ReThrow; |
---|
423 | } |
---|
424 | } |
---|
425 | |
---|
426 | #ifdef use_namespace |
---|
427 | } |
---|
428 | #endif |
---|
429 | |
---|