OpendTect-6_4  6.4
array2dmatrix.h
Go to the documentation of this file.
1 #ifndef array2dmatrix_h
2 #define array2dmatrix_h
3 
4 /*@+
5 ________________________________________________________________________
6 
7  (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
8  Author: Bert
9  Date: Apr 2014
10  RCS: $Id$
11 ________________________________________________________________________
12 
13 
14 @$*/
15 
16 #include "algomod.h"
17 #include "arrayndimpl.h"
18 #include "math2.h"
19 
20 
22 
31 template <class fT>
33 {
34 public:
35  Array2DMatrix( int sz0=1, int sz1=0 )
36  : a2d_(sz0<1?1:sz0,
37  sz1<1?(sz0<1?1:sz0):sz1) { setAll(); }
38  Array2DMatrix( const Array2DMatrix& oth )
39  : a2d_(oth.a2d_) {}
41  : a2d_(a2d) {}
42  Array2DMatrix( const Array2D<fT>& a2d )
43  : a2d_(a2d) {}
44  inline Array2DMatrix& operator =( const Array2DMatrix& oth )
45  { return a2d_ = oth.a2d_; }
46  inline Array2DMatrix& operator =( const Array2DImpl<fT>& a2d )
47  { return a2d_ = a2d; }
48  inline Array2DMatrix& operator =( const Array2D<fT>& a2d )
49  { return a2d_ = a2d; }
50  inline bool operator ==( const Array2DMatrix<fT>& oth ) const
51  { return isEq( oth.a2d_, fT(1e-6)) ; }
52  inline bool isEq(const Array2D<fT>&,fT eps=fT(1e-6)) const;
53 
54  inline int size(bool dim1=false) const;
55  inline void set( int i0, int i1, fT v ) { a2d_.set(i0,i1,v); }
56  inline fT& get(int i0,int i1);
57  inline fT get( int i0, int i1 ) const { return a2d_.get(i0,i1); }
58 
59  inline void setAll( fT v=fT(0) ) { a2d_.setAll( v ); }
60  inline void setDiagonal(fT);
61  inline void setToIdentity() { setAll(0); setDiagonal(1); }
62 
63  inline void add(fT);
64  inline void add(const Array2DMatrix&);
65  inline void multiply(fT);
66  inline void multiply(const Array2DMatrix&);
67  inline void transpose();
68 
69  inline void getSum(const Array2DMatrix&,Array2DMatrix&) const;
70  inline void getProduct(const Array1DVector&,Array1DVector&) const;
71  inline void getProduct(const Array2DMatrix&,Array2DMatrix&) const;
72  inline void getTransposed(Array2DMatrix&);
73  inline bool getCholesky(Array2DMatrix&) const;
74 
76 
77 };
78 
79 
81 #define mDefineA2DMatSizes(m,nm) \
82  const int nm##0 = (m).size( false ); const int nm##1 = (m).size( true )
83 
84 
85 template <class fT>
86 inline int Array2DMatrix<fT>::size( bool dim1 ) const
87 {
88  return a2d_.info().getSize( dim1 ? 1 : 0 );
89 }
90 
91 
92 template <class fT>
93 inline fT& Array2DMatrix<fT>::get( int i0, int i1 )
94 {
95  const od_int64 offset = a2d_.info().getOffset( i0, i1 );
96  return a2d_.getData()[offset];
97 }
98 
99 
100 #define mDefineImplA2DMatSizes mDefineA2DMatSizes(*this,sz)
101 
102 
103 template <class fT>
104 inline bool Array2DMatrix<fT>::isEq( const Array2D<fT>& a2d, fT eps ) const
105 {
107  if ( a2d_.info().getSize(0) != sz0 || a2d_.info().getSize(1) != sz1 )
108  return false;
109 
110  for ( int idx0=0; idx0<sz0; idx0++ )
111  {
112  for ( int idx1=0; idx1<sz1; idx1++ )
113  {
114  if ( !isFPEqual( get(idx0,idx1), a2d.get(idx0,idx1), eps ) )
115  return false;
116  }
117  }
118  return true;
119 }
120 
121 
122 template <class fT>
124 {
126  const int sz = sz0 > sz1 ? sz1 : sz0;
127 
128  for ( int idx=0; idx<sz; idx++ )
129  set( idx, idx, v );
130 }
131 
132 
133 template <class fT>
134 inline void Array2DMatrix<fT>::add( fT val )
135 {
137  for ( int idx0=0; idx0<sz0; idx0++ )
138  for ( int idx1=0; idx1<sz1; idx1++ )
139  get( idx0, idx1 ) += val;
140 }
141 
142 
143 template <class fT>
144 inline void Array2DMatrix<fT>::add( const Array2DMatrix& in )
145 {
147  mDefineA2DMatSizes( in, insz );
148  const int outsz0 = insz0 > sz0 ? sz0 : insz0;
149  const int outsz1 = insz1 > sz1 ? sz1 : insz1;
150 
151  for ( int idx0=0; idx0<outsz0; idx0++ )
152  {
153  for ( int idx1=0; idx1<outsz1; idx1++ )
154  {
155  fT res = 0;
156  for ( int idx=0; idx<sz1; idx++ )
157  get( idx0, idx1 ) += in.get( idx0, idx1 );
158  }
159  }
160 }
161 
162 
163 template <class fT>
164 inline void Array2DMatrix<fT>::multiply( fT fac )
165 {
167  for ( int idx0=0; idx0<sz0; idx0++ )
168  for ( int idx1=0; idx1<sz1; idx1++ )
169  get( idx0, idx1 ) *= fac;
170 }
171 
172 
173 template <class fT>
174 inline void Array2DMatrix<fT>::multiply( const Array2DMatrix& in )
175 {
176  const Array2DMatrix copy( *this );
177  copy.getProduct( in, *this );
178 }
179 
180 
181 #define mA2DMatHandleDimErr(v1,v2) \
182 if ( v1 != v2 ) \
183 { \
184  BufferString emsg( "Dim error: " #v1 "=", v1, " " #v2 "=" ); \
185  emsg.add( v2 ); pErrMsg( emsg ); \
186  if ( v2 > v1 ) \
187  const_cast<int&>( v2 ) = v1; \
188  else \
189  const_cast<int&>( v1 ) = v2; \
190 }
191 
192 
193 template <class fT>
194 inline void Array2DMatrix<fT>::getSum( const Array2DMatrix& in,
195  Array2DMatrix& out ) const
196 {
198  mDefineA2DMatSizes( in, insz );
199  const int outsz0 = insz0 > sz0 ? sz0 : insz0;
200  const int outsz1 = insz1 > sz1 ? sz1 : insz1;
201  out.a2d_.setSize( outsz0, outsz1 );
202 
203  for ( int idx0=0; idx0<outsz0; idx0++ )
204  {
205  for ( int idx1=0; idx1<outsz1; idx1++ )
206  {
207  fT res = 0;
208  for ( int idx=0; idx<sz1; idx++ )
209  out.set( idx0, idx1, get(idx0,idx1) + in.get(idx0,idx1) );
210  }
211  }
212 }
213 
214 
215 template <class fT>
217  Array1DVector& vout ) const
218 {
220  const int vsz = vin.info().getSize(0);
221  mA2DMatHandleDimErr(vsz,sz1)
222  vout.setSize( vsz );
223 
224  for ( int idx0=0; idx0<sz0; idx0++ )
225  {
226  fT res = 0;
227  for ( int idx1=0; idx1<sz1; idx1++ )
228  res += get( idx0, idx1 ) * vin.get( idx1 );
229  vout.set( idx0, res );
230  }
231 }
232 
233 
234 template <class fT>
235 inline void Array2DMatrix<fT>::getProduct( const Array2DMatrix& in,
236  Array2DMatrix& out ) const
237 {
239  mDefineA2DMatSizes( in, insz );
240  mA2DMatHandleDimErr(sz1,insz0)
241  out.a2d_.setSize( sz0, insz1 );
242 
243  for ( int idx0=0; idx0<sz0; idx0++ )
244  {
245  for ( int idx1=0; idx1<insz1; idx1++ )
246  {
247  fT res = 0;
248  for ( int idx=0; idx<sz1; idx++ )
249  res += in.get( idx, idx1 ) * get( idx0, idx );
250  out.set( idx0, idx1, res );
251  }
252  }
253 }
254 
255 
256 template <class fT>
258 {
259  const Array2DMatrix copy( *this );
260  copy.getTransposed( *this );
261 }
262 
263 
264 template <class fT>
266 {
268  out.a2d_.setSize( sz1, sz0 );
269 
270  for ( int idx0=0; idx0<sz0; idx0++ )
271  {
272  for ( int idx1=0; idx1<sz1; idx1++ )
273  out.set( idx1, idx0, get( idx0, idx1 ) );
274  }
275 }
276 
277 
278 template <class fT>
279 inline bool Array2DMatrix<fT>::getCholesky( Array2DMatrix& out ) const
280 {
282  mA2DMatHandleDimErr(sz0,sz1)
283  out.a2d_.setSize( sz0, sz0 );
284 
285  out.setAll( 0 );
286 
287  for( int idx0=0; idx0<sz0; idx0++ )
288  {
289  for ( int idx1=0; idx1<=idx0; idx1++ )
290  {
291  fT sum = 0;
292  for ( int j=0; j<idx1; j++ )
293  sum += out.get( idx0, j ) * out.get( idx1, j );
294 
295  fT val = 0;
296  if ( idx0 == idx1 )
297  {
298  val = get( idx0, idx0 ) - sum;
299  if ( val < 0 )
300  return false;
301  val = Math::Sqrt( val );
302  }
303  else
304  {
305  const fT dividend = get( idx0, idx1 ) - sum;
306  const fT divideby = out.get( idx1, idx1 );
307  if ( divideby )
308  val = dividend / divideby;
309  else if ( dividend )
310  return false;
311  }
312  out.set( idx0, idx1, val );
313  }
314  }
315 
316  return true;
317 }
318 
319 #endif
Array2DMatrix(const Array2D< fT > &a2d)
Definition: array2dmatrix.h:42
Array1DImpl< float > Array1DVector
Definition: array2dmatrix.h:21
Array2DMatrix(const Array2DMatrix &oth)
Definition: array2dmatrix.h:38
void getTransposed(Array2DMatrix &)
Definition: array2dmatrix.h:265
void multiply(fT)
Definition: array2dmatrix.h:164
bool operator==(const ArrayNDInfo &a1, const ArrayNDInfo &a2)
Definition: arrayndinfo.h:53
virtual T get(int p0, int p1) const =0
void add(fT)
Definition: array2dmatrix.h:134
#define mDefineA2DMatSizes(m, nm)
easily define the matrix dimension sizes
Definition: array2dmatrix.h:81
#define od_int64
Definition: plftypes.h:36
void getProduct(const Array1DVector &, Array1DVector &) const
Definition: array2dmatrix.h:216
bool isEq(const Array2D< fT > &, fT eps=fT(1e-6)) const
Definition: array2dmatrix.h:104
fT & get(int i0, int i1)
Definition: array2dmatrix.h:93
Matrix class based on Array2D. Initialized to 0.
Definition: array2dmatrix.h:32
void set(int pos, T)
Definition: arrayndimpl.h:360
#define mA2DMatHandleDimErr(v1, v2)
Definition: array2dmatrix.h:181
void set(int i0, int i1, fT v)
Definition: array2dmatrix.h:55
virtual int getSize(int dim) const =0
bool isFPEqual(T1 v1, T2 v2, eT eps)
Definition: commondefs.h:42
Array2D ( Subclass of ArrayND ) is a two dimensional array.
Definition: arraynd.h:131
bool setSize(int)
Definition: arrayndimpl.h:401
Array2DImpl< fT > a2d_
Definition: array2dmatrix.h:75
void setDiagonal(fT)
Definition: array2dmatrix.h:123
void setAll(fT v=fT(0))
Definition: array2dmatrix.h:59
void getProduct(const ArrayND< T > &in1, const ArrayND< T > &in2, ArrayND< T > &out, bool noudf, bool parallel)
computes the product array between two arrays
Definition: arrayndalgo.h:1747
void getSum(const Array2DMatrix &, Array2DMatrix &) const
Definition: array2dmatrix.h:194
#define mDefineImplA2DMatSizes
Definition: array2dmatrix.h:100
Array2DMatrix(int sz0=1, int sz1=0)
Definition: array2dmatrix.h:35
bool setSize(int, int)
Definition: arrayndimpl.h:516
void setToIdentity()
Definition: array2dmatrix.h:61
int size(bool dim1=false) const
Definition: array2dmatrix.h:86
Array2DMatrix(const Array2DImpl< fT > &a2d)
Definition: array2dmatrix.h:40
void copy(TypeSetBase< T, I > &to, const TypeSetBase< S, I > &from)
Definition: typeset.h:212
#define mClass(module)
Definition: commondefs.h:164
bool getCholesky(Array2DMatrix &) const
Definition: array2dmatrix.h:279
const Array1DInfo & info() const
Definition: arrayndimpl.h:76
float Sqrt(float)
void transpose()
Definition: array2dmatrix.h:257
T getSum(const ArrayND< T > &in, bool noudf, bool parallel)
returns the sum of all defined values in the Array. Returns UDF if empty or only udfs encountered...
Definition: arrayndalgo.h:1594
T get(int pos) const
Definition: arrayndimpl.h:372

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