OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
newmat1.cpp
Go to the documentation of this file.
1 //$$ newmat1.cpp Matrix type functions
2 
3 // Copyright (C) 1991,2,3,4: R B Davies
4 
5 //#define WANT_STREAM
6 
7 #include <cmath>
8 #include <ossim/matrix/newmat.h>
9 
10 #ifdef use_namespace
11 namespace NEWMAT {
12 #endif
13 
14 #ifdef DO_REPORT
15 #define REPORT { static ExeCounter ExeCount(__LINE__,1); ++ExeCount; }
16 #else
17 #define REPORT {}
18 #endif
19 
20 
21 /************************* MatrixType functions *****************************/
22 
23 
24 // Skew needs more work <<<<<<<<<
25 
26 // all operations to return MatrixTypes which correspond to valid types
27 // of matrices.
28 // Eg: if it has the Diagonal attribute, then it must also have
29 // Upper, Lower, Band, Square and Symmetric.
30 
31 
33 {
34  REPORT
35  int a = attribute & mt.attribute & ~(Symmetric | Skew);
36  a |= (a & Diagonal) * 63; // recognise diagonal
37  return MatrixType(a);
38 }
39 
41 // elementwise product
42 // Lower, Upper, Diag, Band if only one is
43 // Symmetric, Ones, Valid (and Real) if both are
44 // Need to include Lower & Upper => Diagonal
45 // Will need to include both Skew => Symmetric
46 {
47  REPORT
48  int a = ((attribute | mt.attribute) & ~(Symmetric + Skew + Valid + Ones))
49  | (attribute & mt.attribute);
50  if ((a & Lower) != 0 && (a & Upper) != 0) a |= Diagonal;
51  if ((attribute & Skew) != 0)
52  {
53  if ((mt.attribute & Symmetric) != 0) a |= Skew;
54  if ((mt.attribute & Skew) != 0) { a &= ~Skew; a |= Symmetric; }
55  }
56  else if ((mt.attribute & Skew) != 0 && (attribute & Symmetric) != 0)
57  a |= Skew;
58  a |= (a & Diagonal) * 63; // recognise diagonal
59  return MatrixType(a);
60 }
61 
63 // Kronecker product
64 // Lower, Upper, Diag, Symmetric, Band, Valid if both are
65 // Band if LHS is band & other is square
66 // Ones is complicated so leave this out
67 {
68  REPORT
69  int a = (attribute & mt.attribute) & ~Ones;
70  if ((attribute & Band) != 0 && (mt.attribute & Square) != 0)
71  a |= Band;
72  //int a = ((attribute & mt.attribute) | (attribute & Band)) & ~Ones;
73 
74  return MatrixType(a);
75 }
76 
77 MatrixType MatrixType::i() const // type of inverse
78 {
79  REPORT
80  int a = attribute & ~(Band+LUDeco);
81  a |= (a & Diagonal) * 63; // recognise diagonal
82  return MatrixType(a);
83 }
84 
86 // swap lower and upper attributes
87 // assume Upper is in bit above Lower
88 {
89  REPORT
90  int a = attribute;
91  a ^= (((a >> 1) ^ a) & Lower) * 3;
92  return MatrixType(a);
93 }
94 
96 {
97  REPORT
98  // remove symmetric attribute unless diagonal
99  return (attribute >= Dg) ? attribute : (attribute & ~Symmetric);
100 }
101 
102 // this is used for deciding type of multiplication
104 {
105  REPORT
106  return
107  ((a.attribute | b.attribute | c.attribute)
109 }
110 
111 const char* MatrixType::Value() const
112 {
113 // make a string with the name of matrix with the given attributes
114  switch (attribute)
115  {
116  case Valid: REPORT return "Rect ";
117  case Valid+Square: REPORT return "Squ ";
118  case Valid+Symmetric+Square: REPORT return "Sym ";
119  case Valid+Skew+Square: REPORT return "Skew ";
120  case Valid+Band+Square: REPORT return "Band ";
121  case Valid+Symmetric+Band+Square: REPORT return "SmBnd";
122  case Valid+Upper+Square: REPORT return "UT ";
123  case Valid+Diagonal+Symmetric+Band+Upper+Lower+Square:
124  REPORT return "Diag ";
125  case Valid+Diagonal+Symmetric+Band+Upper+Lower+Ones+Square:
126  REPORT return "Ident";
127  case Valid+Band+Upper+Square: REPORT return "UpBnd";
128  case Valid+Lower+Square: REPORT return "LT ";
129  case Valid+Band+Lower+Square: REPORT return "LwBnd";
130  default:
131  REPORT
132  if (!(attribute & Valid)) return "UnSp ";
133  if (attribute & LUDeco)
134  return (attribute & Band) ? "BndLU" : "Crout";
135  return "?????";
136  }
137 }
138 
139 
140 GeneralMatrix* MatrixType::New(int nr, int nc, BaseMatrix* bm) const
141 {
142 // make a new matrix with the given attributes
143 
144  Tracer tr("New"); GeneralMatrix* gm=0; // initialised to keep gnu happy
145  switch (attribute)
146  {
147  case Valid:
148  REPORT
149  if (nc==1) { gm = new ColumnVector(nr); break; }
150  if (nr==1) { gm = new RowVector(nc); break; }
151  gm = new Matrix(nr, nc); break;
152 
153  case Valid+Square:
154  REPORT
155  if (nc!=nr) { Throw(NotSquareException()); }
156  gm = new SquareMatrix(nr); break;
157 
158  case Valid+Symmetric+Square:
159  REPORT gm = new SymmetricMatrix(nr); break;
160 
161  case Valid+Band+Square:
162  {
163  REPORT
164  MatrixBandWidth bw = bm->BandWidth();
165  gm = new BandMatrix(nr,bw.lower,bw.upper); break;
166  }
167 
168  case Valid+Symmetric+Band+Square:
169  REPORT gm = new SymmetricBandMatrix(nr,bm->BandWidth().lower); break;
170 
171  case Valid+Upper+Square:
172  REPORT gm = new UpperTriangularMatrix(nr); break;
173 
174  case Valid+Diagonal+Symmetric+Band+Upper+Lower+Square:
175  REPORT gm = new DiagonalMatrix(nr); break;
176 
177  case Valid+Band+Upper+Square:
178  REPORT gm = new UpperBandMatrix(nr,bm->BandWidth().upper); break;
179 
180  case Valid+Lower+Square:
181  REPORT gm = new LowerTriangularMatrix(nr); break;
182 
183  case Valid+Band+Lower+Square:
184  REPORT gm = new LowerBandMatrix(nr,bm->BandWidth().lower); break;
185 
186  case Valid+Diagonal+Symmetric+Band+Upper+Lower+Ones+Square:
187  REPORT gm = new IdentityMatrix(nr); break;
188 
189  default:
190  Throw(ProgramException("Invalid matrix type"));
191  }
192 
193  MatrixErrorNoSpace(gm); gm->Protect(); return gm;
194 }
195 
196 
197 #ifdef use_namespace
198 }
199 #endif
200 
#define REPORT
Definition: newmat1.cpp:17
virtual MatrixBandWidth BandWidth() const
Definition: newmat4.cpp:431
GeneralMatrix * New() const
int attribute
Definition: newmat.h:131
void MatrixErrorNoSpace(const void *)
Definition: newmatex.cpp:292
MatrixType MultRHS() const
Definition: newmat1.cpp:95
bool Rectangular(MatrixType a, MatrixType b, MatrixType c)
Definition: newmat1.cpp:103
Definition: newmat.h:543
MatrixType t() const
Definition: newmat1.cpp:85
MatrixType i() const
Definition: newmat1.cpp:77
const char * Value() const
Definition: newmat1.cpp:111
MatrixType SP(const MatrixType &) const
Definition: newmat1.cpp:40
void Protect()
Definition: newmat.h:437
MatrixType operator*(const MatrixType &) const
Definition: newmat1.cpp:32
MatrixType KP(const MatrixType &) const
Definition: newmat1.cpp:62