OpendTect-6_4  6.4
linsolv.h
Go to the documentation of this file.
1 #ifndef linsolv_h
2 #define linsolv_h
3 
4 /*@+
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: Kristofer Tingdahl
9  Date: 07-10-1999
10 ________________________________________________________________________
11 
12 @$
13 */
14 
15 #include "arrayndimpl.h"
16 #include "executor.h"
17 #include "uistrings.h"
18 #include <math.h>
19 
26 template <class T>
27 mClass(Algo) LinSolver
28 {
29 public:
30  LinSolver(const Array2D<T>& A);
31  LinSolver(const LinSolver&);
32 
33  ~LinSolver();
34 
35  bool init(TaskRunner* =0);
36  bool ready() const { return ready_; }
37  int size() const { return n_; }
38 
39  void apply(const T* b,T* x) const;
40 
41 protected:
43  int* croutsidx_;
44  int n_;
45  bool parity_;
46  bool ready_;
47 
48 };
49 
50 
51 template <class T>
54 public:
57 
58 protected:
59 
60  int nextStep();
62  {
63  return errmsg_.isEmpty()
64  ? tr("Generating linear model")
65  : errmsg_;
66  }
68  { return tr("Nr points processed"); }
69 
70  od_int64 nrDone() const { return curidx_; }
71  od_int64 totalNr() const { return totalnr_; }
72 
73  int curidx_;
74  int totalnr_;
75  int imax_;
76  int* croutsidx_;
80 };
81 
82 
83 template <class T>
85  : Executor("Generating linear model")
86  , croutsmatrix_(A)
87  , totalnr_(A.info().getSize(0))
88  , curidx_(0)
89  , imax_(mUdf(int))
90  , vv_(totalnr_,0)
91  , croutsidx_(croutsidx)
92 {
93  if ( A.info().getSize(0) != A.info().getSize(1) )
94  {
96  return;
97  }
98 
99  for ( int idx=0; idx<totalnr_; idx++ )
100  {
101  T maxval = 0;
102  for ( int idy=0; idy<totalnr_; idy++ )
103  {
104  const T temp = fabs( croutsmatrix_.get(idx,idy) );
105  if ( temp > maxval )
106  maxval = temp;
107  }
108 
109  if ( mIsZero(maxval,mDefEps) )
110  {
112  return;
113  }
114 
115  vv_[idx] = 1.0f/maxval;
116  }
117 }
118 
119 
120 #define TINY 1.0e-20
121 
122 template <class T>
124 {
125  if ( !errmsg_.isEmpty() )
126  return ErrorOccurred();
127 
128  if ( curidx_ >= totalnr_ )
129  return Finished();
130 
131  for (int idx=0; idx<curidx_; idx++ )
132  {
133  T sum = croutsmatrix_.get( idx, curidx_ );
134  for ( int idz=0; idz<idx; idz++ )
135  sum -= croutsmatrix_.get(idx,idz) * croutsmatrix_.get(idz,curidx_);
136 
137  croutsmatrix_.set( idx, curidx_, sum );
138  }
139 
140  T big = 0.0;
141  for ( int idx=curidx_; idx<totalnr_; idx++ )
142  {
143  T sum = croutsmatrix_.get( idx, curidx_ );
144  for ( int idz=0; idz<curidx_; idz++ )
145  {
146  sum -= croutsmatrix_.get(idx,idz)*croutsmatrix_.get(idz,curidx_);
147  }
148 
149  croutsmatrix_.set( idx, curidx_, sum );
150 
151  T dum = vv_[idx]*fabs(sum);
152 
153  if ( dum >= big )
154  {
155  big = dum;
156  imax_ = idx;
157  }
158  }
159 
160  if ( curidx_ != imax_ )
161  {
162  for ( int idz=0; idz<totalnr_; idz++ )
163  {
164  const T dum = croutsmatrix_.get( imax_, idz );
165  croutsmatrix_.set( imax_, idz, croutsmatrix_.get(curidx_,idz) );
166  croutsmatrix_.set( curidx_, idz, dum );
167  }
168 
169  vv_[imax_] = vv_[curidx_];
170  }
171 
173 
174  if ( mIsZero(croutsmatrix_.get(curidx_,curidx_),mDefEps) )
175  croutsmatrix_.set(curidx_,curidx_,TINY);
176 
177  if ( curidx_ != totalnr_-1 )
178  {
179  const T dum = 1.0f/croutsmatrix_.get(curidx_,curidx_);
180  for ( int idx=curidx_+1; idx<totalnr_; idx++ )
181  croutsmatrix_.set(idx, curidx_, dum*croutsmatrix_.get(idx,curidx_));
182  }
183 
184  return ++curidx_>=totalnr_ ? Finished() : MoreToDo();
185 }
186 
187 #undef TINY
188 
189 
190 template <class T> inline
192  : croutsmatrix_(A)
193  , croutsidx_(new int[A.info().getSize(0)])
194  , n_(A.info().getSize(0))
195  , ready_(false)
196  , parity_(true)
197 {
198 }
199 
200 
201 template <class T> inline
203 {
205  ready_ = TaskRunner::execute( taskr, task );
206  return ready_;
207 }
208 
209 
210 template <class T> inline
213  , croutsidx_(0)
214  , n_(s.n_)
215  , parity_(s.parity_)
216  , ready_(s.ready_)
217 {
218  if ( s.croutsidx_ )
219  {
220  croutsidx_ = new int[s.n_];
221  for ( int idx=0; idx<s.n_; idx++ )
222  croutsidx_[idx] = s.croutsidx_[idx];
223  }
224 }
225 
226 
227 template <class T> inline
229 {
230  delete [] croutsidx_;
231 }
232 
233 
234 template <class T> inline
235 void LinSolver<T>::apply( const T* b, T* x ) const
236 {
237  if ( !ready_ )
238  {
239  pErrMsg("Cannot apply. Did you forget to call LinSolver::init()?");
240  return;
241  }
242 
243  for ( int idx=0; idx<n_; idx++ )
244  x[idx] = b[idx];
245 
246  int ii=-1;
247 
248  for ( int i=0; i<n_; i++ )
249  {
250  int ip=croutsidx_[i];
251  T sum=x[ip];
252  x[ip]=x[i];
253 
254  if ( ii != -1 )
255  {
256  for ( int j=ii; j<=i-1; j++ )
257  {
258  sum -= croutsmatrix_.get(i,j)*x[j];
259  }
260  }
261  else if ( !mIsZero(sum,mDefEps) )
262  ii=i;
263 
264  x[i]=sum;
265  }
266 
267  for ( int i=n_-1; i>=0; i-- )
268  {
269  T sum=x[i];
270  for ( int j=i+1; j<n_; j++ )
271  sum -= croutsmatrix_.get(i,j)*x[j];
272 
273  x[i]=sum/croutsmatrix_.get(i,i);
274  }
275 
276 }
277 
278 #endif
int * croutsidx_
Definition: linsolv.h:76
bool isEmpty() const
bool parity_
Definition: linsolv.h:45
const Array2DInfo & info() const
Definition: arrayndimpl.h:124
int imax_
Definition: linsolv.h:75
Array2DImpl< T > & croutsmatrix_
Definition: linsolv.h:78
#define mODTextTranslationClass(clss)
Definition: uistring.h:38
static int ErrorOccurred()
Definition: task.h:141
bool init(TaskRunner *=0)
Definition: linsolv.h:202
LinSolverTask(Array2DImpl< T > &, int *)
Definition: linsolv.h:84
#define mIsZero(x, eps)
Definition: commondefs.h:53
~LinSolverTask()
Definition: linsolv.h:56
int * croutsidx_
Definition: linsolv.h:43
#define od_int64
Definition: plftypes.h:36
bool init()
Implementation of Array2D.
Definition: arrayndimpl.h:102
int n_
Definition: linsolv.h:44
#define TINY
Definition: linsolv.h:120
Definition: uistring.h:89
static int MoreToDo()
Definition: task.h:143
static bool execute(TaskRunner *tr, Task &)
Taskrunner may be zero.
virtual int getSize(int dim) const =0
int curidx_
Definition: linsolv.h:73
static int Finished()
Definition: task.h:142
Set of (small) copyable elements.
Definition: commontypes.h:30
Array2D ( Subclass of ArrayND ) is a two dimensional array.
Definition: arraynd.h:131
Class that can execute a task.
Definition: task.h:169
LinSolver - Solves linear systems of equations on the form A*x=B. A is a matrix of the size N*N...
Definition: gridder2d.h:24
#define mUdf(type)
Use this macro to get the undefined for simple types.
Definition: undefval.h:272
od_int64 nrDone() const
Definition: linsolv.h:70
od_int64 totalNr() const
Definition: linsolv.h:71
#define mDefEps
Definition: commondefs.h:58
TypeSet< T > vv_
Definition: linsolv.h:77
int totalnr_
Definition: linsolv.h:74
bool isEmpty() const
Definition: odstring.h:51
BufferString errmsg_
Definition: horizontracker.h:119
static uiString sInput()
int size() const
Definition: linsolv.h:37
Definition: linsolv.h:52
bool ready_
Definition: linsolv.h:46
static uiString phrInvalid(const uiString &string)
"Invalid <string>"
bool ready() const
Definition: linsolv.h:36
#define mClass(module)
Definition: commondefs.h:164
uiString uiMessage() const
will be message() again in 7.x
Definition: linsolv.h:61
#define pErrMsg(msg)
Definition: errmsg.h:60
LinSolver(const Array2D< T > &A)
Definition: linsolv.h:191
Array2DImpl< T > croutsmatrix_
Definition: linsolv.h:42
uiString uiNrDoneText() const
will be nrDoneText() in 7.x
Definition: linsolv.h:67
Specification to enable chunkwise execution of a process.
Definition: executor.h:39
~LinSolver()
Definition: linsolv.h:228
void apply(const T *b, T *x) const
Definition: linsolv.h:235
uiString errmsg_
Definition: linsolv.h:79
int nextStep()
Definition: linsolv.h:123

Generated at for the OpendTect seismic interpretation project. Copyright (C): dGB Beheer B. V. 2019