OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
newmat5.cpp
Go to the documentation of this file.
1 //$$ newmat5.cpp Transpose, evaluate etc
2 
3 // Copyright (C) 1991,2,3,4: R B Davies
4 
5 //#define WANT_STREAM
6 
7 #include <ossim/matrix/include.h>
8 
9 #include <ossim/matrix/newmat.h>
10 #include <ossim/matrix/newmatrc.h>
11 
12 #ifdef use_namespace
13 namespace NEWMAT {
14 #endif
15 
16 
17 #ifdef DO_REPORT
18 #define REPORT { static ExeCounter ExeCount(__LINE__,5); ++ExeCount; }
19 #else
20 #define REPORT {}
21 #endif
22 
23 
24 /************************ carry out operations ******************************/
25 
26 
28 {
29  GeneralMatrix* gm1;
30 
31  if (Compare(Type().t(),mt))
32  {
33  REPORT
34  gm1 = mt.New(ncols,nrows,tm);
35  for (int i=0; i<ncols; i++)
36  {
37  MatrixRow mr(gm1, StoreOnExit+DirectPart, i);
38  MatrixCol mc(this, mr.Data(), LoadOnEntry, i);
39  }
40  }
41  else
42  {
43  REPORT
44  gm1 = mt.New(ncols,nrows,tm);
45  MatrixRow mr(this, LoadOnEntry);
47  int i = nrows;
48  while (i--) { mc.Copy(mr); mr.Next(); mc.Next(); }
49  }
50  tDelete(); gm1->ReleaseAndDelete(); return gm1;
51 }
52 
54 { REPORT return Evaluate(mt); }
55 
56 
58 { REPORT return Evaluate(mt); }
59 
61 {
62  REPORT
64  gmx->nrows = 1; gmx->ncols = gmx->storage = storage;
65  return BorrowStore(gmx,mt);
66 }
67 
69 {
70  REPORT
72  gmx->ncols = 1; gmx->nrows = gmx->storage = storage;
73  return BorrowStore(gmx,mt);
74 }
75 
77 { REPORT return Evaluate(mt); }
78 
80 {
81  if (Compare(this->Type(),mt)) { REPORT return this; }
82  REPORT
83  GeneralMatrix* gmx = mt.New(nrows,ncols,this);
84  MatrixRow mr(this, LoadOnEntry);
86  int i=nrows;
87  while (i--) { mrx.Copy(mr); mrx.Next(); mr.Next(); }
88  tDelete(); gmx->ReleaseAndDelete(); return gmx;
89 }
90 
92  { REPORT return gm->Evaluate(mt); }
93 
95 {
96  gm=((BaseMatrix*&)bm)->Evaluate();
97  int nr=gm->Nrows(); int nc=gm->Ncols();
98  Compare(gm->Type().AddEqualEl(),mt);
99  if (!(mt==gm->Type()))
100  {
101  REPORT
102  GeneralMatrix* gmx = mt.New(nr,nc,this);
103  MatrixRow mr(gm, LoadOnEntry);
104  MatrixRow mrx(gmx, StoreOnExit+DirectPart);
105  while (nr--) { mrx.Add(mr,f); mrx.Next(); mr.Next(); }
106  gmx->ReleaseAndDelete(); gm->tDelete();
107  return gmx;
108  }
109  else if (gm->reuse())
110  {
111  REPORT gm->Add(f);
112  return gm;
113  }
114  else
115  {
116  REPORT GeneralMatrix* gmy = gm->Type().New(nr,nc,this);
117  gmy->ReleaseAndDelete(); gmy->Add(gm,f);
118  return gmy;
119  }
120 }
121 
123 {
124  gm=((BaseMatrix*&)bm)->Evaluate();
125  int nr=gm->Nrows(); int nc=gm->Ncols();
126  Compare(gm->Type().AddEqualEl(),mt);
127  if (!(mt==gm->Type()))
128  {
129  REPORT
130  GeneralMatrix* gmx = mt.New(nr,nc,this);
131  MatrixRow mr(gm, LoadOnEntry);
132  MatrixRow mrx(gmx, StoreOnExit+DirectPart);
133  while (nr--) { mrx.NegAdd(mr,f); mrx.Next(); mr.Next(); }
134  gmx->ReleaseAndDelete(); gm->tDelete();
135  return gmx;
136  }
137  else if (gm->reuse())
138  {
139  REPORT gm->NegAdd(f);
140  return gm;
141  }
142  else
143  {
144  REPORT GeneralMatrix* gmy = gm->Type().New(nr,nc,this);
145  gmy->ReleaseAndDelete(); gmy->NegAdd(gm,f);
146  return gmy;
147  }
148 }
149 
151 {
152  gm=((BaseMatrix*&)bm)->Evaluate();
153  int nr=gm->Nrows(); int nc=gm->Ncols();
154  if (Compare(gm->Type(),mt))
155  {
156  if (gm->reuse())
157  {
158  REPORT gm->Multiply(f);
159  return gm;
160  }
161  else
162  {
163  REPORT GeneralMatrix* gmx = gm->Type().New(nr,nc,this);
164  gmx->ReleaseAndDelete(); gmx->Multiply(gm,f);
165  return gmx;
166  }
167  }
168  else
169  {
170  REPORT
171  GeneralMatrix* gmx = mt.New(nr,nc,this);
172  MatrixRow mr(gm, LoadOnEntry);
173  MatrixRow mrx(gmx, StoreOnExit+DirectPart);
174  while (nr--) { mrx.Multiply(mr,f); mrx.Next(); mr.Next(); }
175  gmx->ReleaseAndDelete(); gm->tDelete();
176  return gmx;
177  }
178 }
179 
181 {
182  gm=((BaseMatrix*&)bm)->Evaluate();
183  int nr=gm->Nrows(); int nc=gm->Ncols();
184  if (Compare(gm->Type(),mt))
185  {
186  if (gm->reuse())
187  {
188  REPORT gm->Negate();
189  return gm;
190  }
191  else
192  {
193  REPORT
194  GeneralMatrix* gmx = gm->Type().New(nr,nc,this);
195  gmx->ReleaseAndDelete(); gmx->Negate(gm);
196  return gmx;
197  }
198  }
199  else
200  {
201  REPORT
202  GeneralMatrix* gmx = mt.New(nr,nc,this);
203  MatrixRow mr(gm, LoadOnEntry);
204  MatrixRow mrx(gmx, StoreOnExit+DirectPart);
205  while (nr--) { mrx.Negate(mr); mrx.Next(); mr.Next(); }
206  gmx->ReleaseAndDelete(); gm->tDelete();
207  return gmx;
208  }
209 }
210 
212 {
213  gm=((BaseMatrix*&)bm)->Evaluate(); GeneralMatrix* gmx;
214 
215  if ((gm->Type()).IsBand() && ! (gm->Type()).IsDiagonal())
216  {
217  gm->tDelete();
218  Throw(NotDefinedException("Reverse", "band matrices"));
219  }
220 
221  if (gm->reuse()) { REPORT gm->ReverseElements(); gmx = gm; }
222  else
223  {
224  REPORT
225  gmx = gm->Type().New(gm->Nrows(), gm->Ncols(), this);
226  gmx->ReverseElements(gm); gmx->ReleaseAndDelete();
227  }
228  return gmx->Evaluate(mt); // target matrix is different type?
229 
230 }
231 
233 {
234  REPORT
235  gm=((BaseMatrix*&)bm)->Evaluate();
236  Compare(gm->Type().t(),mt);
237  GeneralMatrix* gmx=gm->Transpose(this, mt);
238  return gmx;
239 }
240 
242 {
243  gm = ((BaseMatrix*&)bm)->Evaluate();
244  GeneralMatrix* gmx = new RowVector; MatrixErrorNoSpace(gmx);
245  gmx->nrows = 1; gmx->ncols = gmx->storage = gm->storage;
246  return gm->BorrowStore(gmx,mt);
247 }
248 
250 {
251  gm = ((BaseMatrix*&)bm)->Evaluate();
253  gmx->ncols = 1; gmx->nrows = gmx->storage = gm->storage;
254  return gm->BorrowStore(gmx,mt);
255 }
256 
258 {
259  gm = ((BaseMatrix*&)bm)->Evaluate();
261  gmx->nrows = gmx->ncols = gmx->storage = gm->storage;
262  return gm->BorrowStore(gmx,mt);
263 }
264 
266 {
267  Tracer tr("MatedMatrix::Evaluate");
268  gm = ((BaseMatrix*&)bm)->Evaluate();
269  GeneralMatrix* gmx = new Matrix; MatrixErrorNoSpace(gmx);
270  gmx->nrows = nr; gmx->ncols = nc; gmx->storage = gm->storage;
271  if (nr*nc != gmx->storage)
273  return gm->BorrowStore(gmx,mt);
274 }
275 
277 {
278  REPORT
279  Tracer tr("SubMatrix(evaluate)");
280  gm = ((BaseMatrix*&)bm)->Evaluate();
281  if (row_number < 0) row_number = gm->Nrows();
282  if (col_number < 0) col_number = gm->Ncols();
283  if (row_skip+row_number > gm->Nrows() || col_skip+col_number > gm->Ncols())
284  {
285  gm->tDelete();
287  }
288  if (IsSym) Compare(gm->Type().ssub(), mt);
289  else Compare(gm->Type().sub(), mt);
290  GeneralMatrix* gmx = mt.New(row_number, col_number, this);
291  int i = row_number;
292  MatrixRow mr(gm, LoadOnEntry, row_skip);
293  MatrixRow mrx(gmx, StoreOnExit+DirectPart);
294  MatrixRowCol sub;
295  while (i--)
296  {
297  mr.SubRowCol(sub, col_skip, col_number); // put values in sub
298  mrx.Copy(sub); mrx.Next(); mr.Next();
299  }
300  gmx->ReleaseAndDelete(); gm->tDelete();
301  return gmx;
302 }
303 
304 
306 {
307  return gm->Evaluate(mt);
308 }
309 
310 
312 {
313  REPORT
314  Real* s1=gm1->store; Real* s=store; int i=(storage >> 2);
315  while (i--)
316  { *s++ = *s1++ + f; *s++ = *s1++ + f; *s++ = *s1++ + f; *s++ = *s1++ + f; }
317  i = storage & 3; while (i--) *s++ = *s1++ + f;
318 }
319 
321 {
322  REPORT
323  Real* s=store; int i=(storage >> 2);
324  while (i--) { *s++ += f; *s++ += f; *s++ += f; *s++ += f; }
325  i = storage & 3; while (i--) *s++ += f;
326 }
327 
329 {
330  REPORT
331  Real* s1=gm1->store; Real* s=store; int i=(storage >> 2);
332  while (i--)
333  { *s++ = f - *s1++; *s++ = f - *s1++; *s++ = f - *s1++; *s++ = f - *s1++; }
334  i = storage & 3; while (i--) *s++ = f - *s1++;
335 }
336 
338 {
339  REPORT
340  Real* s=store; int i=(storage >> 2);
341  while (i--)
342  {
343  *s = f - *s; s++; *s = f - *s; s++;
344  *s = f - *s; s++; *s = f - *s; s++;
345  }
346  i = storage & 3; while (i--) { *s = f - *s; s++; }
347 }
348 
350 {
351  // change sign of elements
352  REPORT
353  Real* s1=gm1->store; Real* s=store; int i=(storage >> 2);
354  while (i--)
355  { *s++ = -(*s1++); *s++ = -(*s1++); *s++ = -(*s1++); *s++ = -(*s1++); }
356  i = storage & 3; while(i--) *s++ = -(*s1++);
357 }
358 
360 {
361  REPORT
362  Real* s=store; int i=(storage >> 2);
363  while (i--)
364  { *s = -(*s); s++; *s = -(*s); s++; *s = -(*s); s++; *s = -(*s); s++; }
365  i = storage & 3; while(i--) { *s = -(*s); s++; }
366 }
367 
369 {
370  REPORT
371  Real* s1=gm1->store; Real* s=store; int i=(storage >> 2);
372  while (i--)
373  { *s++ = *s1++ * f; *s++ = *s1++ * f; *s++ = *s1++ * f; *s++ = *s1++ * f; }
374  i = storage & 3; while (i--) *s++ = *s1++ * f;
375 }
376 
378 {
379  REPORT
380  Real* s=store; int i=(storage >> 2);
381  while (i--) { *s++ *= f; *s++ *= f; *s++ *= f; *s++ *= f; }
382  i = storage & 3; while (i--) *s++ *= f;
383 }
384 
385 
386 /************************ MatrixInput routines ****************************/
387 
388 // int MatrixInput::n; // number values still to be read
389 // Real* MatrixInput::r; // pointer to next location to be read to
390 
392 {
393  REPORT
394  Tracer et("MatrixInput");
395  if (n<=0) Throw(ProgramException("List of values too long"));
396  *r = f; int n1 = n-1; n=0; // n=0 so we won't trigger exception
397  return MatrixInput(n1, r+1);
398 }
399 
400 
402 {
403  REPORT
404  Tracer et("MatrixInput");
405  int n = Storage();
406  if (n<=0) Throw(ProgramException("Loading data to zero length matrix"));
407  Real* r; r = Store(); *r = f; n--;
408  return MatrixInput(n, r+1);
409 }
410 
412 {
413  REPORT
414  Tracer et("MatrixInput (GetSubMatrix)");
415  SetUpLHS();
416  if (row_number != 1 || col_skip != 0 || col_number != gm->Ncols())
417  {
418  Throw(ProgramException("MatrixInput requires complete rows"));
419  }
420  MatrixRow mr(gm, DirectPart, row_skip); // to pick up location and length
421  int n = mr.Storage();
422  if (n<=0)
423  {
424  Throw(ProgramException("Loading data to zero length row"));
425  }
426  Real* r; r = mr.Data(); *r = f; n--;
427  if (+(mr.cw*HaveStore))
428  {
429  Throw(ProgramException("Fails with this matrix type"));
430  }
431  return MatrixInput(n, r+1);
432 }
433 
435 {
436  REPORT
437  Tracer et("MatrixInput");
438  if (n!=0) Throw(ProgramException("A list of values was too short"));
439 }
440 
442 {
443  Tracer et("MatrixInput");
444  // bool dummy = true;
445  // if (dummy) // get rid of warning message
446  Throw(ProgramException("Cannot use list read with a BandMatrix"));
447  return MatrixInput(0, 0);
448 }
449 
451 { Throw(ProgramException("Cannot use array read with a BandMatrix")); }
452 
453 void BandMatrix::operator<<(const int*)
454 { Throw(ProgramException("Cannot use array read with a BandMatrix")); }
455 
457 { Throw(ProgramException("Cannot use array read with a BandMatrix")); }
458 
460 { Throw(ProgramException("Cannot use array read with a BandMatrix")); }
461 
462 // ************************* Reverse order of elements ***********************
463 
465 {
466  // reversing into a new matrix
467  REPORT
468  int n = Storage(); Real* rx = Store() + n; Real* x = gm->Store();
469  while (n--) *(--rx) = *(x++);
470 }
471 
473 {
474  // reversing in place
475  REPORT
476  int n = Storage(); Real* x = Store(); Real* rx = x + n;
477  n /= 2;
478  while (n--) { Real t = *(--rx); *rx = *x; *(x++) = t; }
479 }
480 
481 
482 #ifdef use_namespace
483 }
484 #endif
485 
void Add(GeneralMatrix *, Real)
Definition: newmat5.cpp:311
void operator<<(const BaseMatrix &)
Definition: submat.cpp:98
ossim_uint32 x
GeneralMatrix * Evaluate(MatrixType mt=MatrixTypeUnSp)
Definition: newmat5.cpp:257
void Copy(const MatrixRowCol &)
Definition: newmat2.cpp:413
double Real
Definition: include.h:57
GeneralMatrix * Transpose(TransposedMatrix *, MatrixType)
Definition: newmat5.cpp:68
void operator<<(const Real *r)
Definition: newmat5.cpp:456
int Storage()
Definition: newmatrc.h:92
void tDelete()
Definition: newmat4.cpp:535
void NegAdd(const MatrixRowCol &, Real)
Definition: newmat2.cpp:249
GeneralMatrix * Transpose(TransposedMatrix *, MatrixType)
Definition: newmat5.cpp:60
void Add(const MatrixRowCol &)
Definition: newmat2.cpp:30
void Negate()
Definition: newmat5.cpp:359
GeneralMatrix * Evaluate(MatrixType mt=MatrixTypeUnSp)
Definition: newmat5.cpp:94
void NegAdd(GeneralMatrix *, Real)
Definition: newmat5.cpp:328
MatrixInput operator<<(Real)
Definition: newmat5.cpp:391
GeneralMatrix * Evaluate(MatrixType mt=MatrixTypeUnSp)
Definition: newmat5.cpp:79
GeneralMatrix * New() const
MatrixInput operator<<(Real)
Definition: newmat5.cpp:441
GeneralMatrix * Evaluate(MatrixType mt=MatrixTypeUnSp)
Definition: newmat5.cpp:150
GeneralMatrix * Transpose(TransposedMatrix *, MatrixType)
Definition: newmat5.cpp:57
GeneralMatrix * Transpose(TransposedMatrix *, MatrixType)
Definition: newmat5.cpp:53
GeneralMatrix * Evaluate(MatrixType mt=MatrixTypeUnSp)
Definition: newmat5.cpp:241
bool Compare(const MatrixType &source, MatrixType &destination)
Definition: newmat4.cpp:729
GeneralMatrix * Evaluate(MatrixType mt=MatrixTypeUnSp)
Definition: newmat5.cpp:211
void MatrixErrorNoSpace(const void *)
Definition: newmatex.cpp:292
void Negate(const MatrixRowCol &)
Definition: newmat2.cpp:464
void ReleaseAndDelete()
Definition: newmat.h:442
GeneralMatrix * Evaluate(MatrixType mt=MatrixTypeUnSp)
Definition: newmat5.cpp:180
Real * Data()
Definition: newmatrc.h:90
void Multiply(const MatrixRowCol &)
Definition: newmat2.cpp:308
#define REPORT
Definition: newmat5.cpp:20
os2<< "> n<< " > nendobj n
GeneralMatrix * Evaluate(MatrixType mt=MatrixTypeUnSp)
Definition: newmat5.cpp:122
GeneralMatrix * Evaluate(MatrixType mt=MatrixTypeUnSp)
Definition: newmat5.cpp:305
LoadAndStoreFlag cw
Definition: newmatrc.h:44
Definition: newmat.h:543
void Multiply(GeneralMatrix *, Real)
Definition: newmat5.cpp:368
Real * store
Definition: newmat.h:393
void SubRowCol(MatrixRowCol &, int, int) const
Definition: newmat2.cpp:629
GeneralMatrix * Evaluate(MatrixType mt=MatrixTypeUnSp)
Definition: newmat5.cpp:232
void operator<<(const Real *)
Definition: newmat6.cpp:422
void Next()
Definition: newmatrc.h:163
void ReverseElements()
Definition: newmat5.cpp:472
virtual MatrixType Type() const =0
int Nrows() const
Definition: newmat.h:430
GeneralMatrix * Evaluate(MatrixType mt=MatrixTypeUnSp)
Definition: newmat5.cpp:265
Real * Store() const
Definition: newmat.h:433
int storage
Definition: newmat.h:392
virtual GeneralMatrix * Transpose(TransposedMatrix *, MatrixType)
Definition: newmat5.cpp:27
GeneralMatrix * Transpose(TransposedMatrix *, MatrixType)
Definition: newmat5.cpp:76
GeneralMatrix * Evaluate(MatrixType=MatrixTypeUnSp)
Definition: newmat5.cpp:91
GeneralMatrix * Evaluate(MatrixType mt=MatrixTypeUnSp)
Definition: newmat5.cpp:276
void Next()
Definition: newmatrc.h:149
GeneralMatrix * Evaluate(MatrixType mt=MatrixTypeUnSp)
Definition: newmat5.cpp:249