OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
newmatrm.cpp
Go to the documentation of this file.
1 //$$newmatrm.cpp rectangular matrix operations
2 
3 // Copyright (C) 1991,2,3,4: R B Davies
4 
5 #define WANT_MATH
6 
7 #include <cmath>
8 #include <ossim/matrix/newmat.h>
10 
11 #ifdef use_namespace
12 namespace NEWMAT {
13 #endif
14 
15 #ifdef DO_REPORT
16 #define REPORT { static ExeCounter ExeCount(__LINE__,12); ++ExeCount; }
17 #else
18 #define REPORT {}
19 #endif
20 
21 
22 // operations on rectangular matrices
23 
24 
25 void RectMatrixRow::Reset (const Matrix& M, int row, int skip, int length)
26 {
27  REPORT
29  ( M.Store()+row*M.Ncols()+skip, length, 1, M.Ncols() );
30 }
31 
32 void RectMatrixRow::Reset (const Matrix& M, int row)
33 {
34  REPORT
35  RectMatrixRowCol::Reset( M.Store()+row*M.Ncols(), M.Ncols(), 1, M.Ncols() );
36 }
37 
38 void RectMatrixCol::Reset (const Matrix& M, int skip, int col, int length)
39 {
40  REPORT
42  ( M.Store()+col+skip*M.Ncols(), length, M.Ncols(), 1 );
43 }
44 
45 void RectMatrixCol::Reset (const Matrix& M, int col)
46 {
47  REPORT
48  RectMatrixRowCol::Reset( M.Store()+col, M.Nrows(), M.Ncols(), 1 );
49 }
50 
51 
53 {
54  REPORT
55  long_Real sum = 0.0; int i = n; Real* s = store; int d = spacing;
56  // while (i--) { sum += (long_Real)*s * *s; s += d; }
57  if (i) for(;;)
58  { sum += (long_Real)*s * *s; if (!(--i)) break; s += d; }
59  return (Real)sum;
60 }
61 
63 {
64  REPORT
65  long_Real sum = 0.0; int i = n;
66  Real* s = store; int d = spacing;
67  Real* s1 = rmrc.store; int d1 = rmrc.spacing;
68  if (i!=rmrc.n)
69  {
70  Tracer tr("newmatrm");
71  Throw(InternalException("Dimensions differ in *"));
72  }
73  // while (i--) { sum += (long_Real)*s * *s1; s += d; s1 += d1; }
74  if (i) for(;;)
75  { sum += (long_Real)*s * *s1; if (!(--i)) break; s += d; s1 += d1; }
76  return (Real)sum;
77 }
78 
80 {
81  REPORT
82  int i = n; Real* s = store; int d = spacing;
83  Real* s1 = rmrc.store; int d1 = rmrc.spacing;
84  if (i!=rmrc.n)
85  {
86  Tracer tr("newmatrm");
87  Throw(InternalException("Dimensions differ in AddScaled"));
88  }
89  // while (i--) { *s += *s1 * r; s += d; s1 += d1; }
90  if (i) for (;;)
91  { *s += *s1 * r; if (!(--i)) break; s += d; s1 += d1; }
92 }
93 
95 {
96  REPORT
97  int i = n; Real* s = store; int d = spacing;
98  Real* s1 = rmrc.store; int d1 = rmrc.spacing;
99  if (i!=rmrc.n)
100  {
101  Tracer tr("newmatrm");
102  Throw(InternalException("Dimensions differ in Divide"));
103  }
104  // while (i--) { *s = *s1 / r; s += d; s1 += d1; }
105  if (i) for (;;) { *s = *s1 / r; if (!(--i)) break; s += d; s1 += d1; }
106 }
107 
109 {
110  REPORT
111  int i = n; Real* s = store; int d = spacing;
112  // while (i--) { *s /= r; s += d; }
113  if (i) for (;;) { *s /= r; if (!(--i)) break; s += d; }
114 }
115 
117 {
118  REPORT
119  int i = n; Real* s = store; int d = spacing;
120  // while (i--) { *s = - *s; s += d; }
121  if (i) for (;;) { *s = - *s; if (!(--i)) break; s += d; }
122 }
123 
125 {
126  REPORT
127  int i = n; Real* s = store; int d = spacing;
128  // while (i--) { *s = 0.0; s += d; }
129  if (i) for (;;) { *s = 0.0; if (!(--i)) break; s += d; }
130 }
131 
133 {
134  REPORT
135  int n = U.n;
136  if (n != V.n)
137  {
138  Tracer tr("newmatrm");
139  Throw(InternalException("Dimensions differ in ComplexScale"));
140  }
141  Real* u = U.store; Real* v = V.store;
142  int su = U.spacing; int sv = V.spacing;
143  //while (n--)
144  //{
145  // Real z = *u * x - *v * y; *v = *u * y + *v * x; *u = z;
146  // u += su; v += sv;
147  //}
148  if (n) for (;;)
149  {
150  Real z = *u * x - *v * y; *v = *u * y + *v * x; *u = z;
151  if (!(--n)) break;
152  u += su; v += sv;
153  }
154 }
155 
157 {
158  REPORT
159  // (U, V) = (U, V) * (c, s) where tau = s/(1+c), c^2 + s^2 = 1
160  int n = U.n;
161  if (n != V.n)
162  {
163  Tracer tr("newmatrm");
164  Throw(InternalException("Dimensions differ in Rotate"));
165  }
166  Real* u = U.store; Real* v = V.store;
167  int su = U.spacing; int sv = V.spacing;
168  //while (n--)
169  //{
170  // Real zu = *u; Real zv = *v;
171  // *u -= s * (zv + zu * tau); *v += s * (zu - zv * tau);
172  // u += su; v += sv;
173  //}
174  if (n) for(;;)
175  {
176  Real zu = *u; Real zv = *v;
177  *u -= s * (zv + zu * tau); *v += s * (zu - zv * tau);
178  if (!(--n)) break;
179  u += su; v += sv;
180  }
181 }
182 
183 
184 // misc procedures for numerical things
185 
186 Real pythag(Real f, Real g, Real& c, Real& s)
187 // return z=std::sqrt(f*f+g*g), c=f/z, s=g/z
188 // set c=1,s=0 if z==0
189 // avoid floating point overflow or divide by zero
190 {
191  if (f==0 && g==0) { c=1.0; s=0.0; return 0.0; }
192  Real af = f>=0 ? f : -f;
193  Real ag = g>=0 ? g : -g;
194  if (ag<af)
195  {
196  REPORT
197  Real h = g/f; Real sq = std::sqrt(1.0+h*h);
198  if (f<0) sq = -sq; // make return value non-negative
199  c = 1.0/sq; s = h/sq; return sq*f;
200  }
201  else
202  {
203  REPORT
204  Real h = f/g; Real sq = std::sqrt(1.0+h*h);
205  if (g<0) sq = -sq;
206  s = 1.0/sq; c = h/sq; return sq*g;
207  }
208 }
209 
210 
211 
212 
213 #ifdef use_namespace
214 }
215 #endif
216 
217 
Real SumSquare() const
Definition: newmatrm.cpp:52
ossim_uint32 x
double long_Real
Definition: include.h:59
double Real
Definition: include.h:57
void Reset(Real *st, int nx, int sp, int sh)
Definition: newmatrm.h:29
ossim_uint32 y
Real operator*(const RectMatrixRowCol &) const
Definition: newmatrm.cpp:62
void Rotate(RectMatrixCol &U, RectMatrixCol &V, Real tau, Real s)
Definition: newmatrm.cpp:156
int Ncols() const
Definition: newmat.h:431
Real pythag(Real f, Real g, Real &c, Real &s)
Definition: newmatrm.cpp:186
#define sq(a)
Definition: auxiliary.h:77
void Reset(const Matrix &, int, int, int)
Definition: newmatrm.cpp:25
void ComplexScale(RectMatrixCol &U, RectMatrixCol &V, Real x, Real y)
Definition: newmatrm.cpp:132
os2<< "> n<< " > nendobj n
void Divide(const RectMatrixRowCol &, Real)
Definition: newmatrm.cpp:94
Definition: newmat.h:543
int Nrows() const
Definition: newmat.h:430
Real * store
Definition: newmatrm.h:23
void AddScaled(const RectMatrixRowCol &, Real)
Definition: newmatrm.cpp:79
Real * Store() const
Definition: newmat.h:433
#define REPORT
Definition: newmatrm.cpp:18
void Reset(const Matrix &, int, int, int)
Definition: newmatrm.cpp:38