source: ntrip/trunk/BNC/newmat/newmat7.cpp@ 10881

Last change on this file since 10881 was 10791, checked in by mervart, 5 months ago

BNC Multifrequency and PPPAR Client (initial version)

File size: 30.5 KB
Line 
1/// \ingroup newmat
2///@{
3
4/// \file newmat7.cpp
5/// Invert, solve, binary operations.
6
7// Copyright (C) 1991,2,3,4: R B Davies
8
9#include "include.h"
10
11#include "newmat.h"
12#include "newmatrc.h"
13
14#ifdef use_namespace
15namespace NEWMAT {
16#endif
17
18
19#ifdef DO_REPORT
20#define REPORT { static ExeCounter ExeCount(__LINE__,7); ++ExeCount; }
21#else
22#define REPORT {}
23#endif
24
25
26//***************************** solve routines ******************************/
27
28GeneralMatrix* GeneralMatrix::MakeSolver()
29{
30 REPORT
31 GeneralMatrix* gm = new CroutMatrix(*this);
32 MatrixErrorNoSpace(gm); gm->ReleaseAndDelete(); return gm;
33}
34
35GeneralMatrix* Matrix::MakeSolver()
36{
37 REPORT
38 GeneralMatrix* gm = new CroutMatrix(*this);
39 MatrixErrorNoSpace(gm); gm->ReleaseAndDelete(); return gm;
40}
41
42void CroutMatrix::Solver(MatrixColX& mcout, const MatrixColX& mcin)
43{
44 REPORT
45 int i = mcin.skip; Real* el = mcin.data-i; Real* el1 = el;
46 while (i--) *el++ = 0.0;
47 el += mcin.storage; i = nrows_val - mcin.skip - mcin.storage;
48 while (i--) *el++ = 0.0;
49 lubksb(el1, mcout.skip);
50}
51
52
53// Do we need check for entirely zero output?
54
55void UpperTriangularMatrix::Solver(MatrixColX& mcout,
56 const MatrixColX& mcin)
57{
58 REPORT
59 int i = mcin.skip-mcout.skip; Real* elx = mcin.data-i;
60 while (i-- > 0) *elx++ = 0.0;
61 int nr = mcin.skip+mcin.storage;
62 elx = mcin.data+mcin.storage; Real* el = elx;
63 int j = mcout.skip+mcout.storage-nr;
64 int nc = ncols_val-nr; i = nr-mcout.skip;
65 while (j-- > 0) *elx++ = 0.0;
66 Real* Ael = store + (nr*(2*ncols_val-nr+1))/2; j = 0;
67 while (i-- > 0)
68 {
69 elx = el; Real sum = 0.0; int jx = j++; Ael -= nc;
70 while (jx--) sum += *(--Ael) * *(--elx);
71 elx--; *elx = (*elx - sum) / *(--Ael);
72 }
73}
74
75void LowerTriangularMatrix::Solver(MatrixColX& mcout,
76 const MatrixColX& mcin)
77{
78 REPORT
79 int i = mcin.skip-mcout.skip; Real* elx = mcin.data-i;
80 while (i-- > 0) *elx++ = 0.0;
81 int nc = mcin.skip; i = nc+mcin.storage; elx = mcin.data+mcin.storage;
82 int nr = mcout.skip+mcout.storage; int j = nr-i; i = nr-nc;
83 while (j-- > 0) *elx++ = 0.0;
84 Real* el = mcin.data; Real* Ael = store + (nc*(nc+1))/2; j = 0;
85 while (i-- > 0)
86 {
87 elx = el; Real sum = 0.0; int jx = j++; Ael += nc;
88 while (jx--) sum += *Ael++ * *elx++;
89 *elx = (*elx - sum) / *Ael++;
90 }
91}
92
93//******************* carry out binary operations *************************/
94
95static GeneralMatrix*
96 GeneralMult(GeneralMatrix*,GeneralMatrix*,MultipliedMatrix*,MatrixType);
97static GeneralMatrix*
98 GeneralSolv(GeneralMatrix*,GeneralMatrix*,BaseMatrix*,MatrixType);
99static GeneralMatrix*
100 GeneralSolvI(GeneralMatrix*,BaseMatrix*,MatrixType);
101static GeneralMatrix*
102 GeneralKP(GeneralMatrix*,GeneralMatrix*,KPMatrix*,MatrixType);
103
104GeneralMatrix* MultipliedMatrix::Evaluate(MatrixType mt)
105{
106 REPORT
107 gm2 = ((BaseMatrix*&)bm2)->Evaluate();
108 gm2 = gm2->Evaluate(gm2->type().MultRHS()); // no symmetric on RHS
109 gm1 = ((BaseMatrix*&)bm1)->Evaluate();
110 return GeneralMult(gm1, gm2, this, mt);
111}
112
113GeneralMatrix* SolvedMatrix::Evaluate(MatrixType mt)
114{
115 REPORT
116 gm1 = ((BaseMatrix*&)bm1)->Evaluate();
117 gm2 = ((BaseMatrix*&)bm2)->Evaluate();
118 return GeneralSolv(gm1,gm2,this,mt);
119}
120
121GeneralMatrix* KPMatrix::Evaluate(MatrixType mt)
122{
123 REPORT
124 gm1 = ((BaseMatrix*&)bm1)->Evaluate();
125 gm2 = ((BaseMatrix*&)bm2)->Evaluate();
126 return GeneralKP(gm1,gm2,this,mt);
127}
128
129// routines for adding or subtracting matrices of identical storage structure
130
131static void Add(GeneralMatrix* gm, GeneralMatrix* gm1, GeneralMatrix* gm2)
132{
133 REPORT
134 Real* s1=gm1->Store(); Real* s2=gm2->Store();
135 Real* s=gm->Store(); int i=gm->Storage() >> 2;
136 while (i--)
137 {
138 *s++ = *s1++ + *s2++; *s++ = *s1++ + *s2++;
139 *s++ = *s1++ + *s2++; *s++ = *s1++ + *s2++;
140 }
141 i=gm->Storage() & 3; while (i--) *s++ = *s1++ + *s2++;
142}
143
144static void AddTo(GeneralMatrix* gm, const GeneralMatrix* gm2)
145{
146 REPORT
147 const Real* s2=gm2->Store(); Real* s=gm->Store(); int i=gm->Storage() >> 2;
148 while (i--)
149 { *s++ += *s2++; *s++ += *s2++; *s++ += *s2++; *s++ += *s2++; }
150 i=gm->Storage() & 3; while (i--) *s++ += *s2++;
151}
152
153void GeneralMatrix::PlusEqual(const GeneralMatrix& gm)
154{
155 REPORT
156 if (nrows_val != gm.nrows_val || ncols_val != gm.ncols_val)
157 Throw(IncompatibleDimensionsException(*this, gm));
158 AddTo(this, &gm);
159}
160
161static void Subtract(GeneralMatrix* gm, GeneralMatrix* gm1, GeneralMatrix* gm2)
162{
163 REPORT
164 Real* s1=gm1->Store(); Real* s2=gm2->Store();
165 Real* s=gm->Store(); int i=gm->Storage() >> 2;
166 while (i--)
167 {
168 *s++ = *s1++ - *s2++; *s++ = *s1++ - *s2++;
169 *s++ = *s1++ - *s2++; *s++ = *s1++ - *s2++;
170 }
171 i=gm->Storage() & 3; while (i--) *s++ = *s1++ - *s2++;
172}
173
174static void SubtractFrom(GeneralMatrix* gm, const GeneralMatrix* gm2)
175{
176 REPORT
177 const Real* s2=gm2->Store(); Real* s=gm->Store(); int i=gm->Storage() >> 2;
178 while (i--)
179 { *s++ -= *s2++; *s++ -= *s2++; *s++ -= *s2++; *s++ -= *s2++; }
180 i=gm->Storage() & 3; while (i--) *s++ -= *s2++;
181}
182
183void GeneralMatrix::MinusEqual(const GeneralMatrix& gm)
184{
185 REPORT
186 if (nrows_val != gm.nrows_val || ncols_val != gm.ncols_val)
187 Throw(IncompatibleDimensionsException(*this, gm));
188 SubtractFrom(this, &gm);
189}
190
191static void ReverseSubtract(GeneralMatrix* gm, const GeneralMatrix* gm2)
192{
193 REPORT
194 const Real* s2=gm2->Store(); Real* s=gm->Store(); int i=gm->Storage() >> 2;
195 while (i--)
196 {
197 *s = *s2++ - *s; s++; *s = *s2++ - *s; s++;
198 *s = *s2++ - *s; s++; *s = *s2++ - *s; s++;
199 }
200 i=gm->Storage() & 3; while (i--) { *s = *s2++ - *s; s++; }
201}
202
203static void SP(GeneralMatrix* gm, GeneralMatrix* gm1, GeneralMatrix* gm2)
204{
205 REPORT
206 Real* s1=gm1->Store(); Real* s2=gm2->Store();
207 Real* s=gm->Store(); int i=gm->Storage() >> 2;
208 while (i--)
209 {
210 *s++ = *s1++ * *s2++; *s++ = *s1++ * *s2++;
211 *s++ = *s1++ * *s2++; *s++ = *s1++ * *s2++;
212 }
213 i=gm->Storage() & 3; while (i--) *s++ = *s1++ * *s2++;
214}
215
216static void SP(GeneralMatrix* gm, GeneralMatrix* gm2)
217{
218 REPORT
219 Real* s2=gm2->Store(); Real* s=gm->Store(); int i=gm->Storage() >> 2;
220 while (i--)
221 { *s++ *= *s2++; *s++ *= *s2++; *s++ *= *s2++; *s++ *= *s2++; }
222 i=gm->Storage() & 3; while (i--) *s++ *= *s2++;
223}
224
225// routines for adding or subtracting matrices of different storage structure
226
227static void AddDS(GeneralMatrix* gm, GeneralMatrix* gm1, GeneralMatrix* gm2)
228{
229 REPORT
230 int nr = gm->Nrows();
231 MatrixRow mr1(gm1, LoadOnEntry); MatrixRow mr2(gm2, LoadOnEntry);
232 MatrixRow mr(gm, StoreOnExit+DirectPart);
233 while (nr--) { mr.Add(mr1,mr2); mr1.Next(); mr2.Next(); mr.Next(); }
234}
235
236static void AddDS(GeneralMatrix* gm, GeneralMatrix* gm2)
237// Add into first argument
238{
239 REPORT
240 int nr = gm->Nrows();
241 MatrixRow mr(gm, StoreOnExit+LoadOnEntry+DirectPart);
242 MatrixRow mr2(gm2, LoadOnEntry);
243 while (nr--) { mr.Add(mr2); mr.Next(); mr2.Next(); }
244}
245
246static void SubtractDS
247 (GeneralMatrix* gm, GeneralMatrix* gm1, GeneralMatrix* gm2)
248{
249 REPORT
250 int nr = gm->Nrows();
251 MatrixRow mr1(gm1, LoadOnEntry); MatrixRow mr2(gm2, LoadOnEntry);
252 MatrixRow mr(gm, StoreOnExit+DirectPart);
253 while (nr--) { mr.Sub(mr1,mr2); mr1.Next(); mr2.Next(); mr.Next(); }
254}
255
256static void SubtractDS(GeneralMatrix* gm, GeneralMatrix* gm2)
257{
258 REPORT
259 int nr = gm->Nrows();
260 MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart);
261 MatrixRow mr2(gm2, LoadOnEntry);
262 while (nr--) { mr.Sub(mr2); mr.Next(); mr2.Next(); }
263}
264
265static void ReverseSubtractDS(GeneralMatrix* gm, GeneralMatrix* gm2)
266{
267 REPORT
268 int nr = gm->Nrows();
269 MatrixRow mr(gm, LoadOnEntry+StoreOnExit+DirectPart);
270 MatrixRow mr2(gm2, LoadOnEntry);
271 while (nr--) { mr.RevSub(mr2); mr2.Next(); mr.Next(); }
272}
273
274static void SPDS(GeneralMatrix* gm, GeneralMatrix* gm1, GeneralMatrix* gm2)
275{
276 REPORT
277 int nr = gm->Nrows();
278 MatrixRow mr1(gm1, LoadOnEntry); MatrixRow mr2(gm2, LoadOnEntry);
279 MatrixRow mr(gm, StoreOnExit+DirectPart);
280 while (nr--) { mr.Multiply(mr1,mr2); mr1.Next(); mr2.Next(); mr.Next(); }
281}
282
283static void SPDS(GeneralMatrix* gm, GeneralMatrix* gm2)
284// SP into first argument
285{
286 REPORT
287 int nr = gm->Nrows();
288 MatrixRow mr(gm, StoreOnExit+LoadOnEntry+DirectPart);
289 MatrixRow mr2(gm2, LoadOnEntry);
290 while (nr--) { mr.Multiply(mr2); mr.Next(); mr2.Next(); }
291}
292
293static GeneralMatrix* GeneralMult1(GeneralMatrix* gm1, GeneralMatrix* gm2,
294 MultipliedMatrix* mm, MatrixType mtx)
295{
296 REPORT
297 Tracer tr("GeneralMult1");
298 int nr=gm1->Nrows(); int nc=gm2->Ncols();
299 if (gm1->Ncols() !=gm2->Nrows())
300 Throw(IncompatibleDimensionsException(*gm1, *gm2));
301 GeneralMatrix* gmx = mtx.New(nr,nc,mm);
302
303 MatrixCol mcx(gmx, StoreOnExit+DirectPart);
304 MatrixCol mc2(gm2, LoadOnEntry);
305 while (nc--)
306 {
307 MatrixRow mr1(gm1, LoadOnEntry, mcx.Skip());
308 Real* el = mcx.Data(); // pointer to an element
309 int n = mcx.Storage();
310 while (n--) { *(el++) = DotProd(mr1,mc2); mr1.Next(); }
311 mc2.Next(); mcx.Next();
312 }
313 gmx->ReleaseAndDelete(); gm1->tDelete(); gm2->tDelete(); return gmx;
314}
315
316static GeneralMatrix* GeneralMult2(GeneralMatrix* gm1, GeneralMatrix* gm2,
317 MultipliedMatrix* mm, MatrixType mtx)
318{
319 // version that accesses by row only - not good for thin matrices
320 // or column vectors in right hand term.
321 REPORT
322 Tracer tr("GeneralMult2");
323 int nr=gm1->Nrows(); int nc=gm2->Ncols();
324 if (gm1->Ncols() !=gm2->Nrows())
325 Throw(IncompatibleDimensionsException(*gm1, *gm2));
326 GeneralMatrix* gmx = mtx.New(nr,nc,mm);
327
328 MatrixRow mrx(gmx, LoadOnEntry+StoreOnExit+DirectPart);
329 MatrixRow mr1(gm1, LoadOnEntry);
330 while (nr--)
331 {
332 MatrixRow mr2(gm2, LoadOnEntry, mr1.Skip());
333 Real* el = mr1.Data(); // pointer to an element
334 int n = mr1.Storage();
335 mrx.Zero();
336 while (n--) { mrx.AddScaled(mr2, *el++); mr2.Next(); }
337 mr1.Next(); mrx.Next();
338 }
339 gmx->ReleaseAndDelete(); gm1->tDelete(); gm2->tDelete(); return gmx;
340}
341
342static GeneralMatrix* mmMult(GeneralMatrix* gm1, GeneralMatrix* gm2)
343{
344 // matrix multiplication for type Matrix only
345 REPORT
346 Tracer tr("MatrixMult");
347
348 int nr=gm1->Nrows(); int ncr=gm1->Ncols(); int nc=gm2->Ncols();
349 if (ncr != gm2->Nrows()) Throw(IncompatibleDimensionsException(*gm1,*gm2));
350
351 Matrix* gm = new Matrix(nr,nc); MatrixErrorNoSpace(gm);
352
353 Real* s1=gm1->Store(); Real* s2=gm2->Store(); Real* s=gm->Store();
354
355 if (ncr)
356 {
357 while (nr--)
358 {
359 Real* s2x = s2; int j = ncr;
360 Real* sx = s; Real f = *s1++; int k = nc;
361 while (k--) *sx++ = f * *s2x++;
362 while (--j)
363 { sx = s; f = *s1++; k = nc; while (k--) *sx++ += f * *s2x++; }
364 s = sx;
365 }
366 }
367 else *gm = 0.0;
368
369 gm->ReleaseAndDelete(); gm1->tDelete(); gm2->tDelete(); return gm;
370}
371
372static GeneralMatrix* GeneralMult(GeneralMatrix* gm1, GeneralMatrix* gm2,
373 MultipliedMatrix* mm, MatrixType mtx)
374{
375 if ( Rectangular(gm1->type(), gm2->type(), mtx))
376 { REPORT return mmMult(gm1, gm2); }
377 Compare(gm1->type() * gm2->type(),mtx);
378 int nr = gm2->Nrows(); int nc = gm2->Ncols();
379 if (nc <= 5 && nr > nc) { REPORT return GeneralMult1(gm1, gm2, mm, mtx); }
380 REPORT return GeneralMult2(gm1, gm2, mm, mtx);
381}
382
383static GeneralMatrix* GeneralKP(GeneralMatrix* gm1, GeneralMatrix* gm2,
384 KPMatrix* kp, MatrixType mtx)
385{
386 REPORT
387 Tracer tr("GeneralKP");
388 int nr1 = gm1->Nrows(); int nc1 = gm1->Ncols();
389 int nr2 = gm2->Nrows(); int nc2 = gm2->Ncols();
390 Compare((gm1->type()).KP(gm2->type()),mtx);
391 GeneralMatrix* gmx = mtx.New(nr1*nr2, nc1*nc2, kp);
392 MatrixRow mrx(gmx, LoadOnEntry+StoreOnExit+DirectPart);
393 MatrixRow mr1(gm1, LoadOnEntry);
394 for (int i = 1; i <= nr1; ++i)
395 {
396 MatrixRow mr2(gm2, LoadOnEntry);
397 for (int j = 1; j <= nr2; ++j)
398 { mrx.KP(mr1,mr2); mr2.Next(); mrx.Next(); }
399 mr1.Next();
400 }
401 gmx->ReleaseAndDelete(); gm1->tDelete(); gm2->tDelete(); return gmx;
402}
403
404static GeneralMatrix* GeneralSolv(GeneralMatrix* gm1, GeneralMatrix* gm2,
405 BaseMatrix* sm, MatrixType mtx)
406{
407 REPORT
408 Tracer tr("GeneralSolv");
409 Compare(gm1->type().i() * gm2->type(),mtx);
410 int nr = gm1->Nrows();
411 if (nr != gm1->Ncols()) Throw(NotSquareException(*gm1));
412 int nc = gm2->Ncols();
413 if (gm1->Ncols() != gm2->Nrows())
414 Throw(IncompatibleDimensionsException(*gm1, *gm2));
415 GeneralMatrix* gmx = mtx.New(nr,nc,sm); MatrixErrorNoSpace(gmx);
416 Real* r = new Real [nr]; MatrixErrorNoSpace(r);
417 MONITOR_REAL_NEW("Make (GenSolv)",nr,r)
418 GeneralMatrix* gms = gm1->MakeSolver();
419 Try
420 {
421
422 MatrixColX mcx(gmx, r, StoreOnExit+DirectPart); // copy to and from r
423 // this must be inside Try so mcx is destroyed before gmx
424 MatrixColX mc2(gm2, r, LoadOnEntry);
425 while (nc--) { gms->Solver(mcx, mc2); mcx.Next(); mc2.Next(); }
426 }
427 CatchAll
428 {
429 if (gms) gms->tDelete();
430 delete gmx; // <--------------------
431 gm2->tDelete();
432 MONITOR_REAL_DELETE("Delete (GenSolv)",nr,r)
433 // AT&T version 2.1 gives an internal error
434 delete [] r;
435 ReThrow;
436 }
437 gms->tDelete(); gmx->ReleaseAndDelete(); gm2->tDelete();
438 MONITOR_REAL_DELETE("Delete (GenSolv)",nr,r)
439 // AT&T version 2.1 gives an internal error
440 delete [] r;
441 return gmx;
442}
443
444// version for inverses - gm2 is identity
445static GeneralMatrix* GeneralSolvI(GeneralMatrix* gm1, BaseMatrix* sm,
446 MatrixType mtx)
447{
448 REPORT
449 Tracer tr("GeneralSolvI");
450 Compare(gm1->type().i(),mtx);
451 int nr = gm1->Nrows();
452 if (nr != gm1->Ncols()) Throw(NotSquareException(*gm1));
453 int nc = nr;
454 // DiagonalMatrix I(nr); I = 1;
455 IdentityMatrix I(nr);
456 GeneralMatrix* gmx = mtx.New(nr,nc,sm); MatrixErrorNoSpace(gmx);
457 Real* r = new Real [nr]; MatrixErrorNoSpace(r);
458 MONITOR_REAL_NEW("Make (GenSolvI)",nr,r)
459 GeneralMatrix* gms = gm1->MakeSolver();
460 Try
461 {
462
463 MatrixColX mcx(gmx, r, StoreOnExit+DirectPart); // copy to and from r
464 // this must be inside Try so mcx is destroyed before gmx
465 MatrixColX mc2(&I, r, LoadOnEntry);
466 while (nc--) { gms->Solver(mcx, mc2); mcx.Next(); mc2.Next(); }
467 }
468 CatchAll
469 {
470 if (gms) gms->tDelete();
471 delete gmx;
472 MONITOR_REAL_DELETE("Delete (GenSolvI)",nr,r)
473 // AT&T version 2.1 gives an internal error
474 delete [] r;
475 ReThrow;
476 }
477 gms->tDelete(); gmx->ReleaseAndDelete();
478 MONITOR_REAL_DELETE("Delete (GenSolvI)",nr,r)
479 // AT&T version 2.1 gives an internal error
480 delete [] r;
481 return gmx;
482}
483
484GeneralMatrix* InvertedMatrix::Evaluate(MatrixType mtx)
485{
486 // Matrix Inversion - use solve routines
487 Tracer tr("InvertedMatrix::Evaluate");
488 REPORT
489 gm=((BaseMatrix*&)bm)->Evaluate();
490 return GeneralSolvI(gm,this,mtx);
491}
492
493//*************************** New versions ************************
494
495GeneralMatrix* AddedMatrix::Evaluate(MatrixType mtd)
496{
497 REPORT
498 Tracer tr("AddedMatrix::Evaluate");
499 gm1=((BaseMatrix*&)bm1)->Evaluate(); gm2=((BaseMatrix*&)bm2)->Evaluate();
500 int nr=gm1->Nrows(); int nc=gm1->Ncols();
501 if (nr!=gm2->Nrows() || nc!=gm2->Ncols())
502 {
503 Try { Throw(IncompatibleDimensionsException(*gm1, *gm2)); }
504 CatchAll
505 {
506 gm1->tDelete(); gm2->tDelete();
507 ReThrow;
508 }
509 }
510 MatrixType mt1 = gm1->type(), mt2 = gm2->type(); MatrixType mts = mt1 + mt2;
511 if (!mtd) { REPORT mtd = mts; }
512 else if (!(mtd.DataLossOK || mtd >= mts))
513 {
514 REPORT
515 gm1->tDelete(); gm2->tDelete();
516 Throw(ProgramException("Illegal Conversion", mts, mtd));
517 }
518 GeneralMatrix* gmx;
519 bool c1 = (mtd == mt1), c2 = (mtd == mt2);
520 if ( c1 && c2 && (gm1->SimpleAddOK(gm2) == 0) )
521 {
522 if (gm1->reuse()) { REPORT AddTo(gm1,gm2); gm2->tDelete(); gmx = gm1; }
523 else if (gm2->reuse()) { REPORT AddTo(gm2,gm1); gmx = gm2; }
524 else
525 {
526 REPORT
527 // what if new throws an exception
528 Try { gmx = mt1.New(nr,nc,this); }
529 CatchAll
530 {
531 ReThrow;
532 }
533 gmx->ReleaseAndDelete(); Add(gmx,gm1,gm2);
534 }
535 }
536 else
537 {
538 if (c1 && c2)
539 {
540 short SAO = gm1->SimpleAddOK(gm2);
541 if (SAO & 1) { REPORT c1 = false; }
542 if (SAO & 2) { REPORT c2 = false; }
543 }
544 if (c1 && gm1->reuse() ) // must have type test first
545 { REPORT AddDS(gm1,gm2); gm2->tDelete(); gmx = gm1; }
546 else if (c2 && gm2->reuse() )
547 { REPORT AddDS(gm2,gm1);
548 if (!c1) gm1->tDelete();
549 gmx = gm2;
550 }
551 else
552 {
553 REPORT
554 Try { gmx = mtd.New(nr,nc,this); }
555 CatchAll
556 {
557 if (!c1) gm1->tDelete();
558 if (!c2) gm2->tDelete();
559 ReThrow;
560 }
561 AddDS(gmx,gm1,gm2);
562 if (!c1) gm1->tDelete();
563 if (!c2) gm2->tDelete();
564 gmx->ReleaseAndDelete();
565 }
566 }
567 return gmx;
568}
569
570GeneralMatrix* SubtractedMatrix::Evaluate(MatrixType mtd)
571{
572 REPORT
573 Tracer tr("SubtractedMatrix::Evaluate");
574 gm1=((BaseMatrix*&)bm1)->Evaluate(); gm2=((BaseMatrix*&)bm2)->Evaluate();
575 int nr=gm1->Nrows(); int nc=gm1->Ncols();
576 if (nr!=gm2->Nrows() || nc!=gm2->Ncols())
577 {
578 Try { Throw(IncompatibleDimensionsException(*gm1, *gm2)); }
579 CatchAll
580 {
581 gm1->tDelete(); gm2->tDelete();
582 ReThrow;
583 }
584 }
585 MatrixType mt1 = gm1->type(), mt2 = gm2->type(); MatrixType mts = mt1 + mt2;
586 if (!mtd) { REPORT mtd = mts; }
587 else if (!(mtd.DataLossOK || mtd >= mts))
588 {
589 gm1->tDelete(); gm2->tDelete();
590 Throw(ProgramException("Illegal Conversion", mts, mtd));
591 }
592 GeneralMatrix* gmx;
593 bool c1 = (mtd == mt1), c2 = (mtd == mt2);
594 if ( c1 && c2 && (gm1->SimpleAddOK(gm2) == 0) )
595 {
596 if (gm1->reuse())
597 { REPORT SubtractFrom(gm1,gm2); gm2->tDelete(); gmx = gm1; }
598 else if (gm2->reuse()) { REPORT ReverseSubtract(gm2,gm1); gmx = gm2; }
599 else
600 {
601 REPORT
602 Try { gmx = mt1.New(nr,nc,this); }
603 CatchAll
604 {
605 ReThrow;
606 }
607 gmx->ReleaseAndDelete(); Subtract(gmx,gm1,gm2);
608 }
609 }
610 else
611 {
612 if (c1 && c2)
613 {
614 short SAO = gm1->SimpleAddOK(gm2);
615 if (SAO & 1) { REPORT c1 = false; }
616 if (SAO & 2) { REPORT c2 = false; }
617 }
618 if (c1 && gm1->reuse() ) // must have type test first
619 { REPORT SubtractDS(gm1,gm2); gm2->tDelete(); gmx = gm1; }
620 else if (c2 && gm2->reuse() )
621 {
622 REPORT ReverseSubtractDS(gm2,gm1);
623 if (!c1) gm1->tDelete();
624 gmx = gm2;
625 }
626 else
627 {
628 REPORT
629 // what if New throws and exception
630 Try { gmx = mtd.New(nr,nc,this); }
631 CatchAll
632 {
633 if (!c1) gm1->tDelete();
634 if (!c2) gm2->tDelete();
635 ReThrow;
636 }
637 SubtractDS(gmx,gm1,gm2);
638 if (!c1) gm1->tDelete();
639 if (!c2) gm2->tDelete();
640 gmx->ReleaseAndDelete();
641 }
642 }
643 return gmx;
644}
645
646GeneralMatrix* SPMatrix::Evaluate(MatrixType mtd)
647{
648 REPORT
649 Tracer tr("SPMatrix::Evaluate");
650 gm1=((BaseMatrix*&)bm1)->Evaluate(); gm2=((BaseMatrix*&)bm2)->Evaluate();
651 int nr=gm1->Nrows(); int nc=gm1->Ncols();
652 if (nr!=gm2->Nrows() || nc!=gm2->Ncols())
653 {
654 Try { Throw(IncompatibleDimensionsException(*gm1, *gm2)); }
655 CatchAll
656 {
657 gm1->tDelete(); gm2->tDelete();
658 ReThrow;
659 }
660 }
661 MatrixType mt1 = gm1->type(), mt2 = gm2->type();
662 MatrixType mts = mt1.SP(mt2);
663 if (!mtd) { REPORT mtd = mts; }
664 else if (!(mtd.DataLossOK || mtd >= mts))
665 {
666 gm1->tDelete(); gm2->tDelete();
667 Throw(ProgramException("Illegal Conversion", mts, mtd));
668 }
669 GeneralMatrix* gmx;
670 bool c1 = (mtd == mt1), c2 = (mtd == mt2);
671 if ( c1 && c2 && (gm1->SimpleAddOK(gm2) == 0) )
672 {
673 if (gm1->reuse()) { REPORT SP(gm1,gm2); gm2->tDelete(); gmx = gm1; }
674 else if (gm2->reuse()) { REPORT SP(gm2,gm1); gmx = gm2; }
675 else
676 {
677 REPORT
678 Try { gmx = mt1.New(nr,nc,this); }
679 CatchAll
680 {
681 ReThrow;
682 }
683 gmx->ReleaseAndDelete(); SP(gmx,gm1,gm2);
684 }
685 }
686 else
687 {
688 if (c1 && c2)
689 {
690 short SAO = gm1->SimpleAddOK(gm2);
691 if (SAO & 1) { REPORT c2 = false; } // c1 and c2 swapped
692 if (SAO & 2) { REPORT c1 = false; }
693 }
694 if (c1 && gm1->reuse() ) // must have type test first
695 { REPORT SPDS(gm1,gm2); gm2->tDelete(); gmx = gm1; }
696 else if (c2 && gm2->reuse() )
697 { REPORT SPDS(gm2,gm1); if (!c1) gm1->tDelete(); gmx = gm2; }
698 else
699 {
700 REPORT
701 // what if New throws and exception
702 Try { gmx = mtd.New(nr,nc,this); }
703 CatchAll
704 {
705 if (!c1) gm1->tDelete();
706 if (!c2) gm2->tDelete();
707 ReThrow;
708 }
709 SPDS(gmx,gm1,gm2);
710 if (!c1) gm1->tDelete();
711 if (!c2) gm2->tDelete();
712 gmx->ReleaseAndDelete();
713 }
714 }
715 return gmx;
716}
717
718
719
720//*************************** norm functions ****************************/
721
722Real BaseMatrix::norm1() const
723{
724 // maximum of sum of absolute values of a column
725 REPORT
726 GeneralMatrix* gm = ((BaseMatrix&)*this).Evaluate();
727 int nc = gm->Ncols(); Real value = 0.0;
728 MatrixCol mc(gm, LoadOnEntry);
729 while (nc--)
730 { Real v = mc.SumAbsoluteValue(); if (value < v) value = v; mc.Next(); }
731 gm->tDelete(); return value;
732}
733
734Real BaseMatrix::norm_infinity() const
735{
736 // maximum of sum of absolute values of a row
737 REPORT
738 GeneralMatrix* gm = ((BaseMatrix&)*this).Evaluate();
739 int nr = gm->Nrows(); Real value = 0.0;
740 MatrixRow mr(gm, LoadOnEntry);
741 while (nr--)
742 { Real v = mr.SumAbsoluteValue(); if (value < v) value = v; mr.Next(); }
743 gm->tDelete(); return value;
744}
745
746//********************** Concatenation and stacking *************************/
747
748GeneralMatrix* ConcatenatedMatrix::Evaluate(MatrixType mtx)
749{
750 REPORT
751 Tracer tr("Concatenate");
752 gm2 = ((BaseMatrix*&)bm2)->Evaluate();
753 gm1 = ((BaseMatrix*&)bm1)->Evaluate();
754 Compare(gm1->type() | gm2->type(),mtx);
755 int nr=gm1->Nrows(); int nc = gm1->Ncols() + gm2->Ncols();
756 if (nr != gm2->Nrows())
757 Throw(IncompatibleDimensionsException(*gm1, *gm2));
758 GeneralMatrix* gmx = mtx.New(nr,nc,this);
759 MatrixRow mr1(gm1, LoadOnEntry); MatrixRow mr2(gm2, LoadOnEntry);
760 MatrixRow mr(gmx, StoreOnExit+DirectPart);
761 while (nr--) { mr.ConCat(mr1,mr2); mr1.Next(); mr2.Next(); mr.Next(); }
762 gmx->ReleaseAndDelete(); gm1->tDelete(); gm2->tDelete(); return gmx;
763}
764
765GeneralMatrix* StackedMatrix::Evaluate(MatrixType mtx)
766{
767 REPORT
768 Tracer tr("Stack");
769 gm2 = ((BaseMatrix*&)bm2)->Evaluate();
770 gm1 = ((BaseMatrix*&)bm1)->Evaluate();
771 Compare(gm1->type() & gm2->type(),mtx);
772 int nc=gm1->Ncols();
773 int nr1 = gm1->Nrows(); int nr2 = gm2->Nrows();
774 if (nc != gm2->Ncols())
775 Throw(IncompatibleDimensionsException(*gm1, *gm2));
776 GeneralMatrix* gmx = mtx.New(nr1+nr2,nc,this);
777 MatrixRow mr1(gm1, LoadOnEntry); MatrixRow mr2(gm2, LoadOnEntry);
778 MatrixRow mr(gmx, StoreOnExit+DirectPart);
779 while (nr1--) { mr.Copy(mr1); mr1.Next(); mr.Next(); }
780 while (nr2--) { mr.Copy(mr2); mr2.Next(); mr.Next(); }
781 gmx->ReleaseAndDelete(); gm1->tDelete(); gm2->tDelete(); return gmx;
782}
783
784// ************************* equality of matrices ******************** //
785
786static bool RealEqual(Real* s1, Real* s2, int n)
787{
788 int i = n >> 2;
789 while (i--)
790 {
791 if (*s1++ != *s2++) return false;
792 if (*s1++ != *s2++) return false;
793 if (*s1++ != *s2++) return false;
794 if (*s1++ != *s2++) return false;
795 }
796 i = n & 3; while (i--) if (*s1++ != *s2++) return false;
797 return true;
798}
799
800static bool intEqual(int* s1, int* s2, int n)
801{
802 int i = n >> 2;
803 while (i--)
804 {
805 if (*s1++ != *s2++) return false;
806 if (*s1++ != *s2++) return false;
807 if (*s1++ != *s2++) return false;
808 if (*s1++ != *s2++) return false;
809 }
810 i = n & 3; while (i--) if (*s1++ != *s2++) return false;
811 return true;
812}
813
814
815bool operator==(const BaseMatrix& A, const BaseMatrix& B)
816{
817 Tracer tr("BaseMatrix ==");
818 REPORT
819 GeneralMatrix* gmA = ((BaseMatrix&)A).Evaluate();
820 GeneralMatrix* gmB = ((BaseMatrix&)B).Evaluate();
821
822 if (gmA == gmB) // same matrix
823 { REPORT gmA->tDelete(); return true; }
824
825 if ( gmA->Nrows() != gmB->Nrows() || gmA->Ncols() != gmB->Ncols() )
826 // different dimensions
827 { REPORT gmA->tDelete(); gmB->tDelete(); return false; }
828
829 // check for CroutMatrix or BandLUMatrix
830 MatrixType AType = gmA->type(); MatrixType BType = gmB->type();
831 if (AType.CannotConvert() || BType.CannotConvert() )
832 {
833 REPORT
834 bool bx = gmA->IsEqual(*gmB);
835 gmA->tDelete(); gmB->tDelete();
836 return bx;
837 }
838
839 // is matrix storage the same
840 // will need to modify if further matrix structures are introduced
841 if (AType == BType && gmA->bandwidth() == gmB->bandwidth())
842 { // compare store
843 REPORT
844 bool bx = RealEqual(gmA->Store(),gmB->Store(),gmA->Storage());
845 gmA->tDelete(); gmB->tDelete();
846 return bx;
847 }
848
849 // matrix storage different - just subtract
850 REPORT return is_zero(*gmA-*gmB);
851}
852
853bool operator==(const GeneralMatrix& A, const GeneralMatrix& B)
854{
855 Tracer tr("GeneralMatrix ==");
856 // May or may not call tDeletes
857 REPORT
858
859 if (&A == &B) // same matrix
860 { REPORT return true; }
861
862 if ( A.Nrows() != B.Nrows() || A.Ncols() != B.Ncols() )
863 { REPORT return false; } // different dimensions
864
865 // check for CroutMatrix or BandLUMatrix
866 MatrixType AType = A.Type(); MatrixType BType = B.Type();
867 if (AType.CannotConvert() || BType.CannotConvert() )
868 { REPORT return A.IsEqual(B); }
869
870 // is matrix storage the same
871 // will need to modify if further matrix structures are introduced
872 if (AType == BType && A.bandwidth() == B.bandwidth())
873 { REPORT return RealEqual(A.Store(),B.Store(),A.Storage()); }
874
875 // matrix storage different - just subtract
876 REPORT return is_zero(A-B);
877}
878
879bool GeneralMatrix::is_zero() const
880{
881 REPORT
882 Real* s=store; int i = storage >> 2;
883 while (i--)
884 {
885 if (*s++) return false;
886 if (*s++) return false;
887 if (*s++) return false;
888 if (*s++) return false;
889 }
890 i = storage & 3; while (i--) if (*s++) return false;
891 return true;
892}
893
894bool is_zero(const BaseMatrix& A)
895{
896 Tracer tr("BaseMatrix::is_zero");
897 REPORT
898 GeneralMatrix* gm1 = 0; bool bx;
899 Try { gm1=((BaseMatrix&)A).Evaluate(); bx = gm1->is_zero(); }
900 CatchAll { if (gm1) gm1->tDelete(); ReThrow; }
901 gm1->tDelete();
902 return bx;
903}
904
905// IsEqual functions - insist matrices are of same type
906// as well as equal values to be equal
907
908bool GeneralMatrix::IsEqual(const GeneralMatrix& A) const
909{
910 Tracer tr("GeneralMatrix IsEqual");
911 if (A.type() != type()) // not same types
912 { REPORT return false; }
913 if (&A == this) // same matrix
914 { REPORT return true; }
915 if (A.nrows_val != nrows_val || A.ncols_val != ncols_val)
916 // different dimensions
917 { REPORT return false; }
918 // is matrix storage the same - compare store
919 REPORT
920 return RealEqual(A.store,store,storage);
921}
922
923bool CroutMatrix::IsEqual(const GeneralMatrix& A) const
924{
925 Tracer tr("CroutMatrix IsEqual");
926 if (A.type() != type()) // not same types
927 { REPORT return false; }
928 if (&A == this) // same matrix
929 { REPORT return true; }
930 if (A.nrows_val != nrows_val || A.ncols_val != ncols_val)
931 // different dimensions
932 { REPORT return false; }
933 // is matrix storage the same - compare store
934 REPORT
935 return RealEqual(A.store,store,storage)
936 && intEqual(((CroutMatrix&)A).indx, indx, nrows_val);
937}
938
939
940bool BandLUMatrix::IsEqual(const GeneralMatrix& A) const
941{
942 Tracer tr("BandLUMatrix IsEqual");
943 if (A.type() != type()) // not same types
944 { REPORT return false; }
945 if (&A == this) // same matrix
946 { REPORT return true; }
947 if ( A.Nrows() != nrows_val || A.Ncols() != ncols_val
948 || ((BandLUMatrix&)A).m1 != m1 || ((BandLUMatrix&)A).m2 != m2 )
949 // different dimensions
950 { REPORT return false; }
951
952 // matrix storage the same - compare store
953 REPORT
954 return RealEqual(A.Store(),store,storage)
955 && RealEqual(((BandLUMatrix&)A).store2,store2,storage2)
956 && intEqual(((BandLUMatrix&)A).indx, indx, nrows_val);
957}
958
959
960// ************************* cross products ******************** //
961
962inline void crossproduct_body(Real* a, Real* b, Real* c)
963{
964 c[0] = a[1] * b[2] - a[2] * b[1];
965 c[1] = a[2] * b[0] - a[0] * b[2];
966 c[2] = a[0] * b[1] - a[1] * b[0];
967}
968
969Matrix crossproduct(const Matrix& A, const Matrix& B)
970{
971 REPORT
972 int ac = A.Ncols(); int ar = A.Nrows();
973 int bc = B.Ncols(); int br = B.Nrows();
974 Real* a = A.Store(); Real* b = B.Store();
975 if (ac == 3)
976 {
977 if (bc != 3 || ar != 1 || br != 1)
978 { Tracer et("crossproduct"); IncompatibleDimensionsException(A, B); }
979 REPORT
980 RowVector C(3); Real* c = C.Store(); crossproduct_body(a, b, c);
981 return (Matrix&)C;
982 }
983 else
984 {
985 if (ac != 1 || bc != 1 || ar != 3 || br != 3)
986 { Tracer et("crossproduct"); IncompatibleDimensionsException(A, B); }
987 REPORT
988 ColumnVector C(3); Real* c = C.Store(); crossproduct_body(a, b, c);
989 return (Matrix&)C;
990 }
991}
992
993ReturnMatrix crossproduct_rows(const Matrix& A, const Matrix& B)
994{
995 REPORT
996 int n = A.Nrows();
997 if (A.Ncols() != 3 || B.Ncols() != 3 || n != B.Nrows())
998 {
999 Tracer et("crossproduct_rows"); IncompatibleDimensionsException(A, B);
1000 }
1001 Matrix C(n, 3);
1002 Real* a = A.Store(); Real* b = B.Store(); Real* c = C.Store();
1003 if (n--)
1004 {
1005 for (;;)
1006 {
1007 crossproduct_body(a, b, c);
1008 if (!(n--)) break;
1009 a += 3; b += 3; c += 3;
1010 }
1011 }
1012
1013 return C.ForReturn();
1014}
1015
1016ReturnMatrix crossproduct_columns(const Matrix& A, const Matrix& B)
1017{
1018 REPORT
1019 int n = A.Ncols();
1020 if (A.Nrows() != 3 || B.Nrows() != 3 || n != B.Ncols())
1021 {
1022 Tracer et("crossproduct_columns");
1023 IncompatibleDimensionsException(A, B);
1024 }
1025 Matrix C(3, n);
1026 Real* a = A.Store(); Real* b = B.Store(); Real* c = C.Store();
1027 Real* an = a+n; Real* an2 = an+n;
1028 Real* bn = b+n; Real* bn2 = bn+n;
1029 Real* cn = c+n; Real* cn2 = cn+n;
1030
1031 int i = n;
1032 while (i--)
1033 {
1034 *c++ = *an * *bn2 - *an2 * *bn;
1035 *cn++ = *an2++ * *b - *a * *bn2++;
1036 *cn2++ = *a++ * *bn++ - *an++ * *b++;
1037 }
1038
1039 return C.ForReturn();
1040}
1041
1042
1043#ifdef use_namespace
1044}
1045#endif
1046
1047///@}
1048
Note: See TracBrowser for help on using the repository browser.